From 68a9d520f2164bd82aaf413c0d156a41a2241598 Mon Sep 17 00:00:00 2001 From: jedi Date: Sat, 1 Jul 2023 20:42:57 +0200 Subject: [PATCH] refactor tests for better composability --- backend/authentication/tests/helpers.py | 50 +++++--- backend/authentication/tests/test_auth.py | 150 +++++++++++----------- backend/toolshed/tests/__init__.py | 1 + backend/toolshed/tests/fixtures.py | 25 ++++ backend/toolshed/tests/test_api.py | 33 +++-- backend/toolshed/tests/test_category.py | 48 +++---- backend/toolshed/tests/test_friend.py | 94 +++++++------- backend/toolshed/tests/test_inventory.py | 76 ++++------- backend/toolshed/tests/test_property.py | 68 +++++----- backend/toolshed/tests/test_tag.py | 72 ++++++----- 10 files changed, 316 insertions(+), 301 deletions(-) create mode 100644 backend/toolshed/tests/fixtures.py diff --git a/backend/authentication/tests/helpers.py b/backend/authentication/tests/helpers.py index 3eaaa55..e8ef702 100644 --- a/backend/authentication/tests/helpers.py +++ b/backend/authentication/tests/helpers.py @@ -36,51 +36,63 @@ class SignatureAuthClient: base = Client(SERVER_NAME='testserver') def get(self, target, user, **kwargs): + if user is None: + raise ValueError("User must not be None") signature = user.sign("http://testserver" + target) header = {'HTTP_AUTHORIZATION': 'Signature ' + str(user) + ':' + signature} return self.base.get(target, **header, **kwargs) def post(self, target, user, data, **kwargs): + if user is None: + raise ValueError("User must not be None") json_data = json.dumps(data, separators=(',', ':')) signature = user.sign("http://testserver" + target + json_data) header = {'HTTP_AUTHORIZATION': 'Signature ' + str(user) + ':' + signature} return self.base.post(target, json_data, **header, content_type='application/json', **kwargs) def put(self, target, user, data, **kwargs): + if user is None: + raise ValueError("User must not be None") json_data = json.dumps(data, separators=(',', ':')) signature = user.sign("http://testserver" + target + json_data) header = {'HTTP_AUTHORIZATION': 'Signature ' + str(user) + ':' + signature} return self.base.put(target, json_data, **header, content_type='application/json', **kwargs) def patch(self, target, user, data, **kwargs): + if user is None: + raise ValueError("User must not be None") json_data = json.dumps(data, separators=(',', ':')) signature = user.sign("http://testserver" + target + json_data) header = {'HTTP_AUTHORIZATION': 'Signature ' + str(user) + ':' + signature} return self.base.patch(target, json_data, **header, content_type='application/json', **kwargs) def delete(self, target, user, **kwargs): + if user is None: + raise ValueError("User must not be None") signature = user.sign("http://testserver" + target) header = {'HTTP_AUTHORIZATION': 'Signature ' + str(user) + ':' + signature} return self.base.delete(target, **header, **kwargs) -class UserTestCase(TestCase): - ext_user1 = None - ext_user2 = None - local_user1 = None - local_user2 = None +class ToolshedTestCase(TestCase): + f = {} - def setUp(self): - admin = ToolshedUser.objects.create_superuser('admin', 'admin@localhost', '') - admin.set_password('testpassword') - admin.save() - example_com = Domain.objects.create(name='example.com', owner=admin, open_registration=True) - example_com.save() - self.local_user1 = ToolshedUser.objects.create_user('testuser', 'test@abc.de', '', domain=example_com.name) - self.local_user1.set_password('testpassword2') - self.local_user1.save() - self.local_user2 = ToolshedUser.objects.create_user('testuser2', 'test2@abc.de', '', domain=example_com.name) - self.local_user2.set_password('testpassword3') - self.local_user2.save() - self.ext_user1 = DummyExternalUser('extuser1', 'external.org') - self.ext_user2 = DummyExternalUser('extuser2', 'external.org') + +class UserTestMixin: + + def prepare_users(self): + self.f['admin'] = ToolshedUser.objects.create_superuser('testadmin', 'testadmin@localhost', '') + self.f['admin'].set_password('testpassword') + self.f['admin'].save() + self.f['example_com'] = Domain.objects.create(name='example.com', owner=self.f['admin'], open_registration=True) + self.f['example_com'].save() + self.f['local_user1'] = ToolshedUser.objects.create_user('testuser1', 'test1@abc.de', '', + domain=self.f['example_com'].name) + self.f['local_user1'].set_password('testpassword2') + self.f['local_user1'].save() + self.f['local_user2'] = ToolshedUser.objects.create_user('testuser2', 'test2@abc.de', '', + domain=self.f['example_com'].name) + self.f['local_user2'].set_password('testpassword3') + self.f['local_user2'].save() + self.f['ext_user1'] = DummyExternalUser('extuser1', 'external.org') + self.f['ext_user2'] = DummyExternalUser('extuser2', 'external.org') diff --git a/backend/authentication/tests/test_auth.py b/backend/authentication/tests/test_auth.py index 2089e52..692bdd4 100644 --- a/backend/authentication/tests/test_auth.py +++ b/backend/authentication/tests/test_auth.py @@ -1,15 +1,15 @@ import json -from django.test import TestCase, Client, RequestFactory +from django.test import Client, RequestFactory from nacl.encoding import HexEncoder from nacl.signing import SigningKey from authentication.models import ToolshedUser, KnownIdentity -from authentication.tests import UserTestCase, SignatureAuthClient, DummyExternalUser +from authentication.tests import UserTestMixin, SignatureAuthClient, DummyExternalUser, ToolshedTestCase from hostadmin.models import Domain -class AuthorizationTestCase(TestCase): +class AuthorizationTestCase(ToolshedTestCase): def setUp(self): self.client = Client() @@ -88,7 +88,7 @@ class AuthorizationTestCase(TestCase): self.assertEqual(signature_bytes_hex, self.signature_with_data) -class KnownIdentityTestCase(TestCase): +class KnownIdentityTestCase(ToolshedTestCase): key = None def setUp(self): @@ -138,36 +138,37 @@ class KnownIdentityTestCase(TestCase): identity.verify(message, bytes.fromhex(signed.decode('utf-8'))) -class UserModelTestCase(UserTestCase): +class UserModelTestCase(UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() + self.prepare_users() def test_admin(self): - user = ToolshedUser.objects.get(username='admin') + user = self.f['admin'] self.assertTrue(user.is_superuser) self.assertTrue(user.is_staff) self.assertTrue(user.is_active) self.assertEqual(user.domain, 'localhost') - self.assertEqual(user.email, 'admin@localhost') - self.assertEqual(user.username, 'admin') + self.assertEqual(user.email, 'testadmin@localhost') + self.assertEqual(user.username, 'testadmin') def test_user(self): - user = ToolshedUser.objects.get(username='testuser') + user = self.f['local_user1'] self.assertFalse(user.is_superuser) self.assertFalse(user.is_staff) self.assertTrue(user.is_active) self.assertEqual(user.domain, 'example.com') - self.assertEqual(user.email, 'test@abc.de') - self.assertEqual(user.username, 'testuser') + self.assertEqual(user.email, 'test1@abc.de') + self.assertEqual(user.username, 'testuser1') self.assertEqual(len(user.private_key), 64) self.assertEqual(type(user.public_identity), KnownIdentity) self.assertEqual(user.public_identity.domain, 'example.com') - self.assertEqual(user.public_identity.username, 'testuser') + self.assertEqual(user.public_identity.username, 'testuser1') self.assertEqual(len(user.public_identity.public_key), 64) def test_create_existing_user(self): with self.assertRaises(ValueError): - ToolshedUser.objects.create_user('testuser', 'test3@abc.de', '', domain='example.com') + ToolshedUser.objects.create_user('testuser1', 'test3@abc.de', '', domain='example.com') def test_create_existing_user2(self): key = SigningKey.generate() @@ -178,7 +179,7 @@ class UserModelTestCase(UserTestCase): def test_create_reuse_email(self): with self.assertRaises(ValueError): - ToolshedUser.objects.create_user('testuser3', 'test@abc.de', '', domain='example.com') + ToolshedUser.objects.create_user('testuser3', 'test1@abc.de', '', domain='example.com') def test_create_user_invalid_private_key(self): with self.assertRaises(TypeError): @@ -193,60 +194,61 @@ class UserModelTestCase(UserTestCase): 'Z123456789abcdef0123456789abcdef') def test_signature(self): - user = self.local_user1 + user = self.f['local_user1'] message = 'some message' signature = user.sign(message) self.assertEqual(len(signature), 128) self.assertTrue(user.public_identity.verify(message, signature)) def test_signature_fail(self): - user = self.local_user1 + user = self.f['local_user1'] message = 'some message' signature = user.sign(message) self.assertFalse(user.public_identity.verify(message + 'x', signature)) def test_signature_fail2(self): - user = self.local_user1 + user = self.f['local_user1'] message = 'some message' signature = user.sign(message) signature = signature[:-2] + 'ee' self.assertFalse(user.public_identity.verify(message, signature)) def test_signature_fail3(self): - user1 = self.local_user1 - user2 = self.local_user2 + user1 = self.f['local_user1'] + user2 = self.f['local_user2'] message = 'some message' signature = user1.sign(message) self.assertFalse(user2.public_identity.verify(message, signature)) def test_signature_fail4(self): - user = self.local_user1 + user = self.f['local_user1'] message = 'some message' with self.assertRaises(TypeError): user.sign(message.encode('utf-8')) -class UserApiTestCase(UserTestCase): +class UserApiTestCase(UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() + self.prepare_users() self.anonymous_client = Client(SERVER_NAME='testserver') self.client = SignatureAuthClient() def test_user_info(self): - reply = self.client.get('/auth/user/', self.local_user1) + reply = self.client.get('/auth/user/', self.f['local_user1']) self.assertEqual(reply.status_code, 200) - self.assertEqual(reply.json()['username'], 'testuser') + self.assertEqual(reply.json()['username'], 'testuser1') self.assertEqual(reply.json()['domain'], 'example.com') - self.assertEqual(reply.json()['email'], 'test@abc.de') + self.assertEqual(reply.json()['email'], 'test1@abc.de') def test_user_info2(self): target = "/auth/user/" - signature = self.local_user1.sign("http://testserver" + target) - header = {'HTTP_AUTHORIZATION': 'Signature ' + str(self.local_user1) + ':' + signature} + signature = self.f['local_user1'].sign("http://testserver" + target) + header = {'HTTP_AUTHORIZATION': 'Signature ' + str(self.f['local_user1']) + ':' + signature} reply = self.anonymous_client.get(target, **header) self.assertEqual(reply.status_code, 200) - self.assertEqual(reply.json()['username'], 'testuser') + self.assertEqual(reply.json()['username'], 'testuser1') self.assertEqual(reply.json()['domain'], 'example.com') def test_user_info_fail(self): @@ -254,74 +256,75 @@ class UserApiTestCase(UserTestCase): self.assertEqual(reply.status_code, 403) def test_user_info_fail2(self): - reply = self.client.get('/auth/user/', self.ext_user1) + reply = self.client.get('/auth/user/', self.f['ext_user1']) self.assertEqual(reply.status_code, 403) def test_user_info_fail3(self): target = "/auth/user/" - signature = self.local_user1.sign("http://testserver2" + target) - header = {'HTTP_AUTHORIZATION': 'Signature ' + str(self.local_user1) + ':' + signature} + signature = self.f['local_user1'].sign("http://testserver2" + target) + header = {'HTTP_AUTHORIZATION': 'Signature ' + str(self.f['local_user1']) + ':' + signature} reply = self.anonymous_client.get(target, **header) self.assertEqual(reply.status_code, 403) def test_user_info_fail4(self): target = "/auth/user/" - signature = self.local_user1.sign("http://testserver" + target) - header = {'HTTP_AUTHORIZATION': 'Auth ' + str(self.local_user1) + ':' + signature} + signature = self.f['local_user1'].sign("http://testserver" + target) + header = {'HTTP_AUTHORIZATION': 'Auth ' + str(self.f['local_user1']) + ':' + signature} reply = self.anonymous_client.get(target, **header) self.assertEqual(reply.status_code, 403) def test_user_info_fail5(self): target = "/auth/user/" - signature = self.local_user1.sign("http://testserver" + target) - header = {'HTTP_AUTHORIZATION': 'Signature ' + str(self.local_user1)} + signature = self.f['local_user1'].sign("http://testserver" + target) + header = {'HTTP_AUTHORIZATION': 'Signature ' + str(self.f['local_user1'])} reply = self.anonymous_client.get(target, **header) self.assertEqual(reply.status_code, 403) def test_user_info_fail6(self): target = "/auth/user/" - signature = self.local_user1.sign("http://testserver" + target) - header = {'HTTP_AUTHORIZATION': 'Signature ' + str(self.local_user1) + ':' + signature + 'f'} + signature = self.f['local_user1'].sign("http://testserver" + target) + header = {'HTTP_AUTHORIZATION': 'Signature ' + str(self.f['local_user1']) + ':' + signature + 'f'} reply = self.anonymous_client.get(target, **header) self.assertEqual(reply.status_code, 403) def test_user_info_fail7(self): target = "/auth/user/" - signature = self.local_user1.sign("http://testserver" + target) - header = {'HTTP_AUTHORIZATION': 'Signature ' + self.local_user1.username + ':' + signature} + signature = self.f['local_user1'].sign("http://testserver" + target) + header = {'HTTP_AUTHORIZATION': 'Signature ' + self.f['local_user1'].username + ':' + signature} reply = self.anonymous_client.get(target, **header) self.assertEqual(reply.status_code, 403) def test_user_info_fail8(self): target = "/auth/user/" - signature = self.local_user1.sign("http://testserver" + target) - header = {'HTTP_AUTHORIZATION': 'Signature ' + self.local_user1.username + '@:' + signature} + signature = self.f['local_user1'].sign("http://testserver" + target) + header = {'HTTP_AUTHORIZATION': 'Signature ' + self.f['local_user1'].username + '@:' + signature} reply = self.anonymous_client.get(target, **header) self.assertEqual(reply.status_code, 403) def test_user_info_fail9(self): target = "/auth/user/" - signature = self.local_user1.sign("http://testserver" + target) - header = {'HTTP_AUTHORIZATION': 'Signature @' + self.local_user1.domain + ':' + signature} + signature = self.f['local_user1'].sign("http://testserver" + target) + header = {'HTTP_AUTHORIZATION': 'Signature @' + self.f['local_user1'].domain + ':' + signature} reply = self.anonymous_client.get(target, **header) self.assertEqual(reply.status_code, 403) -class FriendApiTestCase(UserTestCase): +class FriendApiTestCase(UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() - self.local_user1.friends.add(self.local_user2.public_identity) - self.local_user1.friends.add(self.ext_user1.public_identity) - self.ext_user1.friends.add(self.local_user1.public_identity) + self.prepare_users() + self.f['local_user1'].friends.add(self.f['local_user2'].public_identity) + self.f['local_user1'].friends.add(self.f['ext_user1'].public_identity) + self.f['ext_user1'].friends.add(self.f['local_user1'].public_identity) self.anonymous_client = Client(SERVER_NAME='testserver') self.client = SignatureAuthClient() def test_friend_local(self): - reply = self.client.get('/api/friends/', self.local_user1) + reply = self.client.get('/api/friends/', self.f['local_user1']) self.assertEqual(reply.status_code, 200) def test_friend_external(self): - reply = self.client.get('/api/friends/', self.ext_user1) + reply = self.client.get('/api/friends/', self.f['ext_user1']) self.assertEqual(reply.status_code, 200) def test_friend_fail(self): @@ -330,8 +333,8 @@ class FriendApiTestCase(UserTestCase): def test_friend_fail2(self): target = "/api/friends/" - signature = self.local_user1.sign("http://testserver2" + target) - header = {'HTTP_AUTHORIZATION': 'Signature ' + str(self.local_user1) + ':' + signature} + signature = self.f['local_user1'].sign("http://testserver2" + target) + header = {'HTTP_AUTHORIZATION': 'Signature ' + str(self.f['local_user1']) + ':' + signature} reply = self.anonymous_client.get(target, **header) self.assertEqual(reply.status_code, 403) @@ -345,19 +348,20 @@ class FriendApiTestCase(UserTestCase): def test_friend_fail4(self): target = "/api/friends/" - signature = self.local_user1.sign("http://testserver" + target) - header = {'HTTP_AUTHORIZATION': 'Auth ' + str(self.local_user1) + ':' + signature} + signature = self.f['local_user1'].sign("http://testserver" + target) + header = {'HTTP_AUTHORIZATION': 'Auth ' + str(self.f['local_user1']) + ':' + signature} reply = self.anonymous_client.get(target, **header) self.assertEqual(reply.status_code, 403) -class LoginApiTestCase(UserTestCase): +class LoginApiTestCase(UserTestMixin, ToolshedTestCase): user = None client = Client(SERVER_NAME='testserver') def setUp(self): super().setUp() - self.user = self.local_user1 + self.prepare_users() + self.user = self.f['local_user1'] def test_login(self): reply = self.client.post('/auth/token/', @@ -383,21 +387,15 @@ class LoginApiTestCase(UserTestCase): self.assertTrue('error' in reply.json()) -class RegistrationApiTestCase(TestCase): +class RegistrationApiTestCase(UserTestMixin, ToolshedTestCase): client = Client(SERVER_NAME='testserver') def setUp(self): - admin = ToolshedUser.objects.create_superuser('admin', 'admin@localhost', '') - admin.set_password('testpassword') - admin.save() - example_com = Domain.objects.create(name='example.com', owner=admin, open_registration=True) - example_com.save() - user2 = ToolshedUser.objects.create_user('testuser2', 'test2@abc.de', '', domain=example_com.name) - user2.set_password('testpassword3') - user2.save() + super().setUp() + self.prepare_users() def test_registration(self): - self.assertEqual(ToolshedUser.objects.all().count(), 2) + self.assertEqual(ToolshedUser.objects.all().count(), 3) reply = self.client.post('/auth/register/', {'username': 'testuser', 'password': 'testpassword2', 'domain': 'example.com', 'email': 'test@abc.de'}) @@ -408,7 +406,7 @@ class RegistrationApiTestCase(TestCase): self.assertEqual(user.email, 'test@abc.de') self.assertEqual(user.domain, 'example.com') self.assertTrue(user.check_password('testpassword2')) - self.assertEqual(ToolshedUser.objects.all().count(), 3) + self.assertEqual(ToolshedUser.objects.all().count(), 4) def test_registration_fail(self): reply = self.client.post('/auth/register/', @@ -417,7 +415,7 @@ class RegistrationApiTestCase(TestCase): self.assertEqual(reply.status_code, 400) self.assertTrue('errors' in reply.json()) self.assertTrue('username' in reply.json()['errors']) - self.assertEqual(ToolshedUser.objects.all().count(), 2) + self.assertEqual(ToolshedUser.objects.all().count(), 3) def test_registration_fail2(self): reply = self.client.post('/auth/register/', @@ -425,7 +423,7 @@ class RegistrationApiTestCase(TestCase): self.assertEqual(reply.status_code, 400) self.assertTrue('errors' in reply.json()) self.assertTrue('username' in reply.json()['errors']) - self.assertEqual(ToolshedUser.objects.all().count(), 2) + self.assertEqual(ToolshedUser.objects.all().count(), 3) def test_registration_fail3(self): reply = self.client.post('/auth/register/', @@ -434,7 +432,7 @@ class RegistrationApiTestCase(TestCase): self.assertEqual(reply.status_code, 400) self.assertTrue('errors' in reply.json()) self.assertTrue('password' in reply.json()['errors']) - self.assertEqual(ToolshedUser.objects.all().count(), 2) + self.assertEqual(ToolshedUser.objects.all().count(), 3) def test_registration_fail4(self): reply = self.client.post('/auth/register/', @@ -442,7 +440,7 @@ class RegistrationApiTestCase(TestCase): self.assertEqual(reply.status_code, 400) self.assertTrue('errors' in reply.json()) self.assertTrue('password' in reply.json()['errors']) - self.assertEqual(ToolshedUser.objects.all().count(), 2) + self.assertEqual(ToolshedUser.objects.all().count(), 3) def test_registration_fail5(self): reply = self.client.post('/auth/register/', @@ -451,7 +449,7 @@ class RegistrationApiTestCase(TestCase): self.assertEqual(reply.status_code, 400) self.assertTrue('errors' in reply.json()) self.assertTrue('domain' in reply.json()['errors']) - self.assertEqual(ToolshedUser.objects.all().count(), 2) + self.assertEqual(ToolshedUser.objects.all().count(), 3) def test_registration_fail6(self): reply = self.client.post('/auth/register/', @@ -459,7 +457,7 @@ class RegistrationApiTestCase(TestCase): self.assertEqual(reply.status_code, 400) self.assertTrue('errors' in reply.json()) self.assertTrue('domain' in reply.json()['errors']) - self.assertEqual(ToolshedUser.objects.all().count(), 2) + self.assertEqual(ToolshedUser.objects.all().count(), 3) def test_registration_fail7(self): reply = self.client.post('/auth/register/', @@ -468,7 +466,7 @@ class RegistrationApiTestCase(TestCase): self.assertEqual(reply.status_code, 400) self.assertTrue('errors' in reply.json()) self.assertTrue('email' in reply.json()['errors']) - self.assertEqual(ToolshedUser.objects.all().count(), 2) + self.assertEqual(ToolshedUser.objects.all().count(), 3) def test_registration_fail8(self): reply = self.client.post('/auth/register/', @@ -476,7 +474,7 @@ class RegistrationApiTestCase(TestCase): self.assertEqual(reply.status_code, 400) self.assertTrue('errors' in reply.json()) self.assertTrue('email' in reply.json()['errors']) - self.assertEqual(ToolshedUser.objects.all().count(), 2) + self.assertEqual(ToolshedUser.objects.all().count(), 3) def test_registration_existing_user(self): reply = self.client.post('/auth/register/', @@ -485,7 +483,7 @@ class RegistrationApiTestCase(TestCase): self.assertEqual(reply.status_code, 400) self.assertTrue('errors' in reply.json()) # TODO: check for sensible error message - self.assertEqual(ToolshedUser.objects.all().count(), 2) + self.assertEqual(ToolshedUser.objects.all().count(), 3) def test_registration_foreign_domain(self): reply = self.client.post('/auth/register/', @@ -494,7 +492,7 @@ class RegistrationApiTestCase(TestCase): self.assertEqual(reply.status_code, 400) self.assertTrue('errors' in reply.json()) self.assertTrue('domain' in reply.json()['errors']) - self.assertEqual(ToolshedUser.objects.all().count(), 2) + self.assertEqual(ToolshedUser.objects.all().count(), 3) def test_registration_reuse_email(self): reply = self.client.post('/auth/register/', @@ -503,4 +501,4 @@ class RegistrationApiTestCase(TestCase): self.assertEqual(reply.status_code, 400) self.assertTrue('errors' in reply.json()) self.assertTrue('email' in reply.json()['errors']) - self.assertEqual(ToolshedUser.objects.all().count(), 2) + self.assertEqual(ToolshedUser.objects.all().count(), 3) diff --git a/backend/toolshed/tests/__init__.py b/backend/toolshed/tests/__init__.py index e69de29..faab4b4 100644 --- a/backend/toolshed/tests/__init__.py +++ b/backend/toolshed/tests/__init__.py @@ -0,0 +1 @@ +from .fixtures import * \ No newline at end of file diff --git a/backend/toolshed/tests/fixtures.py b/backend/toolshed/tests/fixtures.py new file mode 100644 index 0000000..a59a36c --- /dev/null +++ b/backend/toolshed/tests/fixtures.py @@ -0,0 +1,25 @@ +from toolshed.models import Category, Tag, Property, InventoryItem, ItemProperty + + +class InventoryTestMixin: + def prepare_inventory(self): + self.f['local_user1'].friends.add(self.f['local_user2'].public_identity) + self.f['cat1'] = Category.objects.create(name='cat1') + self.f['cat2'] = Category.objects.create(name='cat2') + self.f['tag1'] = Tag.objects.create(name='tag1', category=self.f['cat1']) + self.f['tag2'] = Tag.objects.create(name='tag2', category=self.f['cat1']) + self.f['tag3'] = Tag.objects.create(name='tag3') + self.f['prop1'] = Property.objects.create(name='prop1') + self.f['prop2'] = Property.objects.create(name='prop2') + self.f['prop3'] = Property.objects.create(name='prop3', category=self.f['cat1']) + + self.f['item1'] = InventoryItem.objects.create(owner=self.f['local_user1'], owned_quantity=1, name='test1', + description='test', + category=self.f['cat1'], availability_policy='friends') + self.f['item2'] = InventoryItem.objects.create(owner=self.f['local_user1'], owned_quantity=1, name='test2', + description='test2', + category=self.f['cat1'], availability_policy='friends') + self.f['item2'].tags.add(self.f['tag1'], through_defaults={}) + self.f['item2'].tags.add(self.f['tag2'], through_defaults={}) + ItemProperty.objects.create(inventory_item=self.f['item2'], property=self.f['prop1'], value='value1').save() + ItemProperty.objects.create(inventory_item=self.f['item2'], property=self.f['prop2'], value='value2').save() diff --git a/backend/toolshed/tests/test_api.py b/backend/toolshed/tests/test_api.py index e8f41c8..bc23fb8 100644 --- a/backend/toolshed/tests/test_api.py +++ b/backend/toolshed/tests/test_api.py @@ -1,25 +1,25 @@ -from django.test import TestCase, Client -from authentication.tests import UserTestCase, SignatureAuthClient +from django.test import Client +from authentication.tests import UserTestMixin, SignatureAuthClient, ToolshedTestCase from toolshed.models import Category, Tag, Property anonymous_client = Client() client = SignatureAuthClient() -class CombinedApiTestCase(UserTestCase): +class CombinedApiTestCase(UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() - self.cat1 = Category.objects.create(name='cat1') - self.cat2 = Category.objects.create(name='cat2') - self.cat3 = Category.objects.create(name='cat3') - self.tag1 = Tag.objects.create(name='tag1') - self.tag2 = Tag.objects.create(name='tag2') - self.tag3 = Tag.objects.create(name='tag3') - self.prop1 = Property.objects.create(name='prop1') - self.prop2 = Property.objects.create(name='prop2') - self.prop3 = Property.objects.create(name='prop3') - + self.prepare_users() + self.f['cat1'] = Category.objects.create(name='cat1') + self.f['cat2'] = Category.objects.create(name='cat2') + self.f['cat3'] = Category.objects.create(name='cat3') + self.f['tag1'] = Tag.objects.create(name='tag1') + self.f['tag2'] = Tag.objects.create(name='tag2') + self.f['tag3'] = Tag.objects.create(name='tag3') + self.f['prop1'] = Property.objects.create(name='prop1') + self.f['prop2'] = Property.objects.create(name='prop2') + self.f['prop3'] = Property.objects.create(name='prop3') def test_domains_anonymous(self): response = anonymous_client.get('/api/domains/') @@ -27,7 +27,7 @@ class CombinedApiTestCase(UserTestCase): self.assertEqual(response.json(), ['example.com']) def test_domains_authenticated(self): - response = client.get('/api/domains/', self.local_user1) + response = client.get('/api/domains/', self.f['local_user1']) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), ['example.com']) @@ -36,7 +36,7 @@ class CombinedApiTestCase(UserTestCase): self.assertEqual(response.status_code, 403) def test_combined_api(self): - response = client.get('/api/info/', self.local_user1) + response = client.get('/api/info/', self.f['local_user1']) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['policies'], ['private', 'friends', 'internal', 'public']) self.assertEqual(response.json()['categories'], ['cat1', 'cat2', 'cat3']) @@ -48,7 +48,6 @@ class CombinedApiTestCase(UserTestCase): self.assertEqual(response.status_code, 403) def test_policy_api(self): - response = client.get('/api/availability_policies/', self.local_user1) + response = client.get('/api/availability_policies/', self.f['local_user1']) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), ['private', 'friends', 'internal', 'public']) - diff --git a/backend/toolshed/tests/test_category.py b/backend/toolshed/tests/test_category.py index 0620c8c..1953aff 100644 --- a/backend/toolshed/tests/test_category.py +++ b/backend/toolshed/tests/test_category.py @@ -1,51 +1,53 @@ -from authentication.tests import UserTestCase, SignatureAuthClient +from authentication.tests import SignatureAuthClient, UserTestMixin, ToolshedTestCase from toolshed.models import Category client = SignatureAuthClient() -class CategoryTestCase(UserTestCase): +class CategoryTestCase(UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() - self.cat1 = Category.objects.create(name='cat1') - self.cat1.save() - self.cat2 = Category.objects.create(name='cat2') - self.cat2.save() - self.cat3 = Category.objects.create(name='cat3') - self.cat3.save() + self.prepare_users() + self.f['cat1'] = Category.objects.create(name='cat1') + self.f['cat1'].save() + self.f['cat2'] = Category.objects.create(name='cat2') + self.f['cat2'].save() + self.f['cat3'] = Category.objects.create(name='cat3') + self.f['cat3'].save() def test_get_categories(self): - subcat1 = Category.objects.create(name='subcat1', parent=self.cat1) + subcat1 = Category.objects.create(name='subcat1', parent=self.f['cat1']) subcat1.save() - subcat2 = Category.objects.create(name='subcat2', parent=self.cat1) + subcat2 = Category.objects.create(name='subcat2', parent=self.f['cat1']) subcat2.save() subcat3 = Category.objects.create(name='subcat3', parent=subcat1) subcat3.save() - self.assertEqual(self.cat1.children.count(), 2) - self.assertEqual(self.cat1.children.first(), subcat1) - self.assertEqual(self.cat1.children.last(), subcat2) - self.assertEqual(subcat1.parent, self.cat1) - self.assertEqual(subcat2.parent, self.cat1) + self.assertEqual(self.f['cat1'].children.count(), 2) + self.assertEqual(self.f['cat1'].children.first(), subcat1) + self.assertEqual(self.f['cat1'].children.last(), subcat2) + self.assertEqual(subcat1.parent, self.f['cat1']) + self.assertEqual(subcat2.parent, self.f['cat1']) self.assertEqual(subcat3.parent, subcat1) self.assertEqual(str(subcat1), 'cat1/subcat1') self.assertEqual(str(subcat2), 'cat1/subcat2') self.assertEqual(str(subcat3), 'cat1/subcat1/subcat3') -class CategoryApiTestCase(UserTestCase): +class CategoryApiTestCase(UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() - self.cat1 = Category.objects.create(name='cat1') - self.cat1.save() - self.cat2 = Category.objects.create(name='cat2') - self.cat2.save() - self.cat3 = Category.objects.create(name='cat3') - self.cat3.save() + self.prepare_users() + self.f['cat1'] = Category.objects.create(name='cat1') + self.f['cat1'].save() + self.f['cat2'] = Category.objects.create(name='cat2') + self.f['cat2'].save() + self.f['cat3'] = Category.objects.create(name='cat3') + self.f['cat3'].save() def test_get_categories(self): - reply = client.get('/api/categories/', self.local_user1) + reply = client.get('/api/categories/', self.f['local_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 3) self.assertEqual(reply.json()[0], 'cat1') diff --git a/backend/toolshed/tests/test_friend.py b/backend/toolshed/tests/test_friend.py index aa0f647..fec6971 100644 --- a/backend/toolshed/tests/test_friend.py +++ b/backend/toolshed/tests/test_friend.py @@ -1,75 +1,77 @@ -from authentication.tests import UserTestCase, SignatureAuthClient +from authentication.tests import SignatureAuthClient, UserTestMixin, ToolshedTestCase client = SignatureAuthClient() -class FriendTestCase(UserTestCase): +class FriendTestCase(UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() + self.prepare_users() def test_friendship_iternal(self): - self.assertEqual(self.local_user1.friends.count(), 0) - self.assertEqual(self.local_user2.friends.count(), 0) - self.assertEqual(self.ext_user1.friends.count(), 0) - self.assertEqual(self.ext_user2.friends.count(), 0) - self.local_user1.friends.add(self.local_user2.public_identity) - self.assertEqual(self.local_user1.friends.count(), 1) - self.assertEqual(self.local_user2.friends.count(), 1) - self.assertEqual(self.ext_user1.friends.count(), 0) - self.assertEqual(self.ext_user2.friends.count(), 0) - self.assertEqual(self.local_user1.friends.first(), self.local_user2.public_identity) - self.assertEqual(self.local_user2.friends.first(), self.local_user1.public_identity) + self.assertEqual(self.f['local_user1'].friends.count(), 0) + self.assertEqual(self.f['local_user2'].friends.count(), 0) + self.assertEqual(self.f['ext_user1'].friends.count(), 0) + self.assertEqual(self.f['ext_user2'].friends.count(), 0) + self.f['local_user1'].friends.add(self.f['local_user2'].public_identity) + self.assertEqual(self.f['local_user1'].friends.count(), 1) + self.assertEqual(self.f['local_user2'].friends.count(), 1) + self.assertEqual(self.f['ext_user1'].friends.count(), 0) + self.assertEqual(self.f['ext_user2'].friends.count(), 0) + self.assertEqual(self.f['local_user1'].friends.first(), self.f['local_user2'].public_identity) + self.assertEqual(self.f['local_user2'].friends.first(), self.f['local_user1'].public_identity) def test_friendship_external(self): - self.assertEqual(self.local_user1.friends.count(), 0) - self.assertEqual(self.local_user2.friends.count(), 0) - self.assertEqual(self.ext_user1.friends.count(), 0) - self.assertEqual(self.ext_user2.friends.count(), 0) - self.local_user1.friends.add(self.ext_user1.public_identity) - self.assertEqual(self.local_user1.friends.count(), 1) - self.assertEqual(self.local_user2.friends.count(), 0) - self.assertEqual(self.ext_user1.friends.count(), 1) - self.assertEqual(self.ext_user2.friends.count(), 0) - self.assertEqual(self.local_user1.friends.first(), self.ext_user1.public_identity) - self.assertEqual(self.ext_user1.friends.first(), self.local_user1.public_identity) + self.assertEqual(self.f['local_user1'].friends.count(), 0) + self.assertEqual(self.f['local_user2'].friends.count(), 0) + self.assertEqual(self.f['ext_user1'].friends.count(), 0) + self.assertEqual(self.f['ext_user2'].friends.count(), 0) + self.f['local_user1'].friends.add(self.f['ext_user1'].public_identity) + self.assertEqual(self.f['local_user1'].friends.count(), 1) + self.assertEqual(self.f['local_user2'].friends.count(), 0) + self.assertEqual(self.f['ext_user1'].friends.count(), 1) + self.assertEqual(self.f['ext_user2'].friends.count(), 0) + self.assertEqual(self.f['local_user1'].friends.first(), self.f['ext_user1'].public_identity) + self.assertEqual(self.f['ext_user1'].friends.first(), self.f['local_user1'].public_identity) def test_friend_from_external(self): - self.assertEqual(self.local_user1.friends.count(), 0) - self.assertEqual(self.local_user2.friends.count(), 0) - self.assertEqual(self.ext_user1.friends.count(), 0) - self.assertEqual(self.ext_user2.friends.count(), 0) - self.ext_user1.friends.add(self.local_user1.public_identity) - self.assertEqual(self.local_user1.friends.count(), 1) - self.assertEqual(self.local_user2.friends.count(), 0) - self.assertEqual(self.ext_user1.friends.count(), 1) - self.assertEqual(self.ext_user2.friends.count(), 0) - self.assertEqual(self.local_user1.friends.first(), self.ext_user1.public_identity) - self.assertEqual(self.ext_user1.friends.first(), self.local_user1.public_identity) + self.assertEqual(self.f['local_user1'].friends.count(), 0) + self.assertEqual(self.f['local_user2'].friends.count(), 0) + self.assertEqual(self.f['ext_user1'].friends.count(), 0) + self.assertEqual(self.f['ext_user2'].friends.count(), 0) + self.f['ext_user1'].friends.add(self.f['local_user1'].public_identity) + self.assertEqual(self.f['local_user1'].friends.count(), 1) + self.assertEqual(self.f['local_user2'].friends.count(), 0) + self.assertEqual(self.f['ext_user1'].friends.count(), 1) + self.assertEqual(self.f['ext_user2'].friends.count(), 0) + self.assertEqual(self.f['local_user1'].friends.first(), self.f['ext_user1'].public_identity) + self.assertEqual(self.f['ext_user1'].friends.first(), self.f['local_user1'].public_identity) -class FriendApiTestCase(UserTestCase): +class FriendApiTestCase(UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() - self.local_user1.friends.add(self.local_user2.public_identity) - self.local_user1.friends.add(self.ext_user1.public_identity) - self.ext_user1.friends.add(self.local_user1.public_identity) + self.prepare_users() + self.f['local_user1'].friends.add(self.f['local_user2'].public_identity) + self.f['local_user1'].friends.add(self.f['ext_user1'].public_identity) + self.f['ext_user1'].friends.add(self.f['local_user1'].public_identity) def test_friend_list_internal1(self): - reply = client.get('/api/friends/', self.local_user1) + reply = client.get('/api/friends/', self.f['local_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 2) - self.assertEqual(reply.json()[0]['username'], str(self.local_user2)) - self.assertEqual(reply.json()[1]['username'], str(self.ext_user1)) + self.assertEqual(reply.json()[0]['username'], str(self.f['local_user2'])) + self.assertEqual(reply.json()[1]['username'], str(self.f['ext_user1'])) def test_friend_list_internal2(self): - reply = client.get('/api/friends/', self.local_user2) + reply = client.get('/api/friends/', self.f['local_user2']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 1) - self.assertEqual(reply.json()[0]['username'], str(self.local_user1)) + self.assertEqual(reply.json()[0]['username'], str(self.f['local_user1'])) def test_friend_list_external(self): - reply = client.get('/api/friends/', self.ext_user1) + reply = client.get('/api/friends/', self.f['ext_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 1) - self.assertEqual(reply.json()[0]['username'], str(self.local_user1)) + self.assertEqual(reply.json()[0]['username'], str(self.f['local_user1'])) diff --git a/backend/toolshed/tests/test_inventory.py b/backend/toolshed/tests/test_inventory.py index 98555e5..e3c775a 100644 --- a/backend/toolshed/tests/test_inventory.py +++ b/backend/toolshed/tests/test_inventory.py @@ -1,47 +1,19 @@ -from authentication.models import ToolshedUser -from authentication.tests import UserTestCase, SignatureAuthClient -from toolshed.models import InventoryItem, Tag, Property, ItemProperty, Category +from authentication.tests import SignatureAuthClient, UserTestMixin, ToolshedTestCase +from toolshed.models import InventoryItem, Category +from toolshed.tests import InventoryTestMixin client = SignatureAuthClient() -class InventoryTestCase(UserTestCase): +class InventoryTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): def setUp(self): super().setUp() - self.user1 = ToolshedUser.objects.get(username="testuser") - self.user2 = ToolshedUser.objects.get(username="testuser2") - self.user1.friends.add(self.user2.public_identity) - self.user2.friends.add(self.user1.public_identity) - self.cat1 = Category.objects.create(name='cat1') - self.cat1.save() - self.cat2 = Category.objects.create(name='cat2') - self.cat2.save() - self.tag1 = Tag.objects.create(name='tag1', category=self.cat1) - self.tag1.save() - self.tag2 = Tag.objects.create(name='tag2', category=self.cat1) - self.tag2.save() - self.tag3 = Tag.objects.create(name='tag3') - self.tag3.save() - self.prop1 = Property.objects.create(name='prop1') - self.prop1.save() - self.prop2 = Property.objects.create(name='prop2') - self.prop2.save() - self.prop3 = Property.objects.create(name='prop3', category=self.cat1) - self.prop3.save() - - InventoryItem.objects.create(owner=self.user1, owned_quantity=1, name='test1', description='test', - category=self.cat1, availability_policy='friends').save() - item2 = InventoryItem.objects.create(owner=self.user1, owned_quantity=1, name='test2', description='test2', - category=self.cat1, availability_policy='friends') - item2.save() - item2.tags.add(self.tag1, through_defaults={}) - item2.tags.add(self.tag2, through_defaults={}) - ItemProperty.objects.create(inventory_item=item2, property=self.prop1, value='value1').save() - ItemProperty.objects.create(inventory_item=item2, property=self.prop2, value='value2').save() + self.prepare_users() + self.prepare_inventory() def test_get_inventory(self): - reply = client.get('/api/inventory_items/', self.user1) + reply = client.get('/api/inventory_items/', self.f['local_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 2) self.assertEqual(reply.json()[0]['name'], 'test1') @@ -61,7 +33,7 @@ class InventoryTestCase(UserTestCase): self.assertEqual(reply.json()[1]['availability_policy'], 'friends') def test_post_new_item(self): - reply = client.post('/api/inventory_items/', self.user1, { + reply = client.post('/api/inventory_items/', self.f['local_user1'], { 'availability_policy': 'friends', 'category': 'cat2', 'name': 'test3', @@ -79,12 +51,12 @@ class InventoryTestCase(UserTestCase): self.assertEqual(item.name, 'test3') self.assertEqual(item.description, 'test') self.assertEqual(item.owned_quantity, 1) - self.assertEqual([t for t in item.tags.all()], [self.tag1, self.tag2]) - self.assertEqual([p for p in item.properties.all()], [self.prop1, self.prop2]) + self.assertEqual([t for t in item.tags.all()], [self.f['tag1'], self.f['tag2']]) + self.assertEqual([p for p in item.properties.all()], [self.f['prop1'], self.f['prop2']]) self.assertEqual([p.value for p in item.itemproperty_set.all()], ['value3', 'value4']) def test_post_new_item2(self): - reply = client.post('/api/inventory_items/', self.user1, { + reply = client.post('/api/inventory_items/', self.f['local_user1'], { 'availability_policy': 'friends', 'name': 'test3', 'description': 'test', @@ -103,7 +75,7 @@ class InventoryTestCase(UserTestCase): self.assertEqual([p for p in item.properties.all()], []) def test_post_new_item3(self): - reply = client.post('/api/inventory_items/', self.user1, { + reply = client.post('/api/inventory_items/', self.f['local_user1'], { 'availability_policy': 'friends', 'name': 'test3', 'description': 'test', @@ -123,7 +95,7 @@ class InventoryTestCase(UserTestCase): self.assertEqual([p for p in item.properties.all()], []) def test_put_item(self): - reply = client.put('/api/inventory_items/1/', self.user1, { + reply = client.put('/api/inventory_items/1/', self.f['local_user1'], { 'availability_policy': 'friends', 'name': 'test4', 'description': 'new description', @@ -141,12 +113,12 @@ class InventoryTestCase(UserTestCase): self.assertEqual(item.name, 'test4') self.assertEqual(item.description, 'new description') self.assertEqual(item.owned_quantity, 100) - self.assertEqual([t for t in item.tags.all()], [self.tag1, self.tag2, self.tag3]) - self.assertEqual([p for p in item.properties.all()], [self.prop1, self.prop2, self.prop3]) + self.assertEqual([t for t in item.tags.all()], [self.f['tag1'], self.f['tag2'], self.f['tag3']]) + self.assertEqual([p for p in item.properties.all()], [self.f['prop1'], self.f['prop2'], self.f['prop3']]) self.assertEqual([p.value for p in item.itemproperty_set.all()], ['value5', 'value6', 'value7']) def test_patch_item(self): - reply = client.patch('/api/inventory_items/1/', self.user1, { + reply = client.patch('/api/inventory_items/1/', self.f['local_user1'], { 'description': 'new description2', 'category': 'cat1', 'owned_quantity': 100, @@ -161,12 +133,12 @@ class InventoryTestCase(UserTestCase): self.assertEqual(item.name, 'test1') self.assertEqual(item.description, 'new description2') self.assertEqual(item.owned_quantity, 100) - self.assertEqual([t for t in item.tags.all()], [self.tag3]) - self.assertEqual([p for p in item.properties.all()], [self.prop3]) + self.assertEqual([t for t in item.tags.all()], [self.f['tag3']]) + self.assertEqual([p for p in item.properties.all()], [self.f['prop3']]) self.assertEqual([p.value for p in item.itemproperty_set.all()], ['value8']) def test_patch_item2(self): - reply = client.patch('/api/inventory_items/1/', self.user1, { + reply = client.patch('/api/inventory_items/1/', self.f['local_user1'], { 'description': 'new description2', 'category': None, 'owned_quantity': 100, @@ -185,14 +157,14 @@ class InventoryTestCase(UserTestCase): self.assertEqual([p for p in item.properties.all()], []) def test_delete_item(self): - reply = client.delete('/api/inventory_items/1/', self.user1) + reply = client.delete('/api/inventory_items/1/', self.f['local_user1']) self.assertEqual(reply.status_code, 204) self.assertEqual(InventoryItem.objects.count(), 1) self.assertEqual(InventoryItem.objects.get(id=2).name, 'test2') self.assertEqual(InventoryItem.objects.filter(name='test1').count(), 0) def test_search_items(self): - reply = client.get('/api/search/?query=test', self.user1) + reply = client.get('/api/search/?query=test', self.f['local_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 2) self.assertEqual(reply.json()[0]['name'], 'test1') @@ -212,18 +184,18 @@ class InventoryTestCase(UserTestCase): self.assertEqual(reply.json()[1]['availability_policy'], 'friends') def test_search_items2(self): - reply = client.get('/api/search/?query=test', self.user2) + reply = client.get('/api/search/?query=test', self.f['local_user2']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 2) self.assertEqual(reply.json()[0]['name'], 'test1') self.assertEqual(reply.json()[1]['name'], 'test2') def test_search_items_fail(self): - reply = client.get('/api/search/', self.user1) + reply = client.get('/api/search/', self.f['local_user1']) self.assertEqual(reply.status_code, 400) self.assertEqual(reply.json()['error'], 'No query provided.') def test_search_items_fail2(self): - reply = client.get('/api/search/?query=test', self.ext_user1) + reply = client.get('/api/search/?query=test', self.f['ext_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 0) diff --git a/backend/toolshed/tests/test_property.py b/backend/toolshed/tests/test_property.py index 89fef4f..4e0d748 100644 --- a/backend/toolshed/tests/test_property.py +++ b/backend/toolshed/tests/test_property.py @@ -1,54 +1,56 @@ -from authentication.tests import UserTestCase, SignatureAuthClient +from authentication.tests import SignatureAuthClient, UserTestMixin, ToolshedTestCase from toolshed.models import Property, Category client = SignatureAuthClient() -class PropertyTestCase(UserTestCase): +class PropertyTestCase(UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() - self.cat1 = Category.objects.create(name='cat1') - self.cat1.save() - self.prop1 = Property.objects.create(name='prop1') - self.prop1.save() - self.prop2 = Property.objects.create(name='prop2', description='prop2 description', category=self.cat1) - self.prop2.save() - self.prop3 = Property.objects.create(name='prop3', description='prop3 description', category=self.cat1) - self.prop3.save() + self.prepare_users() + self.f['cat1'] = Category.objects.create(name='cat1') + self.f['cat1'].save() + self.f['prop1'] = Property.objects.create(name='prop1') + self.f['prop1'].save() + self.f['prop2'] = Property.objects.create(name='prop2', description='prop2 description', category=self.f['cat1']) + self.f['prop2'].save() + self.f['prop3'] = Property.objects.create(name='prop3', description='prop3 description', category=self.f['cat1']) + self.f['prop3'].save() def test_properties(self): self.assertEqual(len(Property.objects.all()), 3) - self.assertEqual(self.prop1.name, 'prop1') - self.assertEqual(self.prop1.description, None) - self.assertEqual(self.prop1.category, None) - self.assertEqual(self.prop2.name, 'prop2') - self.assertEqual(self.prop2.description, 'prop2 description') - self.assertEqual(self.prop2.category, self.cat1) - self.assertEqual(self.prop3.name, 'prop3') - self.assertEqual(self.prop3.description, 'prop3 description') - self.assertEqual(self.prop3.category, self.cat1) - self.assertEqual(str(self.prop1), 'prop1') - self.assertEqual(str(self.prop2), 'prop2') - self.assertEqual(str(self.prop3), 'prop3') - self.assertEqual(self.cat1.properties.count(), 2) - self.assertEqual(self.cat1.properties.first(), self.prop2) - self.assertEqual(self.cat1.properties.last(), self.prop3) + self.assertEqual(self.f['prop1'].name, 'prop1') + self.assertEqual(self.f['prop1'].description, None) + self.assertEqual(self.f['prop1'].category, None) + self.assertEqual(self.f['prop2'].name, 'prop2') + self.assertEqual(self.f['prop2'].description, 'prop2 description') + self.assertEqual(self.f['prop2'].category, self.f['cat1']) + self.assertEqual(self.f['prop3'].name, 'prop3') + self.assertEqual(self.f['prop3'].description, 'prop3 description') + self.assertEqual(self.f['prop3'].category, self.f['cat1']) + self.assertEqual(str(self.f['prop1']), 'prop1') + self.assertEqual(str(self.f['prop2']), 'prop2') + self.assertEqual(str(self.f['prop3']), 'prop3') + self.assertEqual(self.f['cat1'].properties.count(), 2) + self.assertEqual(self.f['cat1'].properties.first(), self.f['prop2']) + self.assertEqual(self.f['cat1'].properties.last(), self.f['prop3']) -class PropertyApiTestCase(UserTestCase): +class PropertyApiTestCase(UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() - self.cat1 = Category.objects.create(name='cat1') - self.cat1.save() - self.cat2 = Category.objects.create(name='cat2') - self.cat2.save() - self.cat3 = Category.objects.create(name='cat3') - self.cat3.save() + self.prepare_users() + self.f['cat1'] = Category.objects.create(name='cat1') + self.f['cat1'].save() + self.f['cat2'] = Category.objects.create(name='cat2') + self.f['cat2'].save() + self.f['cat3'] = Category.objects.create(name='cat3') + self.f['cat3'].save() def test_get_properties(self): - reply = client.get('/api/properties/', self.local_user1) + reply = client.get('/api/properties/', self.f['local_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 0) prop1 = Property.objects.create(name='prop1') diff --git a/backend/toolshed/tests/test_tag.py b/backend/toolshed/tests/test_tag.py index d026a25..e0ccfeb 100644 --- a/backend/toolshed/tests/test_tag.py +++ b/backend/toolshed/tests/test_tag.py @@ -1,56 +1,58 @@ -from authentication.tests import UserTestCase, SignatureAuthClient +from authentication.tests import SignatureAuthClient, UserTestMixin, ToolshedTestCase from toolshed.models import Tag, Category client = SignatureAuthClient() -class TagTestCase(UserTestCase): +class TagTestCase(UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() - self.cat1 = Category.objects.create(name='cat1') - self.cat1.save() - self.tag1 = Tag.objects.create(name='tag1', description='tag1 description', category=self.cat1) - self.tag1.save() - self.tag2 = Tag.objects.create(name='tag2', description='tag2 description', category=self.cat1) - self.tag2.save() - self.tag3 = Tag.objects.create(name='tag3') - self.tag3.save() + self.prepare_users() + self.f['cat1'] = Category.objects.create(name='cat1') + self.f['cat1'].save() + self.f['tag1'] = Tag.objects.create(name='tag1', description='tag1 description', category=self.f['cat1']) + self.f['tag1'].save() + self.f['tag2'] = Tag.objects.create(name='tag2', description='tag2 description', category=self.f['cat1']) + self.f['tag2'].save() + self.f['tag3'] = Tag.objects.create(name='tag3') + self.f['tag3'].save() def test_tags(self): self.assertEqual(len(Tag.objects.all()), 3) - self.assertEqual(self.tag1.name, 'tag1') - self.assertEqual(self.tag1.description, 'tag1 description') - self.assertEqual(self.tag1.category, self.cat1) - self.assertEqual(self.tag2.name, 'tag2') - self.assertEqual(self.tag2.description, 'tag2 description') - self.assertEqual(self.tag2.category, self.cat1) - self.assertEqual(self.tag3.name, 'tag3') - self.assertEqual(self.tag3.description, None) - self.assertEqual(self.tag3.category, None) - self.assertEqual(str(self.tag1), 'tag1') - self.assertEqual(str(self.tag2), 'tag2') - self.assertEqual(str(self.tag3), 'tag3') - self.assertEqual(self.cat1.tags.count(), 2) - self.assertEqual(self.cat1.tags.first(), self.tag1) - self.assertEqual(self.cat1.tags.last(), self.tag2) + self.assertEqual(self.f['tag1'].name, 'tag1') + self.assertEqual(self.f['tag1'].description, 'tag1 description') + self.assertEqual(self.f['tag1'].category, self.f['cat1']) + self.assertEqual(self.f['tag2'].name, 'tag2') + self.assertEqual(self.f['tag2'].description, 'tag2 description') + self.assertEqual(self.f['tag2'].category, self.f['cat1']) + self.assertEqual(self.f['tag3'].name, 'tag3') + self.assertEqual(self.f['tag3'].description, None) + self.assertEqual(self.f['tag3'].category, None) + self.assertEqual(str(self.f['tag1']), 'tag1') + self.assertEqual(str(self.f['tag2']), 'tag2') + self.assertEqual(str(self.f['tag3']), 'tag3') + self.assertEqual(self.f['cat1'].tags.count(), 2) + self.assertEqual(self.f['cat1'].tags.first(), self.f['tag1']) + self.assertEqual(self.f['cat1'].tags.last(), self.f['tag2']) -class TagApiTestCase(UserTestCase): +class TagApiTestCase(UserTestMixin, ToolshedTestCase): def setUp(self): super().setUp() - self.cat1 = Category.objects.create(name='cat1') - self.cat1.save() - self.tag1 = Tag.objects.create(name='tag1', description='tag1 description', category=self.cat1) - self.tag1.save() - self.tag2 = Tag.objects.create(name='tag2', description='tag2 description', category=self.cat1) - self.tag2.save() - self.tag3 = Tag.objects.create(name='tag3') - self.tag3.save() + self.prepare_users() + self.f['cat1'] = Category.objects.create(name='cat1') + self.f['cat1'].save() + self.f['tag1'] = Tag.objects.create(name='tag1', description='tag1 description', category=self.f['cat1']) + self.f['tag1'].save() + self.f['tag2'] = Tag.objects.create(name='tag2', description='tag2 description', category=self.f['cat1']) + self.f['tag2'].save() + self.f['tag3'] = Tag.objects.create(name='tag3') + self.f['tag3'].save() def test_get_tags(self): - reply = client.get('/api/tags/', self.local_user1) + reply = client.get('/api/tags/', self.f['local_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 3) self.assertEqual(reply.json()[0], 'tag1')