|
| 1 | +#!/usr/bin/env python |
| 2 | +# Licensed to Cloudera, Inc. under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. Cloudera, Inc. licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +from unittest.mock import Mock, patch |
| 18 | + |
| 19 | +from rest_framework import status |
| 20 | + |
| 21 | +from about.api import get_usage_analytics, update_usage_analytics |
| 22 | + |
| 23 | + |
| 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) |
| 30 | + |
| 31 | + request = Mock(method='GET', user=Mock()) |
| 32 | + response = get_usage_analytics(request) |
| 33 | + |
| 34 | + assert response.status_code == status.HTTP_200_OK |
| 35 | + assert response.data == {'analytics_enabled': True} |
| 36 | + |
| 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 |
| 40 | + |
| 41 | + request = Mock(method='GET', user=Mock()) |
| 42 | + response = get_usage_analytics(request) |
| 43 | + |
| 44 | + assert response.status_code == status.HTTP_403_FORBIDDEN |
| 45 | + assert response.data['message'] == "You must be a Hue admin to access this endpoint." |
| 46 | + |
| 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") |
| 52 | + |
| 53 | + request = Mock(method='GET', user=Mock()) |
| 54 | + response = get_usage_analytics(request) |
| 55 | + |
| 56 | + assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR |
| 57 | + assert "Error retrieving usage analytics" in response.data['message'] |
| 58 | + |
| 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()) |
| 64 | + |
| 65 | + request = Mock(method='POST', user=Mock(), POST={'analytics_enabled': 'true'}) |
| 66 | + response = update_usage_analytics(request) |
| 67 | + |
| 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} |
| 71 | + |
| 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 |
| 75 | + |
| 76 | + request = Mock(method='POST', user=Mock(), data={'analytics_enabled': 'true'}) |
| 77 | + response = update_usage_analytics(request) |
| 78 | + |
| 79 | + assert response.status_code == status.HTTP_403_FORBIDDEN |
| 80 | + assert response.data['message'] == "You must be a Hue admin to access this endpoint." |
| 81 | + |
| 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 |
| 85 | + |
| 86 | + request = Mock(method='POST', user=Mock(), POST={}) |
| 87 | + response = update_usage_analytics(request) |
| 88 | + |
| 89 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 90 | + assert response.data['message'] == 'Missing parameter: analytics_enabled is required.' |
| 91 | + |
| 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") |
| 97 | + |
| 98 | + request = Mock(method='POST', user=Mock(), POST={'analytics_enabled': 'true'}) |
| 99 | + response = update_usage_analytics(request) |
| 100 | + |
| 101 | + assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR |
| 102 | + assert "Error updating usage analytics" in response.data['message'] |
0 commit comments