libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

routines.kt (2790B)


      1 /*
      2  * This file is part of LibEuFin.
      3  * Copyright (C) 2024 Taler Systems S.A.
      4 
      5  * LibEuFin is free software; you can redistribute it and/or modify
      6  * it under the terms of the GNU Affero General Public License as
      7  * published by the Free Software Foundation; either version 3, or
      8  * (at your option) any later version.
      9 
     10  * LibEuFin is distributed in the hope that it will be useful, but
     11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
     13  * Public License for more details.
     14 
     15  * You should have received a copy of the GNU Affero General Public
     16  * License along with LibEuFin; see the file COPYING.  If not, see
     17  * <http://www.gnu.org/licenses/>
     18  */
     19 
     20 import io.ktor.client.request.*
     21 import io.ktor.http.*
     22 import io.ktor.server.testing.*
     23 import tech.libeufin.common.TalerErrorCode
     24 import tech.libeufin.common.assertBadRequest
     25 import tech.libeufin.common.assertUnauthorized
     26 import tech.libeufin.common.test.abstractHistoryRoutine
     27 
     28 // Test endpoint is correctly authenticated 
     29 suspend fun ApplicationTestBuilder.authRoutine(
     30     method: HttpMethod,
     31     path: String,
     32     token: Boolean = true
     33 ) { 
     34     // No header
     35     client.request(path) {
     36         this.method = method
     37     }.assertUnauthorized(TalerErrorCode.GENERIC_PARAMETER_MISSING)
     38 
     39     // Bad header
     40     client.request(path) {
     41         this.method = method
     42         headers[HttpHeaders.Authorization] = "WTF"
     43     }.assertBadRequest(TalerErrorCode.GENERIC_HTTP_HEADERS_MALFORMED)
     44 
     45     // Wrong scheme
     46     if (token) {
     47         client.request(path) {
     48             this.method = method
     49             headers[HttpHeaders.Authorization] = "Basic bad-token"
     50         }.assertUnauthorized(TalerErrorCode.GENERIC_UNAUTHORIZED)
     51     } else {
     52         client.request(path) {
     53             this.method = method
     54             headers[HttpHeaders.Authorization] = "Bearer bad-token"
     55         }.assertUnauthorized(TalerErrorCode.GENERIC_UNAUTHORIZED)
     56     }
     57 
     58     // Bad token
     59     if (token) {
     60         client.request(path) {
     61             this.method = method
     62             headers[HttpHeaders.Authorization] = "Bearer bad-token"
     63         }.assertUnauthorized(TalerErrorCode.GENERIC_TOKEN_UNKNOWN)
     64     } else {
     65         client.request(path) {
     66             this.method = method
     67             basicAuth("username", "bad-password")
     68         }.assertUnauthorized(TalerErrorCode.GENERIC_TOKEN_UNKNOWN)
     69     }
     70 }
     71 
     72 
     73 suspend inline fun <reified B> ApplicationTestBuilder.historyRoutine(
     74     url: String,
     75     crossinline ids: (B) -> List<Long>,
     76     registered: List<suspend () -> Unit>,
     77     ignored: List<suspend () -> Unit> = listOf(),
     78     polling: Boolean = true
     79 ) {
     80     abstractHistoryRoutine(ids, registered, ignored, polling) { params: String ->
     81         client.getA("$url?$params")
     82     }
     83 }