Fakultas Ilmu Komputer UI

Commit 2dd0d75c authored by Moh Faisal's avatar Moh Faisal
Browse files

Merge branch '1706039502-10' into 'master'

[#10] Auth: Register Form Validation

See merge request !27
parents 0a815a6d 6d61dad3
Pipeline #58083 passed with stages
in 12 minutes and 36 seconds
# 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),
),
]
......@@ -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="")
......
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
......@@ -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>
......
......@@ -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>
......
......@@ -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
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