Fakultas Ilmu Komputer UI

Commit 5fe1f20c authored by Jonathan Christopher Jakub's avatar Jonathan Christopher Jakub
Browse files

[REFACTOR] Refactor tests to reuse same assertion series

parent 220a4c5e
Pipeline #47734 passed with stages
in 3 minutes and 22 seconds
...@@ -5,6 +5,7 @@ from django.contrib.auth.models import User ...@@ -5,6 +5,7 @@ from django.contrib.auth.models import User
class CustomAuthTest(APITestCase): class CustomAuthTest(APITestCase):
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
cls.URL = "/auth/token/" cls.URL = "/auth/token/"
...@@ -14,35 +15,38 @@ class CustomAuthTest(APITestCase): ...@@ -14,35 +15,38 @@ class CustomAuthTest(APITestCase):
user.save() user.save()
cls.user = user cls.user = user
def _test_success(self, data):
response = self.client.post(path=self.URL, data=data, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertTrue(Token.objects.filter(user=self.user).exists())
def _test_fails(self, data):
response = self.client.post(path=self.URL, data=data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_login_success_with_case_insensitive_username(self): def test_login_success_with_case_insensitive_username(self):
data = { data = {
"username": "JoNaThAn", "username": "JoNaThAn",
"password": "justpass", "password": "justpass",
} }
response = self.client.post(path=self.URL, data=data, format="json") self._test_success(data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertTrue(Token.objects.filter(user=self.user).exists())
def test_login_success_with_exact_username(self): def test_login_success_with_exact_username(self):
data = { data = {
"username": "jonathan", "username": "jonathan",
"password": "justpass", "password": "justpass",
} }
response = self.client.post(path=self.URL, data=data, format="json") self._test_success(data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertTrue(Token.objects.filter(user=self.user).exists())
def test_login_fails_with_incomplete_credentials(self): def test_login_fails_with_incomplete_credentials(self):
data = { data = {
"username": "JoNaThAn", "username": "JoNaThAn",
} }
response = self.client.post(path=self.URL, data=data, format="json") self._test_fails(data)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_login_fails_with_wrong_credentials(self): def test_login_fails_with_wrong_credentials(self):
data = { data = {
"username": "jonathanjojo", "username": "jonathanjojo",
"password": "cantpass", "password": "cantpass",
} }
response = self.client.post(path=self.URL, data=data, format="json") self._test_fails(data)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment