-
Notifications
You must be signed in to change notification settings - Fork 3
feat: ci integration #154
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
Draft
Andrei997
wants to merge
15
commits into
main
Choose a base branch
from
feat-ci-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
feat: ci integration #154
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ae26f7d
feat: add StripeClient for CI integration
Andrei997 c482b4b
test: add example sdk test and fix test client
Andrei997 e9016e3
test: and submit usage report test
Andrei997 555a073
test: add test clock usage example
Andrei997 c2e5d0f
test: add tests for get_authorized_jwt
Andrei997 1469e2e
test: add non paying user tests
Andrei997 0f21927
test: configure payment ci test
Andrei997 3082eee
chore: add date-util to dev req
Andrei997 da2bd8e
test: correctly ignore payment tests
Andrei997 f103fe0
fix: ignore payment tests again
Andrei997 8b46b3d
test: add test logging
Andrei997 8fbd5a7
fix: specify hubble url correctly
Andrei997 fdc4d1d
fix: remove extra slash
Andrei997 ce2753f
fix: do not return None object
Andrei997 0519bd1
refactor: remove useless logging code
Andrei997 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,39 @@ | ||
| import os | ||
| import tempfile | ||
|
|
||
| import pytest | ||
| from hubble.payment.client import PaymentClient | ||
|
|
||
| from .utils.stripe import StripeClient | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def tmpfile(tmpdir): | ||
| tmpfile = f'jina_test_{next(tempfile._get_candidate_names())}.db' | ||
| return tmpdir / tmpfile | ||
|
|
||
|
|
||
| @pytest.fixture(scope='session') | ||
| def m2m_token(): | ||
| return os.environ.get('M2M_TOKEN', None) | ||
|
|
||
|
|
||
| # fixture for acquiring a 'cached' instance of StripeClient | ||
| @pytest.fixture(scope='session') | ||
| def stripe_client(): | ||
| api_key = os.environ.get('STRIPE_SECRET_KEY', None) | ||
| client = StripeClient(api_key) | ||
| yield client | ||
| client.cleanup() | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def payment_client(m2m_token): | ||
| payment_client = PaymentClient(m2m_token=m2m_token) | ||
| yield payment_client | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def user_token(payment_client, request): | ||
| user_token = payment_client.get_user_token(user_id=request.param)['data'] | ||
| yield user_token |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import time | ||
| from datetime import datetime | ||
|
|
||
| import pytest | ||
| from dateutil.relativedelta import relativedelta | ||
| from mock import patch # noqa: F401 | ||
|
|
||
| INTERNAL_APP_ID = 'hubble-sdk' | ||
| INTERNAL_PRODUCT_ID = 'hubble-sdk' | ||
|
|
||
| PRICE_STRIPE_ID = 'price_1MTl37AkuPxeor9kLZxJ5lfd' | ||
|
|
||
| NON_PAYING_USER_ID_1 = '63d75509234f12b36dbd8b36' | ||
| NON_PAYING_USER_EMAIL_1 = 'hubble_sdk_user_3@jina.ai' | ||
|
|
||
| NON_PAYING_USER_ID_2 = '63d7551c234f12b36dbd8dca' | ||
| NON_PAYING_USER_EMAIL_2 = 'hubble_sdk_user_4@jina.ai' | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('user_token', [NON_PAYING_USER_ID_1], indirect=True) | ||
| def test_get_summary(stripe_client, payment_client, user_token): | ||
|
|
||
| # creating stripe customer for user | ||
| customer = stripe_client.get_customer(email=NON_PAYING_USER_EMAIL_1) | ||
|
|
||
| summary = payment_client.get_summary(token=user_token, app_id=INTERNAL_APP_ID) | ||
| expected_result = {'subscriptionItems': [], 'hasPaymentMethod': False} | ||
| assert summary['data'] == expected_result | ||
|
|
||
| # creating subscription | ||
| stripe_client.create_subscription( | ||
| customer_id=customer['customer_id'], items=[PRICE_STRIPE_ID] | ||
| ) | ||
|
|
||
| summary = payment_client.get_summary(token=user_token, app_id=INTERNAL_APP_ID) | ||
|
|
||
| expected_result = { | ||
| 'subscriptionItems': [ | ||
| { | ||
| 'internalAppId': INTERNAL_APP_ID, | ||
| 'internalProductId': INTERNAL_PRODUCT_ID, | ||
| 'usageQuantity': 0, | ||
| } | ||
| ], | ||
| 'hasPaymentMethod': False, | ||
| } | ||
|
|
||
| assert summary['data'] == expected_result | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('user_token', [NON_PAYING_USER_ID_2], indirect=True) | ||
| def test_submit_usage_report(stripe_client, payment_client, user_token): | ||
|
|
||
| # try to submit a usage report | ||
| customer = stripe_client.get_customer(email=NON_PAYING_USER_EMAIL_2) | ||
|
|
||
| stripe_client.create_subscription( | ||
| customer_id=customer['customer_id'], items=[PRICE_STRIPE_ID] | ||
| ) | ||
|
|
||
| payment_client.report_usage( | ||
| token=user_token, | ||
| app_id=INTERNAL_APP_ID, | ||
| product_id=INTERNAL_PRODUCT_ID, | ||
| quantity=100, | ||
| ) | ||
|
|
||
| # NOTE: sleeping to wait for the usage report to be processed | ||
| time.sleep(75) | ||
|
|
||
| summary = payment_client.get_summary(token=user_token, app_id=INTERNAL_APP_ID) | ||
|
|
||
| expected_result = { | ||
| 'subscriptionItems': [ | ||
| { | ||
| 'internalAppId': INTERNAL_APP_ID, | ||
| 'internalProductId': INTERNAL_PRODUCT_ID, | ||
| 'usageQuantity': 100, | ||
| } | ||
| ], | ||
| 'hasPaymentMethod': False, | ||
| } | ||
|
|
||
| assert summary['data'] == expected_result | ||
|
|
||
| # advancing test clock by one month | ||
| # this will trigger a new subscription period | ||
| now = datetime.now() | ||
| later = now + relativedelta(days=+35) | ||
| stripe_client.advance_clock(test_clock_id=customer['test_clock_id'], date=later) | ||
|
|
||
| summary = payment_client.get_summary(token=user_token, app_id=INTERNAL_APP_ID) | ||
|
|
||
| expected_result = {'subscriptionItems': [], 'hasPaymentMethod': False} | ||
|
|
||
| assert summary['data'] == expected_result | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'user_token', [NON_PAYING_USER_ID_1, NON_PAYING_USER_ID_2], indirect=True | ||
| ) | ||
| def test_get_authorized_jwt(payment_client, user_token): | ||
| jwt = payment_client.get_authorized_jwt(token=user_token)['data'] | ||
| is_authorized = payment_client.verify_authorized_jwt(token=jwt) | ||
| assert is_authorized is True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import time | ||
| from datetime import datetime | ||
|
|
||
| import pytest | ||
| from dateutil.relativedelta import relativedelta | ||
| from mock import patch # noqa: F401 | ||
|
|
||
| INTERNAL_APP_ID = 'hubble-sdk' | ||
| INTERNAL_PRODUCT_ID = 'hubble-sdk' | ||
|
|
||
| PRICE_STRIPE_ID = 'price_1MTl37AkuPxeor9kLZxJ5lfd' | ||
|
|
||
| PAYING_USER_ID_1 = '63d754c6234f12b36dbd827f' | ||
| PAYING_USER_EMAIL_1 = 'hubble_sdk_user_1@jina.ai' | ||
|
|
||
| PAYING_USER_ID_2 = '63d754de234f12b36dbd8580' | ||
| PAYING_USER_EMAIL_2 = 'hubble_sdk_user_2@jina.ai' | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('user_token', [PAYING_USER_ID_1], indirect=True) | ||
| def test_get_summary(stripe_client, payment_client, user_token): | ||
|
|
||
| # creating stripe customer for user | ||
| customer = stripe_client.get_customer( | ||
| email=PAYING_USER_EMAIL_1, payment_method='pm_card_visa' | ||
| ) | ||
|
|
||
| summary = payment_client.get_summary(token=user_token, app_id=INTERNAL_APP_ID) | ||
| expected_result = {'subscriptionItems': [], 'hasPaymentMethod': True} | ||
| assert summary['data'] == expected_result | ||
|
|
||
| # creating subscription | ||
| stripe_client.create_subscription( | ||
| customer_id=customer['customer_id'], items=[PRICE_STRIPE_ID] | ||
| ) | ||
|
|
||
| summary = payment_client.get_summary(token=user_token, app_id=INTERNAL_APP_ID) | ||
|
|
||
| expected_result = { | ||
| 'subscriptionItems': [ | ||
| { | ||
| 'internalAppId': INTERNAL_APP_ID, | ||
| 'internalProductId': INTERNAL_PRODUCT_ID, | ||
| 'usageQuantity': 0, | ||
| } | ||
| ], | ||
| 'hasPaymentMethod': True, | ||
| } | ||
|
|
||
| assert summary['data'] == expected_result | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('user_token', [PAYING_USER_ID_2], indirect=True) | ||
| def test_submit_usage_report(stripe_client, payment_client, user_token): | ||
|
|
||
| # try to submit a usage report | ||
| customer = stripe_client.get_customer( | ||
| email=PAYING_USER_EMAIL_2, payment_method='pm_card_visa' | ||
| ) | ||
|
|
||
| stripe_client.create_subscription( | ||
| customer_id=customer['customer_id'], items=[PRICE_STRIPE_ID] | ||
| ) | ||
|
|
||
| payment_client.report_usage( | ||
| token=user_token, | ||
| app_id=INTERNAL_APP_ID, | ||
| product_id=INTERNAL_PRODUCT_ID, | ||
| quantity=100, | ||
| ) | ||
|
|
||
| # NOTE: sleeping to wait for the usage report to be processed | ||
| time.sleep(75) | ||
|
|
||
| summary = payment_client.get_summary(token=user_token, app_id=INTERNAL_APP_ID) | ||
|
|
||
| expected_result = { | ||
| 'subscriptionItems': [ | ||
| { | ||
| 'internalAppId': INTERNAL_APP_ID, | ||
| 'internalProductId': INTERNAL_PRODUCT_ID, | ||
| 'usageQuantity': 100, | ||
| } | ||
| ], | ||
| 'hasPaymentMethod': True, | ||
| } | ||
|
|
||
| assert summary['data'] == expected_result | ||
|
|
||
| # advancing test clock by one month | ||
| # this will trigger a new subscription period | ||
| now = datetime.now() | ||
| later = now + relativedelta(days=+35) | ||
| stripe_client.advance_clock(test_clock_id=customer['test_clock_id'], date=later) | ||
|
|
||
| summary = payment_client.get_summary(token=user_token, app_id=INTERNAL_APP_ID) | ||
|
|
||
| expected_result = { | ||
| 'subscriptionItems': [ | ||
| { | ||
| 'internalAppId': INTERNAL_APP_ID, | ||
| 'internalProductId': INTERNAL_PRODUCT_ID, | ||
| 'usageQuantity': 0, | ||
| } | ||
| ], | ||
| 'hasPaymentMethod': True, | ||
| } | ||
|
|
||
| assert summary['data'] == expected_result | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'user_token', [PAYING_USER_ID_1, PAYING_USER_ID_2], indirect=True | ||
| ) | ||
| def test_get_authorized_jwt(payment_client, user_token): | ||
| jwt = payment_client.get_authorized_jwt(token=user_token)['data'] | ||
| is_authorized = payment_client.verify_authorized_jwt(token=jwt) | ||
| assert is_authorized is True |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary change (
' 'at the end of line)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'm moving these two PR's back to draft mode and re-factor after payment v4.