From 3a929b42d32474121c37951af85ae902e39159b9 Mon Sep 17 00:00:00 2001 From: dlamarmorgan Date: Fri, 23 Sep 2022 17:56:13 -0700 Subject: [PATCH] test/{test-invoice,test-payment}: Add test for receiving payments during invoicing Add a test to verify that receiving a payment when invoicing are being created will not cause issues for a user balance. Change-Id: I430eab9b7f6baacbd992279c70c1c5d3285d5f51 --- test/test-invoice.sh | 47 ++++++++++++------ test/test-payment.sh | 109 +++++++++++++++++++++++++++++++++++++++++ test/test-reorg.sh | 27 ++++++---- test/test-storjscan.sh | 23 ++++++--- 4 files changed, 176 insertions(+), 30 deletions(-) create mode 100644 test/test-payment.sh diff --git a/test/test-invoice.sh b/test/test-invoice.sh index fd73c3c..25446a0 100644 --- a/test/test-invoice.sh +++ b/test/test-invoice.sh @@ -3,6 +3,17 @@ cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" set -ex +cleanup() { + if [ -f "docker-compose.yaml" ] + then + docker compose down + fi + rm -rf .contracts.yaml + rm -rf storjscan + rm -rf geth + rm -rf docker-compose.yaml +} + go install storj.io/storj-up if [ ! "$(which storjscan )" ]; then @@ -15,14 +26,16 @@ fi export STORJUP_NO_HISTORY=true +cleanup + storj-up init storj,db,billing storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_PROVIDER=stripecoinpayments storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_STRIPE_COIN_PAYMENTS_STRIPE_PUBLIC_KEY="$STRIPE_PUBLIC_KEY" storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_STRIPE_COIN_PAYMENTS_STRIPE_SECRET_KEY="$STRIPE_SECRET_KEY" storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_BILLING_CONFIG_INTERVAL=5s storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_STORJSCAN_INTERVAL=5s +storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_STORJSCAN_CONFIRMATIONS=12 -docker compose down -v docker compose up -d storj-up health @@ -47,28 +60,38 @@ curl -X POST 'http://localhost:10000/api/v0/payments/wallet' --header "$COOKIE" ADDRESS=$(curl -X GET -s http://localhost:10000/api/v0/payments/wallet --header "$COOKIE" | jq -r '.address') #ACCOUNT is defined with environment variables above -for i in {1..15}; do cethacea token transfer 10 0x"$ADDRESS"; done +for i in {1..15}; do cethacea token transfer 1000000000 0x"$ADDRESS"; done storj-up health -t billing_transactions -n 3 -d 12 curl -X GET http://localhost:10000/api/v0/payments/wallet --header "$COOKIE" curl -X POST http://localhost:10000/api/v0/payments/account --header "$COOKIE" +STRIPE_CUSTOMERS=$(docker exec test-cockroach-1 cockroach sql --insecure -d master -e "SELECT customer_id FROM stripe_customers";) +CUSTOMER_ID=$(echo "$STRIPE_CUSTOMERS" | grep -o 'cus_[^[:blank:]]*') +PAYMENT_INTENT=$(curl https://api.stripe.com/v1/payment_intents -u "${STRIPE_SECRET_KEY}:" -d customer="$CUSTOMER_ID" -d amount=16742 -d currency=usd -d payment_method=pm_card_visa -d setup_future_usage=off_session ) +PAYMENT_METHOD=$(jq -r '.payment_method' <<< "${PAYMENT_INTENT}") +curl https://api.stripe.com/v1/payment_methods/"$PAYMENT_METHOD"/attach -u "$STRIPE_SECRET_KEY": -d customer="$CUSTOMER_ID" +curl https://api.stripe.com/v1/customers/"$CUSTOMER_ID" -u "${STRIPE_SECRET_KEY}:" -d "invoice_settings[default_payment_method]"="$PAYMENT_METHOD" # invoicing storj-up testdata project-usage +DAY=$(date +%d) +MONTH=$(date +%m) YEAR=$(date +%Y) if [ $(uname -s) == "Darwin" ] then - MONTH=$(date -v-1m +%m) + LAST_MONTH=$(date -v-1m +%m) + LAST_MONTH_YEAR=$(date -v-1m +%Y) else - MONTH=$(date +%m -d 'last month') + LAST_MONTH=$(date -d "$(date +%Y-%m-1) -1 month" +%m) + LAST_MONTH_YEAR=$(date -d "$(date +%Y-%m-1) -1 month" +%Y) fi -docker-compose exec satellite-admin satellite billing prepare-invoice-records "$MONTH"/"$YEAR" --log.level=info --log.output=stdout -docker-compose exec satellite-admin satellite billing create-project-invoice-items "$MONTH"/"$YEAR" --log.level=info --log.output=stdout -docker-compose exec satellite-admin satellite billing create-invoices "$MONTH"/"$YEAR" --log.level=info --log.output=stdout -docker-compose exec satellite-admin satellite billing create-token-invoice-items "$MONTH"/"$YEAR" --log.level=info --log.output=stdout -docker-compose exec satellite-admin satellite billing finalize-invoices --log.level=info --log.output=stdout +docker compose exec satellite-admin satellite billing prepare-invoice-records "$LAST_MONTH"/"$LAST_MONTH_YEAR" --log.level=info --log.output=stdout +docker compose exec satellite-admin satellite billing create-project-invoice-items "$LAST_MONTH"/"$LAST_MONTH_YEAR" --log.level=info --log.output=stdout +docker compose exec satellite-admin satellite billing create-invoices "$LAST_MONTH"/"$LAST_MONTH_YEAR" --log.level=info --log.output=stdout +docker compose exec satellite-admin satellite billing finalize-invoices --log.level=info --log.output=stdout +docker compose exec satellite-admin satellite billing pay-invoices "$DAY"/"$MONTH"/"$YEAR" --log.level=info --log.output=stdout BALANCE=$(curl -X GET -s http://localhost:10000/api/v0/payments/wallet --header "$COOKIE" | jq -r '.balance') @@ -77,8 +100,4 @@ then exit 1 fi -docker compose down -rm -rf .contracts.yaml -rm -rf storjscan -rm -rf geth -rm -rf docker-compose.yaml +cleanup diff --git a/test/test-payment.sh b/test/test-payment.sh new file mode 100644 index 0000000..fa16d57 --- /dev/null +++ b/test/test-payment.sh @@ -0,0 +1,109 @@ +#!/usr/bin/bash +cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" + +set -ex + +cleanup() { + if [ -f "docker-compose.yaml" ] + then + docker compose down + fi + rm -rf .contracts.yaml + rm -rf storjscan + rm -rf geth + rm -rf docker-compose.yaml +} + +go install storj.io/storj-up + +if [ ! "$(which storjscan )" ]; then + go install storj.io/storjscan/cmd/storjscan@latest +fi + +if [ ! "$(which cethacea)" ]; then + go install github.com/elek/cethacea@latest +fi + +export STORJUP_NO_HISTORY=true + +cleanup + +storj-up init storj,db,billing +storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_PROVIDER=stripecoinpayments +storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_STRIPE_COIN_PAYMENTS_STRIPE_PUBLIC_KEY="$STRIPE_PUBLIC_KEY" +storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_STRIPE_COIN_PAYMENTS_STRIPE_SECRET_KEY="$STRIPE_SECRET_KEY" +storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_BILLING_CONFIG_INTERVAL=5s +storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_STORJSCAN_CONFIRMATIONS=12 +storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_STORJSCAN_INTERVAL=5s +storj-up env setenv storjscan STORJ_TOKEN_PRICE_USE_TEST_PRICES=true + +docker compose up -d + +storj-up health + +eval $(storj-up credentials -e) +COOKIE=$(storj-up credentials | grep -o 'Cookie.*') + +export CETH_CHAIN=http://localhost:8545 +export CETH_ACCOUNT=2e9a0761ce9815b95b2389634f6af66abe5fec2b1e04b772728442b4c35ea365 +export CETH_CONTRACT=$(cethacea contract deploy --quiet --name TOKEN storjscan/test-contract/TestToken.bin --abi storjscan/test-contract/TestToken.abi '(uint256)' 1000000000000) + +curl -X GET -u "eu1:eu1secret" http://127.0.0.1:12000/api/v0/auth/whoami +curl -X GET -u "us1:us1secret" http://127.0.0.1:12000/api/v0/auth/whoami + +storjscan mnemonic > .mnemonic +storjscan generate --api-key us1 --api-secret us1secret --address http://127.0.0.1:12000 +storjscan mnemonic > .mnemonic +storjscan generate --api-key eu1 --api-secret eu1secret --address http://127.0.0.1:12000 +rm -rf .mnemonic + +curl -X POST 'http://localhost:10000/api/v0/payments/wallet' --header "$COOKIE" +ADDRESS=$(curl -X GET -s http://localhost:10000/api/v0/payments/wallet --header "$COOKIE" | jq -r '.address') + +#ACCOUNT is defined with environment variables above +for i in {1..15}; do cethacea token transfer 1000000000 0x"$ADDRESS"; done + +storj-up health -t billing_transactions -n 3 -d 12 + +curl -X GET http://localhost:10000/api/v0/payments/wallet --header "$COOKIE" +curl -X POST http://localhost:10000/api/v0/payments/account --header "$COOKIE" +STRIPE_CUSTOMERS=$(docker exec test-cockroach-1 cockroach sql --insecure -d master -e "SELECT customer_id FROM stripe_customers";) +CUSTOMER_ID=$(echo "$STRIPE_CUSTOMERS" | grep -o 'cus_[^[:blank:]]*') +PAYMENT_INTENT=$(curl https://api.stripe.com/v1/payment_intents -u "${STRIPE_SECRET_KEY}:" -d customer="$CUSTOMER_ID" -d amount=16742 -d currency=usd -d payment_method=pm_card_visa -d setup_future_usage=off_session ) +PAYMENT_METHOD=$(jq -r '.payment_method' <<< "${PAYMENT_INTENT}") +curl https://api.stripe.com/v1/payment_methods/"$PAYMENT_METHOD"/attach -u "$STRIPE_SECRET_KEY": -d customer="$CUSTOMER_ID" +curl https://api.stripe.com/v1/customers/"$CUSTOMER_ID" -u "${STRIPE_SECRET_KEY}:" -d "invoice_settings[default_payment_method]"="$PAYMENT_METHOD" + +# invoicing +storj-up testdata project-usage +DAY=$(date +%d) +MONTH=$(date +%m) +YEAR=$(date +%Y) +if [ $(uname -s) == "Darwin" ] +then + LAST_MONTH=$(date -v-1m +%m) + LAST_MONTH_YEAR=$(date -v-1m +%Y) +else + LAST_MONTH=$(date -d "$(date +%Y-%m-1) -1 month" +%m) + LAST_MONTH_YEAR=$(date -d "$(date +%Y-%m-1) -1 month" +%Y) +fi + +docker compose exec satellite-admin satellite billing prepare-invoice-records "$LAST_MONTH"/"$LAST_MONTH_YEAR" --log.level=info --log.output=stdout +docker compose exec satellite-admin satellite billing create-project-invoice-items "$LAST_MONTH"/"$LAST_MONTH_YEAR" --log.level=info --log.output=stdout +docker compose exec satellite-admin satellite billing create-invoices "$LAST_MONTH"/"$LAST_MONTH_YEAR" --log.level=info --log.output=stdout +docker compose exec satellite-admin satellite billing finalize-invoices --log.level=info --log.output=stdout + +#adding 5 transactions for a total of 20 means 8 should be fully confirmed +for i in {1..5}; do cethacea token transfer 1000000000 0x"$ADDRESS"; done +storj-up health -t billing_transactions -n 8 -d 12 + +docker compose exec satellite-admin satellite billing pay-invoices "$DAY"/"$MONTH"/"$YEAR" --log.level=info --log.output=stdout + +BALANCE=$(curl -X GET -s http://localhost:10000/api/v0/payments/wallet --header "$COOKIE" | jq -r '.balance') + +if [[ $BALANCE == -* ]] +then + exit 1 +fi + +cleanup diff --git a/test/test-reorg.sh b/test/test-reorg.sh index 1ef21ad..15d27e5 100644 --- a/test/test-reorg.sh +++ b/test/test-reorg.sh @@ -12,6 +12,17 @@ while ! { curl -s -X POST -H "Content-Type: application/json" --data '{"jsonrpc" done } +cleanup() { + if [ -f "docker-compose.yaml" ] + then + docker compose down + fi + rm -rf .contracts.yaml + rm -rf storjscan + rm -rf geth + rm -rf docker-compose.yaml +} + go install storj.io/storj-up if [ ! "$(which storjscan )" ]; then @@ -24,11 +35,13 @@ fi export STORJUP_NO_HISTORY=true +cleanup + storj-up init storj,db,billing storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_BILLING_CONFIG_INTERVAL=5s storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_STORJSCAN_INTERVAL=5s +storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_STORJSCAN_CONFIRMATIONS=12 -docker compose down -v docker compose up -d storj-up health @@ -48,7 +61,7 @@ curl -X POST 'http://localhost:10000/api/v0/payments/wallet' --header "$COOKIE" ADDRESS=$(curl -X GET -s http://localhost:10000/api/v0/payments/wallet --header "$COOKIE" | jq -r '.address') #15 transactions means 3 are fully confirmed -for i in {1..15}; do cethacea token transfer 1000 0x"$ADDRESS"; done +for i in {1..15}; do cethacea token transfer 1000000000 0x"$ADDRESS"; done storj-up health -t billing_transactions -n 3 -d 12 #save the last transaction of the base chain @@ -63,7 +76,7 @@ docker compose up -d wait_for_geth #adding 5 transactions for a total of 20 means 8 should be fully confirmed -for i in {1..5}; do cethacea token transfer 1 0x"$ADDRESS"; done +for i in {1..5}; do cethacea token transfer 2000000000 0x"$ADDRESS"; done storj-up health -t billing_transactions -n 8 -d 12 #save the last transaction (0) and compare to the base chain @@ -85,7 +98,7 @@ wait_for_geth storj-up health -t storjscan_payments -n 15 -d 12 #adding 5 different transactions to simulate reorg -for i in {1..5}; do cethacea token transfer 1000 0x"$ADDRESS"; done +for i in {1..5}; do cethacea token transfer 3000000000 0x"$ADDRESS"; done storj-up health -t storjscan_payments -n 20 -d 12 CONTENT=$(curl -s -X GET http://localhost:10000/api/v0/payments/wallet/payments --header "$COOKIE") @@ -100,8 +113,4 @@ if [[ $PREREORG4 == $POSTREORG4 ]]; then exit 1 fi -docker compose down -rm -rf .contracts.yaml -rm -rf storjscan -rm -rf geth -rm -rf docker-compose.yaml +cleanup \ No newline at end of file diff --git a/test/test-storjscan.sh b/test/test-storjscan.sh index 09cf73e..75fa5b2 100644 --- a/test/test-storjscan.sh +++ b/test/test-storjscan.sh @@ -3,13 +3,26 @@ cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" set -ex +cleanup() { + if [ -f "docker-compose.yaml" ] + then + docker compose down + fi + rm -rf .contracts.yaml + rm -rf storjscan + rm -rf geth + rm -rf docker-compose.yaml +} + export STORJUP_NO_HISTORY=true +cleanup + storj-up init storj,db,billing storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_BILLING_CONFIG_INTERVAL=5s storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_STORJSCAN_INTERVAL=5s +storj-up env setenv satellite-api satellite-core satellite-admin STORJ_PAYMENTS_STORJSCAN_CONFIRMATIONS=12 -docker compose down -v docker compose up -d storj-up health @@ -34,14 +47,10 @@ curl -X POST 'http://localhost:10000/api/v0/payments/wallet' --header "$COOKIE" ADDRESS=$(curl -X GET -s http://localhost:10000/api/v0/payments/wallet --header "$COOKIE" | jq -r '.address') #ACCOUNT is defined with environment variables above -for i in {1..15}; do cethacea token transfer 1000 0x"$ADDRESS"; done +for i in {1..15}; do cethacea token transfer 1000000000 0x"$ADDRESS"; done storj-up health -t billing_transactions -n 3 -d 12 curl -X GET http://localhost:10000/api/v0/payments/wallet --header "$COOKIE" -docker compose down -rm -rf .contracts.yaml -rm -rf storjscan -rm -rf geth -rm -rf docker-compose.yaml +cleanup