From 846f2fc6566374174011c3a1b63597b301ff77e5 Mon Sep 17 00:00:00 2001 From: Michael Simacek Date: Tue, 12 Sep 2017 20:44:39 +0300 Subject: [PATCH] [add-package] Fix group handling Fixes #185 --- koschei/frontend/views.py | 2 +- test/frontend_test.py | 67 ++++++++++++++++++++++++++++++++++++--- test/model_test.py | 14 +++++++- 3 files changed, 77 insertions(+), 6 deletions(-) diff --git a/koschei/frontend/views.py b/koschei/frontend/views.py index 9f0a7ae3..5e10e276 100644 --- a/koschei/frontend/views.py +++ b/koschei/frontend/views.py @@ -764,7 +764,7 @@ def add_packages(): abort(404) if form.group.data: - name, namespace = PackageGroup.parse_name(form.group.data) + namespace, name = PackageGroup.parse_name(form.group.data) group = db.query(PackageGroup)\ .filter_by(namespace=namespace, name=name)\ .first_or_404() diff --git a/test/frontend_test.py b/test/frontend_test.py index 597756ee..7ec491d0 100644 --- a/test/frontend_test.py +++ b/test/frontend_test.py @@ -43,8 +43,8 @@ def authenticate(fn): @wraps(fn) def decorated(*args, **kwargs): self = args[0] - user = self.prepare_user(name='jdoe', admin=False) - login(self.client, user) + self.user = self.prepare_user(name='jdoe', admin=False) + login(self.client, self.user) return fn(*args, **kwargs) return decorated @@ -53,13 +53,17 @@ def authenticate_admin(fn): @wraps(fn) def decorated(*args, **kwargs): self = args[0] - user = self.prepare_user(name='admin', admin=True) - login(self.client, user) + self.user = self.prepare_user(name='admin', admin=True) + login(self.client, self.user) return fn(*args, **kwargs) return decorated class FrontendTest(DBTest): + def __init__(self, *args, **kwargs): + super(FrontendTest, self).__init__(*args, **kwargs) + self.user = None + def get_session(self): return db @@ -184,6 +188,61 @@ def test_add_package(self): self.assertIn('Packages added: xpp3', reply.data.decode('utf-8')) self.assertTrue(pkg.tracked) + def _test_add_package_group(self, namespace): + pkg = self.prepare_packages('xpp3')[0] + group = self.prepare_group( + name='foo', + namespace=namespace, + content=['bar'], + owners=[self.user.name], + ) + pkg.tracked = False + self.db.commit() + reply = self.client.post( + 'add-packages', + data=dict( + packages='xpp3', + collection=self.collection.name, + group=group.full_name, + ), + follow_redirects=True, + ) + self.assertEqual(200, reply.status_code) + self.assertIn('Packages added: xpp3', reply.data.decode('utf-8')) + self.assertTrue(pkg.tracked) + self.assertEqual([group], pkg.groups) + + @authenticate + def test_add_package_user_group(self): + self._test_add_package_group(None) + + @authenticate + def test_add_package_global_group(self): + self._test_add_package_group('user') + + @authenticate + def test_add_package_grup_no_permission(self): + pkg = self.prepare_packages('xpp3')[0] + group = self.prepare_group( + name='foo', + content=['bar'], + owners=['someone_else'], + ) + pkg.tracked = False + self.db.commit() + reply = self.client.post( + 'add-packages', + data=dict( + packages='xpp3', + collection=self.collection.name, + group=group.full_name, + ), + follow_redirects=True, + ) + self.assertEqual(400, reply.status_code) + self.assertFalse(pkg.tracked) + self.assertEqual(0, len(pkg.groups)) + def test_create_group_unauth(self): reply = self.client.post( 'add-group', diff --git a/test/model_test.py b/test/model_test.py index 740654ed..08bb1390 100644 --- a/test/model_test.py +++ b/test/model_test.py @@ -20,11 +20,23 @@ from sqlalchemy import literal_column from datetime import datetime, timedelta -from koschei.models import Package, Collection, Build, ResourceConsumptionStats, ScalarStats, KojiTask +from koschei.models import ( + Package, Collection, Build, ResourceConsumptionStats, ScalarStats, KojiTask, + PackageGroup, +) from test.common import DBTest class GroupTest(DBTest): + def test_group_name_format(self): + group1 = self.prepare_group('foo', content=['foo']) + group2 = self.prepare_group('bar', namespace='ns', content=['foo']) + self.assertEqual('foo', group1.full_name) + self.assertEqual('ns/bar', group2.full_name) + + def test_group_name_parse(self): + self.assertEqual((None, 'foo'), PackageGroup.parse_name('foo')) + self.assertEqual(('ns', 'bar'), PackageGroup.parse_name('ns/bar')) def test_group_cardinality(self): group = self.prepare_group('xyzzy', content=['foo', 'bar', 'baz'])