-
Notifications
You must be signed in to change notification settings - Fork 4
Update linkedin #94
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
davidslusser
wants to merge
3
commits into
main
Choose a base branch
from
update_linkedin
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
Update linkedin #94
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
163 changes: 163 additions & 0 deletions
163
src/django_project/tests/unit/web/test_linkedin_notifier.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,163 @@ | ||
| import os | ||
| import tempfile | ||
| import unittest | ||
| from pathlib import Path | ||
| from unittest.mock import Mock, patch | ||
|
|
||
| import requests | ||
|
|
||
| BASE_DIR = Path(__file__).parents[4] | ||
| os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings") | ||
| os.environ.setdefault("ENV_PATH", f"{BASE_DIR}/envs/.env.test") | ||
|
|
||
| from web.utilities.notifiers.linkedin import LinkedInOrganizationClient | ||
|
|
||
|
|
||
| class DummyCredentialManager: | ||
| def __init__(self, credential): | ||
| self.credential = credential | ||
|
|
||
| def select_for_update(self): | ||
| return self | ||
|
|
||
| def get(self, pk): | ||
| return self.credential | ||
|
|
||
|
|
||
| class DummyCredential: | ||
| objects = None | ||
|
|
||
| def __init__(self): | ||
| self.pk = 1 | ||
| self.access_token = "old-token" | ||
| self.refresh_token = "refresh-token" | ||
| self.access_token_expires_at = None | ||
| self.refresh_token_expires_at = None | ||
| self.save = Mock() | ||
|
|
||
|
|
||
| class LinkedInOrganizationClientTests(unittest.TestCase): | ||
| def build_client(self, env_path: str | None = None, access_token: str | None = "old-token") -> LinkedInOrganizationClient: | ||
| return LinkedInOrganizationClient( | ||
| access_token=access_token, | ||
| organization_urn="urn:li:organization:107506588", | ||
| client_id="client-id", | ||
| client_secret="client-secret", | ||
| refresh_token="refresh-token", | ||
| env_path=env_path, | ||
| ) | ||
|
|
||
| def test_refresh_access_token_updates_settings_and_env_file(self): | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| temp_env = Path(temp_dir) / ".env.test" | ||
| temp_env.write_text( | ||
| 'LINKEDIN_ACCESS_TOKEN="old-token"\nLINKEDIN_REFRESH_TOKEN="refresh-token"\n', | ||
| encoding="utf-8", | ||
| ) | ||
| client = self.build_client(env_path=str(temp_env)) | ||
|
|
||
| response = Mock() | ||
| response.json.return_value = { | ||
| "access_token": "new-token", | ||
| "refresh_token": "new-refresh-token", | ||
| } | ||
| response.raise_for_status.return_value = None | ||
|
|
||
| with ( | ||
| patch("web.utilities.notifiers.linkedin.requests.post", return_value=response) as mock_post, | ||
| patch("web.utilities.notifiers.linkedin.settings") as mock_settings, | ||
| ): | ||
| client.refresh_access_token() | ||
|
|
||
| self.assertEqual(client.access_token, "new-token") | ||
| self.assertEqual(client.refresh_token, "new-refresh-token") | ||
| self.assertIn('LINKEDIN_ACCESS_TOKEN="new-token"', temp_env.read_text(encoding="utf-8")) | ||
| self.assertIn('LINKEDIN_REFRESH_TOKEN="new-refresh-token"', temp_env.read_text(encoding="utf-8")) | ||
| mock_post.assert_called_once() | ||
| self.assertEqual(mock_settings.LINKEDIN_ACCESS_TOKEN, "new-token") | ||
| self.assertEqual(mock_settings.LINKEDIN_REFRESH_TOKEN, "new-refresh-token") | ||
|
|
||
| def test_post_retries_once_after_auth_failure(self): | ||
| client = self.build_client() | ||
|
|
||
| auth_failure_response = Mock(status_code=401) | ||
| auth_failure_response.raise_for_status.side_effect = requests.HTTPError(response=auth_failure_response) # type: ignore[name-defined] | ||
|
|
||
| refresh_response = Mock() | ||
| refresh_response.json.return_value = { | ||
| "access_token": "new-token", | ||
| "refresh_token": "new-refresh-token", | ||
| } | ||
| refresh_response.raise_for_status.return_value = None | ||
|
|
||
| success_response = Mock(status_code=201) | ||
| success_response.raise_for_status.return_value = None | ||
|
|
||
| with ( | ||
| patch( | ||
| "web.utilities.notifiers.linkedin.requests.post", | ||
| side_effect=[auth_failure_response, refresh_response, success_response], | ||
| ) as mock_post, | ||
| patch("web.utilities.notifiers.linkedin.settings"), | ||
| ): | ||
| response = client.post_organization_post("hello world") | ||
|
|
||
| self.assertIs(response, success_response) | ||
| self.assertEqual(client.access_token, "new-token") | ||
| self.assertEqual(mock_post.call_count, 3) | ||
|
|
||
| def test_refresh_access_token_updates_db_credential_when_present(self): | ||
| credential = DummyCredential() | ||
| credential.__class__.objects = DummyCredentialManager(credential) | ||
|
|
||
| client = LinkedInOrganizationClient( | ||
| access_token="old-token", | ||
| organization_urn="urn:li:organization:107506588", | ||
| client_id="client-id", | ||
| client_secret="client-secret", | ||
| refresh_token="refresh-token", | ||
| credential=credential, | ||
| ) | ||
|
|
||
| refresh_response = Mock() | ||
| refresh_response.json.return_value = { | ||
| "access_token": "new-token", | ||
| "refresh_token": "new-refresh-token", | ||
| "expires_in": 3600, | ||
| "refresh_token_expires_in": 7200, | ||
| } | ||
| refresh_response.raise_for_status.return_value = None | ||
|
|
||
| with ( | ||
| patch("web.utilities.notifiers.linkedin.requests.post", return_value=refresh_response), | ||
| patch("web.utilities.notifiers.linkedin.settings"), | ||
| ): | ||
| client.refresh_access_token() | ||
|
|
||
| self.assertEqual(credential.access_token, "new-token") | ||
| self.assertEqual(credential.refresh_token, "new-refresh-token") | ||
| credential.save.assert_called_once() | ||
|
|
||
| def test_missing_tokens_can_be_bootstrapped_from_refresh_credentials(self): | ||
| client = self.build_client(access_token=None) | ||
|
|
||
| refresh_response = Mock() | ||
| refresh_response.json.return_value = { | ||
| "access_token": "new-token", | ||
| "refresh_token": "new-refresh-token", | ||
| } | ||
| refresh_response.raise_for_status.return_value = None | ||
|
|
||
| success_response = Mock(status_code=201) | ||
| success_response.raise_for_status.return_value = None | ||
|
|
||
| with ( | ||
| patch( | ||
| "web.utilities.notifiers.linkedin.requests.post", | ||
| side_effect=[refresh_response, success_response], | ||
| ), | ||
| patch("web.utilities.notifiers.linkedin.settings"), | ||
| ): | ||
| client.post_organization_post("hello world") | ||
|
|
||
| self.assertEqual(client.access_token, "new-token") |
79 changes: 79 additions & 0 deletions
79
src/django_project/tests/unit/web/test_linkedin_oauth_command.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,79 @@ | ||
| import os | ||
| import unittest | ||
| from pathlib import Path | ||
| from unittest.mock import Mock, patch | ||
|
|
||
| BASE_DIR = Path(__file__).parents[4] | ||
| os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings") | ||
| os.environ.setdefault("ENV_PATH", f"{BASE_DIR}/envs/.env.test") | ||
|
|
||
| from web.management.commands.linkedin_oauth import Command | ||
|
|
||
|
|
||
| class LinkedInOAuthCommandTests(unittest.TestCase): | ||
| def test_show_url_outputs_authorization_url(self): | ||
| command = Command() | ||
| command.stdout = Mock() | ||
|
|
||
| with ( | ||
| patch("web.management.commands.linkedin_oauth.apps.get_model") as mock_get_model, | ||
| patch("web.management.commands.linkedin_oauth.settings") as mock_settings, | ||
| patch("web.management.commands.linkedin_oauth.LinkedInOrganizationClient") as mock_client_class, | ||
| ): | ||
| mock_settings.LINKEDIN_CLIENT_ID = "client-id" | ||
| mock_settings.LINKEDIN_CLIENT_SECRET = "client-secret" | ||
| mock_settings.LINKEDIN_ACCESS_TOKEN = None | ||
| mock_settings.LINKEDIN_ORGANIZATION_URN = "urn:li:organization:107506588" | ||
| mock_settings.LINKEDIN_REFRESH_TOKEN = None | ||
| mock_settings.ENV_PATH = "/tmp/.env.test" | ||
|
|
||
| mock_credential = Mock(access_token=None, refresh_token=None) | ||
| mock_get_model.return_value.objects.get_or_create.return_value = (mock_credential, True) | ||
|
|
||
| mock_client = mock_client_class.return_value | ||
| mock_client.build_authorization_url.return_value = "https://www.linkedin.com/oauth/v2/authorization?x=1" | ||
|
|
||
| command.handle(redirect_uri="https://example.com/callback", scope=command.default_scope, state=None, code=None) | ||
|
|
||
| command.stdout.write.assert_any_call("Open this URL in a browser and complete the LinkedIn consent flow:") | ||
| command.stdout.write.assert_any_call("https://www.linkedin.com/oauth/v2/authorization?x=1") | ||
|
|
||
| def test_code_exchange_stores_tokens(self): | ||
| command = Command() | ||
| command.stdout = Mock() | ||
|
|
||
| with ( | ||
| patch("web.management.commands.linkedin_oauth.apps.get_model") as mock_get_model, | ||
| patch("web.management.commands.linkedin_oauth.settings") as mock_settings, | ||
| patch("web.management.commands.linkedin_oauth.LinkedInOrganizationClient") as mock_client_class, | ||
| ): | ||
| mock_settings.LINKEDIN_CLIENT_ID = "client-id" | ||
| mock_settings.LINKEDIN_CLIENT_SECRET = "client-secret" | ||
| mock_settings.LINKEDIN_ACCESS_TOKEN = None | ||
| mock_settings.LINKEDIN_ORGANIZATION_URN = "urn:li:organization:107506588" | ||
| mock_settings.LINKEDIN_REFRESH_TOKEN = None | ||
| mock_settings.ENV_PATH = "/tmp/.env.test" | ||
|
|
||
| mock_credential = Mock(access_token=None, refresh_token=None) | ||
| mock_get_model.return_value.objects.get_or_create.return_value = (mock_credential, True) | ||
|
|
||
| mock_client = mock_client_class.return_value | ||
| mock_client.exchange_authorization_code.return_value = { | ||
| "access_token": "new-token", | ||
| "refresh_token": "new-refresh-token", | ||
| "expires_in": 5184000, | ||
| "refresh_token_expires_in": 31536000, | ||
| } | ||
|
|
||
| command.handle( | ||
| redirect_uri="https://example.com/callback", | ||
| scope=command.default_scope, | ||
| state=None, | ||
| code="auth-code", | ||
| show_url=False, | ||
| ) | ||
|
|
||
| mock_client.exchange_authorization_code.assert_called_once_with( | ||
| code="auth-code", | ||
| redirect_uri="https://example.com/callback", | ||
| ) |
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 @@ | ||
|
|
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 @@ | ||
|
|
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.
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.
Suggestion: This adds a publicly reachable OAuth callback endpoint that maps to a view which returns the raw authorization
codeandstatein the response body. Exposing OAuth callback parameters this way can leak short-lived auth codes through browser history sharing, proxy/body logging, and observability tooling. Restrict this route to non-production/admin-only usage or handle the code server-side and immediately redirect without echoing secrets. [security]Severity Level: Critical 🚨
Steps of Reproduction ✅
Fix in Cursor | Fix in VSCode Claude
(Use Cmd/Ctrl + Click for best experience)
Prompt for AI Agent 🤖