Fakultas Ilmu Komputer UI
Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
PMPL
Class Project
DIGIPUS
Commits
6d61dad3
Commit
6d61dad3
authored
Oct 09, 2020
by
Moh Faisal
Browse files
[
#10
] Auth: Register Form Validation
parent
0a815a6d
Changes
6
Hide whitespace changes
Inline
Side-by-side
authentication/migrations/0007_auto_20201009_1415.py
0 → 100644
View file @
6d61dad3
# 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
),
),
]
authentication/models.py
View file @
6d61dad3
...
...
@@ -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
=
""
)
...
...
register/forms.py
View file @
6d61dad3
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
register/templates/index.html
View file @
6d61dad3
...
...
@@ -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>
...
...
register/templates/index_admin.html
View file @
6d61dad3
...
...
@@ -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>
...
...
register/tests.py
View file @
6d61dad3
...
...
@@ -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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment