summaryrefslogtreecommitdiffstats
path: root/omaha_server/crash/tests/test_forms.py
blob: 9ddf2024091c47a8887f4b7b4d5ed5816755701e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# coding: utf8

"""
This software is licensed under the Apache 2 license, quoted below.

Copyright 2014 Crystalnix Limited

Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
"""

import os
import string
import random

from django.test import TestCase
from django.core.files.uploadedfile import SimpleUploadedFile

from crash.forms import SymbolsAdminForm, CrashFrom


BASE_DIR = os.path.dirname(__file__)
TEST_DATA_DIR = os.path.join(BASE_DIR, 'testdata')
SYM_FILE = os.path.join(TEST_DATA_DIR, 'BreakpadTestApp.sym')
TAR_FILE = os.path.join(TEST_DATA_DIR, 'foo.tar')


def string_generator(size, chars=string.ascii_uppercase + string.ascii_lowercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))


class SymbolsAdminFormTest(TestCase):
    def test_form(self):
        form_data = {}

        with open(SYM_FILE, 'rb') as f:
            form_file_data = {'file': SimpleUploadedFile('BreakpadTestApp.sym', f.read())}

        form = SymbolsAdminForm(form_data, form_file_data)

        self.assertTrue(form.is_valid())
        self.assertEqual(form.cleaned_data['debug_id'], 'C1C0FA629EAA4B4D9DD2ADE270A231CC1')
        self.assertEqual(form.cleaned_data['debug_file'], 'BreakpadTestApp.pdb')
        self.assertEqual(form.cleaned_data['file_size'], 68149)


class CrashFormTest(TestCase):
    def test_form(self):
        form_file_data = dict(upload_file_minidump=SimpleUploadedFile(
            "7b05e196-7e23-416b-bd13-99287924e214.dmp", b"content"))
        form_data = dict(
            appid='{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}',
            userid='{2882CF9B-D9C2-4edb-9AAF-8ED5FCF366F7}',
        )

        form = CrashFrom(form_data, form_file_data)

        self.assertTrue(form.is_valid())
        self.assertEqual(form.cleaned_data['upload_file_minidump'].name, '7b05e196-7e23-416b-bd13-99287924e214.dmp')
        self.assertEqual(form.cleaned_data['archive_size'], 0)
        self.assertEqual(form.cleaned_data['minidump_size'], 7)

    def test_form_tar_file(self):
        with open(TAR_FILE, 'rb') as f:
            form_file_data = dict(upload_file_minidump=SimpleUploadedFile(
                "foo.tar", f.read()))
        form_data = dict(
            appid='{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}',
            userid='{2882CF9B-D9C2-4edb-9AAF-8ED5FCF366F7}',
        )

        form = CrashFrom(form_data, form_file_data)

        self.assertTrue(form.is_valid())
        self.assertEqual(form.cleaned_data['upload_file_minidump'].name, '7b05e196-7e23-416b-bd13-99287924e214.dmp')
        self.assertEqual(form.cleaned_data['archive'].name, 'foo.tar')
        self.assertEqual(form.cleaned_data['archive_size'], 85504)
        self.assertEqual(form.cleaned_data['minidump_size'], 14606)

    def test_invalid_data(self):
        with open(TAR_FILE, 'rb') as f:
            form_file_data = dict(upload_file_minidump=SimpleUploadedFile(
                "foo.tar", f.read(100)))
        form_data = dict(
            appid=string_generator(40),
            userid=string_generator(40),
            meta=string_generator(40),
            stacktrace=string_generator(40),
            stacktrace_json=string_generator(40),
            signature=string_generator(256),
            ip=string_generator(40),
            groupid=string_generator(40),
            eventid=string_generator(40),
        )

        form = CrashFrom(form_data, form_file_data)
        self.assertFalse(form.is_valid())
        self.assertIn('upload_file_minidump', form.errors)
        self.assertIn('appid', form.errors)
        self.assertIn('userid', form.errors)
        self.assertIn('meta', form.errors)
        self.assertNotIn('stacktrace', form.errors)
        self.assertIn('stacktrace_json', form.errors)
        self.assertIn('signature', form.errors)
        self.assertIn('ip', form.errors)
        self.assertIn('groupid', form.errors)
        self.assertIn('eventid', form.errors)