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
class CustomAuthTest(APITestCase):
@classmethod
def setUpTestData(cls):
cls.URL = "/auth/token/"
......@@ -14,35 +15,38 @@ class CustomAuthTest(APITestCase):
user.save()
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):
data = {
"username": "JoNaThAn",
"password": "justpass",
}
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())
self._test_success(data)
def test_login_success_with_exact_username(self):
data = {
"username": "jonathan",
"password": "justpass",
}
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())
self._test_success(data)
def test_login_fails_with_incomplete_credentials(self):
data = {
"username": "JoNaThAn",
}
response = self.client.post(path=self.URL, data=data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self._test_fails(data)
def test_login_fails_with_wrong_credentials(self):
data = {
"username": "jonathanjojo",
"password": "cantpass",
}
response = self.client.post(path=self.URL, data=data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self._test_fails(data)
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