|
| 1 | +import pytest |
| 2 | + |
| 3 | +from tests.utils.syncify import syncify |
| 4 | +from workos.pipes import AsyncPipes, Pipes |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.sync_and_async(Pipes, AsyncPipes) |
| 8 | +class TestPipes: |
| 9 | + @pytest.fixture |
| 10 | + def mock_access_token(self): |
| 11 | + return { |
| 12 | + "object": "access_token", |
| 13 | + "access_token": "test_access_token_123", |
| 14 | + "expires_at": "2026-01-09T12:00:00.000Z", |
| 15 | + "scopes": ["read:users", "write:users"], |
| 16 | + "missing_scopes": [], |
| 17 | + } |
| 18 | + |
| 19 | + def test_get_access_token_success_with_expiry( |
| 20 | + self, |
| 21 | + module_instance, |
| 22 | + mock_access_token, |
| 23 | + capture_and_mock_http_client_request, |
| 24 | + ): |
| 25 | + response_body = { |
| 26 | + "active": True, |
| 27 | + "access_token": mock_access_token, |
| 28 | + } |
| 29 | + request_kwargs = capture_and_mock_http_client_request( |
| 30 | + module_instance._http_client, response_body, 200 |
| 31 | + ) |
| 32 | + |
| 33 | + result = syncify( |
| 34 | + module_instance.get_access_token( |
| 35 | + provider="test-provider", |
| 36 | + user_id="user_123", |
| 37 | + ) |
| 38 | + ) |
| 39 | + |
| 40 | + assert request_kwargs["url"].endswith("data-integrations/test-provider/token") |
| 41 | + assert request_kwargs["method"] == "post" |
| 42 | + assert request_kwargs["json"]["user_id"] == "user_123" |
| 43 | + assert result.active is True |
| 44 | + assert result.access_token.access_token == mock_access_token["access_token"] |
| 45 | + assert result.access_token.scopes == mock_access_token["scopes"] |
| 46 | + |
| 47 | + def test_get_access_token_success_without_expiry( |
| 48 | + self, |
| 49 | + module_instance, |
| 50 | + capture_and_mock_http_client_request, |
| 51 | + ): |
| 52 | + response_body = { |
| 53 | + "active": True, |
| 54 | + "access_token": { |
| 55 | + "object": "access_token", |
| 56 | + "access_token": "test_token", |
| 57 | + "expires_at": None, |
| 58 | + "scopes": ["read"], |
| 59 | + "missing_scopes": [], |
| 60 | + }, |
| 61 | + } |
| 62 | + capture_and_mock_http_client_request( |
| 63 | + module_instance._http_client, response_body, 200 |
| 64 | + ) |
| 65 | + |
| 66 | + result = syncify( |
| 67 | + module_instance.get_access_token( |
| 68 | + provider="test-provider", |
| 69 | + user_id="user_123", |
| 70 | + ) |
| 71 | + ) |
| 72 | + |
| 73 | + assert result.active is True |
| 74 | + assert result.access_token.expires_at is None |
| 75 | + |
| 76 | + def test_get_access_token_with_organization_id( |
| 77 | + self, |
| 78 | + module_instance, |
| 79 | + mock_access_token, |
| 80 | + capture_and_mock_http_client_request, |
| 81 | + ): |
| 82 | + response_body = { |
| 83 | + "active": True, |
| 84 | + "access_token": mock_access_token, |
| 85 | + } |
| 86 | + request_kwargs = capture_and_mock_http_client_request( |
| 87 | + module_instance._http_client, response_body, 200 |
| 88 | + ) |
| 89 | + |
| 90 | + syncify( |
| 91 | + module_instance.get_access_token( |
| 92 | + provider="test-provider", |
| 93 | + user_id="user_123", |
| 94 | + organization_id="org_456", |
| 95 | + ) |
| 96 | + ) |
| 97 | + |
| 98 | + assert request_kwargs["json"]["organization_id"] == "org_456" |
| 99 | + |
| 100 | + def test_get_access_token_without_organization_id( |
| 101 | + self, |
| 102 | + module_instance, |
| 103 | + mock_access_token, |
| 104 | + capture_and_mock_http_client_request, |
| 105 | + ): |
| 106 | + response_body = { |
| 107 | + "active": True, |
| 108 | + "access_token": mock_access_token, |
| 109 | + } |
| 110 | + request_kwargs = capture_and_mock_http_client_request( |
| 111 | + module_instance._http_client, response_body, 200 |
| 112 | + ) |
| 113 | + |
| 114 | + syncify( |
| 115 | + module_instance.get_access_token( |
| 116 | + provider="test-provider", |
| 117 | + user_id="user_123", |
| 118 | + ) |
| 119 | + ) |
| 120 | + |
| 121 | + assert "organization_id" not in request_kwargs["json"] |
| 122 | + |
| 123 | + def test_get_access_token_not_installed( |
| 124 | + self, |
| 125 | + module_instance, |
| 126 | + capture_and_mock_http_client_request, |
| 127 | + ): |
| 128 | + response_body = { |
| 129 | + "active": False, |
| 130 | + "error": "not_installed", |
| 131 | + } |
| 132 | + capture_and_mock_http_client_request( |
| 133 | + module_instance._http_client, response_body, 200 |
| 134 | + ) |
| 135 | + |
| 136 | + result = syncify( |
| 137 | + module_instance.get_access_token( |
| 138 | + provider="test-provider", |
| 139 | + user_id="user_123", |
| 140 | + ) |
| 141 | + ) |
| 142 | + |
| 143 | + assert result.active is False |
| 144 | + assert result.error == "not_installed" |
| 145 | + |
| 146 | + def test_get_access_token_needs_reauthorization( |
| 147 | + self, |
| 148 | + module_instance, |
| 149 | + capture_and_mock_http_client_request, |
| 150 | + ): |
| 151 | + response_body = { |
| 152 | + "active": False, |
| 153 | + "error": "needs_reauthorization", |
| 154 | + } |
| 155 | + capture_and_mock_http_client_request( |
| 156 | + module_instance._http_client, response_body, 200 |
| 157 | + ) |
| 158 | + |
| 159 | + result = syncify( |
| 160 | + module_instance.get_access_token( |
| 161 | + provider="test-provider", |
| 162 | + user_id="user_123", |
| 163 | + ) |
| 164 | + ) |
| 165 | + |
| 166 | + assert result.active is False |
| 167 | + assert result.error == "needs_reauthorization" |
0 commit comments