From 741dba08455b6456c2d8186308e6da09efa1a029 Mon Sep 17 00:00:00 2001 From: Antoine A <> Date: Fri, 29 Dec 2023 18:14:51 +0000 Subject: Simplify tests --- bank/src/test/kotlin/CoreBankApiTest.kt | 64 ++++++++++++++++----------------- bank/src/test/kotlin/helpers.kt | 22 ++++++++++++ bank/src/test/kotlin/routines.kt | 36 ------------------- 3 files changed, 52 insertions(+), 70 deletions(-) diff --git a/bank/src/test/kotlin/CoreBankApiTest.kt b/bank/src/test/kotlin/CoreBankApiTest.kt index c647af14..ff2548e2 100644 --- a/bank/src/test/kotlin/CoreBankApiTest.kt +++ b/bank/src/test/kotlin/CoreBankApiTest.kt @@ -330,30 +330,28 @@ class CoreBankAccountsApiTest { client.deleteA("/accounts/exchange") .assertConflict(TalerErrorCode.BANK_RESERVED_USERNAME_CONFLICT) - tanRoutine("john", prepare = { - client.post("/accounts") { - json { - "username" to "john" - "password" to "john-password" - "name" to "John" - "internal_payto_uri" to genTmpPayTo() - } - }.assertOk() - }) { challenge -> - // Fail to delete, due to a non-zero balance. - tx("customer", "KUDOS:1", "john") - client.deleteA("/accounts/john") - .assertConflict(TalerErrorCode.BANK_ACCOUNT_BALANCE_NOT_ZERO) - // Sucessful deletion - tx("john", "KUDOS:1", "customer") - client.deleteA("/accounts/john") - .challenge() - .assertNoContent() - // Account no longer exists - client.delete("/accounts/john") { - pwAuth("admin") - }.assertNotFound(TalerErrorCode.BANK_UNKNOWN_ACCOUNT) - } + client.post("/accounts") { + json { + "username" to "john" + "password" to "john-password" + "name" to "John" + "internal_payto_uri" to genTmpPayTo() + } + }.assertOk() + fillTanInfo("john") + // Fail to delete, due to a non-zero balance. + tx("customer", "KUDOS:1", "john") + client.deleteA("/accounts/john") + .assertConflict(TalerErrorCode.BANK_ACCOUNT_BALANCE_NOT_ZERO) + // Sucessful deletion + tx("john", "KUDOS:1", "customer") + client.deleteA("/accounts/john") + .assertChallenge() + .assertNoContent() + // Account no longer exists + client.delete("/accounts/john") { + pwAuth("admin") + }.assertNotFound(TalerErrorCode.BANK_UNKNOWN_ACCOUNT) } // Test admin-only account deletion @@ -471,18 +469,16 @@ class CoreBankAccountsApiTest { }.assertConflict(TalerErrorCode.END) // Check 2FA - tanRoutine("merchant", prepare = { - client.patch("/accounts/merchant") { - pwAuth("admin") - json { "is_public" to true } - } - }) { challenge -> - val challengeId = client.patchA("/accounts/merchant") { - json { "is_public" to false } - }.challenge() + fillTanInfo("merchant") + client.patchA("/accounts/merchant") { + json { "is_public" to false } + }.assertChallenge { client.getA("/accounts/merchant").assertOkJson { obj -> - assert(!obj.is_public) + assert(obj.is_public) } + }.assertNoContent(); + client.getA("/accounts/merchant").assertOkJson { obj -> + assert(!obj.is_public) } } diff --git a/bank/src/test/kotlin/helpers.kt b/bank/src/test/kotlin/helpers.kt index 24954856..71c5cf23 100644 --- a/bank/src/test/kotlin/helpers.kt +++ b/bank/src/test/kotlin/helpers.kt @@ -254,6 +254,18 @@ suspend fun ApplicationTestBuilder.fillCashoutInfo(account: String) { }.assertNoContent() } +suspend fun ApplicationTestBuilder.fillTanInfo(account: String) { + client.patch("/accounts/$account") { + pwAuth("admin") + json { + "contact_data" to obj { + "phone" to "+42" + } + "tan_channel" to "sms" + } + }.assertNoContent() +} + suspend fun ApplicationTestBuilder.withdrawalSelect(uuid: String) { client.post("/taler-integration/withdrawal-operation/$uuid") { json { @@ -313,6 +325,16 @@ suspend fun HttpResponse.assertErr(code: TalerErrorCode): HttpResponse { return this } +suspend fun HttpResponse.assertChallenge(check: suspend () -> Unit = {}): HttpResponse { + val id = this.assertAcceptedJson().challenge_id + val username = this.call.request.url.pathSegments[2] + this.call.client.postA("/accounts/$username/challenge/$id").assertOk() + check() + return this.call.client.postA("/accounts/$username/challenge/$id/confirm") { + json { "tan" to smsCode("+42") } + } +} + suspend fun assertTime(min: Int, max: Int, lambda: suspend () -> Unit) { val start = System.currentTimeMillis() lambda() diff --git a/bank/src/test/kotlin/routines.kt b/bank/src/test/kotlin/routines.kt index 45ede5eb..8c7476b1 100644 --- a/bank/src/test/kotlin/routines.kt +++ b/bank/src/test/kotlin/routines.kt @@ -81,42 +81,6 @@ suspend fun ApplicationTestBuilder.authRoutine( } } -// Test endpoint is correctly protected using 2fa -suspend fun ApplicationTestBuilder.tanRoutine( - username: String, - prepare: suspend () -> Unit = {}, - routine: suspend (suspend HttpResponse.() -> HttpResponse) -> Unit, -) { - // Check without 2FA - prepare() - client.patch("/accounts/$username") { - pwAuth("admin") - json { - "tan_channel" to null as Int? - } - }.assertNoContent() - routine({ this }) - - // Check with 2FA - prepare() - client.patch("/accounts/$username") { - pwAuth("admin") - json { - "contact_data" to obj { - "phone" to "+42" - } - "tan_channel" to "sms" - } - }.assertNoContent() - routine({ - val id = this.assertAcceptedJson().challenge_id - client.postA("/accounts/$username/challenge/$id").assertOk() - client.postA("/accounts/$username/challenge/$id/confirm") { - json { "tan" to smsCode("+42") } - } - }) -} - inline suspend fun ApplicationTestBuilder.historyRoutine( url: String, crossinline ids: (B) -> List, -- cgit v1.2.3