Completed
Push — master ( 319370...171ce4 )
by
unknown
01:23
created

VersionModelTest.test_post_save_callback()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 8
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_callbac(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
    def test_post_save_callback(self):
109
        version = VersionFactory.create(file=SimpleUploadedFile('./chrome_installer.exe', b''))
110
        install_actions = Action.objects.filter(version=version, event=1)
111
        self.assertEqual(len(install_actions), 1)
112
        self.assertEqual(install_actions.first().event, 1)
113
        update_actions = Action.objects.filter(version=version, event=1)
114
        self.assertEqual(len(update_actions), 1)
115
        self.assertEqual(update_actions.first().event, 1)
116
117
118
class ActionModelTest(test.TestCase):
119
    @temporary_media_root()
120
    def test_get_attributes(self):
121
        ver = VersionFactory.create(file=SimpleUploadedFile('./chrome_installer.exe', False))
122
        action = Action(
123
            version=ver,
124
            arguments='--do-not-launch-chrome',
125
            event=EVENT_DICT_CHOICES['install'],
126
            run='chrome_installer.exe'
127
        )
128
129
        self.assertDictEqual(
130
            action.get_attributes(),
131
            dict(
132
                arguments='--do-not-launch-chrome',
133
                run='chrome_installer.exe',
134
            ))
135
136
        action = Action(
137
            version=ver,
138
            terminateallbrowsers=True,
139
            event=EVENT_DICT_CHOICES['postinstall'],
140
            other=dict(
141
                version='13.0.782.112',
142
                onsuccess='exitsilentlyonlaunchcmd')
143
        )
144
145
        self.assertDictEqual(
146
            action.get_attributes(),
147
            dict(
148
                terminateallbrowsers='true',
149
                version='13.0.782.112',
150
                onsuccess='exitsilentlyonlaunchcmd',
151
            )
152
        )
153