Fakultas Ilmu Komputer UI

Commit 4af8c772 authored by Dave Nathanael's avatar Dave Nathanael
Browse files

Merge branch 'jojo/fix-phone-validator' into 'staging'

Fix: Phone Validator on Accounts

See merge request !47
parents 1dcd8cfe 5a9a6f75
Pipeline #46171 passed with stages
in 20 minutes
import uuid
from django.contrib.auth.models import User
from django.db import models
from django.core.validators import RegexValidator
from apps.commons.managers import SoftDeleteManager
from apps.commons.models import HistoryEnabledModel, HistoryModel
......@@ -32,7 +33,15 @@ class Account(HistoryEnabledModel):
user = models.OneToOneField(User, on_delete=models.CASCADE)
name = models.CharField(max_length=128)
email = models.EmailField(max_length=128)
phone_number = models.CharField(max_length=64)
phone_number = models.CharField(
max_length=64,
validators=[
RegexValidator(
regex=r'^\+?\d{9,15}$',
message=('Phone number has 9-15 digits, allowed to have + prefix'),
),
]
)
area = models.CharField(max_length=128)
is_admin = models.BooleanField(default=False)
is_verified = models.BooleanField(default=False)
......
......@@ -25,7 +25,7 @@ class AccountFactory(factory.DjangoModelFactory):
user = factory.SubFactory(UserFactory)
name = faker.name()
email = faker.email()
phone_number = faker.phone_number()
phone_number = "+999999999999"
area = faker.city()
is_active = True
is_verified = True
......@@ -88,7 +88,7 @@ class AccountViewTest(APITestCase):
"username": _account_id,
"password": "justpass",
"email": _account_id,
"phone_number": self.faker.phone_number(),
"phone_number": "+999999999999",
"area": self.faker.city(),
"is_admin": True,
}
......@@ -110,7 +110,7 @@ class AccountViewTest(APITestCase):
"username": _account_id,
"password": "justpass",
"email": _account_id,
"phone_number": self.faker.phone_number(),
"phone_number": "+999999999999",
"area": self.faker.city(),
"is_admin": False,
"is_verified": True,
......@@ -145,7 +145,7 @@ class AccountViewTest(APITestCase):
"username": "user_1",
"password": "justpass",
"email": self.faker.email(),
"phone_number": self.faker.phone_number(),
"phone_number": "+999999999999",
"area": self.faker.city(),
"is_admin": False,
"is_verified": True,
......@@ -163,21 +163,21 @@ class AccountViewTest(APITestCase):
"email": _account_id,
"username": _account_id,
"password": "12345678",
"phone_number": self.faker.phone_number(),
"phone_number": "+999999999999",
"area": self.faker.city(),
"is_admin": False,
}
response = self.client.post(path=url, data=data, format="json",)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_edit_account_success_by(self):
def test_edit_account_success(self):
url = self.BASE_URL + str(self.officer.id) + "/"
data = {
"id": str(self.officer.id),
"name": self.faker.name(),
"email": self.faker.email(),
"phone_number": self.faker.phone_number(),
"phone_number": "+999999999999",
"area": self.faker.city(),
"is_admin": False,
"is_verified": True,
......@@ -213,13 +213,43 @@ class AccountViewTest(APITestCase):
data = {
"id": str(self.officer.id),
"phone_number": self.faker.phone_number(),
"phone_number": "+999999999999",
"area": self.faker.city(),
}
response = self.client.put(path=url, data=data, format="json",)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_edit_account_fail_with_wrong_email_or_phone_format(self):
url = self.BASE_URL + str(self.officer.id) + "/"
data = {
"id": str(self.officer.id),
"name": self.faker.name(),
"email": self.faker.email(),
"phone_number": "+999aaa999",
"area": self.faker.city(),
"is_admin": False,
"is_verified": True,
"is_active": True,
}
response = self.client.put(path=url, data=data, format="json",)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
data = {
"id": str(self.officer.id),
"name": self.faker.name(),
"email": "email",
"phone_number": "+999999999999",
"area": self.faker.city(),
"is_admin": False,
"is_verified": True,
"is_active": True,
}
response = self.client.put(path=url, data=data, format="json",)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_delete_success(self):
url = self.BASE_URL + str(self.officer.id) + "/"
......
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