from django.contrib.auth.models import User from rest_framework import status from rest_framework.test import APITestCase, APIRequestFactory from webapp.models import RemoveJobAd from webapp.serializers import JobAdSerializer API = "/api/jobads/" class JobAdTestCase(APITestCase): def setUp(self): self.prefilled_jobad = RemoveJobAd.objects.create( title_fi="ABB Test", title_en="ABB Test", visible=True, description_fi="desc", description_en="desc", content_fi="lorem", content_en="lorem", ) username, password = "test_admin", "password123" self.authClient = User.objects.create_superuser( username, "myemail@test.com", password ) def test_get_jobads(self): response = self.client.get(API, format="json") expected = JobAdSerializer(self.prefilled_jobad).data self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data["results"][0], expected) def test_post_jobad(self): data = { "title_fi": "testtitle", "title_en": "testtitle", "visible": "True", "description_fi": "liirumlaarum", "description_en": "liirumlaarum", "content_fi": "lorem ipsum", "content_en": "lorem ipsum", "autohide_enabled": "True", } # Try post without authentication response = self.client.post(API, data, format="json") self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) self.assertEqual(RemoveJobAd.objects.count(), 1) # Authenticate self.client.force_authenticate(user=self.authClient) response = self.client.post(API, data, format="json") # Return success and check object was created self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(RemoveJobAd.objects.count(), 2)