summaryrefslogtreecommitdiffstats
path: root/omaha_server/crash/tests/test_serializers.py
blob: 34350ecb71980736952ff4cd4fea3248cb547ad4 (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
# 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.
"""

from builtins import str

import os

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

from omaha.tests.utils import temporary_media_root

from crash.models import Symbols, Crash
from crash.serializers import SymbolsSerializer, CrashSerializer


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')


class SymbolsSerializerTest(TestCase):
    def test_serializer(self):
        data = dict(file=SimpleUploadedFile('./test.pdb', False),
                    debug_id='C1C0FA629EAA4B4D9DD2ADE270A231CC1',
                    debug_file='BreakpadTestApp.pdb')
        symbols = Symbols.objects.create(**data)
        self.assertDictEqual(SymbolsSerializer(symbols).data,
                             dict(id=symbols.id,
                                  debug_id='C1C0FA629EAA4B4D9DD2ADE270A231CC1',
                                  debug_file='BreakpadTestApp.pdb',
                                  file=symbols.file.url,
                                  created=symbols.created.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
                                  modified=symbols.modified.strftime('%Y-%m-%dT%H:%M:%S.%fZ'), ))

    @temporary_media_root(MEDIA_URL='http://cache.pack.google.com/edgedl/chrome/install/782.112/')
    def test_auto_fill_file_size(self):
        with open(SYM_FILE, 'rb') as f:
            data = dict(file=SimpleUploadedFile('./BreakpadTestApp.sym', f.read()))

        symbols = SymbolsSerializer(data=data)
        self.assertTrue(symbols.is_valid())
        symbols_instance = symbols.save()
        self.assertEqual(symbols_instance.debug_id, 'C1C0FA629EAA4B4D9DD2ADE270A231CC1')
        self.assertEqual(symbols_instance.debug_file, 'BreakpadTestApp.pdb')


class CrashSerializerTest(TestCase):
    maxDiff = None

    @temporary_media_root(
        CELERY_ALWAYS_EAGER=False,
        CELERY_EAGER_PROPAGATES_EXCEPTIONS=False,
    )
    def test_serializer(self):
        meta = dict(
            lang='en',
            version='1.0.0.1',
        )
        stacktrace_json = dict(
            crashing_thread={},
        )
        app_id = '{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}'
        user_id = '{2882CF9B-D9C2-4edb-9AAF-8ED5FCF366F7}'
        crash = Crash.objects.create(
            appid=app_id,
            userid=user_id,
            upload_file_minidump=SimpleUploadedFile('./dump.dat', b''),
            meta=meta,
            stacktrace_json=stacktrace_json
        )
        self.assertDictEqual(CrashSerializer(crash).data,
                             dict(id=crash.id,
                                  upload_file_minidump=crash.upload_file_minidump.url,
                                  archive=None,
                                  appid=str(crash.appid),
                                  userid=str(crash.userid),
                                  meta=meta,
                                  signature=crash.signature,
                                  stacktrace_json=crash.stacktrace_json,
                                  created=crash.created.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
                                  modified=crash.modified.strftime('%Y-%m-%dT%H:%M:%S.%fZ')))