|
1 | 1 | """Tests for the webserver module.""" |
2 | 2 |
|
3 | | -import os |
4 | 3 | from unittest.mock import MagicMock, PropertyMock, patch |
5 | 4 |
|
6 | 5 | import pytest |
@@ -28,74 +27,65 @@ def mock_handler(self): |
28 | 27 | return handler |
29 | 28 |
|
30 | 29 | def test_store_sso_tokens_called_when_keyring_enabled(self, mock_handler): |
31 | | - """Verify store_sso_tokens is called when keyring is enabled.""" |
32 | | - # Ensure env var is not set |
33 | | - env = os.environ.copy() |
34 | | - env.pop("CLOUDSMITH_NO_KEYRING", None) |
| 30 | + """Verify store_sso_tokens is called and returns True when keyring is enabled.""" |
| 31 | + with patch( |
| 32 | + "cloudsmith_cli.cli.webserver.store_sso_tokens", return_value=True |
| 33 | + ) as mock_store: |
| 34 | + with patch.object(mock_handler, "_return_success_response"): |
| 35 | + with patch.object( |
| 36 | + AuthenticationWebRequestHandler, |
| 37 | + "query_data", |
| 38 | + new_callable=PropertyMock, |
| 39 | + ) as mock_query: |
| 40 | + with patch.object( |
| 41 | + AuthenticationWebRequestHandler, |
| 42 | + "api_host", |
| 43 | + new_callable=PropertyMock, |
| 44 | + ) as mock_host: |
| 45 | + mock_query.return_value = { |
| 46 | + "access_token": "test_access_token", |
| 47 | + "refresh_token": "test_refresh_token", |
| 48 | + } |
| 49 | + mock_host.return_value = "https://api.cloudsmith.io" |
35 | 50 |
|
36 | | - with patch.dict(os.environ, env, clear=True): |
37 | | - with patch("cloudsmith_cli.cli.webserver.store_sso_tokens") as mock_store: |
38 | | - with patch( |
39 | | - "cloudsmith_cli.cli.webserver.should_use_keyring", return_value=True |
40 | | - ): |
41 | | - with patch.object(mock_handler, "_return_success_response"): |
| 51 | + mock_handler.do_GET() |
| 52 | + |
| 53 | + mock_store.assert_called_once_with( |
| 54 | + "https://api.cloudsmith.io", |
| 55 | + "test_access_token", |
| 56 | + "test_refresh_token", |
| 57 | + ) |
| 58 | + |
| 59 | + def test_message_shown_when_keyring_disabled(self, mock_handler): |
| 60 | + """Verify message is shown when store_sso_tokens returns False.""" |
| 61 | + with patch( |
| 62 | + "cloudsmith_cli.cli.webserver.store_sso_tokens", return_value=False |
| 63 | + ) as mock_store: |
| 64 | + with patch("click.echo") as mock_echo: |
| 65 | + with patch.object(mock_handler, "_return_success_response"): |
| 66 | + with patch.object( |
| 67 | + AuthenticationWebRequestHandler, |
| 68 | + "query_data", |
| 69 | + new_callable=PropertyMock, |
| 70 | + ) as mock_query: |
42 | 71 | with patch.object( |
43 | 72 | AuthenticationWebRequestHandler, |
44 | | - "query_data", |
| 73 | + "api_host", |
45 | 74 | new_callable=PropertyMock, |
46 | | - ) as mock_query: |
47 | | - with patch.object( |
48 | | - AuthenticationWebRequestHandler, |
49 | | - "api_host", |
50 | | - new_callable=PropertyMock, |
51 | | - ) as mock_host: |
52 | | - mock_query.return_value = { |
53 | | - "access_token": "test_access_token", |
54 | | - "refresh_token": "test_refresh_token", |
55 | | - } |
56 | | - mock_host.return_value = "https://api.cloudsmith.io" |
57 | | - |
58 | | - mock_handler.do_GET() |
59 | | - |
60 | | - mock_store.assert_called_once_with( |
61 | | - "https://api.cloudsmith.io", |
62 | | - "test_access_token", |
63 | | - "test_refresh_token", |
64 | | - ) |
65 | | - |
66 | | - def test_store_sso_tokens_not_called_when_keyring_disabled(self, mock_handler): |
67 | | - """Verify store_sso_tokens is NOT called when CLOUDSMITH_NO_KEYRING=1.""" |
68 | | - with patch.dict(os.environ, {"CLOUDSMITH_NO_KEYRING": "1"}): |
69 | | - with patch("cloudsmith_cli.cli.webserver.store_sso_tokens") as mock_store: |
70 | | - with patch( |
71 | | - "cloudsmith_cli.cli.webserver.should_use_keyring", |
72 | | - return_value=False, |
73 | | - ): |
74 | | - with patch("click.echo") as mock_echo: |
75 | | - with patch.object(mock_handler, "_return_success_response"): |
76 | | - with patch.object( |
77 | | - AuthenticationWebRequestHandler, |
78 | | - "query_data", |
79 | | - new_callable=PropertyMock, |
80 | | - ) as mock_query: |
81 | | - with patch.object( |
82 | | - AuthenticationWebRequestHandler, |
83 | | - "api_host", |
84 | | - new_callable=PropertyMock, |
85 | | - ) as mock_host: |
86 | | - mock_query.return_value = { |
87 | | - "access_token": "test_access_token", |
88 | | - "refresh_token": "test_refresh_token", |
89 | | - } |
90 | | - mock_host.return_value = "https://api.cloudsmith.io" |
| 75 | + ) as mock_host: |
| 76 | + mock_query.return_value = { |
| 77 | + "access_token": "test_access_token", |
| 78 | + "refresh_token": "test_refresh_token", |
| 79 | + } |
| 80 | + mock_host.return_value = "https://api.cloudsmith.io" |
91 | 81 |
|
92 | | - mock_handler.do_GET() |
| 82 | + mock_handler.do_GET() |
93 | 83 |
|
94 | | - # store_sso_tokens should NOT be called |
95 | | - mock_store.assert_not_called() |
| 84 | + # store_sso_tokens should be called (returns False) |
| 85 | + mock_store.assert_called_once() |
96 | 86 |
|
97 | | - # Message should be displayed to stderr |
98 | | - mock_echo.assert_called_once_with( |
99 | | - "SSO tokens not stored (CLOUDSMITH_NO_KEYRING is set)", |
100 | | - err=True, |
101 | | - ) |
| 87 | + # Message should be displayed to stderr |
| 88 | + mock_echo.assert_called_once_with( |
| 89 | + "SSO tokens not stored (CLOUDSMITH_NO_KEYRING is set)", |
| 90 | + err=True, |
| 91 | + ) |
0 commit comments