libeufin

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

commit 7b0b802861fcf5a7e75e61237ee4c82e0cd6079e
parent 6824b0d897699ca3236b42be1d3391ac29cc482a
Author: MS <ms@taler.net>
Date:   Mon,  1 Jun 2020 15:35:00 +0200

Loopback (minimal) test.

Diffstat:
Aintegration-tests/test-loopback-highlevel.py | 120+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mnexus/src/main/kotlin/tech/libeufin/nexus/Main.kt | 11+++++++++++
2 files changed, 131 insertions(+), 0 deletions(-)

diff --git a/integration-tests/test-loopback-highlevel.py b/integration-tests/test-loopback-highlevel.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python3 + +from requests import post, get +from subprocess import call, Popen, PIPE +from time import sleep +import os +import socket +import hashlib +import base64 + +# Steps implemented in this test. +# +# 0 Prepare nexus. +# -> (a) Make a Nexus user, (b) make a loopback bank connection +# associated to that user + +# Nexus user details +USERNAME = "person" +PASSWORD = "y" +USER_AUTHORIZATION_HEADER = "basic {}".format( + base64.b64encode(b"person:y").decode("utf-8") +) + +# Admin authentication +ADMIN_AUTHORIZATION_HEADER = "basic {}".format( + base64.b64encode(b"admin:x").decode("utf-8") +) + +BANK_ACCOUNT_LABEL = "savings" + +# Databases +NEXUS_DB="test-nexus.sqlite3" + +def fail(msg): + print(msg) + nexus.terminate() + sandbox.terminate() + exit(1) + + +def checkPorts(ports): + for i in ports: + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + try: + s.bind(("0.0.0.0", i)) + s.close() + except: + print("Port {} is not available".format(i)) + exit(77) + + +def assertResponse(response): + if response.status_code != 200: + print("Test failed on URL: {}".format(response.url)) + # stdout/stderr from both services is A LOT of text. + # Confusing to dump all that to console. + print("Check nexus.log and sandbox.log, probably under /tmp") + nexus.terminate() + sandbox.terminate() + exit(1) + # Allows for finer grained checks. + return response + + +# -1 Clean databases and start services. +os.chdir("..") +assert 0 == call(["rm", "-f", "nexus/{}".format(NEXUS_DB)]) +DEVNULL = open(os.devnull, "w") + +assert 0 == call( + ["./gradlew", "nexus:run", "--console=plain", "--args=superuser admin --password x --db-name={}".format(NEXUS_DB)] +) + +# Start nexus +checkPorts([5001]) +nexus = Popen( + ["./gradlew", "nexus:run", "--console=plain", "--args=serve --db-name={}".format(NEXUS_DB)], + stdout=PIPE, + stderr=PIPE, +) +for i in range(10): + try: + get("http://localhost:5001/") + except: + if i == 9: + nexus.terminate() + stdout, stderr = nexus.communicate() + print("Nexus timed out") + print("{}\n{}".format(stdout.decode(), stderr.decode())) + exit(77) + sleep(2) + continue + break + +# 0.a, make a new nexus user. +assertResponse( + post( + "http://localhost:5001/users", + headers=dict(Authorization=ADMIN_AUTHORIZATION_HEADER), + json=dict(username=USERNAME, password=PASSWORD), + ) +) + +print("creating bank connection") + +# 0.b, make a ebics bank connection for the new user. +assertResponse( + post( + "http://localhost:5001/bank-connections", + json=dict( + name="my-loopback", + source="new", + type="loopback", + ), + headers=dict(Authorization=USER_AUTHORIZATION_HEADER), + ) +) + +nexus.terminate() +print("Test passed!") diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt @@ -192,6 +192,13 @@ fun createEbicsBankConnectionFromBackup( return } +fun createLoopbackBankConnection(bankConnectionName: String, user: NexusUserEntity, data: JsonNode) { + val bankConn = NexusBankConnectionEntity.new(bankConnectionName) { + owner = user + type = "loopback" + } +} + fun createEbicsBankConnection(bankConnectionName: String, user: NexusUserEntity, data: JsonNode) { val bankConn = NexusBankConnectionEntity.new(bankConnectionName) { owner = user @@ -691,6 +698,10 @@ fun serverMain(dbName: String) { "ebics" -> { createEbicsBankConnection(body.name, user, body.data) } + "loopback" -> { + createLoopbackBankConnection(body.name, user, body.data) + + } else -> { throw NexusError(HttpStatusCode.BadRequest, "connection type not supported") }