Completed
Pull Request — master (#260)
by Kirill
01:25
created

VersionModelTest.test_pre_save_callback()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
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
from django import test
22
from django.core.files.uploadedfile import SimpleUploadedFile
23
24
from mock import patch
25
26
from omaha.models import Application, Channel, Platform, Version, Action, EVENT_DICT_CHOICES
27
from omaha.models import version_upload_to
28
from omaha.factories import ApplicationFactory, ChannelFactory, PlatformFactory, VersionFactory
29
from omaha.tests.utils import temporary_media_root
30
31
32
class ApplicationModelTest(test.SimpleTestCase):
33
    allow_database_queries = True
34
35
    def test_factory(self):
36
        app = ApplicationFactory.create()
37
        self.assertTrue(Application.objects.get(id=app.id))
38
39
40
class ChannelModelTest(test.TestCase):
41
    def test_factory(self):
42
        channel = ChannelFactory.create()
43
        self.assertTrue(Channel.objects.get(id=channel.id))
44
45
46
class PlatformModelTest(test.TestCase):
47
    def test_factory(self):
48
        platform = PlatformFactory.create()
49
        self.assertTrue(Platform.objects.get(id=platform.id))
50
51
52
class VersionModelTest(test.TestCase):
53
    @temporary_media_root()
54
    def test_version_upload_to(self):
55
        version = VersionFactory.create(file=SimpleUploadedFile('./chrome_installer.exe', False))
56
        self.assertEqual(version_upload_to(version, 'chrome_installer.exe'),
57
                         'build/{}/{}/{}/{}/chrome_installer.exe'.format(
58
                             version.app.name,
59
                             version.channel.name,
60
                             version.platform.name,
61
                             version.version,
62
                             version.file.name,
63
                         ))
64
65
66
    @temporary_media_root()
67
    def test_factory(self):
68
        version = VersionFactory.create(file=SimpleUploadedFile('./chrome_installer.exe', False))
69
        self.assertTrue(Version.objects.get(id=version.id))
70
71
    @temporary_media_root(MEDIA_URL='http://cache.pack.google.com/edgedl/chrome/install/782.112/')
72
    @patch('omaha.models.version_upload_to', lambda o, f: f)
73
    def test_property(self):
74
        version = VersionFactory.create(file=SimpleUploadedFile('./chrome_installer.exe', ''))
75
        self.assertEqual(version.file_absolute_url,
76
                         'http://cache.pack.google.com/edgedl/chrome/install/782.112/chrome_installer.exe')
77
        self.assertEqual(version.file_package_name, 'chrome_installer.exe')
78
        self.assertEqual(version.file_url,
79
                         u'http://cache.pack.google.com/edgedl/chrome/install/782.112/')
80
81
    @temporary_media_root()
82
    @test.override_settings(OMAHA_URL_PREFIX='http://example.com')
83
    def test_property_default_storage(self):
84
        version = VersionFactory.create(file=SimpleUploadedFile('./chrome_installer.exe', ''))
85
        _url = 'http://example.com/static/media/build/%s/%s/%s/37.0.2062.124/chrome_installer.exe' \
86
              % (version.app.name, version.channel.name, version.platform.name)
87
        self.assertEqual(version.file_absolute_url, _url)
88
        self.assertEqual(version.file_package_name, 'chrome_installer.exe')
89
        _url = u'http://example.com/static/media/build/%s/%s/%s/37.0.2062.124/' \
90
               % (version.app.name, version.channel.name, version.platform.name)
91
        self.assertEqual(version.file_url, _url)
92
93
    @temporary_media_root(MEDIA_URL='http://cache.pack.google.com/edgedl/chrome/install/782.112/')
94
    @patch('omaha.models.version_upload_to', lambda o, f: f)
95
    def test_property_s3_meta_data(self):
96
        version = VersionFactory.create(file=SimpleUploadedFile('./chrome_installer.exe', ''))
97
        self.assertEqual(version.file_package_name, 'chrome_installer.exe')
98
        self.assertEqual(version.file_url,
99
                         'http://cache.pack.google.com/edgedl/chrome/install/782.112/')
100
        self.assertEqual(version.size, 123)
101
102
    @temporary_media_root()
103
    def test_pre_save_callback(self):
104
        version = VersionFactory.create(file=SimpleUploadedFile('./chrome_installer.exe', b''))
105
        self.assertEqual(version.file.size, 0)
106
        self.assertEqual(version.file_hash, '2jmj7l5rSw0yVb/vlWAYkK/YBwk=')
107
108
109
class ActionModelTest(test.TestCase):
110
    @temporary_media_root()
111
    def test_get_attributes(self):
112
        ver = VersionFactory.create(file=SimpleUploadedFile('./chrome_installer.exe', False))
113
        action = Action(
114
            version=ver,
115
            arguments='--do-not-launch-chrome',
116
            event=EVENT_DICT_CHOICES['install'],
117
            run='chrome_installer.exe'
118
        )
119
120
        self.assertDictEqual(
121
            action.get_attributes(),
122
            dict(
123
                arguments='--do-not-launch-chrome',
124
                run='chrome_installer.exe',
125
            ))
126
127
        action = Action(
128
            version=ver,
129
            terminateallbrowsers=True,
130
            event=EVENT_DICT_CHOICES['postinstall'],
131
            other=dict(
132
                version='13.0.782.112',
133
                onsuccess='exitsilentlyonlaunchcmd')
134
        )
135
136
        self.assertDictEqual(
137
            action.get_attributes(),
138
            dict(
139
                terminateallbrowsers='true',
140
                version='13.0.782.112',
141
                onsuccess='exitsilentlyonlaunchcmd',
142
            )
143
        )
144