|
14 | 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | 15 | # See the License for the specific language governing permissions and |
16 | 16 | # limitations under the License. |
17 | | -from unittest.mock import Mock, patch |
| 17 | +from unittest.mock import MagicMock, patch |
18 | 18 |
|
| 19 | +import pytest |
| 20 | +from django.core.exceptions import ObjectDoesNotExist |
| 21 | +from django.urls import reverse |
19 | 22 | from rest_framework import status |
| 23 | +from rest_framework.test import APIClient |
20 | 24 |
|
21 | | -from about.api import get_usage_analytics, update_usage_analytics |
| 25 | +from useradmin.models import User |
22 | 26 |
|
23 | 27 |
|
24 | | -class TestUsageAnalyticsAPI: |
25 | | - def test_get_usage_analytics_success(self): |
26 | | - with patch('about.api.is_admin') as mock_is_admin: |
27 | | - with patch('about.api.Settings.get_settings') as mock_get_settings: |
28 | | - mock_is_admin.return_value = True |
29 | | - mock_get_settings.return_value = Mock(collect_usage=True) |
| 28 | +@pytest.mark.django_db |
| 29 | +class TestUsageAnalyticsSettingsAPI: |
| 30 | + @pytest.fixture |
| 31 | + def api_client(self) -> APIClient: |
| 32 | + return APIClient() |
30 | 33 |
|
31 | | - request = Mock(method='GET', user=Mock()) |
32 | | - response = get_usage_analytics(request) |
| 34 | + @pytest.fixture |
| 35 | + def regular_user(self, db) -> User: |
| 36 | + return User.objects.create_user(username="testuser", password="") |
33 | 37 |
|
34 | | - assert response.status_code == status.HTTP_200_OK |
35 | | - assert response.data == {'analytics_enabled': True} |
| 38 | + @pytest.fixture |
| 39 | + def admin_user(self, db) -> User: |
| 40 | + return User.objects.create_superuser(username="adminuser", password="") |
36 | 41 |
|
37 | | - def test_get_usage_analytics_unauthorized(self): |
38 | | - with patch('about.api.is_admin') as mock_is_admin: |
39 | | - mock_is_admin.return_value = False |
| 42 | + @pytest.fixture |
| 43 | + def analytics_settings_url(self) -> str: |
| 44 | + return reverse("api:core_usage_analytics") |
40 | 45 |
|
41 | | - request = Mock(method='GET', user=Mock()) |
42 | | - response = get_usage_analytics(request) |
| 46 | + @patch("desktop.auth.api_permissions.is_admin", return_value=True) |
| 47 | + @patch("about.api.Settings.get_settings") |
| 48 | + def test_get_settings_as_admin_success(self, mock_get_settings, mock_is_admin, api_client, admin_user, analytics_settings_url): |
| 49 | + mock_get_settings.return_value = MagicMock(collect_usage=True) |
| 50 | + api_client.force_authenticate(user=admin_user) |
43 | 51 |
|
44 | | - assert response.status_code == status.HTTP_403_FORBIDDEN |
45 | | - assert response.data['message'] == "You must be a Hue admin to access this endpoint." |
| 52 | + response = api_client.get(analytics_settings_url) |
46 | 53 |
|
47 | | - def test_get_usage_analytics_error(self): |
48 | | - with patch('about.api.is_admin') as mock_is_admin: |
49 | | - with patch('about.api.Settings.get_settings') as mock_get_settings: |
50 | | - mock_is_admin.return_value = True |
51 | | - mock_get_settings.side_effect = Exception("Test error") |
| 54 | + assert response.status_code == status.HTTP_200_OK |
| 55 | + assert response.data == {"collect_usage": True} |
52 | 56 |
|
53 | | - request = Mock(method='GET', user=Mock()) |
54 | | - response = get_usage_analytics(request) |
| 57 | + @patch("desktop.auth.api_permissions.is_admin", return_value=False) |
| 58 | + def test_get_settings_as_non_admin_forbidden(self, mock_is_admin, api_client, regular_user, analytics_settings_url): |
| 59 | + api_client.force_authenticate(user=regular_user) |
55 | 60 |
|
56 | | - assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR |
57 | | - assert "Error retrieving usage analytics" in response.data['message'] |
| 61 | + response = api_client.get(analytics_settings_url) |
58 | 62 |
|
59 | | - def test_update_usage_analytics_success(self): |
60 | | - with patch('about.api.is_admin') as mock_is_admin: |
61 | | - with patch('about.api.Settings.get_settings') as mock_get_settings: |
62 | | - mock_is_admin.return_value = True |
63 | | - mock_get_settings.return_value = Mock(save=Mock()) |
| 63 | + assert response.status_code == status.HTTP_403_FORBIDDEN |
| 64 | + assert response.data == {"detail": "You must be a Hue admin to perform this action."} |
64 | 65 |
|
65 | | - request = Mock(method='POST', user=Mock(), POST={'analytics_enabled': 'true'}) |
66 | | - response = update_usage_analytics(request) |
| 66 | + @patch("desktop.auth.api_permissions.is_admin", return_value=True) |
| 67 | + @patch("about.api.Settings.get_settings") |
| 68 | + def test_get_settings_as_admin_error(self, mock_get_settings, mock_is_admin, api_client, admin_user, analytics_settings_url): |
| 69 | + mock_get_settings.side_effect = ObjectDoesNotExist("Settings not found") |
| 70 | + api_client.force_authenticate(user=admin_user) |
67 | 71 |
|
68 | | - assert response.status_code == status.HTTP_200_OK |
69 | | - assert mock_get_settings.return_value.save.called |
70 | | - assert response.data == {'analytics_enabled': True} |
| 72 | + response = api_client.get(analytics_settings_url) |
71 | 73 |
|
72 | | - def test_update_usage_analytics_unauthorized(self): |
73 | | - with patch('about.api.is_admin') as mock_is_admin: |
74 | | - mock_is_admin.return_value = False |
| 74 | + assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR |
| 75 | + assert response.data == {"error": "A server error occurred while retrieving usage analytics settings."} |
75 | 76 |
|
76 | | - request = Mock(method='POST', user=Mock(), data={'analytics_enabled': 'true'}) |
77 | | - response = update_usage_analytics(request) |
| 77 | + @patch("desktop.auth.api_permissions.is_admin", return_value=True) |
| 78 | + @patch("about.api.Settings.get_settings") |
| 79 | + def test_put_settings_as_admin_success(self, mock_get_settings, mock_is_admin, api_client, admin_user, analytics_settings_url): |
| 80 | + mock_settings = MagicMock() |
| 81 | + mock_get_settings.return_value = mock_settings |
| 82 | + api_client.force_authenticate(user=admin_user) |
| 83 | + payload = {"collect_usage": False} |
78 | 84 |
|
79 | | - assert response.status_code == status.HTTP_403_FORBIDDEN |
80 | | - assert response.data['message'] == "You must be a Hue admin to access this endpoint." |
| 85 | + response = api_client.put(analytics_settings_url, data=payload, format="json") |
81 | 86 |
|
82 | | - def test_update_usage_analytics_missing_param(self): |
83 | | - with patch('about.api.is_admin') as mock_is_admin: |
84 | | - mock_is_admin.return_value = True |
| 87 | + assert response.status_code == status.HTTP_200_OK |
| 88 | + assert response.data == payload |
| 89 | + assert mock_settings.collect_usage is False |
| 90 | + mock_settings.save.assert_called_once() |
85 | 91 |
|
86 | | - request = Mock(method='POST', user=Mock(), POST={}) |
87 | | - response = update_usage_analytics(request) |
| 92 | + @patch("desktop.auth.api_permissions.is_admin", return_value=False) |
| 93 | + def test_put_settings_as_non_admin_forbidden(self, mock_is_admin, api_client, regular_user, analytics_settings_url): |
| 94 | + api_client.force_authenticate(user=regular_user) |
88 | 95 |
|
89 | | - assert response.status_code == status.HTTP_400_BAD_REQUEST |
90 | | - assert response.data['message'] == 'Missing parameter: analytics_enabled is required.' |
| 96 | + response = api_client.put(analytics_settings_url) |
91 | 97 |
|
92 | | - def test_update_usage_analytics_error(self): |
93 | | - with patch('about.api.is_admin') as mock_is_admin: |
94 | | - with patch('about.api.Settings.get_settings') as mock_get_settings: |
95 | | - mock_is_admin.return_value = True |
96 | | - mock_get_settings.side_effect = Exception("Test error") |
| 98 | + assert response.status_code == status.HTTP_403_FORBIDDEN |
| 99 | + assert response.data == {"detail": "You must be a Hue admin to perform this action."} |
97 | 100 |
|
98 | | - request = Mock(method='POST', user=Mock(), POST={'analytics_enabled': 'true'}) |
99 | | - response = update_usage_analytics(request) |
| 101 | + @patch("desktop.auth.api_permissions.is_admin", return_value=True) |
| 102 | + @patch("about.api.Settings.get_settings") |
| 103 | + def test_put_settings_as_admin_error(self, mock_get_settings, mock_is_admin, api_client, admin_user, analytics_settings_url): |
| 104 | + mock_get_settings.side_effect = ObjectDoesNotExist("Settings not found") |
| 105 | + api_client.force_authenticate(user=admin_user) |
| 106 | + payload = {"collect_usage": False} |
100 | 107 |
|
101 | | - assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR |
102 | | - assert "Error updating usage analytics" in response.data['message'] |
| 108 | + response = api_client.put(analytics_settings_url, data=payload, format="json") |
| 109 | + |
| 110 | + assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR |
| 111 | + assert response.data == {"error": "A server error occurred while saving the usage analytics settings."} |
| 112 | + |
| 113 | + @patch("desktop.auth.api_permissions.is_admin", return_value=True) |
| 114 | + @patch("about.api.Settings.get_settings") |
| 115 | + def test_put_settings_as_admin_missing_field(self, mock_get_settings, mock_is_admin, api_client, admin_user, analytics_settings_url): |
| 116 | + mock_get_settings.return_value = MagicMock(collect_usage=True) |
| 117 | + api_client.force_authenticate(user=admin_user) |
| 118 | + payload = {} |
| 119 | + |
| 120 | + response = api_client.put(analytics_settings_url, data=payload, format="json") |
| 121 | + |
| 122 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 123 | + assert response.data == {"collect_usage": ["This field is required."]} |
0 commit comments