From 9d3faf2b4cddf45f2f552ceaee9a5dd7e980cf0b Mon Sep 17 00:00:00 2001 From: mohfaisal25 <moh.faisal@ui.ac.id> Date: Fri, 9 Oct 2020 13:40:48 +0700 Subject: [PATCH 1/6] [RED] Add new test for register kontributor --- register/tests.py | 74 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 4 deletions(-) diff --git a/register/tests.py b/register/tests.py index 86be11e..813289d 100644 --- a/register/tests.py +++ b/register/tests.py @@ -60,7 +60,7 @@ class RegisterPageTest(TestCase): { "name": "bob", "instansi": "university", - "nik": "1706074940", + "nik": "3201234567890001", "alamat": "bekasi", "email": "bob@company.com", "nomor_telpon": "087878726602", @@ -76,7 +76,7 @@ class RegisterPageTest(TestCase): { "name": "bob", "instansi": "university", - "nik": "1706074940", + "nik": "3201234567890001", "alamat": "bekasi", "email": "bob@company.com", "nomor_telpon": "087878726602", @@ -93,7 +93,7 @@ class RegisterPageTest(TestCase): { "name": "bob", "instansi": "university", - "nik": "1706074940", + "nik": "3201234567890001", "alamat": "bekasi", "email": "bob@company.com", "nomor_telpon": "087878726602", @@ -107,7 +107,7 @@ class RegisterPageTest(TestCase): { "name": "bob", "instansi": "university", - "nik": "1706074940", + "nik": "3201234567890001", "alamat": "bekasi", "email": "bob@company.com", "nomor_telpon": "087878726602", @@ -119,6 +119,72 @@ class RegisterPageTest(TestCase): self.assertIn( b"Email sudah digunakan untuk mendaftar akun.", response.content) + def test_create_user_with_existing_nik(self): + response = self.client.post( + "/registrasi/", + { + "name": "bob", + "instansi": "university", + "nik": "3201234567890001", + "alamat": "bekasi", + "email": "bob@company.com", + "nomor_telpon": "087878726601", + "password": "123456", + "password2": "123456", + }, + ) + self.assertEqual(User.objects.all().count(), 1) + + response = self.client.post( + "/registrasi/", + { + "name": "budi", + "instansi": "university", + "nik": "3201234567890001", + "alamat": "bekasi", + "email": "budi@company.com", + "nomor_telpon": "087878726602", + "password": "123456", + "password2": "123456", + }, + ) + self.assertEqual(User.objects.all().count(), 1) + self.assertIn( + b"NIK sudah digunakan untuk mendaftar akun", response.content) + + def test_create_user_with_existing_nomor_telpon(self): + response = self.client.post( + "/registrasi/", + { + "name": "bob", + "instansi": "university", + "nik": "3201234567890001", + "alamat": "bekasi", + "email": "bob@company.com", + "nomor_telpon": "087878726602", + "password": "123456", + "password2": "123456", + }, + ) + self.assertEqual(User.objects.all().count(), 1) + + response = self.client.post( + "/registrasi/", + { + "name": "budi", + "instansi": "university", + "nik": "3201234567890002", + "alamat": "bekasi", + "email": "budi@company.com", + "nomor_telpon": "087878726602", + "password": "123456", + "password2": "123456", + }, + ) + self.assertEqual(User.objects.all().count(), 1) + self.assertIn( + b"Nomor telepon sudah digunakan untuk mendaftar akun", response.content) + class RegisterAdminTest(TestCase): def setUp(self): self.client = Client() -- GitLab From 72c7b096bfd6a307ce7012d061119adf43e178d5 Mon Sep 17 00:00:00 2001 From: mohfaisal25 <moh.faisal@ui.ac.id> Date: Fri, 9 Oct 2020 14:08:17 +0700 Subject: [PATCH 2/6] [RED] Modify tests for register admin --- register/tests.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/register/tests.py b/register/tests.py index 813289d..854527a 100644 --- a/register/tests.py +++ b/register/tests.py @@ -239,7 +239,7 @@ class RegisterAdminTest(TestCase): { "name": "bob", "instansi": "university", - "nik": "1706074940", + "nik": "3201234567890002", "alamat": "bekasi", "email": "bob@company.com", "nomor_telpon": "087878726602", @@ -255,7 +255,7 @@ class RegisterAdminTest(TestCase): { "name": "bob", "instansi": "university", - "nik": "1706074940", + "nik": "3201234567890002", "alamat": "bekasi", "email": "bob@company.com", "nomor_telpon": "087878726602", @@ -272,7 +272,7 @@ class RegisterAdminTest(TestCase): { "name": "bob", "instansi": "university", - "nik": "1706074940", + "nik": "3201234567890001", "alamat": "bekasi", "email": "bob@company.com", "nomor_telpon": "087878726602", @@ -286,7 +286,7 @@ class RegisterAdminTest(TestCase): { "name": "bob", "instansi": "university", - "nik": "1706074940", + "nik": "3201234567890002", "alamat": "bekasi", "email": "bob@company.com", "nomor_telpon": "087878726602", @@ -304,7 +304,7 @@ class RegisterAdminTest(TestCase): { "name": "bob", "instansi": "university", - "nik": "1706074940", + "nik": "3201234567890002", "alamat": "bekasi", "email": "bob@company.com", "nomor_telpon": "087878726602", @@ -320,7 +320,7 @@ class RegisterAdminTest(TestCase): { "name": "bob", "instansi": "university", - "nik": "1706074940", + "nik": "3201234567890002", "alamat": "bekasi", "email": "bob@company.com", "nomor_telpon": "087878726602", -- GitLab From 6cdd7360f51c6cb3580b2138f4941418f4422ac3 Mon Sep 17 00:00:00 2001 From: mohfaisal25 <moh.faisal@ui.ac.id> Date: Fri, 9 Oct 2020 14:25:17 +0700 Subject: [PATCH 3/6] [GREEN] Implement form validation for 'nik' and 'nomor_telpon' --- .../migrations/0007_auto_20201009_1415.py | 18 +++++++++++++++ authentication/models.py | 2 +- register/forms.py | 23 +++++++++++++++++++ register/templates/index.html | 2 ++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 authentication/migrations/0007_auto_20201009_1415.py diff --git a/authentication/migrations/0007_auto_20201009_1415.py b/authentication/migrations/0007_auto_20201009_1415.py new file mode 100644 index 0000000..012a5c8 --- /dev/null +++ b/authentication/migrations/0007_auto_20201009_1415.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1 on 2020-10-09 07:15 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('authentication', '0006_auto_20200929_2125'), + ] + + operations = [ + migrations.AlterField( + model_name='user', + name='nik', + field=models.CharField(max_length=16), + ), + ] diff --git a/authentication/models.py b/authentication/models.py index 9822fbc..e5161ab 100644 --- a/authentication/models.py +++ b/authentication/models.py @@ -70,7 +70,7 @@ class User(AbstractUser): is_admin = models.BooleanField(blank=False, default=False) is_contributor = models.BooleanField(blank=False, default=False) instansi = models.CharField(max_length=240) - nik = models.CharField(max_length=240) + nik = models.CharField(max_length=16) alamat = models.CharField(max_length=240) nomor_telpon = models.CharField(max_length=240) linkedin = models.URLField(max_length=200, blank=True, default="") diff --git a/register/forms.py b/register/forms.py index 739a77b..691ccf9 100644 --- a/register/forms.py +++ b/register/forms.py @@ -1,4 +1,6 @@ from django import forms +from django.core.validators import RegexValidator +from django.core import validators from authentication.models import User @@ -17,6 +19,10 @@ class UserForm(forms.ModelForm): field.widget.attrs["class"] = "input100" if field_name == "password1" or field_name == "password2": field.widget.attrs["type"] = "password" + if field_name == "nik": + field.validators = [RegexValidator(regex='^\d{16}$', + message='NIK harus berupa angka 16 digit', + code='nomatch')] self.fields["name"].required = True self.fields["password"].required = True @@ -35,3 +41,20 @@ class UserForm(forms.ModelForm): return email raise forms.ValidationError( "Email sudah digunakan untuk mendaftar akun.") + + def clean_nomor_telpon(self): + nomor_telpon = self.data.get("nomor_telpon") + if not User.objects.filter(nomor_telpon=nomor_telpon).exists(): + try: + no_telp = int(nomor_telpon) + return nomor_telpon + except: + raise forms.ValidationError("Hanya masukkan angka") + raise forms.ValidationError("Nomor telepon sudah digunakan untuk mendaftar akun") + + def clean_nik(self): + nik = self.cleaned_data.get("nik") + if not User.objects.filter(nik=nik).exists(): + return nik + raise forms.ValidationError("NIK sudah digunakan untuk mendaftar akun") + \ No newline at end of file diff --git a/register/templates/index.html b/register/templates/index.html index 58f9a17..97cf8e7 100644 --- a/register/templates/index.html +++ b/register/templates/index.html @@ -55,6 +55,7 @@ </div> <div class="wrap-input100 validate-input" data-validate="Valid email is required: ex@abc.xyz"> <!-- <input class="input100" type="text" name="nama"> --> + {{ form.nik.errors }} {{ form.nik }} <span class="focus-input100"></span> <span class="label-input100">NIK</span> @@ -74,6 +75,7 @@ </div> <div class="wrap-input100 validate-input" data-validate="Valid email is required: ex@abc.xyz"> <!-- <input class="input100" type="text" name="nama"> --> + {{ form.nomor_telpon.errors}} {{ form.nomor_telpon }} <span class="focus-input100"></span> <span class="label-input100">Nomor Telepon</span> -- GitLab From b103d14fe522e3af676a2350e9cb08c91130e71a Mon Sep 17 00:00:00 2001 From: mohfaisal25 <moh.faisal@ui.ac.id> Date: Fri, 9 Oct 2020 15:11:27 +0700 Subject: [PATCH 4/6] Add new test to improve code coverage --- register/tests.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/register/tests.py b/register/tests.py index 854527a..50fc390 100644 --- a/register/tests.py +++ b/register/tests.py @@ -185,6 +185,23 @@ class RegisterPageTest(TestCase): self.assertIn( b"Nomor telepon sudah digunakan untuk mendaftar akun", response.content) + def test_create_user_input_wrong_phone_number_format(self): + response = self.client.post( + "/registrasi/", + { + "name": "bob", + "instansi": "university", + "nik": "3201234567890001", + "alamat": "bekasi", + "email": "bob@company.com", + "nomor_telpon": "abcdefghijkl", + "password": "1234", + "password2": "12345", + }, + ) + self.assertEqual(User.objects.all().count(), 0) + self.assertIn(b"Password tidak sama", response.content) + class RegisterAdminTest(TestCase): def setUp(self): self.client = Client() -- GitLab From 75644086efd6ff2c1b3a94e2fd7564bb779ab709 Mon Sep 17 00:00:00 2001 From: mohfaisal25 <moh.faisal@ui.ac.id> Date: Fri, 9 Oct 2020 15:57:36 +0700 Subject: [PATCH 5/6] [RED] Add new test for validation form register admin --- register/tests.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/register/tests.py b/register/tests.py index 50fc390..8957979 100644 --- a/register/tests.py +++ b/register/tests.py @@ -200,7 +200,7 @@ class RegisterPageTest(TestCase): }, ) self.assertEqual(User.objects.all().count(), 0) - self.assertIn(b"Password tidak sama", response.content) + self.assertIn(b"Hanya masukkan angka", response.content) class RegisterAdminTest(TestCase): def setUp(self): @@ -347,3 +347,84 @@ class RegisterAdminTest(TestCase): ) self.assertEqual(User.objects.count(), 1) self.assertIn("Please wait for our internal team to accept your admin account", response.content.decode()) + + def test_create_user_with_existing_nik(self): + response = self.client.post( + "/registrasi/admin/", + { + "name": "bob", + "instansi": "university", + "nik": "3201234567890001", + "alamat": "bekasi", + "email": "bob@company.com", + "nomor_telpon": "087878726601", + "password": self.random_password, + "password2": self.random_password, + }, + ) + self.assertEqual(User.objects.all().count(), 1) + response = self.client.post( + "/registrasi/admin/", + { + "name": "bob", + "instansi": "university", + "nik": "3201234567890001", + "alamat": "bekasi", + "email": "bob@company.com", + "nomor_telpon": "087878726602", + "password": self.random_password, + "password2": self.random_password, + }, + ) + self.assertEqual(User.objects.all().count(), 1) + self.assertIn( + b"NIK sudah digunakan untuk mendaftar akun", response.content) + + def test_create_user_with_existing_nomor_telpon(self): + response = self.client.post( + "/registrasi/admin/", + { + "name": "bob", + "instansi": "university", + "nik": "3201234567890001", + "alamat": "bekasi", + "email": "bob@company.com", + "nomor_telpon": "087878726601", + "password": self.random_password, + "password2": self.random_password, + }, + ) + self.assertEqual(User.objects.all().count(), 1) + response = self.client.post( + "/registrasi/admin/", + { + "name": "bob", + "instansi": "university", + "nik": "3201234567890002", + "alamat": "bekasi", + "email": "bob@company.com", + "nomor_telpon": "087878726601", + "password": self.random_password, + "password2": self.random_password, + }, + ) + self.assertEqual(User.objects.all().count(), 1) + self.assertIn( + b"Nomor telepon sudah digunakan untuk mendaftar akun", response.content) + + def test_create_user_input_wrong_phone_number_format(self): + response = self.client.post( + "/registrasi/admin/", + { + "name": "bob", + "instansi": "university", + "nik": "3201234567890001", + "alamat": "bekasi", + "email": "bob@company.com", + "nomor_telpon": "abcdefghij", + "password": self.random_password, + "password2": self.random_password, + }, + ) + self.assertEqual(User.objects.all().count(), 0) + self.assertIn(b"Hanya masukkan angka", response.content) \ No newline at end of file -- GitLab From 5059afd629ad36067303c736988f71b0a0749633 Mon Sep 17 00:00:00 2001 From: mohfaisal25 <moh.faisal@ui.ac.id> Date: Fri, 9 Oct 2020 16:19:53 +0700 Subject: [PATCH 6/6] [GREEN] Implement form validation for register admin --- register/forms.py | 2 +- register/templates/index_admin.html | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/register/forms.py b/register/forms.py index 691ccf9..1b3b5ba 100644 --- a/register/forms.py +++ b/register/forms.py @@ -43,7 +43,7 @@ class UserForm(forms.ModelForm): "Email sudah digunakan untuk mendaftar akun.") def clean_nomor_telpon(self): - nomor_telpon = self.data.get("nomor_telpon") + nomor_telpon = self.cleaned_data.get("nomor_telpon") if not User.objects.filter(nomor_telpon=nomor_telpon).exists(): try: no_telp = int(nomor_telpon) diff --git a/register/templates/index_admin.html b/register/templates/index_admin.html index aee58b7..e1bfa08 100644 --- a/register/templates/index_admin.html +++ b/register/templates/index_admin.html @@ -58,6 +58,7 @@ </div> <div class="wrap-input100 validate-input" data-validate="Valid email is required: ex@abc.xyz"> <!-- <input class="input100" type="text" name="nama"> --> + {{ form.nik.errors }} {{ form.nik }} <span class="focus-input100"></span> <span class="label-input100">NIK</span> @@ -77,6 +78,7 @@ </div> <div class="wrap-input100 validate-input" data-validate="Valid email is required: ex@abc.xyz"> <!-- <input class="input100" type="text" name="nama"> --> + {{ form.nomor_telpon.errors }} {{ form.nomor_telpon }} <span class="focus-input100"></span> <span class="label-input100">Nomor Telepon</span> -- GitLab