From 3b487c7ce62b6a65f1f8910188ca0bca31c3f01b Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 3 Mar 2021 12:34:51 +0000 Subject: [PATCH] test fixes for track_user view --- stats/tests.py | 145 ++++++++++++++++++++++++++++--------------------- 1 file changed, 82 insertions(+), 63 deletions(-) diff --git a/stats/tests.py b/stats/tests.py index 5af4e78..bd78892 100644 --- a/stats/tests.py +++ b/stats/tests.py @@ -16,7 +16,7 @@ from .factories import StatsLogFactory # Create your tests here. class TrackUserViewTest(APITestCase): - """ProductViewSet tests + """track_user view method tests """ def setUp(self): @@ -88,90 +88,109 @@ class TrackUserViewTest(APITestCase): """Not logged-in user cannot modify existing instance """ # Create instance - instance = self.factory() + company = CompanyFactory() + + data = { + 'action': 'VIEW', + 'action_object': { + 'model': 'company', + 'id': company.id, + }, + } # Query endpoint - url = self.endpoint + f'{instance.pk}/' - response = self.client.put(url, {}, format='json') + response = self.client.post(self.endpoint, data=data, format='json') # Assert forbidden code - self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) # authenticated user def test_auth_user_can_only_post(self): """Regular logged-in user can list instance """ - # Create instances - instances = [self.factory() for n in range(random.randint(1,5))] - # Authenticate user token = get_tokens_for_user(self.user) self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}") - # Request list - response = self.client.get(self.endpoint) + # Create instance + product = ProductFactory() - # Assert access is allowed - self.assertEqual(response.status_code, status.HTTP_200_OK) - - # Assert all instances are returned - self.assertEqual(len(instances), len(response.data)) - - def test_auth_user_can_register_product_action(self): - """Regular logged-in user can list instance - """ - # Create instances - instance = self.factory() - - # Authenticate user - token = get_tokens_for_user(self.user) - self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}") - - # Request list - url = f"{self.endpoint}{instance.id}/" - response = self.client.get(url) - - # Assert access is allowed - self.assertEqual(response.status_code, status.HTTP_200_OK) - data = json.loads(response.content) - self.assertEquals(instance.id, data['id']) - - def test_auth_user_can_register_company_action(self): - """Regular logged-in user can create new instance - """ - # Define request data - company = CompanyFactory() data = { - 'company': company.id, - 'sku': 'qwerewq', - 'name': 'qwerewq', - 'description': 'qwerewq', - 'url': 'http://qwerewq.com', - 'price': '12.21', - 'shipping_cost': '21.12', - 'shipping_terms': 'qwerewq', - 'source': 'SYNCHRONIZED', - 'sourcing_date': datetime.datetime.now().isoformat()+'Z', - 'update_date': datetime.datetime.now().isoformat()+'Z', - 'discount': '0.05', - 'stock': 22, - 'tags': ['tag1, tag2'], - 'category': 'Mr', - 'attributes': ['color/red', 'size/xxl'], - 'identifiers': '34rf34f43c43', + 'action': 'VIEW', + 'action_object': { + 'model': 'product', + 'id': product.id, + }, } - # Authenticate user - token = get_tokens_for_user(self.user) - self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}") + # Query endpoint + response = self.client.get(self.endpoint) + # Assert access is forbidden + self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) + + # Query endpoint + response = self.client.put(self.endpoint, data={}) + # Assert access is forbidden + self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) + + # Query endpoint + response = self.client.delete(self.endpoint, data={}) + # Assert access is forbidden + self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) # Query endpoint response = self.client.post(self.endpoint, data=data, format='json') - # Assert endpoint returns created status + # Assert access is forbidden self.assertEqual(response.status_code, status.HTTP_201_CREATED) - # Assert instance exists on db - self.assertTrue(self.model.objects.get(id=response.data['id'])) + def test_auth_user_can_register_product_action(self): + """Regular logged-in user can register stat + """ + + # Authenticate user + token = get_tokens_for_user(self.user) + self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}") + + # Create instance + product = ProductFactory() + + data = { + 'action': 'VIEW', + 'action_object': { + 'model': 'product', + 'id': product.id, + }, + } + + # Query endpoint + response = self.client.post(self.endpoint, data=data, format='json') + + # Assert forbidden code + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + + def test_auth_user_can_register_company_action(self): + """Regular logged-in user can register company action + """ + + # Authenticate user + token = get_tokens_for_user(self.user) + self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}") + + # Create instance + company = CompanyFactory() + data = { + 'action': 'VIEW', + 'action_object': { + 'model': 'company', + 'id': company.id, + }, + } + + # Query endpoint + response = self.client.post(self.endpoint, data=data, format='json') + + # Assert forbidden code + self.assertEqual(response.status_code, status.HTTP_201_CREATED) class StatsLogViewSetTest(APITestCase):