diff --git a/authentication/migrations/0007_auto_20201009_1415.py b/authentication/migrations/0007_auto_20201009_1415.py
new file mode 100644
index 0000000000000000000000000000000000000000..012a5c89e717087a23903de27bbadbdf8798d63e
--- /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 9822fbc6325332e823be365a65f88ed31f0b87f3..e5161abbbfbd3f5f4b9a676dab59268264df9994 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 739a77b0899221c55bb0f961425730eaee836a41..1b3b5ba717ffba57f45ea4d4c1f995be00ae2651 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 58f9a1701bec16abc14f7ca460fb5fb7c5b7690f..97cf8e78bff9ce666a5a7aefb9b7fca817352812 100644
--- a/register/templates/index.html
+++ b/register/templates/index.html
@@ -55,6 +55,7 @@
+ {{ form.nik.errors }}
{{ form.nik }}
NIK
@@ -74,6 +75,7 @@
+ {{ form.nomor_telpon.errors}}
{{ form.nomor_telpon }}
Nomor Telepon
diff --git a/register/templates/index_admin.html b/register/templates/index_admin.html
index aee58b799e6b8dabc051c790298218098bfd8c2c..e1bfa08d23e59ead23670c8536ac12166057964d 100644
--- a/register/templates/index_admin.html
+++ b/register/templates/index_admin.html
@@ -58,6 +58,7 @@
+ {{ form.nik.errors }}
{{ form.nik }}
NIK
@@ -77,6 +78,7 @@
+ {{ form.nomor_telpon.errors }}
{{ form.nomor_telpon }}
Nomor Telepon
diff --git a/register/tests.py b/register/tests.py
index 86be11ef80274b60d41936474c00295755de1cdc..895797942b52f7160515d9d81986cb795e7b8809 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