aboutsummaryrefslogtreecommitdiff
path: root/nexus/src/test/kotlin/SandboxLegacyApiTest.kt
blob: bd8b0248938759ea6a5cc6df7af4919554894663 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import com.fasterxml.jackson.databind.ObjectMapper
import io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.testing.*
import io.ktor.util.*
import io.ktor.utils.io.*
import io.netty.handler.codec.http.HttpResponseStatus
import kotlinx.coroutines.runBlocking
import org.junit.Ignore
import org.junit.Test
import tech.libeufin.sandbox.sandboxApp
import tech.libeufin.util.buildBasicAuthLine
import tech.libeufin.util.getIban
import java.io.ByteArrayOutputStream

class SandboxLegacyApiTest {
    fun dbHelper (f: () -> Unit) {
        withTestDatabase {
            prepSandboxDb()
            f()
        }
    }
    val mapper = ObjectMapper()

    // EBICS Subscribers API.
    @Test
    fun adminEbicsSubscribers() {
        dbHelper {
            testApplication {
                application(sandboxApp)
                runBlocking {
                    /**
                     * Create a EBICS subscriber.  That conflicts because
                     * MakeEnv.kt created it already, but tests access control
                     * and conflict detection.
                     */
                    var body = mapper.writeValueAsString(object {
                        val hostID = "eufinSandbox"
                        val userID = "foo"
                        val systemID = "foo"
                        val partnerID = "foo"
                    })
                    var r: HttpResponse = client.post("/admin/ebics/subscribers") {
                        expectSuccess = false
                        contentType(ContentType.Application.Json)
                        basicAuth("admin", "foo")
                        setBody(body)
                    }
                    assert(r.status.value == HttpStatusCode.Conflict.value)

                    // Check that EBICS subscriber indeed exists.
                    r = client.get("/admin/ebics/subscribers") {
                        basicAuth("admin", "foo")
                    }
                    assert(r.status.value == HttpStatusCode.OK.value)
                    val respObj = mapper.readTree(r.bodyAsText())
                    assert("foo" == respObj.get("subscribers").get(0).get("userID").asText())

                    // Try same operations as above, with wrong admin credentials
                    r = client.get("/admin/ebics/subscribers") {
                        expectSuccess = false
                        basicAuth("admin", "wrong")
                    }
                    assert(r.status.value == HttpStatusCode.Unauthorized.value)
                    r = client.post("/admin/ebics/subscribers") {
                        expectSuccess = false
                        basicAuth("admin", "wrong")
                    }
                    assert(r.status.value == HttpStatusCode.Unauthorized.value)
                    // Good credentials, but insufficient rights.
                    r = client.get("/admin/ebics/subscribers") {
                        expectSuccess = false
                        basicAuth("foo", "foo")
                    }
                    assert(r.status.value == HttpStatusCode.Forbidden.value)
                    r = client.post("/admin/ebics/subscribers") {
                        expectSuccess = false
                        basicAuth("foo", "foo")
                    }
                    assert(r.status.value == HttpStatusCode.Forbidden.value)
                    /**
                     * Give a bank account to the existing subscriber.  Bank account
                     * is (implicitly / hard-coded) hosted at default demobank.
                     */
                    // Create new subscriber.  No need to have the related customer.
                    body = mapper.writeValueAsString(object {
                        val hostID = "eufinSandbox"
                        val userID = "baz"
                        val partnerID = "baz"
                        val systemID = "foo"
                    })
                    client.post("/admin/ebics/subscribers") {
                        expectSuccess = true
                        contentType(ContentType.Application.Json)
                        basicAuth("admin", "foo")
                        setBody(body)
                    }
                    // Associate new bank account to it.
                    body = mapper.writeValueAsString(object {
                        val subscriber = object {
                            val userID = "baz"
                            val partnerID = "baz"
                            val systemID = "baz"
                            val hostID = "eufinSandbox"
                        }
                        val iban = getIban()
                        val bic = "SANDBOXX"
                        val name = "Now Have Account"
                        val label = "baz"
                    })
                    client.post("/admin/ebics/bank-accounts") {
                        expectSuccess = true
                        expectSuccess = true
                        contentType(ContentType.Application.Json)
                        basicAuth("admin", "foo")
                        setBody(body)
                    }
                    r = client.get("/admin/ebics/subscribers") {
                        basicAuth("admin", "foo")
                    }
                    assert(r.status.value == HttpStatusCode.OK.value)
                    val respObj_ = mapper.readTree(r.bodyAsText())
                    val bankAccountLabel = respObj_.get("subscribers").get(1).get("demobankAccountLabel").asText()
                    assert("baz" == bankAccountLabel)
                    // Same operation, wrong/unauth credentials.
                    r = client.post("/admin/ebics/bank-accounts") {
                        expectSuccess = false
                        basicAuth("admin", "wrong")
                    }
                    assert(r.status.value == HttpStatusCode.Unauthorized.value)
                    r = client.post("/admin/ebics/bank-accounts") {
                        expectSuccess = false
                        basicAuth("foo", "foo")
                    }
                    assert(r.status.value == HttpStatusCode.Forbidden.value)
                }
            }
        }
    }

    // EBICS Hosts API.
    @Test
    fun adminEbicsCreateHost() {
        dbHelper {
            testApplication {
                application(sandboxApp)
                runBlocking {
                    val body = mapper.writeValueAsString(
                        object {
                            val hostID = "www"
                            var ebicsVersion = "www"
                        }
                    )
                    // Valid request, good credentials.
                    client.post("/admin/ebics/hosts") {
                        expectSuccess = true
                        setBody(body)
                        contentType(ContentType.Application.Json)
                        basicAuth("admin", "foo")
                    }
                    var r = client.get("/admin/ebics/hosts") { expectSuccess = false }
                    assert(r.status.value == HttpResponseStatus.UNAUTHORIZED.code())
                    client.get("/admin/ebics/hosts") {
                        basicAuth("admin", "foo")
                        expectSuccess = true
                    }
                    // Invalid, with good credentials.
                    r = client.post("/admin/ebics/hosts") {
                        expectSuccess = false
                        setBody("invalid")
                        contentType(ContentType.Application.Json)
                        basicAuth("admin", "foo")
                    }
                    assert(r.status.value == HttpStatusCode.BadRequest.value)
                    // Unauth: admin with wrong password.
                    r = client.post("/admin/ebics/hosts") {
                        expectSuccess = false
                        basicAuth("admin", "bar")
                    }
                    assert(r.status.value == HttpStatusCode.Unauthorized.value)
                    // Auth & forbidden resource.
                    r = client.post("/admin/ebics/hosts") {
                        expectSuccess = false
                        basicAuth("foo", "foo")
                    }
                    assert(r.status.value == HttpStatusCode.Forbidden.value)
                }
            }
        }
    }
}