Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(bats): customer #1362

Merged
merged 4 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .lava/server-pid

This file was deleted.

4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 0 additions & 7 deletions bats/cala-gql/simulate-deposit.gql

This file was deleted.

15 changes: 15 additions & 0 deletions bats/ci-setup-suite.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/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

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
}
8 changes: 8 additions & 0 deletions bats/customer-gql/me.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
query me {
me {
customer {
id
customerId
}
}
}
38 changes: 28 additions & 10 deletions bats/customer.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -58,6 +48,34 @@ 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

sleep 0.1 # wait for customer-onboarding steps

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
Expand Down
8 changes: 0 additions & 8 deletions bats/gql/list-accounts.gql

This file was deleted.

21 changes: 0 additions & 21 deletions bats/gql/me.gql

This file was deleted.

5 changes: 0 additions & 5 deletions bats/gql/sumsub-token-create.gql

This file was deleted.

20 changes: 0 additions & 20 deletions bats/gql/user-create.gql

This file was deleted.

73 changes: 30 additions & 43 deletions bats/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does exec_graphql use to do ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

talk to customer gql

exec_customer_graphql() {
local token_name=$1
local query_name=$2
local variables=${3:-"{}"}
Expand All @@ -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="[email protected]"

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
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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"
}
Expand Down
Loading