From 6d61dad378de92c57df1d29b7007b1e828d823f1 Mon Sep 17 00:00:00 2001 From: Moh Faisal <moh.faisal@ui.ac.id> Date: Fri, 9 Oct 2020 16:49:19 +0700 Subject: [PATCH] [#10] Auth: Register Form Validation --- .../migrations/0007_auto_20201009_1415.py | 18 ++ authentication/models.py | 2 +- register/forms.py | 23 +++ register/templates/index.html | 2 + register/templates/index_admin.html | 2 + register/tests.py | 184 +++++++++++++++++- 6 files changed, 220 insertions(+), 11 deletions(-) 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..1b3b5ba 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.cleaned_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> 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> diff --git a/register/tests.py b/register/tests.py index 86be11e..8957979 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,89 @@ 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) + + 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"Hanya masukkan angka", response.content) + class RegisterAdminTest(TestCase): def setUp(self): self.client = Client() @@ -173,7 +256,7 @@ class RegisterAdminTest(TestCase): { "name": "bob", "instansi": "university", - "nik": "1706074940", + "nik": "3201234567890002", "alamat": "bekasi", "email": "bob@company.com", "nomor_telpon": "087878726602", @@ -189,7 +272,7 @@ class RegisterAdminTest(TestCase): { "name": "bob", "instansi": "university", - "nik": "1706074940", + "nik": "3201234567890002", "alamat": "bekasi", "email": "bob@company.com", "nomor_telpon": "087878726602", @@ -206,7 +289,7 @@ class RegisterAdminTest(TestCase): { "name": "bob", "instansi": "university", - "nik": "1706074940", + "nik": "3201234567890001", "alamat": "bekasi", "email": "bob@company.com", "nomor_telpon": "087878726602", @@ -220,7 +303,7 @@ class RegisterAdminTest(TestCase): { "name": "bob", "instansi": "university", - "nik": "1706074940", + "nik": "3201234567890002", "alamat": "bekasi", "email": "bob@company.com", "nomor_telpon": "087878726602", @@ -238,7 +321,7 @@ class RegisterAdminTest(TestCase): { "name": "bob", "instansi": "university", - "nik": "1706074940", + "nik": "3201234567890002", "alamat": "bekasi", "email": "bob@company.com", "nomor_telpon": "087878726602", @@ -254,7 +337,7 @@ class RegisterAdminTest(TestCase): { "name": "bob", "instansi": "university", - "nik": "1706074940", + "nik": "3201234567890002", "alamat": "bekasi", "email": "bob@company.com", "nomor_telpon": "087878726602", @@ -264,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