test fixes for track_user view
This commit is contained in:
145
stats/tests.py
145
stats/tests.py
@@ -16,7 +16,7 @@ from .factories import StatsLogFactory
|
|||||||
|
|
||||||
# Create your tests here.
|
# Create your tests here.
|
||||||
class TrackUserViewTest(APITestCase):
|
class TrackUserViewTest(APITestCase):
|
||||||
"""ProductViewSet tests
|
"""track_user view method tests
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@@ -88,90 +88,109 @@ class TrackUserViewTest(APITestCase):
|
|||||||
"""Not logged-in user cannot modify existing instance
|
"""Not logged-in user cannot modify existing instance
|
||||||
"""
|
"""
|
||||||
# Create instance
|
# Create instance
|
||||||
instance = self.factory()
|
company = CompanyFactory()
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'action': 'VIEW',
|
||||||
|
'action_object': {
|
||||||
|
'model': 'company',
|
||||||
|
'id': company.id,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
# Query endpoint
|
# Query endpoint
|
||||||
url = self.endpoint + f'{instance.pk}/'
|
response = self.client.post(self.endpoint, data=data, format='json')
|
||||||
response = self.client.put(url, {}, format='json')
|
|
||||||
|
|
||||||
# Assert forbidden code
|
# Assert forbidden code
|
||||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||||
|
|
||||||
# authenticated user
|
# authenticated user
|
||||||
def test_auth_user_can_only_post(self):
|
def test_auth_user_can_only_post(self):
|
||||||
"""Regular logged-in user can list instance
|
"""Regular logged-in user can list instance
|
||||||
"""
|
"""
|
||||||
# Create instances
|
|
||||||
instances = [self.factory() for n in range(random.randint(1,5))]
|
|
||||||
|
|
||||||
# Authenticate user
|
# Authenticate user
|
||||||
token = get_tokens_for_user(self.user)
|
token = get_tokens_for_user(self.user)
|
||||||
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
|
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
|
||||||
|
|
||||||
# Request list
|
# Create instance
|
||||||
response = self.client.get(self.endpoint)
|
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 = {
|
data = {
|
||||||
'company': company.id,
|
'action': 'VIEW',
|
||||||
'sku': 'qwerewq',
|
'action_object': {
|
||||||
'name': 'qwerewq',
|
'model': 'product',
|
||||||
'description': 'qwerewq',
|
'id': product.id,
|
||||||
'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',
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Authenticate user
|
# Query endpoint
|
||||||
token = get_tokens_for_user(self.user)
|
response = self.client.get(self.endpoint)
|
||||||
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
|
# 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
|
# Query endpoint
|
||||||
response = self.client.post(self.endpoint, data=data, format='json')
|
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)
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||||
|
|
||||||
# Assert instance exists on db
|
def test_auth_user_can_register_product_action(self):
|
||||||
self.assertTrue(self.model.objects.get(id=response.data['id']))
|
"""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):
|
class StatsLogViewSetTest(APITestCase):
|
||||||
|
|||||||
Reference in New Issue
Block a user