1
# Copyright (C) 2010 Linaro Limited
3
# Author: Zygmunt Krynicki <zygmunt.krynicki@linaro.org>
5
# This file is part of Launch Control.
7
# Launch Control is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Affero General Public License version 3
9
# as published by the Free Software Foundation
11
# Launch Control is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU Affero General Public License
17
# along with Launch Control. If not, see <http://www.gnu.org/licenses/>.
20
Tests for the BundleStream model
23
from django.contrib.auth.models import User, Group
24
from django.core.exceptions import PermissionDenied
25
from django.db import IntegrityError
26
from django_testscenarios.ubertest import TestCase, TestCaseWithScenarios
28
from dashboard_app.models import BundleStream
29
from dashboard_app.tests import fixtures
32
class BundleStreamTests(TestCaseWithScenarios):
40
('anonymous-no-slug', {
41
'pathname': '/anonymous/',
43
'is_anonymous': "true",
46
('anonymous-with-slug', {
50
'is_anonymous': "true",
51
'pathname': '/anonymous/slug/',
54
('personal-no-slug', {
55
'username': _USERNAME,
56
'pathname': '/private/personal/user/',
58
('personal-with-slug', {
59
'username': _USERNAME,
62
'pathname': '/private/personal/user/slug/',
65
'groupname': _GROUPNAME,
66
'pathname': '/private/team/group/',
69
'groupname': _GROUPNAME,
72
'pathname': '/private/team/group/slug/',
86
super(BundleStreamTests, self).setUp()
87
if self.username is not None:
88
self.user = User.objects.create(username='user')
89
if self.groupname is not None:
90
self.group = Group.objects.create(name='group')
92
def test_creation(self):
93
bundle_stream = BundleStream.objects.create(
101
self.assertEqual(bundle_stream.user, self.user)
102
self.assertEqual(bundle_stream.group, self.group)
103
self.assertEqual(bundle_stream.name, self.name)
104
self.assertEqual(bundle_stream.slug, self.slug)
106
def test_team_named_stream(self):
107
bundle_stream = BundleStream.objects.create(
108
user=self.user, group=self.group, name=self.name, slug=self.slug,
109
is_anonymous=self.is_anonymous, is_public=self.is_public)
111
self.assertEqual(bundle_stream.pathname, self.pathname)
113
def test_pathname_uniqueness(self):
114
bundle_stream = BundleStream.objects.create(
124
BundleStream.objects.create,
125
user=self.user, group=self.group, slug=self.slug,
129
def test_pathname_update(self):
130
bundle_stream = BundleStream.objects.create(
138
old_pathname = bundle_stream.pathname
139
bundle_stream.slug += "-changed"
141
self.assertNotEqual(bundle_stream.pathname, old_pathname)
143
bundle_stream.pathname,
144
bundle_stream._calc_pathname()
147
def test_unicode(self):
148
obj = BundleStream(pathname=self.pathname)
149
self.assertEqual(unicode(obj), self.pathname)
152
class BundleStreamPermissionTests(TestCase):
154
def test_can_upload_to_anonymous(self):
155
user = User.objects.create(username='user')
156
bundle_stream = fixtures.create_bundle_stream("/anonymous/")
157
self.assertTrue(bundle_stream.can_upload(user))
159
def test_can_upload_to_owned_stream(self):
160
bundle_stream = fixtures.create_bundle_stream("/public/personal/owner/")
161
user = User.objects.get(username='owner')
162
self.assertTrue(bundle_stream.can_upload(user))
164
def test_can_upload_to_other_stream(self):
165
bundle_stream = fixtures.create_bundle_stream("/public/personal/owner/")
166
user = User.objects.create(username='non-owner')
167
self.assertFalse(bundle_stream.can_upload(user))
169
def test_group_can_upload_to_owned(self):
170
group = Group.objects.create(name='group')
171
member = User.objects.create(username="member")
172
group.user_set.add(member)
173
other = User.objects.create(username="other")
174
bundle_stream = fixtures.create_bundle_stream("/public/team/group/basic/")
175
self.assertTrue(bundle_stream.can_upload(member))
176
self.assertFalse(bundle_stream.can_upload(other))
178
def test_create_from_pathname_permission_denied_user(self):
179
user = User.objects.get_or_create(username="non-owner")[0]
182
BundleStream.create_from_pathname,
183
"/private/personal/owner/name/", user)
185
def test_create_from_pathname_permission_denied_group(self):
186
user = User.objects.get_or_create(username="non-owner")[0]
189
BundleStream.create_from_pathname,
190
"/public/team/group/name/", user)
192
def test_create_from_pathname_permission_denied_anonymous(self):
195
BundleStream.create_from_pathname,
196
"/public/team/group/name/", user=None)
198
def test_create_from_pathname(self):
199
user = User.objects.get_or_create(username="owner")[0]
200
bundle_stream = BundleStream.create_from_pathname(
201
"/private/personal/owner/name/", user)
202
self.assertEqual(bundle_stream.pathname,
203
"/private/personal/owner/name/")
204
group = Group.objects.create(name='group')
205
group.user_set.add(user)
206
bundle_stream = BundleStream.create_from_pathname(
207
"/private/team/group/name/", user)
208
self.assertEqual(bundle_stream.pathname, "/private/team/group/name/")
209
bundle_stream = BundleStream.create_from_pathname(
210
"/anonymous/name/", user=None)
211
self.assertEqual(bundle_stream.pathname, "/anonymous/name/")
213
def test_create_from_pathname_permission_denied_integrity(self):
214
BundleStream.create_from_pathname("/anonymous/name/", user=None)
217
BundleStream.create_from_pathname,
218
"/anonymous/name/", user=None)