-
-
Notifications
You must be signed in to change notification settings - Fork 32
feat: add trial check and don't allow forecast access #365
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
Open
suvanbanerjee
wants to merge
5
commits into
main
Choose a base branch
from
feat/block-trial-users
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
62e9374
feat(v0):add trial check and dont allow forecast access
suvanbanerjee bd1fa6d
Merge branch 'main' into feat/block-trial-users
suvanbanerjee e25b4be
fix(v0): UI returning validation errors for start_datetime_utc and en…
suvanbanerjee 232aaf9
feat(v1): add trial expired check and related tests
suvanbanerjee 31b2754
Merge branch 'main' into feat/block-trial-users
suvanbanerjee 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
18 changes: 18 additions & 0 deletions
18
src/quartz_api/internal/service/uk_national/test_time_utils.py
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,18 @@ | ||
| """Unit tests for the trial-expiry helper in uk_national time_utils.""" | ||
|
|
||
| import datetime as dt | ||
|
|
||
| from .time_utils import trial_expired | ||
|
|
||
|
|
||
| def test_trial_expired_true_when_claim_in_past() -> None: | ||
| now = dt.datetime(2026, 1, 1, tzinfo=dt.UTC) | ||
| auth = {"app_metadata": {"trial_ends_at": "2025-12-31T00:00:00.000Z"}} | ||
| assert trial_expired(auth, now) is True | ||
|
|
||
|
|
||
| def test_trial_expired_false_when_claim_missing_or_future() -> None: | ||
| now = dt.datetime(2026, 1, 1, tzinfo=dt.UTC) | ||
| assert trial_expired({}, now) is False | ||
| auth = {"app_metadata": {"trial_ends_at": "2026-06-01T00:00:00.000Z"}} | ||
| assert trial_expired(auth, now) is False |
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 |
|---|---|---|
|
|
@@ -13,12 +13,34 @@ | |
| # and the time clipping is done then | ||
| INTRADAY_LIMIT_HOURS = int(os.getenv("INTRADAY_LIMIT_HOURS", 8)) | ||
|
|
||
| def trial_expired(auth: AuthDependency, now: dt.datetime) -> bool: | ||
| """Check whether the caller's trial has ended, per the app_metadata.trial_ends_at claim. | ||
|
|
||
| No claim at all (paid/non-trial user) is treated the same as an unexpired | ||
| trial: both mean "don't clamp." Only a claim that has actually passed | ||
| counts as expired. | ||
|
Comment on lines
+19
to
+21
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @braddf I’ve assumed that if a user does not have the trial_ends_at attribute, they are a paid user. Is this assumption correct? |
||
| """ | ||
| app_metadata = auth.get("app_metadata", {}) | ||
| if not isinstance(app_metadata, dict): | ||
| return False | ||
| raw = app_metadata.get("trial_ends_at") | ||
| if raw is None: | ||
| return False | ||
| try: | ||
| ends_at = dt.datetime.fromisoformat(str(raw)) | ||
| except ValueError: | ||
| sentry_sdk.capture_message(f"Unparseable trial_ends_at claim: {raw!r}") | ||
| return False | ||
| if ends_at.tzinfo is None: | ||
| ends_at = ends_at.replace(tzinfo=dt.UTC) | ||
| return ends_at <= now | ||
|
|
||
|
|
||
| def limit_end_datetime_by_permissions( | ||
| auth: AuthDependency, | ||
| end_datetime_utc: models.UTCDatetimeDefaultWindowEndNonAware, | ||
| ) -> dt.datetime: | ||
| """Ensures only users with required permissions can access intraday data.""" | ||
| """Ensures only users with required permissions/an active trial can see future data.""" | ||
| permissions: str | list[str] = auth.get("permissions", []) | ||
| if isinstance(permissions, str): | ||
| permissions = [permissions] | ||
|
|
@@ -30,6 +52,8 @@ def limit_end_datetime_by_permissions( | |
| return end_datetime_utc | ||
|
|
||
| intraday_max_allowed = dt.datetime.now(dt.UTC) + dt.timedelta(hours=INTRADAY_LIMIT_HOURS) | ||
| if trial_expired(auth, dt.datetime.now(dt.UTC)): | ||
| return min(end_datetime_utc, dt.datetime.now(dt.UTC)) | ||
| if "read:uk-intraday" in permissions: | ||
| return min(end_datetime_utc, intraday_max_allowed) | ||
|
|
||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| """Unit tests for the trial-expiry helper in v1 helpers.""" | ||
|
|
||
| import datetime as dt | ||
|
|
||
| from .helpers import trial_expired | ||
|
|
||
|
|
||
| def test_trial_expired_true_when_claim_in_past() -> None: | ||
| now = dt.datetime(2026, 1, 1, tzinfo=dt.UTC) | ||
| auth = {"app_metadata": {"trial_ends_at": "2025-12-31T00:00:00.000Z"}} | ||
| assert trial_expired(auth, now) is True | ||
|
|
||
|
|
||
| def test_trial_expired_false_when_claim_missing_or_future() -> None: | ||
| now = dt.datetime(2026, 1, 1, tzinfo=dt.UTC) | ||
| assert trial_expired({}, now) is False | ||
| auth = {"app_metadata": {"trial_ends_at": "2026-06-01T00:00:00.000Z"}} | ||
| assert trial_expired(auth, now) is False |
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.
@braddf I think it should be
read:uk-intraday? as in thwe time utils we are checking for