Fakultas Ilmu Komputer UI

Commit a660b8bd authored by Steven Kusuman's avatar Steven Kusuman
Browse files

[112] Bugfix: Login

parent 1c36659c
......@@ -1066,7 +1066,7 @@ class GenerateDummyCommandTest(TestCase):
for num_of_materi in self.material_numbers:
call_command("generatedummy", num_of_materi, stdout=self.stdout)
self.assertIn(
f"Successfully created {num_of_materi} materi\n",
f"Successfully created {num_of_materi} materi",
self.stdout.getvalue()
)
......@@ -1090,7 +1090,7 @@ class RemoveDummyCommandTest(TestCase):
call_command("removedummy", stdout=stdout)
self.assertEqual("Successfully remove all dummy object\n", stdout.getvalue())
self.assertIn("Successfully remove all dummy object", stdout.getvalue())
self.assertEqual(User.objects.count(), 0)
self.assertEqual(Category.objects.count(), 0)
self.assertEqual(Materi.objects.count(), 0)
......
......@@ -74,7 +74,7 @@ class UserModelTest(TestCase):
class LoginPageContributorTest(TestCase):
def setUp(self):
self.client = Client()
self.admin = User.objects.create_contributor(email="kontributor@gov.id",
self.kontributor = User.objects.create_contributor(email="kontributor@gov.id",
password="kontributor")
self.url = "/login/"
self.view = Login
......@@ -86,34 +86,34 @@ class LoginPageContributorTest(TestCase):
"wrong_email_or_password": "Email atau Password anda salah.",
}
def test_login_admin_view(self):
def test_login_kontributor_view(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.as_view().__name__)
def test_login_admin_template(self):
def test_login_kontributor_template(self):
# Test
response = self.client.get(self.url)
self.assertTemplateUsed(response, self.template_name)
def test_login_admin_url(self):
def test_login_kontributor_url(self):
# Test
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
def test_login_admin_title(self):
def test_login_kontributor_title(self):
response = self.client.get(self.url)
# Positive tests
self.assertContains(response, "Halo, kontributor")
def test_login_admin_form_field(self):
def test_login_kontributor_form_field(self):
response = self.client.get(self.url)
# Positive tests
self.assertContains(response, "Email")
self.assertContains(response, "Kata Sandi")
def test_admin_login_missing_email_or_password(self):
def test_kontributor_login_missing_email_or_password(self):
response = self.client.post(self.url, {"email": "kontributor@gov.id"})
self.assertIn("error_message", response.context_data)
self.assertIn(self.error_message["empty_email_or_password"],
......@@ -123,7 +123,7 @@ class LoginPageContributorTest(TestCase):
self.assertIn(self.error_message["empty_email_or_password"],
response.context_data["error_message"])
def test_admin_login_wrong_email_or_password(self):
def test_kontributor_login_wrong_email_or_password(self):
# Wrong password
response = self.client.post(
self.url, {"email": "kontributor@gov.id", "pass": "kontributor1"})
......@@ -143,7 +143,7 @@ class LoginPageContributorTest(TestCase):
self.assertIn(self.error_message["wrong_email_or_password"],
response.context_data["error_message"])
def test_admin_login(self):
def test_kontributor_login(self):
# 302 meaning successful login and redirected
expected_redirect_url = "/sukses-kontributor/"
response = self.client.post(
......@@ -151,6 +151,19 @@ class LoginPageContributorTest(TestCase):
self.assertEqual(302, response.status_code)
self.assertEqual(response.url, expected_redirect_url)
def test_kontributor_visit_login_after_auth(self):
# 302 meaning successful login and redirected
expected_redirect_url = "/sukses-kontributor/"
response = self.client.post(
self.url, self.login_credential)
self.assertEqual(302, response.status_code)
self.assertEqual(response.url, expected_redirect_url)
response = self.client.get(
self.url
)
self.assertEqual(302, response.status_code)
self.assertEqual(response.url, expected_redirect_url)
class LoginPageAdminTest(TestCase):
def setUp(self):
......@@ -230,3 +243,17 @@ class LoginPageAdminTest(TestCase):
self.url, self.login_credential)
self.assertEqual(302, response.status_code)
self.assertEqual(response.url, expected_redirect_url)
def test_admin_visit_login_after_auth(self):
# 302 meaning successful login and redirected
expected_redirect_url = "/sukses-admin/"
response = self.client.post(
self.url, self.login_credential)
self.assertEqual(302, response.status_code)
self.assertEqual(response.url, expected_redirect_url)
response = self.client.get(
self.url
)
self.assertEqual(302, response.status_code)
self.assertEqual(response.url, expected_redirect_url)
......@@ -4,6 +4,16 @@ from django.views.generic import TemplateView
class Login(TemplateView):
def dispatch(self, request, *args, **kwargs):
if request.user.is_authenticated:
if request.user.is_admin:
redirect_to = "/sukses-admin/"
elif request.user.is_contributor:
redirect_to = "/sukses-kontributor/"
return HttpResponseRedirect(redirect_to)
return super(Login, self).dispatch(request, *args, **kwargs)
def get_template_names(self):
if self.request.path == "/login_admin/":
template_name = "login_admin.html"
......
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