-
Notifications
You must be signed in to change notification settings - Fork 22
Add list sessions and revoke session methods and tests #483
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
01d9bd4
Add list sessions and revoke session methods and tests
awolfden ffdf169
Refactored types and updated where session is exposed based on review…
awolfden 723c258
Update tests
awolfden 2ee912f
Update implementatino of DEFAULT_LIST_RESPONSE_LIMIT to align with me…
awolfden 16fa806
Update list session arguments to align with type checks
awolfden 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| from typing import Union | ||
|
|
||
| import pytest | ||
|
|
||
| from tests.utils.list_resource import list_response_of | ||
| from tests.utils.syncify import syncify | ||
| from tests.types.test_auto_pagination_function import TestAutoPaginationFunction | ||
| from workos.user_management import AsyncUserManagement, UserManagement | ||
|
|
||
|
|
||
| def _mock_session(id: str): | ||
| now = "2025-07-23T14:00:00.000Z" | ||
| return { | ||
| "object": "session", | ||
| "id": id, | ||
| "user_id": "user_123", | ||
| "organization_id": "org_123", | ||
| "status": "active", | ||
| "auth_method": "password", | ||
| "impersonator": None, | ||
| "ip_address": "192.168.1.1", | ||
| "user_agent": "Mozilla/5.0", | ||
| "expires_at": "2025-07-23T15:00:00.000Z", | ||
| "ended_at": None, | ||
| "created_at": now, | ||
| "updated_at": now, | ||
| } | ||
|
|
||
|
|
||
| @pytest.mark.sync_and_async(UserManagement, AsyncUserManagement) | ||
| class TestUserManagementListSessions: | ||
| @pytest.fixture(autouse=True) | ||
| def setup(self, module_instance: Union[UserManagement, AsyncUserManagement]): | ||
| self.http_client = module_instance._http_client | ||
| self.user_management = module_instance | ||
|
|
||
| def test_list_sessions_query_and_parsing( | ||
| self, capture_and_mock_http_client_request | ||
| ): | ||
| sessions = [_mock_session("session_1"), _mock_session("session_2")] | ||
| response = list_response_of(data=sessions) | ||
| request_kwargs = capture_and_mock_http_client_request( | ||
| self.http_client, response, 200 | ||
| ) | ||
|
|
||
| result = syncify( | ||
| self.user_management.list_sessions( | ||
| user_id="user_123", limit=10, before="before_id", order="desc" | ||
| ) | ||
| ) | ||
|
|
||
| assert request_kwargs["url"].endswith("user_management/users/user_123/sessions") | ||
| assert request_kwargs["method"] == "get" | ||
| assert request_kwargs["params"]["limit"] == 10 | ||
| assert request_kwargs["params"]["before"] == "before_id" | ||
| assert request_kwargs["params"]["order"] == "desc" | ||
| assert "after" not in request_kwargs["params"] | ||
| assert len(result.data) == 2 | ||
| assert result.data[0].id == "session_1" | ||
| assert result.list_metadata.before is None | ||
| assert result.list_metadata.after is None | ||
|
|
||
| def test_list_sessions_auto_pagination( | ||
| self, test_auto_pagination: TestAutoPaginationFunction | ||
| ): | ||
| data = [_mock_session(str(i)) for i in range(40)] | ||
| test_auto_pagination( | ||
| http_client=self.http_client, | ||
| list_function=self.user_management.list_sessions, | ||
| list_function_params={"user_id": "user_123"}, | ||
| expected_all_page_data=data, | ||
| url_path_keys=["user_id"], | ||
| ) |
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,47 @@ | ||
| from typing import Union | ||
|
|
||
| import pytest | ||
|
|
||
| from tests.utils.syncify import syncify | ||
| from workos.user_management import AsyncUserManagement, UserManagement | ||
|
|
||
|
|
||
| def _mock_session(id: str): | ||
| now = "2025-07-23T14:00:00.000Z" | ||
| return { | ||
| "object": "session", | ||
| "id": id, | ||
| "user_id": "user_123", | ||
| "organization_id": "org_123", | ||
| "status": "revoked", | ||
| "auth_method": "password", | ||
| "ip_address": "192.168.1.1", | ||
| "user_agent": "Mozilla/5.0", | ||
| "expires_at": "2025-07-23T15:00:00.000Z", | ||
| "ended_at": now, | ||
| "created_at": now, | ||
| "updated_at": now, | ||
| } | ||
|
|
||
|
|
||
| @pytest.mark.sync_and_async(UserManagement, AsyncUserManagement) | ||
| class TestUserManagementRevokeSession: | ||
| @pytest.fixture(autouse=True) | ||
| def setup(self, module_instance: Union[UserManagement, AsyncUserManagement]): | ||
| self.http_client = module_instance._http_client | ||
| self.user_management = module_instance | ||
|
|
||
| def test_revoke_session(self, capture_and_mock_http_client_request): | ||
| mock = _mock_session("session_abc") | ||
| request_kwargs = capture_and_mock_http_client_request( | ||
| self.http_client, mock, 200 | ||
| ) | ||
|
|
||
| response = syncify( | ||
| self.user_management.revoke_session(session_id="session_abc") | ||
| ) | ||
|
|
||
| assert request_kwargs["url"].endswith("user_management/sessions/revoke") | ||
| assert request_kwargs["method"] == "post" | ||
| assert request_kwargs["json"] == {"session_id": "session_abc"} | ||
| assert response.id == "session_abc" |
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
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.