1
|
|
|
# coding: utf8 |
2
|
|
|
|
3
|
|
|
""" |
4
|
|
|
This software is licensed under the Apache 2 license, quoted below. |
5
|
|
|
|
6
|
|
|
Copyright 2014 Crystalnix Limited |
7
|
|
|
|
8
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not |
9
|
|
|
use this file except in compliance with the License. You may obtain a copy of |
10
|
|
|
the License at |
11
|
|
|
|
12
|
|
|
http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
|
14
|
|
|
Unless required by applicable law or agreed to in writing, software |
15
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
16
|
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
17
|
|
|
License for the specific language governing permissions and limitations under |
18
|
|
|
the License. |
19
|
|
|
""" |
20
|
|
|
from uuid import UUID |
21
|
|
|
from datetime import datetime |
22
|
|
|
|
23
|
|
|
from django.test import TestCase |
24
|
|
|
from django.test.client import Client |
25
|
|
|
from django.contrib.auth import get_user_model |
26
|
|
|
from django.core.urlresolvers import reverse |
27
|
|
|
from django.db import connections |
28
|
|
|
|
29
|
|
|
import mock |
30
|
|
|
from pyquery import PyQuery as pq |
31
|
|
|
|
32
|
|
|
from omaha_server.utils import is_private |
33
|
|
|
from omaha.factories import ApplicationFactory, RequestFactory, AppRequestFactory, EventFactory |
34
|
|
|
from omaha.models import Request, AppRequest |
35
|
|
|
from omaha.views_admin import ( |
36
|
|
|
StatisticsView, |
37
|
|
|
) |
38
|
|
|
|
39
|
|
|
User = get_user_model() |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
class AdminViewStatisticsTest(TestCase): |
43
|
|
|
def setUp(self): |
44
|
|
|
self.app1 = ApplicationFactory.create(name='app1', id='app1') |
45
|
|
|
self.app2 = ApplicationFactory.create(name='app2', id='app2') |
46
|
|
|
self.apps = [self.app1, self.app2] |
47
|
|
|
self.user = User.objects.create_superuser( |
48
|
|
|
username='test', email='[email protected]', password='test') |
49
|
|
|
|
50
|
|
|
@is_private() |
51
|
|
|
def test_statistics_view(self): |
52
|
|
|
view = StatisticsView() |
53
|
|
|
self.assertListEqual(list(view.get_queryset()), self.apps) |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
class ViewsStaffMemberRequiredTest(TestCase): |
57
|
|
|
def setUp(self): |
58
|
|
|
self.client = Client() |
59
|
|
|
self.app = ApplicationFactory.create() |
60
|
|
|
self.request = Request.objects.create(version='1.0.0.0') |
61
|
|
|
self.app_request = AppRequest.objects.create( |
62
|
|
|
request=self.request, |
63
|
|
|
appid=self.app.id, |
64
|
|
|
) |
65
|
|
|
|
66
|
|
|
@is_private() |
67
|
|
|
def test_omaha_statistics(self): |
68
|
|
|
url = reverse('omaha_statistics') |
69
|
|
|
response = self.client.get(url) |
70
|
|
|
self.assertRedirects(response, '/admin/login/?next=%s' % url) |
71
|
|
|
|
72
|
|
|
@is_private() |
73
|
|
|
def test_omaha_statistics_detail(self): |
74
|
|
|
url = reverse('omaha_statistics_detail', kwargs=dict(name=self.app.name)) |
75
|
|
|
response = self.client.get(url) |
76
|
|
|
self.assertRedirects(response, '/admin/login/?next=%s' % url) |
77
|
|
|
|
78
|
|
|
@is_private() |
79
|
|
|
def test_live_statistics(self): |
80
|
|
|
url = reverse('omaha_live_statistics', kwargs=dict(name=self.app.name)) |
81
|
|
|
response = self.client.get(url) |
82
|
|
|
self.assertRedirects(response, '/admin/login/?next=%s' % url) |
83
|
|
|
|
84
|
|
|
@is_private() |
85
|
|
|
def test_omaha_request_list(self): |
86
|
|
|
url = reverse('omaha_request_list', kwargs=dict(name=self.app.name)) |
87
|
|
|
response = self.client.get(url) |
88
|
|
|
self.assertRedirects(response, '/admin/login/?next=%s' % url) |
89
|
|
|
|
90
|
|
|
@is_private() |
91
|
|
|
def test_omaha_request_detail(self): |
92
|
|
|
url = reverse('omaha_request_detail', kwargs=dict(pk=self.app_request.pk)) |
93
|
|
|
response = self.client.get(url) |
94
|
|
|
self.assertRedirects(response, '/admin/login/?next=%s' % url) |
95
|
|
|
|
96
|
|
|
@is_private() |
97
|
|
|
def test_omaha_preferences(self): |
98
|
|
|
url = reverse('set_preferences', args=['']) |
99
|
|
|
response = self.client.get(url) |
100
|
|
|
self.assertRedirects(response, '/admin/login/?next=%s' % url) |
101
|
|
|
|
102
|
|
|
@is_private() |
103
|
|
|
def test_omaha_monitoring(self): |
104
|
|
|
url = reverse('monitoring') |
105
|
|
|
response = self.client.get(url) |
106
|
|
|
self.assertRedirects(response, '/admin/login/?next=%s' % url) |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
class AdminViewPreferencesTest(TestCase): |
110
|
|
|
def setUp(self): |
111
|
|
|
self.client = Client() |
112
|
|
|
self.user = User.objects.create_superuser( |
113
|
|
|
username='test', email='[email protected]', password='test') |
114
|
|
|
self.client.login(username='test', password='test') |
115
|
|
|
|
116
|
|
|
@is_private() |
117
|
|
|
def test_set_timezone(self): |
118
|
|
|
url = reverse('set_preferences', args=['Timezone']) |
119
|
|
|
timezone = 'Asia/Omsk' |
120
|
|
|
self.client.post(url, dict(Timezone__timezone=timezone), follow=True) |
121
|
|
|
response = self.client.get(url) |
122
|
|
|
self.assertEqual(self.client.session["django_timezone"], timezone) |
123
|
|
|
self.assertContains(response, '<option value="Asia/Omsk" selected="selected">Asia/Omsk +0600</option>') |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
class FilteringAppRequestsByUserIdTest(TestCase): |
127
|
|
|
|
128
|
|
|
def setUp(self): |
129
|
|
|
self.client = Client() |
130
|
|
|
self.app = ApplicationFactory.create(id='{D0AB2EBC-931B-4013-9FEB-C9C4C2225C0}') |
131
|
|
|
self.userid1 = UUID(int=1) |
132
|
|
|
self.userid2 = UUID(int=2) |
133
|
|
|
self.app_req1 = AppRequestFactory(request=RequestFactory(userid=self.userid1)) |
134
|
|
|
self.app_req2 = AppRequestFactory(request=RequestFactory(userid=self.userid1)) |
135
|
|
|
self.app_req3 = AppRequestFactory(request=RequestFactory(userid=self.userid2)) |
136
|
|
|
self.user = User.objects.create_superuser( |
137
|
|
|
username='test', email='[email protected]', password='test') |
138
|
|
|
self.client.login(username='test', password='test') |
139
|
|
|
|
140
|
|
|
@is_private() |
141
|
|
|
def test_filtering(self): |
142
|
|
|
url = reverse('omaha_request_list', kwargs=dict(name=self.app.name)) |
143
|
|
|
data = {'request__userid': '', |
144
|
|
|
'request__created': '', |
145
|
|
|
'event_type': '', |
146
|
|
|
'event_result': ''} |
147
|
|
|
resp = self.client.get(url, data) |
148
|
|
|
d = pq(resp.content) |
149
|
|
|
res = d('#apprequest-table tbody tr') |
150
|
|
|
self.assertEqual(len(res), 3) |
151
|
|
|
data = {'request__userid': self.userid1, |
152
|
|
|
'request__created': '', |
153
|
|
|
'event_type': '', |
154
|
|
|
'event_result': ''} |
155
|
|
|
resp = self.client.get(url, data) |
156
|
|
|
d = pq(resp.content) |
157
|
|
|
res = d('#apprequest-table tbody tr') |
158
|
|
|
self.assertEqual(len(res), 2) |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
class VersionUsageViewTest(TestCase): |
162
|
|
|
def setUp(self): |
163
|
|
|
self.client = Client() |
164
|
|
|
self.app = ApplicationFactory.create(id='{D0AB2EBC-931B-4013-9FEB-C9C4C2225C0}') |
165
|
|
|
self.userid1 = UUID(int=1) |
166
|
|
|
self.userid2 = UUID(int=2) |
167
|
|
|
self.userid3 = UUID(int=3) |
168
|
|
|
self.req1 = RequestFactory(userid=self.userid1, created=datetime(2015, 1, 1)) |
169
|
|
|
self.req2 = RequestFactory(userid=self.userid1, created=datetime(2015, 7, 7)) |
170
|
|
|
self.req3 = RequestFactory(userid=self.userid2) |
171
|
|
|
self.req4 = RequestFactory(userid=self.userid3) |
172
|
|
|
self.app_req1 = AppRequestFactory(request=self.req1, events=(EventFactory(eventtype=2, eventresult=1),)) |
173
|
|
|
self.app_req2 = AppRequestFactory(request=self.req2, events=(EventFactory(eventtype=3, eventresult=1),)) |
174
|
|
|
self.app_req3 = AppRequestFactory(request=self.req3, events=(EventFactory(eventtype=2, eventresult=1),)) |
175
|
|
|
self.app_req4 = AppRequestFactory(request=self.req4, events=(EventFactory(eventtype=2, eventresult=5),)) |
176
|
|
|
self.user = User.objects.create_superuser( |
177
|
|
|
username='test', email='[email protected]', password='test') |
178
|
|
|
self.client.login(username='test', password='test') |
179
|
|
|
|
180
|
|
|
@is_private() |
181
|
|
|
def test_table(self): |
182
|
|
|
if connections['default'].settings_dict['ENGINE'] != 'django.db.backends.postgresql_psycopg2': |
183
|
|
|
self.skipTest('Database should be postgreSQL') |
184
|
|
|
url = reverse('omaha_version_usage', kwargs=dict(name=self.app.name)) |
185
|
|
|
resp = self.client.get(url) |
186
|
|
|
d = pq(resp.content) |
187
|
|
|
res = d('#usage-table tbody tr') |
188
|
|
|
self.assertEqual(AppRequest.objects.count(), 4) |
189
|
|
|
self.assertEqual(len(res), 2) |
190
|
|
|
|
191
|
|
|
self.assertEqual(d("#usage-table tbody tr .userid").contents(), |
192
|
|
|
['00000000-0000-0000-0000-000000000001', '00000000-0000-0000-0000-000000000002']) |
193
|
|
|
self.assertEqual(d("#usage-table tbody tr .last_update").contents()[0], '07/07/2015 midnight') |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
class ManualCleanupView(TestCase): |
197
|
|
|
def setUp(self): |
198
|
|
|
self.client = Client() |
199
|
|
|
self.user = User.objects.create_superuser( |
200
|
|
|
username='test', email='[email protected]', password='test') |
201
|
|
|
self.client.login(username='test', password='test') |
202
|
|
|
|
203
|
|
View Code Duplication |
@is_private() |
|
|
|
|
204
|
|
|
def test_limit_size_field(self): |
205
|
|
|
data = dict(limit_size=10) |
206
|
|
|
url = reverse('manual_cleanup', args=['feedback__Feedback']) |
207
|
|
|
with mock.patch('omaha.tasks.deferred_manual_cleanup.apply_async') as mocked: |
208
|
|
|
self.client.post(url, data=data) |
209
|
|
|
|
210
|
|
|
mock_args, mock_kwargs = mocked.call_args |
211
|
|
|
data.update(dict(limit_days=None)) |
212
|
|
|
self.assertTrue(mocked.called) |
213
|
|
|
self.assertDictEqual(mock_args[1], data) |
214
|
|
|
|
215
|
|
View Code Duplication |
@is_private() |
|
|
|
|
216
|
|
|
def test_limit_days_field(self): |
217
|
|
|
data = dict(limit_days=10) |
218
|
|
|
url = reverse('manual_cleanup', args=['feedback__Feedback']) |
219
|
|
|
with mock.patch('omaha.tasks.deferred_manual_cleanup.apply_async') as mocked: |
220
|
|
|
self.client.post(url, data=data) |
221
|
|
|
|
222
|
|
|
mock_args, mock_kwargs = mocked.call_args |
223
|
|
|
data.update(dict(limit_size=None)) |
224
|
|
|
self.assertTrue(mocked.called) |
225
|
|
|
self.assertDictEqual(mock_args[1], data) |
226
|
|
|
|
227
|
|
View Code Duplication |
@is_private() |
|
|
|
|
228
|
|
|
def test_all_fields(self): |
229
|
|
|
data = dict(limit_size=40, limit_days=5) |
230
|
|
|
url = reverse('manual_cleanup', args=['feedback__Feedback']) |
231
|
|
|
with mock.patch('omaha.tasks.deferred_manual_cleanup.apply_async') as mocked: |
232
|
|
|
self.client.post(url, data=data) |
233
|
|
|
|
234
|
|
|
mock_args, mock_kwargs = mocked.call_args |
235
|
|
|
self.assertTrue(mocked.called) |
236
|
|
|
self.assertDictEqual(mock_args[1], data) |
237
|
|
|
|
238
|
|
|
|
239
|
|
|
class CrashManualCleanupView(TestCase): |
240
|
|
|
def setUp(self): |
241
|
|
|
self.client = Client() |
242
|
|
|
self.user = User.objects.create_superuser( |
243
|
|
|
username='test', email='[email protected]', password='test') |
244
|
|
|
self.client.login(username='test', password='test') |
245
|
|
|
|
246
|
|
View Code Duplication |
@is_private() |
|
|
|
|
247
|
|
|
def test_limit_duplictated_field(self): |
248
|
|
|
data = dict(limit_duplicated=10) |
249
|
|
|
url = reverse('manual_cleanup', args=['crash__Crash']) |
250
|
|
|
with mock.patch('omaha.tasks.deferred_manual_cleanup.apply_async') as mocked: |
251
|
|
|
self.client.post(url, data=data) |
252
|
|
|
|
253
|
|
|
mock_args, mock_kwargs = mocked.call_args |
254
|
|
|
data.update(dict(limit_size=None, limit_days=None)) |
255
|
|
|
self.assertTrue(mocked.called) |
256
|
|
|
self.assertDictEqual(mock_args[1], data) |
257
|
|
|
|
258
|
|
View Code Duplication |
@is_private() |
|
|
|
|
259
|
|
|
def test_all_fields(self): |
260
|
|
|
data = dict(limit_size=40, limit_days=5, limit_duplicated=73) |
261
|
|
|
url = reverse('manual_cleanup', args=['crash__Crash']) |
262
|
|
|
with mock.patch('omaha.tasks.deferred_manual_cleanup.apply_async') as mocked: |
263
|
|
|
self.client.post(url, data=data) |
264
|
|
|
|
265
|
|
|
mock_args, mock_kwargs = mocked.call_args |
266
|
|
|
self.assertTrue(mocked.called) |
267
|
|
|
self.assertDictEqual(mock_args[1], data) |
268
|
|
|
|