1
|
|
|
# coding: utf8 |
2
|
|
|
|
3
|
|
|
""" |
4
|
|
|
This software is licensed under the Apache 2 license, quoted below. |
5
|
|
|
|
6
|
|
|
Copyright 2015 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
|
|
|
|
21
|
|
|
import os |
22
|
|
|
|
23
|
|
|
from django import test |
24
|
|
|
from django.core.urlresolvers import reverse |
25
|
|
|
from django.conf import settings |
26
|
|
|
|
27
|
|
|
from feedback.models import Feedback |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
BASE_DIR = os.path.dirname(__file__) |
31
|
|
|
TEST_DATA_DIR = os.path.join(BASE_DIR, 'testdata') |
32
|
|
|
PB_FILE = os.path.join(TEST_DATA_DIR, 'request_tar.pb') |
33
|
|
|
PB_GZ_FILE = os.path.join(TEST_DATA_DIR, 'request_gz.pb') |
34
|
|
|
DESC_ONLY_FILE = os.path.join(TEST_DATA_DIR, 'description_only.pb') |
35
|
|
|
NO_DESC_FILE = os.path.join(TEST_DATA_DIR, 'no_description.pb') |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
class FeedbackViewTest(test.TestCase): |
39
|
|
View Code Duplication |
def test_view(self): |
|
|
|
|
40
|
|
|
with open(PB_FILE, 'rb') as f: |
41
|
|
|
body = f.read() |
42
|
|
|
description = 'Test tar' |
43
|
|
|
email = '' |
44
|
|
|
page_url = 'chrome://newtab/' |
45
|
|
|
|
46
|
|
|
self.assertEqual(Feedback.objects.all().count(), 0) |
47
|
|
|
response = self.client.post( |
48
|
|
|
reverse('feedback'), |
49
|
|
|
data=body, |
50
|
|
|
content_type='application/x-protobuf', |
51
|
|
|
REMOTE_ADDR="8.8.8.8" |
52
|
|
|
) |
53
|
|
|
self.assertEqual(response.status_code, 200) |
54
|
|
|
self.assertEqual(Feedback.objects.all().count(), 1) |
55
|
|
|
obj = Feedback.objects.get() |
56
|
|
|
self.assertEqual(response.content.decode(), str(obj.pk)) |
57
|
|
|
self.assertEqual(obj.description, description) |
58
|
|
|
self.assertEqual(obj.email, email) |
59
|
|
|
self.assertEqual(obj.page_url, page_url) |
60
|
|
|
self.assertFalse(obj.screenshot) |
61
|
|
|
self.assertTrue(obj.blackbox) |
62
|
|
|
self.assertTrue(obj.system_logs) |
63
|
|
|
self.assertTrue(obj.attached_file) |
64
|
|
|
self.assertTrue(obj.feedback_data) |
65
|
|
|
self.assertEqual(obj.ip, "8.8.8.8") |
66
|
|
|
self.assertEqual(os.path.basename(obj.blackbox.name), 'blackbox.tar') |
67
|
|
|
|
68
|
|
View Code Duplication |
def test_view_gz(self): |
|
|
|
|
69
|
|
|
with open(PB_GZ_FILE, 'rb') as f: |
70
|
|
|
body = f.read() |
71
|
|
|
description = 'Test tar gz' |
72
|
|
|
email = '' |
73
|
|
|
page_url = 'chrome://newtab/' |
74
|
|
|
|
75
|
|
|
self.assertEqual(Feedback.objects.all().count(), 0) |
76
|
|
|
response = self.client.post( |
77
|
|
|
reverse('feedback'), |
78
|
|
|
data=body, |
79
|
|
|
content_type='application/x-protobuf', |
80
|
|
|
REMOTE_ADDR="8.8.8.8" |
81
|
|
|
) |
82
|
|
|
self.assertEqual(response.status_code, 200) |
83
|
|
|
self.assertEqual(Feedback.objects.all().count(), 1) |
84
|
|
|
obj = Feedback.objects.get() |
85
|
|
|
self.assertEqual(response.content.decode(), str(obj.pk)) |
86
|
|
|
self.assertEqual(obj.description, description) |
87
|
|
|
self.assertEqual(obj.email, email) |
88
|
|
|
self.assertEqual(obj.page_url, page_url) |
89
|
|
|
self.assertFalse(obj.screenshot) |
90
|
|
|
self.assertTrue(obj.blackbox) |
91
|
|
|
self.assertTrue(obj.system_logs) |
92
|
|
|
self.assertTrue(obj.attached_file) |
93
|
|
|
self.assertTrue(obj.feedback_data) |
94
|
|
|
self.assertEqual(obj.ip, "8.8.8.8") |
95
|
|
|
self.assertEqual( |
96
|
|
|
os.path.basename(obj.blackbox.name), |
97
|
|
|
'blackbox.tar.gz' |
98
|
|
|
) |
99
|
|
|
|
100
|
|
|
def test_view_empty_ip(self): |
101
|
|
|
db_engine = 'django.db.backends.postgresql_psycopg2' |
102
|
|
|
if settings.DATABASES['default']['ENGINE'] == db_engine: |
103
|
|
|
with open(PB_FILE, 'rb') as f: |
104
|
|
|
body = f.read() |
105
|
|
|
description = 'Test tar' |
106
|
|
|
email = '' |
107
|
|
|
page_url = 'chrome://newtab/' |
108
|
|
|
|
109
|
|
|
self.assertEqual(Feedback.objects.all().count(), 0) |
110
|
|
|
response = self.client.post( |
111
|
|
|
reverse('feedback'), |
112
|
|
|
data=body, |
113
|
|
|
content_type='application/x-protobuf', |
114
|
|
|
REMOTE_ADDR="" |
115
|
|
|
) |
116
|
|
|
self.assertEqual(response.status_code, 200) |
117
|
|
|
self.assertEqual(Feedback.objects.all().count(), 1) |
118
|
|
|
obj = Feedback.objects.get() |
119
|
|
|
self.assertEqual(response.content.decode(), str(obj.pk)) |
120
|
|
|
self.assertEqual(obj.description, description) |
121
|
|
|
self.assertEqual(obj.email, email) |
122
|
|
|
self.assertEqual(obj.page_url, page_url) |
123
|
|
|
self.assertFalse(obj.screenshot) |
124
|
|
|
self.assertTrue(obj.blackbox) |
125
|
|
|
self.assertTrue(obj.system_logs) |
126
|
|
|
self.assertTrue(obj.attached_file) |
127
|
|
|
self.assertTrue(obj.feedback_data) |
128
|
|
|
self.assertEqual(obj.ip, None) |
129
|
|
|
|
130
|
|
|
def test_view_desc_only(self): |
131
|
|
|
with open(DESC_ONLY_FILE, 'rb') as f: |
132
|
|
|
body = f.read() |
133
|
|
|
description = 'Description only' |
134
|
|
|
|
135
|
|
|
self.assertEqual(Feedback.objects.all().count(), 0) |
136
|
|
|
response = self.client.post( |
137
|
|
|
reverse('feedback'), |
138
|
|
|
data=body, |
139
|
|
|
content_type='application/x-protobuf' |
140
|
|
|
) |
141
|
|
|
self.assertEqual(response.status_code, 200) |
142
|
|
|
self.assertEqual(Feedback.objects.all().count(), 1) |
143
|
|
|
obj = Feedback.objects.get() |
144
|
|
|
self.assertEqual(response.content.decode(), str(obj.pk)) |
145
|
|
|
self.assertEqual(obj.description, description) |
146
|
|
|
self.assertEqual(obj.email, '') |
147
|
|
|
self.assertEqual(obj.page_url, '') |
148
|
|
|
self.assertFalse(obj.screenshot) |
149
|
|
|
self.assertFalse(obj.blackbox) |
150
|
|
|
self.assertFalse(obj.system_logs) |
151
|
|
|
self.assertFalse(obj.attached_file) |
152
|
|
|
self.assertTrue(obj.feedback_data) |
153
|
|
|
|
154
|
|
|
def test_view_invalid(self): |
155
|
|
|
with open(NO_DESC_FILE, 'rb') as f: |
156
|
|
|
body = f.read() |
157
|
|
|
|
158
|
|
|
# No description provided - form is not valid |
159
|
|
|
self.assertEqual(Feedback.objects.all().count(), 0) |
160
|
|
|
resp = self.client.post( |
161
|
|
|
reverse('feedback'), |
162
|
|
|
data=body, |
163
|
|
|
content_type='application/x-protobuf' |
164
|
|
|
) |
165
|
|
|
self.assertEqual(resp.status_code, 400) |
166
|
|
|
self.assertEqual(Feedback.objects.all().count(), 0) |
167
|
|
|
|
168
|
|
|
# Fail to parse protobuf messages |
169
|
|
|
with self.assertRaises(BaseException): |
170
|
|
|
self.client.post( |
171
|
|
|
reverse('feedback'), |
172
|
|
|
data={}, |
173
|
|
|
) |
174
|
|
|
self.assertEqual(Feedback.objects.all().count(), 0) |
175
|
|
|
|