CrashDescriptionViewTest.test_get_bad()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
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
21
import os
22
23
from django import test
24
from django.core.files.uploadedfile import SimpleUploadedFile
25
from django.core.urlresolvers import reverse
26
from django.conf import settings
27
from django.db import DataError
28
29
from crash.models import Crash, CrashDescription
30
from crash.factories import CrashFactory, CrashDescriptionFactory
31
32
33
BASE_DIR = os.path.dirname(__file__)
34
TEST_DATA_DIR = os.path.join(BASE_DIR, 'testdata')
35
TAR_FILE = os.path.join(TEST_DATA_DIR, 'foo.tar')
36
TAR_FILE_ONLY_INSTRUMENTAL_FILE = os.path.join(TEST_DATA_DIR, 'foo_only_instrumental_file.tar')
37
38
39
class CrashViewTest(test.TestCase):
40 View Code Duplication
    @test.override_settings(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
41
        CELERY_ALWAYS_EAGER=False,
42
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=False,
43
    )
44
    def test_view(self):
45
        meta = dict(
46
            lang='en',
47
            version='1.0.0.1',
48
        )
49
        mini_dump_file = SimpleUploadedFile("minidump.dat", b"content")
50
        form_data = dict(
51
            appid='{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}',
52
            userid='{2882CF9B-D9C2-4edb-9AAF-8ED5FCF366F7}',
53
            upload_file_minidump=mini_dump_file,
54
        )
55
56
        form_data.update(meta)
57
58
        self.assertEqual(Crash.objects.all().count(), 0)
59
        response = self.client.post(reverse('crash'), form_data, REMOTE_ADDR="8.8.8.8")
60
        self.assertEqual(response.status_code, 200)
61
        self.assertEqual(Crash.objects.all().count(), 1)
62
        obj = Crash.objects.get()
63
        self.assertEqual(response.content.decode(), str(obj.pk))
64
        self.assertDictEqual(obj.meta, meta)
65
        self.assertEqual(obj.appid, form_data['appid'])
66
        self.assertEqual(obj.userid, form_data['userid'])
67
        self.assertIsNotNone(obj.upload_file_minidump)
68
        self.assertEquals(obj.ip, '8.8.8.8')
69
70
    @test.override_settings(
71
        CELERY_ALWAYS_EAGER=False,
72
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=False,
73
    )
74
    def test_view_empty_ip(self):
75
        if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql_psycopg2':
76
            meta = dict(
77
                lang='en',
78
                version='1.0.0.1',
79
            )
80
            mini_dump_file = SimpleUploadedFile("minidump.dat", b"content")
81
            form_data = dict(
82
                appid='{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}',
83
                userid='{2882CF9B-D9C2-4edb-9AAF-8ED5FCF366F7}',
84
                upload_file_minidump=mini_dump_file,
85
            )
86
87
            form_data.update(meta)
88
89
            self.assertEqual(Crash.objects.all().count(), 0)
90
            response = self.client.post(reverse('crash'), form_data, REMOTE_ADDR="")
91
            self.assertEqual(response.status_code, 200)
92
            self.assertEqual(Crash.objects.all().count(), 1)
93
            obj = Crash.objects.get()
94
            self.assertEqual(response.content.decode(), str(obj.pk))
95
            self.assertDictEqual(obj.meta, meta)
96
            self.assertEqual(obj.appid, form_data['appid'])
97
            self.assertEqual(obj.userid, form_data['userid'])
98
            self.assertIsNotNone(obj.upload_file_minidump)
99
            self.assertEquals(obj.ip, None)
100
101 View Code Duplication
    @test.override_settings(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
102
        CELERY_ALWAYS_EAGER=False,
103
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=False,
104
    )
105
    def test_view_tar_file(self):
106
        meta = dict(
107
            lang='en',
108
            version='1.0.0.1',
109
        )
110
111
        with open(TAR_FILE, 'rb') as f:
112
            mini_dump_file = SimpleUploadedFile("foo.tar", f.read())
113
114
        form_data = dict(
115
            appid='{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}',
116
            userid='{2882CF9B-D9C2-4edb-9AAF-8ED5FCF366F7}',
117
            upload_file_minidump=mini_dump_file,
118
        )
119
120
        form_data.update(meta)
121
122
        self.assertEqual(Crash.objects.all().count(), 0)
123
        response = self.client.post(reverse('crash'), form_data)
124
        self.assertEqual(response.status_code, 200)
125
        self.assertEqual(Crash.objects.all().count(), 1)
126
        obj = Crash.objects.get()
127
        self.assertEqual(response.content.decode(), str(obj.pk))
128
        self.assertDictEqual(obj.meta, meta)
129
        self.assertEqual(obj.appid, form_data['appid'])
130
        self.assertEqual(obj.userid, form_data['userid'])
131
        self.assertIsNotNone(obj.upload_file_minidump)
132
        self.assertIsNotNone(obj.archive)
133
134 View Code Duplication
    @test.override_settings(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
135
        CELERY_ALWAYS_EAGER=False,
136
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=False,
137
    )
138
    def test_view_tar_only_instrumental_file(self):
139
        meta = dict(
140
            lang='en',
141
            version='1.0.0.1',
142
        )
143
144
        with open(TAR_FILE_ONLY_INSTRUMENTAL_FILE, 'rb') as f:
145
            mini_dump_file = SimpleUploadedFile("foo_only_instrumental_file.tar", f.read())
146
147
        form_data = dict(
148
            appid='{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}',
149
            userid='{2882CF9B-D9C2-4edb-9AAF-8ED5FCF366F7}',
150
            upload_file_minidump=mini_dump_file,
151
        )
152
153
        form_data.update(meta)
154
155
        self.assertEqual(Crash.objects.all().count(), 0)
156
        response = self.client.post(reverse('crash'), form_data)
157
        self.assertEqual(response.status_code, 200)
158
        self.assertEqual(Crash.objects.all().count(), 1)
159
        obj = Crash.objects.get()
160
        self.assertEqual(response.content.decode(), str(obj.pk))
161
        self.assertDictEqual(obj.meta, meta)
162
        self.assertEqual(obj.appid, form_data['appid'])
163
        self.assertEqual(obj.userid, form_data['userid'])
164
        self.assertFalse(obj.upload_file_minidump.name)
165
        self.assertIsNotNone(obj.archive)
166
167
168
class CrashDescriptionViewTest(test.TestCase):
169
    def test_get_bad(self):
170
        # no crash with such id
171
        response = self.client.get(
172
            reverse('crash_description', kwargs=dict(pk=10))
173
        )
174
        self.assertEquals(response.status_code, 400)
175
176
        # crash with description added
177
        description = CrashDescriptionFactory()
178
        response = self.client.get(
179
            reverse('crash_description', kwargs=dict(pk=description.crash.pk))
180
        )
181
        self.assertEquals(response.status_code, 400)
182
183
    def test_get_good(self):
184
        crash = CrashFactory()
185
        comment = 'Crash comment'
186
        data = dict(
187
            comment=comment
188
        )
189
        response = self.client.get(
190
            reverse('crash_description', kwargs=dict(pk=crash.pk)),
191
            data
192
        )
193
        self.assertEquals(response.status_code, 200)
194
        self.assertEquals(response.context['form'].initial['description'], comment)
195
196
    def test_post_bad(self):
197
        summary = 'Test summary'
198
        description = 'Test Description'
199
        form_data = dict(
200
            summary=summary,
201
            description=description
202
        )
203
204
        # no crash with such id
205
        response = self.client.post(
206
            reverse('crash_description', kwargs=dict(pk=10)),
207
            form_data
208
        )
209
        self.assertEquals(response.status_code, 400)
210
211
        # crash with description added
212
        description = CrashDescriptionFactory()
213
        response = self.client.get(
214
            reverse('crash_description', kwargs=dict(pk=description.crash.pk)),
215
            form_data
216
        )
217
        self.assertEquals(response.status_code, 400)
218
219
    def test_post_good(self):
220
        crash = CrashFactory()
221
        summary = 'Test summary'
222
        description = 'Test Description'
223
        form_data = dict(
224
            summary=summary,
225
            description=description
226
        )
227
228
        self.assertEqual(CrashDescription.objects.all().count(), 0)
229
        response = self.client.post(
230
            reverse('crash_description', kwargs=dict(pk=crash.pk)),
231
            form_data,
232
            follow=True
233
        )
234
        self.assertEqual(response.status_code, 200)
235
        self.assertIn('crash/crash_description_submitted.html', response.template_name)
236
        self.assertEqual(CrashDescription.objects.all().count(), 1)
237
        obj = CrashDescription.objects.get()
238
        self.assertEquals(obj.crash, crash)
239
        self.assertEquals(obj.summary, summary)
240
        self.assertEquals(obj.description, description)
241