Skip to content

Commit

Permalink
test/{test-invoice,test-payment}: Add test for receiving payments dur…
Browse files Browse the repository at this point in the history
…ing 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
  • Loading branch information
dlamarmorgan committed Oct 5, 2022
1 parent c50b39c commit 3a929b4
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 30 deletions.
47 changes: 33 additions & 14 deletions test/test-invoice.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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')

Expand All @@ -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
109 changes: 109 additions & 0 deletions test/test-payment.sh
Original file line number Diff line number Diff line change
@@ -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
27 changes: 18 additions & 9 deletions test/test-reorg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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
23 changes: 16 additions & 7 deletions test/test-storjscan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 3a929b4

Please sign in to comment.