|
7 | 7 |
|
8 | 8 | import pytest |
9 | 9 | import os |
| 10 | +import sys |
10 | 11 | from unittest.mock import patch, MagicMock |
11 | 12 |
|
12 | 13 | from praisonai.ui._auth import ( |
@@ -117,75 +118,80 @@ def test_check_auth_callback_with_incorrect_credentials(self): |
117 | 118 | class TestRegisterPasswordAuth: |
118 | 119 | """Test the register_password_auth function.""" |
119 | 120 |
|
120 | | - @patch('praisonai.ui._auth.cl') |
121 | 121 | @patch.dict(os.environ, {}, clear=True) |
122 | | - def test_default_credentials_on_loopback(self, mock_cl): |
| 122 | + def test_default_credentials_on_loopback(self): |
123 | 123 | """Test registration with default credentials on loopback.""" |
124 | | - # Should not raise |
125 | | - register_password_auth(None, bind_host="127.0.0.1") |
126 | | - |
127 | | - # Should have registered a callback |
128 | | - mock_cl.password_auth_callback.assert_called_once() |
| 124 | + mock_cl = MagicMock() |
| 125 | + with patch.dict(sys.modules, {"chainlit": mock_cl}): |
| 126 | + # Should not raise |
| 127 | + register_password_auth(None, bind_host="127.0.0.1") |
| 128 | + |
| 129 | + # Should have registered a callback |
| 130 | + mock_cl.password_auth_callback.assert_called_once() |
129 | 131 |
|
130 | | - @patch('praisonai.ui._auth.cl') |
131 | 132 | @patch.dict(os.environ, {}, clear=True) |
132 | | - def test_default_credentials_on_external_raises_error(self, mock_cl): |
| 133 | + def test_default_credentials_on_external_raises_error(self): |
133 | 134 | """Test registration with default credentials on external interface raises error.""" |
134 | | - with pytest.raises(RuntimeError) as excinfo: |
135 | | - register_password_auth(None, bind_host="0.0.0.0") |
136 | | - |
137 | | - assert "Cannot use default admin/admin credentials on external interface" in str(excinfo.value) |
| 135 | + mock_cl = MagicMock() |
| 136 | + with patch.dict(sys.modules, {"chainlit": mock_cl}): |
| 137 | + with pytest.raises(RuntimeError) as excinfo: |
| 138 | + register_password_auth(None, bind_host="0.0.0.0") |
| 139 | + |
| 140 | + assert "Cannot use default admin/admin credentials on external interface" in str(excinfo.value) |
138 | 141 |
|
139 | | - @patch('praisonai.ui._auth.cl') |
140 | 142 | @patch.dict(os.environ, {"PRAISONAI_ALLOW_DEFAULT_CREDS": "1"}, clear=True) |
141 | | - def test_default_credentials_on_external_with_escape_hatch(self, mock_cl): |
| 143 | + def test_default_credentials_on_external_with_escape_hatch(self): |
142 | 144 | """Test registration with default credentials on external interface with escape hatch.""" |
143 | | - # Should not raise with escape hatch |
144 | | - register_password_auth(None, bind_host="0.0.0.0") |
145 | | - |
146 | | - # Should have registered a callback |
147 | | - mock_cl.password_auth_callback.assert_called_once() |
| 145 | + mock_cl = MagicMock() |
| 146 | + with patch.dict(sys.modules, {"chainlit": mock_cl}): |
| 147 | + # Should not raise with escape hatch |
| 148 | + register_password_auth(None, bind_host="0.0.0.0") |
| 149 | + |
| 150 | + # Should have registered a callback |
| 151 | + mock_cl.password_auth_callback.assert_called_once() |
148 | 152 |
|
149 | | - @patch('praisonai.ui._auth.cl') |
150 | 153 | @patch.dict(os.environ, { |
151 | 154 | "CHAINLIT_USERNAME": "myuser", |
152 | 155 | "CHAINLIT_PASSWORD": "mypassword" |
153 | 156 | }, clear=True) |
154 | | - def test_custom_credentials_on_external(self, mock_cl): |
| 157 | + def test_custom_credentials_on_external(self): |
155 | 158 | """Test registration with custom credentials on external interface.""" |
156 | | - # Should not raise |
157 | | - register_password_auth(None, bind_host="0.0.0.0") |
158 | | - |
159 | | - # Should have registered a callback |
160 | | - mock_cl.password_auth_callback.assert_called_once() |
| 159 | + mock_cl = MagicMock() |
| 160 | + with patch.dict(sys.modules, {"chainlit": mock_cl}): |
| 161 | + # Should not raise |
| 162 | + register_password_auth(None, bind_host="0.0.0.0") |
| 163 | + |
| 164 | + # Should have registered a callback |
| 165 | + mock_cl.password_auth_callback.assert_called_once() |
161 | 166 |
|
162 | | - @patch('praisonai.ui._auth.cl') |
163 | 167 | @patch.dict(os.environ, { |
164 | 168 | "CHAINLIT_USERNAME": "myuser", |
165 | 169 | "CHAINLIT_PASSWORD": "mypassword" |
166 | 170 | }, clear=True) |
167 | | - def test_auth_callback_functionality(self, mock_cl): |
| 171 | + def test_auth_callback_functionality(self): |
168 | 172 | """Test that the registered auth callback works correctly.""" |
169 | | - register_password_auth(None, bind_host="127.0.0.1") |
170 | | - |
171 | | - # Get the registered callback function |
172 | | - callback = mock_cl.password_auth_callback.call_args[0][0] |
173 | | - |
174 | | - # Mock User class |
175 | | - mock_user = MagicMock() |
176 | | - mock_cl.User.return_value = mock_user |
177 | | - |
178 | | - # Test correct credentials |
179 | | - result = callback("myuser", "mypassword") |
180 | | - assert result == mock_user |
181 | | - mock_cl.User.assert_called_with( |
182 | | - identifier="myuser", |
183 | | - metadata={"role": "admin", "provider": "credentials"} |
184 | | - ) |
185 | | - |
186 | | - # Test incorrect credentials |
187 | | - result = callback("wrong", "credentials") |
188 | | - assert result is None |
| 173 | + mock_cl = MagicMock() |
| 174 | + with patch.dict(sys.modules, {"chainlit": mock_cl}): |
| 175 | + register_password_auth(None, bind_host="127.0.0.1") |
| 176 | + |
| 177 | + # Get the registered callback function |
| 178 | + callback = mock_cl.password_auth_callback.call_args[0][0] |
| 179 | + |
| 180 | + # Mock User class |
| 181 | + mock_user = MagicMock() |
| 182 | + mock_cl.User.return_value = mock_user |
| 183 | + |
| 184 | + # Test correct credentials |
| 185 | + result = callback("myuser", "mypassword") |
| 186 | + assert result == mock_user |
| 187 | + mock_cl.User.assert_called_with( |
| 188 | + identifier="myuser", |
| 189 | + metadata={"role": "admin", "provider": "credentials"} |
| 190 | + ) |
| 191 | + |
| 192 | + # Test incorrect credentials |
| 193 | + result = callback("wrong", "credentials") |
| 194 | + assert result is None |
189 | 195 |
|
190 | 196 |
|
191 | 197 | class TestEnvironmentVariables: |
|
0 commit comments