From 0b0f28dad776aeed0cdb6b93f2d99efcc172093b Mon Sep 17 00:00:00 2001 From: Sandipan Dey Date: Wed, 5 Feb 2025 13:23:19 +0530 Subject: [PATCH 1/4] test(bats): customer --- bats/cala-gql/simulate-deposit.gql | 7 --- bats/customer-gql/me.gql | 8 ++++ bats/customer.bats | 36 +++++++++++---- bats/gql/list-accounts.gql | 8 ---- bats/gql/me.gql | 21 --------- bats/gql/sumsub-token-create.gql | 5 -- bats/gql/user-create.gql | 20 -------- bats/helpers.bash | 73 ++++++++++++------------------ 8 files changed, 64 insertions(+), 114 deletions(-) delete mode 100644 bats/cala-gql/simulate-deposit.gql create mode 100644 bats/customer-gql/me.gql delete mode 100644 bats/gql/list-accounts.gql delete mode 100644 bats/gql/me.gql delete mode 100644 bats/gql/sumsub-token-create.gql delete mode 100644 bats/gql/user-create.gql diff --git a/bats/cala-gql/simulate-deposit.gql b/bats/cala-gql/simulate-deposit.gql deleted file mode 100644 index 765d9a7b8..000000000 --- a/bats/cala-gql/simulate-deposit.gql +++ /dev/null @@ -1,7 +0,0 @@ -mutation SimulateDeposit($address: String!, $amount: Decimal!, $currency: String!) { - bitfinex { - simulate { - deposit(depositAddress: $address, amount: $amount, currency: $currency) - } - } -} diff --git a/bats/customer-gql/me.gql b/bats/customer-gql/me.gql new file mode 100644 index 000000000..0501cf01b --- /dev/null +++ b/bats/customer-gql/me.gql @@ -0,0 +1,8 @@ +query me { + me { + customer { + id + customerId + } + } +} diff --git a/bats/customer.bats b/bats/customer.bats index 5788c3f90..503777a9a 100644 --- a/bats/customer.bats +++ b/bats/customer.bats @@ -22,16 +22,6 @@ wait_for_approval() { [[ "$status" == "PENDING_CONFIRMATION" ]] || return 1 } -# @test "customer: unauthorized" { -# cache_value "alice" "invalid-token" -# exec_graphql 'alice' 'me' -# error_code=$(graphql_output '.error.code') -# [[ "$error_code" == 401 ]] || exit 1 -# -# error_status=$(graphql_output '.error.status') -# [[ "$error_status" == "Unauthorized" ]] || exit 1 -# } - @test "customer: can create a customer" { customer_email=$(generate_email) telegramId=$(generate_email) @@ -58,6 +48,32 @@ wait_for_approval() { echo $(graphql_output) | jq . } +@test "customer: can login" { + customer_email=$(generate_email) + telegramId=$(generate_email) + + variables=$( + jq -n \ + --arg email "$customer_email" \ + --arg telegramId "$telegramId" \ + '{ + input: { + email: $email, + telegramId: $telegramId + } + }' + ) + + exec_admin_graphql 'customer-create' "$variables" + customer_id=$(graphql_output .data.customerCreate.customer.customerId) + [[ "$customer_id" != "null" ]] || exit 1 + + login_customer $customer_email + exec_customer_graphql $customer_email 'me' + echo $(graphql_output) | jq . + [[ "$(graphql_output .data.me.customer.customerId)" == "$customer_id" ]] || exit 1 +} + @test "customer: can deposit" { customer_id=$(create_customer) cache_value "customer_id" $customer_id diff --git a/bats/gql/list-accounts.gql b/bats/gql/list-accounts.gql deleted file mode 100644 index c79b20953..000000000 --- a/bats/gql/list-accounts.gql +++ /dev/null @@ -1,8 +0,0 @@ -query listAccounts() { - accounts(first: 10) { - nodes { - id - name - } - } -} diff --git a/bats/gql/me.gql b/bats/gql/me.gql deleted file mode 100644 index 5b5b93200..000000000 --- a/bats/gql/me.gql +++ /dev/null @@ -1,21 +0,0 @@ -query me { - me { - customerId - applicantId - status - btcDepositAddress - ustDepositAddress - email - level - balance { - unallocatedCollateral { - settled { - btcBalance - } - } - checking { - settled - } - } - } -} diff --git a/bats/gql/sumsub-token-create.gql b/bats/gql/sumsub-token-create.gql deleted file mode 100644 index 80088a705..000000000 --- a/bats/gql/sumsub-token-create.gql +++ /dev/null @@ -1,5 +0,0 @@ -mutation sumsubTokenCreate { - sumsubTokenCreate { - token - } -} diff --git a/bats/gql/user-create.gql b/bats/gql/user-create.gql deleted file mode 100644 index 5fddbf1cb..000000000 --- a/bats/gql/user-create.gql +++ /dev/null @@ -1,20 +0,0 @@ -mutation userCreate($input: UserCreateInput!) { - userCreate(input: $input) { - customer { - customerId - btcDepositAddress - ustDepositAddress - email - balance { - unallocatedCollateral { - settled { - btcBalance - } - } - checking { - settled - } - } - } - } -} diff --git a/bats/helpers.bash b/bats/helpers.bash index 18718b249..ce114402a 100644 --- a/bats/helpers.bash +++ b/bats/helpers.bash @@ -4,10 +4,11 @@ COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-${REPO_ROOT##*/}}" CACHE_DIR=${BATS_TMPDIR:-tmp/bats}/galoy-bats-cache mkdir -p "$CACHE_DIR" -KRATOS_PUBLIC_ENDPOINT="http://localhost:4455" -GQL_PUBLIC_ENDPOINT="http://localhost:4455/graphql" -GQL_ADMIN_ENDPOINT="http://localhost:4455/admin/graphql" -GQL_CALA_ENDPOINT="http://localhost:2252/graphql" +OATHKEEPER_PROXY="http://localhost:4455" +MAILHOG_ENDPOINT="http://localhost:8025" + +GQL_APP_ENDPOINT="${OATHKEEPER_PROXY}/app/graphql" +GQL_ADMIN_ENDPOINT="${OATHKEEPER_PROXY}/admin/graphql" LANA_HOME="${LANA_HOME:-.lana}" export LANA_CONFIG="${REPO_ROOT}/bats/lana-sim-time.yml" @@ -79,7 +80,7 @@ gql_query() { } gql_file() { - echo "${REPO_ROOT}/bats/gql/$1.gql" + echo "${REPO_ROOT}/bats/customer-gql/$1.gql" } gql_admin_query() { @@ -90,19 +91,26 @@ gql_admin_file() { echo "${REPO_ROOT}/bats/admin-gql/$1.gql" } -gql_cala_query() { - cat "$(gql_cala_file $1)" | tr '\n' ' ' | sed 's/"/\\"/g' +graphql_output() { + echo $output | jq -r "$@" } -gql_cala_file() { - echo "${REPO_ROOT}/bats/cala-gql/$1.gql" -} +login_customer() { + local email=$1 -graphql_output() { - echo $output | jq -r "$@" + flowId=$(curl -s -X GET -H "Accept: application/json" "${OATHKEEPER_PROXY}/app/self-service/login/api" | jq -r '.id') + variables=$(jq -n --arg email "$email" '{ identifier: $email, method: "code" }' ) + curl -s -X POST -H "Accept: application/json" -H "Content-Type: application/json" -d "$variables" "${OATHKEEPER_PROXY}/app/self-service/login?flow=$flowId" + sleep 1 + + code=$(getEmailCode $email) + variables=$(jq -n --arg email "$email" --arg code "$code" '{ identifier: $email, method: "code", code: $code }' ) + session=$(curl -s -X POST -H "Accept: application/json" -H "Content-Type: application/json" -d "$variables" "${OATHKEEPER_PROXY}/app/self-service/login?flow=$flowId") + token=$(echo $session | jq -r '.session_token') + cache_value "$email" $token } -exec_graphql() { +exec_customer_graphql() { local token_name=$1 local query_name=$2 local variables=${3:-"{}"} @@ -120,20 +128,20 @@ exec_graphql() { ${AUTH_HEADER:+ -H "$AUTH_HEADER"} \ -H "Content-Type: application/json" \ -d "{\"query\": \"$(gql_query $query_name)\", \"variables\": $variables}" \ - "${GQL_PUBLIC_ENDPOINT}" + "${GQL_APP_ENDPOINT}" } login_superadmin() { local email="admin@galoy.io" - flowId=$(curl -s -X GET -H "Accept: application/json" "${KRATOS_PUBLIC_ENDPOINT}/admin/self-service/login/api" | jq -r '.id') + flowId=$(curl -s -X GET -H "Accept: application/json" "${OATHKEEPER_PROXY}/admin/self-service/login/api" | jq -r '.id') variables=$(jq -n --arg email "$email" '{ identifier: $email, method: "code" }' ) - curl -s -X POST -H "Accept: application/json" -H "Content-Type: application/json" -d "$variables" "${KRATOS_PUBLIC_ENDPOINT}/admin/self-service/login?flow=$flowId" + curl -s -X POST -H "Accept: application/json" -H "Content-Type: application/json" -d "$variables" "${OATHKEEPER_PROXY}/admin/self-service/login?flow=$flowId" sleep 1 code=$(getEmailCode $email) variables=$(jq -n --arg email "$email" --arg code "$code" '{ identifier: $email, method: "code", code: $code }' ) - session=$(curl -s -X POST -H "Accept: application/json" -H "Content-Type: application/json" -d "$variables" "${KRATOS_PUBLIC_ENDPOINT}/admin/self-service/login?flow=$flowId") + session=$(curl -s -X POST -H "Accept: application/json" -H "Content-Type: application/json" -d "$variables" "${OATHKEEPER_PROXY}/admin/self-service/login?flow=$flowId") token=$(echo $session | jq -r '.session_token') cache_value "superadmin" $token } @@ -175,24 +183,6 @@ exec_admin_graphql_upload() { "${GQL_ADMIN_ENDPOINT}" } -exec_cala_graphql() { - local query_name=$1 - local variables=${2:-"{}"} - - if [[ "${BATS_TEST_DIRNAME}" != "" ]]; then - run_cmd="run" - else - run_cmd="" - fi - - ${run_cmd} curl -s \ - -X POST \ - ${AUTH_HEADER:+ -H "$AUTH_HEADER"} \ - -H "Content-Type: application/json" \ - -d "{\"query\": \"$(gql_cala_query $query_name)\", \"variables\": $variables}" \ - "${GQL_CALA_ENDPOINT}" -} - # Run the given command in the background. Useful for starting a # node and then moving on with commands that exercise it for the # test. @@ -254,20 +244,17 @@ reset_log_files() { done } -KRATOS_PG_CON="postgres://dbuser:secret@localhost:5434/default?sslmode=disable" - getEmailCode() { local email="$1" - local query="SELECT body FROM courier_messages WHERE recipient='${email}' ORDER BY created_at DESC LIMIT 1;" - - local result=$(psql $KRATOS_PG_CON -t -c "${query}") - if [[ -z "$result" ]]; then - echo "No message for email ${email}" >&2 + local emails=$(curl -s -X GET "${MAILHOG_ENDPOINT}/api/v2/search?kind=to&query=${email}") + if [[ $(echo "$emails" | jq '.total') -eq 0 ]]; then + echo "No message for email ${email}" exit 1 fi - local code=$(echo "$result" | grep -Eo '[0-9]{6}' | head -n1) + local email_content=$(echo "$emails" | jq '.items[0].MIME.Parts[0].Body' | tr -d '"') + local code=$(echo "$email_content" | grep -Eo '[0-9]{6}' | head -n1) echo "$code" } From 3029ca1c81ca6859ab20931d0df610ebc3d6277e Mon Sep 17 00:00:00 2001 From: Sandipan Dey Date: Wed, 5 Feb 2025 13:29:34 +0530 Subject: [PATCH 2/4] fix: jwt claims --- bats/customer.bats | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bats/customer.bats b/bats/customer.bats index 503777a9a..14eadbbe6 100644 --- a/bats/customer.bats +++ b/bats/customer.bats @@ -68,6 +68,8 @@ wait_for_approval() { customer_id=$(graphql_output .data.customerCreate.customer.customerId) [[ "$customer_id" != "null" ]] || exit 1 + sleep 0.1 # wait for customer-onboarding steps + login_customer $customer_email exec_customer_graphql $customer_email 'me' echo $(graphql_output) | jq . From c55db20e2d15a8ef2f2d4f1f8bbcfa08192e8228 Mon Sep 17 00:00:00 2001 From: Sandipan Dey Date: Tue, 18 Feb 2025 13:05:37 +0530 Subject: [PATCH 3/4] ci: use bats setup suite --- Makefile | 4 ++-- bats/ci-setup-suite.bash | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100755 bats/ci-setup-suite.bash diff --git a/Makefile b/Makefile index 7f980fb7e..96ecddc2e 100644 --- a/Makefile +++ b/Makefile @@ -54,11 +54,11 @@ build-for-tests: SQLX_OFFLINE=true cargo build --locked --features sim-time e2e: clean-deps start-deps build-for-tests - bats -t bats + bats --setup-suite-file bats/ci-setup-suite.bash -t bats e2e-in-ci: clean-deps start-deps build-for-tests lsof -i :5253 | tail -n 1 | cut -d" " -f2 | xargs -L 1 kill -9 || true - SA_CREDS_BASE64=$$(cat ./dev/fake-service-account.json | tr -d '\n' | base64 -w 0) bats -t bats + SA_CREDS_BASE64=$$(cat ./dev/fake-service-account.json | tr -d '\n' | base64 -w 0) bats --setup-suite-file bats/ci-setup-suite.bash -t bats sdl: diff --git a/bats/ci-setup-suite.bash b/bats/ci-setup-suite.bash new file mode 100755 index 000000000..7a53dff2d --- /dev/null +++ b/bats/ci-setup-suite.bash @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +export REPO_ROOT=$(git rev-parse --show-toplevel) +source "${REPO_ROOT}/bats/helpers.bash" + +setup_suite() { + start_server +} + +teardown_suite() { + stop_server +} From dd5623332e03e733aff19602d4323680aa85fa51 Mon Sep 17 00:00:00 2001 From: Sandipan Dey Date: Tue, 18 Feb 2025 13:36:05 +0530 Subject: [PATCH 4/4] test: kill via pid in bats setup suite --- .lava/server-pid | 1 - bats/ci-setup-suite.bash | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-) delete mode 100644 .lava/server-pid diff --git a/.lava/server-pid b/.lava/server-pid deleted file mode 100644 index 620bf7c62..000000000 --- a/.lava/server-pid +++ /dev/null @@ -1 +0,0 @@ -80202 \ No newline at end of file diff --git a/bats/ci-setup-suite.bash b/bats/ci-setup-suite.bash index 7a53dff2d..352f4d553 100755 --- a/bats/ci-setup-suite.bash +++ b/bats/ci-setup-suite.bash @@ -9,4 +9,7 @@ setup_suite() { teardown_suite() { stop_server + + lsof -i :5253 | tail -n 1 | cut -d" " -f2 | xargs -L 1 kill -9 || true + lsof -i :5254 | tail -n 1 | cut -d" " -f2 | xargs -L 1 kill -9 || true }