import json, pytz from datetime import datetime from rest_framework.test import APITestCase from account.models import User from rest_framework import status from http.cookies import SimpleCookie from collection.models import Collection, Guide from location.models import Country from establishment.models import Establishment, EstablishmentType # Create your tests here. class BaseTestCase(APITestCase): def setUp(self): self.username = 'sedragurda' self.password = 'sedragurdaredips19' self.email = 'sedragurda@desoz.com' self.newsletter = True self.user = User.objects.create_user( username=self.username, email=self.email, password=self.password) # get tokens tokens = User.create_jwt_tokens(self.user) self.client.cookies = SimpleCookie( {'access_token': tokens.get('access_token'), 'refresh_token': tokens.get('refresh_token'), 'country_code': 'en'}) class CollectionListTests(BaseTestCase): def test_collection_list_Read(self): response = self.client.get('/api/web/collections/', format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) class CollectionDetailTests(BaseTestCase): def setUp(self): super().setUp() country = Country.objects.first() if not country: country = Country.objects.create( name=json.dumps({"en-GB": "Test country"}), code="en" ) self.collection = Collection.objects.create( name='Test collection', is_publish=True, start=datetime.now(pytz.utc), end=datetime.now(pytz.utc), country=country, slug='test-collection-slug', ) def test_collection_detail_Read(self): response = self.client.get(f'/api/web/collections/{self.collection.slug}/establishments/?country_code=en', format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) class CollectionGuideTests(CollectionDetailTests): def test_guide_list_Read(self): response = self.client.get('/api/web/collections/guides/', format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) class CollectionGuideDetailTests(CollectionDetailTests): def setUp(self): super().setUp() self.guide = Guide.objects.create( collection=self.collection, start=datetime.now(pytz.utc), end=datetime.now(pytz.utc) ) def test_guide_detail_Read(self): response = self.client.get(f'/api/web/collections/guides/{self.guide.id}/', format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) class CollectionWebHomeTests(CollectionDetailTests): def setUp(self): super().setUp() self.establishment_type = EstablishmentType.objects.create(name="Test establishment type") for i in range(1, 5): setattr(self, f"establishment{i}", Establishment.objects.create( name=f"Test establishment {i}", establishment_type_id=self.establishment_type.id, is_publish=True, slug=f"test-establishment-{i}" ) ) getattr(self, f"establishment{i}").collections.add(self.collection) def test_collection_list_filter(self): response = self.client.get('/api/web/collections/?country_code=en', format='json') data = response.json() self.assertIn('count', data) self.assertGreater(data['count'], 0)