-
Notifications
You must be signed in to change notification settings - Fork 114
feat(api): per-user resource selection endpoint — Part of #586 #981
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
skypank-coder
wants to merge
9
commits into
OWASP:main
Choose a base branch
from
skypank-coder:feat/586-resource-selection-api
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
9 commits
Select commit
Hold shift + click to select a range
6710627
feat(db): add User model + resource-selection storage + login upsert …
skypank-coder 780a40a
Merge branch 'main' into feat/586-users
skypank-coder 2c0cd6e
fix(db): concurrency-safe upsert_user + drop redundant unique constra…
skypank-coder 9d9a62a
Merge branch 'main' into feat/586-users
skypank-coder 9041061
Merge branch 'main' into feat/586-users
skypank-coder 1bb2424
fix(api): redact login-persistence error log + cover missing-sub skip…
skypank-coder 5156c51
feat(api): add GET/PUT /rest/v1/user/resources (#586)
skypank-coder 95f914c
fix(api): address review — concurrency, request-body spec, value trim…
skypank-coder 7c4fe2c
docs(api): document 401 and require 'selected' in resource-selection …
skypank-coder 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| """Tests for user persistence and per-user resource selection (issue #586, RFC #876 TODO 1/2).""" | ||
|
|
||
| import os | ||
| import unittest | ||
|
|
||
| from sqlalchemy.exc import IntegrityError | ||
|
|
||
| from application import create_app, sqla | ||
| from application.database import db | ||
|
|
||
|
|
||
| class TestUserModel(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
| # These tests exercise only the SQL layer; skip the Neo4j graph load. | ||
| os.environ["NO_LOAD_GRAPH_DB"] = "1" | ||
| self.app = create_app(mode="test") | ||
| self.app_context = self.app.app_context() | ||
| self.app_context.push() | ||
| sqla.create_all() | ||
| self.collection = db.Node_collection() | ||
|
|
||
| def tearDown(self) -> None: | ||
| sqla.session.remove() | ||
| sqla.drop_all() | ||
| self.app_context.pop() | ||
| os.environ.pop("NO_LOAD_GRAPH_DB", None) | ||
|
|
||
| def test_upsert_user_creates_single_row(self) -> None: | ||
| user = self.collection.upsert_user( | ||
| google_sub="sub-123", email="a@example.com", display_name="Alice" | ||
| ) | ||
| self.assertIsNotNone(user.id) | ||
| self.assertEqual(user.google_sub, "sub-123") | ||
| self.assertEqual(user.email, "a@example.com") | ||
| self.assertEqual(user.display_name, "Alice") | ||
| self.assertIsNotNone(user.created_at) | ||
| self.assertIsNotNone(user.last_seen_at) | ||
| self.assertEqual(sqla.session.query(db.User).count(), 1) | ||
|
|
||
| def test_upsert_user_is_idempotent_and_updates_in_place(self) -> None: | ||
| first = self.collection.upsert_user( | ||
| google_sub="sub-123", email="old@example.com", display_name="Alice" | ||
| ) | ||
| first_id = first.id | ||
| created_at = first.created_at | ||
|
|
||
| second = self.collection.upsert_user( | ||
| google_sub="sub-123", email="new@example.com", display_name="Alice B" | ||
| ) | ||
|
|
||
| # No duplicate row, same identity, refreshed profile fields. | ||
| self.assertEqual(sqla.session.query(db.User).count(), 1) | ||
| self.assertEqual(second.id, first_id) | ||
| self.assertEqual(second.email, "new@example.com") | ||
| self.assertEqual(second.display_name, "Alice B") | ||
| self.assertEqual(second.created_at, created_at) | ||
| self.assertGreaterEqual(second.last_seen_at, created_at) | ||
|
|
||
| def test_upsert_user_tolerates_missing_email_and_name(self) -> None: | ||
| user = self.collection.upsert_user( | ||
| google_sub="sub-noemail", email="", display_name=None | ||
| ) | ||
| self.assertEqual(user.email, "") | ||
| self.assertIsNone(user.display_name) | ||
| self.assertEqual(sqla.session.query(db.User).count(), 1) | ||
|
|
||
| def test_get_user_by_sub(self) -> None: | ||
| self.collection.upsert_user( | ||
| google_sub="sub-123", email="a@example.com", display_name="Alice" | ||
| ) | ||
| found = self.collection.get_user_by_sub("sub-123") | ||
| self.assertIsNotNone(found) | ||
| self.assertEqual(found.google_sub, "sub-123") | ||
| self.assertIsNone(self.collection.get_user_by_sub("does-not-exist")) | ||
|
|
||
| def test_resource_selection_round_trips(self) -> None: | ||
| user = self.collection.upsert_user( | ||
| google_sub="sub-123", email="a@example.com", display_name="Alice" | ||
| ) | ||
| # Empty by default. | ||
| self.assertEqual(self.collection.get_user_resource_selection(user.id), []) | ||
|
|
||
| stored = self.collection.set_user_resource_selection( | ||
| user.id, ["ASVS", "CWE", "SAMM"] | ||
| ) | ||
| self.assertEqual(sorted(stored), ["ASVS", "CWE", "SAMM"]) | ||
| self.assertEqual( | ||
| self.collection.get_user_resource_selection(user.id), | ||
| ["ASVS", "CWE", "SAMM"], | ||
| ) | ||
|
|
||
| def test_set_resource_selection_replaces_previous(self) -> None: | ||
| user = self.collection.upsert_user( | ||
| google_sub="sub-123", email="a@example.com", display_name="Alice" | ||
| ) | ||
| self.collection.set_user_resource_selection(user.id, ["ASVS", "CWE"]) | ||
| self.collection.set_user_resource_selection(user.id, ["SAMM"]) | ||
| self.assertEqual(self.collection.get_user_resource_selection(user.id), ["SAMM"]) | ||
| self.assertEqual( | ||
| sqla.session.query(db.UserResourceSelection) | ||
| .filter(db.UserResourceSelection.user_id == user.id) | ||
| .count(), | ||
| 1, | ||
| ) | ||
|
|
||
| def test_set_resource_selection_dedupes_input(self) -> None: | ||
| user = self.collection.upsert_user( | ||
| google_sub="sub-123", email="a@example.com", display_name="Alice" | ||
| ) | ||
| self.collection.set_user_resource_selection(user.id, ["ASVS", "ASVS", "CWE"]) | ||
| self.assertEqual( | ||
| self.collection.get_user_resource_selection(user.id), ["ASVS", "CWE"] | ||
| ) | ||
|
|
||
| def test_resource_selection_is_per_user(self) -> None: | ||
| alice = self.collection.upsert_user( | ||
| google_sub="sub-a", email="a@example.com", display_name="Alice" | ||
| ) | ||
| bob = self.collection.upsert_user( | ||
| google_sub="sub-b", email="b@example.com", display_name="Bob" | ||
| ) | ||
| self.collection.set_user_resource_selection(alice.id, ["ASVS"]) | ||
| self.collection.set_user_resource_selection(bob.id, ["CWE"]) | ||
| self.assertEqual( | ||
| self.collection.get_user_resource_selection(alice.id), ["ASVS"] | ||
| ) | ||
| self.assertEqual(self.collection.get_user_resource_selection(bob.id), ["CWE"]) | ||
|
|
||
| def test_resource_selection_unique_constraint_enforced(self) -> None: | ||
| user = self.collection.upsert_user( | ||
| google_sub="sub-123", email="a@example.com", display_name="Alice" | ||
| ) | ||
| from datetime import datetime, timezone | ||
|
|
||
| sqla.session.add( | ||
| db.UserResourceSelection( | ||
| user_id=user.id, | ||
| standard_name="ASVS", | ||
| created_at=datetime.now(timezone.utc), | ||
| ) | ||
| ) | ||
| sqla.session.add( | ||
| db.UserResourceSelection( | ||
| user_id=user.id, | ||
| standard_name="ASVS", | ||
| created_at=datetime.now(timezone.utc), | ||
| ) | ||
| ) | ||
| with self.assertRaises(IntegrityError): | ||
| sqla.session.commit() | ||
| sqla.session.rollback() | ||
|
|
||
| def test_deleting_user_cascades_to_selection(self) -> None: | ||
| user = self.collection.upsert_user( | ||
| google_sub="sub-123", email="a@example.com", display_name="Alice" | ||
| ) | ||
| self.collection.set_user_resource_selection(user.id, ["ASVS", "CWE"]) | ||
| sqla.session.delete(user) | ||
| sqla.session.commit() | ||
| self.assertEqual(sqla.session.query(db.UserResourceSelection).count(), 0) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.