|
| 1 | +"""Tests for CampusRequest class, specifically around authentication modes.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import unittest |
| 5 | + |
| 6 | +from campus_python.json_client import CampusRequest |
| 7 | + |
| 8 | + |
| 9 | +class TestCampusRequestModes(unittest.TestCase): |
| 10 | + """Test CampusRequest initialization with different authentication modes.""" |
| 11 | + |
| 12 | + def setUp(self): |
| 13 | + """Save and clear environment variables before each test.""" |
| 14 | + self.saved_client_id = os.environ.get('CLIENT_ID') |
| 15 | + self.saved_client_secret = os.environ.get('CLIENT_SECRET') |
| 16 | + |
| 17 | + # Clear env vars to ensure clean state |
| 18 | + if 'CLIENT_ID' in os.environ: |
| 19 | + del os.environ['CLIENT_ID'] |
| 20 | + if 'CLIENT_SECRET' in os.environ: |
| 21 | + del os.environ['CLIENT_SECRET'] |
| 22 | + |
| 23 | + def tearDown(self): |
| 24 | + """Restore original environment variables after each test.""" |
| 25 | + # Restore original values |
| 26 | + if self.saved_client_id is not None: |
| 27 | + os.environ['CLIENT_ID'] = self.saved_client_id |
| 28 | + elif 'CLIENT_ID' in os.environ: |
| 29 | + del os.environ['CLIENT_ID'] |
| 30 | + |
| 31 | + if self.saved_client_secret is not None: |
| 32 | + os.environ['CLIENT_SECRET'] = self.saved_client_secret |
| 33 | + elif 'CLIENT_SECRET' in os.environ: |
| 34 | + del os.environ['CLIENT_SECRET'] |
| 35 | + |
| 36 | + def test_server_mode_requires_credentials(self): |
| 37 | + """Test that server mode (default) requires CLIENT_ID and CLIENT_SECRET.""" |
| 38 | + with self.assertRaises(OSError) as context: |
| 39 | + CampusRequest(base_url="http://localhost", mode="server") |
| 40 | + |
| 41 | + # Verify error message mentions both variables |
| 42 | + self.assertIn('CLIENT_ID', str(context.exception)) |
| 43 | + self.assertIn('CLIENT_SECRET', str(context.exception)) |
| 44 | + |
| 45 | + def test_device_mode_no_credentials_required(self): |
| 46 | + """Test that device mode does NOT require CLIENT_ID or CLIENT_SECRET.""" |
| 47 | + # Should not raise any exception |
| 48 | + client = CampusRequest(base_url="http://localhost", mode="device") |
| 49 | + |
| 50 | + # Verify the client was created successfully |
| 51 | + self.assertIsNotNone(client) |
| 52 | + self.assertEqual(client.base_url, "http://localhost") |
| 53 | + |
| 54 | + def test_server_mode_with_credentials(self): |
| 55 | + """Test that server mode works when credentials are provided.""" |
| 56 | + os.environ['CLIENT_ID'] = 'test-client-id' |
| 57 | + os.environ['CLIENT_SECRET'] = 'test-client-secret' |
| 58 | + |
| 59 | + # Should not raise |
| 60 | + client = CampusRequest(base_url="http://localhost", mode="server") |
| 61 | + |
| 62 | + # Verify the client was created successfully |
| 63 | + self.assertIsNotNone(client) |
| 64 | + |
| 65 | + # Verify Basic auth header was set |
| 66 | + auth_header = client._session.headers.get('Authorization') |
| 67 | + self.assertIsNotNone(auth_header) |
| 68 | + self.assertTrue(auth_header.startswith('Basic ')) |
| 69 | + |
| 70 | + def test_default_mode_is_server(self): |
| 71 | + """Test that the default mode is 'server' which requires credentials.""" |
| 72 | + # Without specifying mode, it should default to "server" |
| 73 | + with self.assertRaises(OSError): |
| 74 | + CampusRequest(base_url="http://localhost") |
| 75 | + |
| 76 | + def test_bearer_authorization_in_device_mode(self): |
| 77 | + """Test that Bearer authorization can be set in device mode.""" |
| 78 | + # Create client in device mode (no credentials required) |
| 79 | + client = CampusRequest(base_url="http://localhost", mode="device") |
| 80 | + |
| 81 | + # Verify no Authorization header initially |
| 82 | + auth_header = client._session.headers.get('Authorization') |
| 83 | + self.assertIsNone(auth_header) |
| 84 | + |
| 85 | + # Set Bearer authorization |
| 86 | + test_token = "test-bearer-token-12345" |
| 87 | + client.set_bearer_authorization(test_token) |
| 88 | + |
| 89 | + # Verify Bearer auth header was set |
| 90 | + auth_header = client._session.headers.get('Authorization') |
| 91 | + self.assertIsNotNone(auth_header) |
| 92 | + self.assertEqual(auth_header, f'Bearer {test_token}') |
| 93 | + |
| 94 | + def test_bearer_authorization_overrides_basic(self): |
| 95 | + """Test that set_bearer_authorization overrides Basic auth.""" |
| 96 | + os.environ['CLIENT_ID'] = 'test-client-id' |
| 97 | + os.environ['CLIENT_SECRET'] = 'test-client-secret' |
| 98 | + |
| 99 | + # Create client in server mode (sets Basic auth) |
| 100 | + client = CampusRequest(base_url="http://localhost", mode="server") |
| 101 | + |
| 102 | + # Verify Basic auth was set initially |
| 103 | + auth_header = client._session.headers.get('Authorization') |
| 104 | + self.assertTrue(auth_header.startswith('Basic ')) |
| 105 | + |
| 106 | + # Set Bearer authorization (should override) |
| 107 | + test_token = "test-bearer-token-67890" |
| 108 | + client.set_bearer_authorization(test_token) |
| 109 | + |
| 110 | + # Verify Bearer auth header replaced Basic auth |
| 111 | + auth_header = client._session.headers.get('Authorization') |
| 112 | + self.assertEqual(auth_header, f'Bearer {test_token}') |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + unittest.main() |
0 commit comments