Skip to content

Commit

Permalink
Write unit test for register view
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-nghiem-goldenowl committed Dec 29, 2023
1 parent 037c2d0 commit d8053c8
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ dump.rdb
# static folder
static_root
static

.DS_Store
Empty file.
16 changes: 16 additions & 0 deletions authentication/factories/customer_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from faker import Faker
from authentication.models import Customer
import factory

fake = Faker()

class CustomerFactory(factory.django.DjangoModelFactory):
username = fake.profile()['username']
password = fake.password()
email = fake.email()
bio = fake.text()
birth_date = fake.date_object()
address = fake.address()

class Meta:
model = Customer
17 changes: 17 additions & 0 deletions authentication/migrations/0002_alter_customer_address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2 on 2023-12-27 09:47

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("authentication", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="customer",
name="address",
field=models.CharField(blank=True, max_length=200, verbose_name="Address"),
),
]
2 changes: 1 addition & 1 deletion authentication/models/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class Customer(User):
bio = models.TextField(verbose_name="Bio info", max_length=500, blank=True)
birth_date = models.DateField(verbose_name="Birth date", null=True, blank=True)
address = models.CharField(verbose_name="Address", max_length=30, blank=True)
address = models.CharField(verbose_name="Address", max_length=200, blank=True)

class Meta:
db_table = "customer"
Expand Down
29 changes: 18 additions & 11 deletions authentication/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
from django.contrib.auth.models import User
from authentication.factories.customer_factory import CustomerFactory
from django.test import TestCase
from rest_framework_simplejwt.tokens import RefreshToken
from authentication.models import Customer
from faker import Faker

fake = Faker()

class TestCalls(TestCase):
@classmethod
def setUpTestData(cls):
cls.user = User.objects.create_user(
username="nguyen.hung",
password="1234ABbcd!@",
email="[email protected]",
cls.password = fake.password()
cls.username = fake.profile()["username"]
cls.email = fake.email()
cls.customer = User.objects.create_user(
username=cls.username,
password=cls.password,
email=cls.email,
)

def test_call_register(self):
user_name = fake.profile()["username"]
data = {
"username": "bao.binh",
"password": "1234ABbcd!@",
"email": "[email protected]",
"username": user_name,
"password": fake.password(),
"email": fake.email(),
}

response = self.client.post(
Expand All @@ -26,21 +34,20 @@ def test_call_register(self):
self.assertEqual(response.status_code, 201)
self.assertIn("username", response.json())
self.assertIn("email", response.json())
self.assertEqual(response.json().get("username"), "bao.binh")
self.assertEqual(response.json().get("username"), user_name)

def test_call_login(self):
data = {"username": "nguyen.hung", "password": "1234ABbcd!@"}
data = {"username": TestCalls.username, "password": TestCalls.password}

response = self.client.post(
"/api/v1/auth/login/", data, content_type="application/json"
)

self.assertEqual(response.status_code, 200)
self.assertIn("access", response.json())
self.assertIn("refresh", response.json())

def test_call_refresh_token(self):
refresh = RefreshToken.for_user(TestCalls.user)
refresh = RefreshToken.for_user(TestCalls.customer)
data = {
"refresh": str(refresh),
}
Expand Down
2 changes: 2 additions & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
-r base.txt
coverage==7.3.2
factory_boy==3.3.0
Faker==21.0.0

0 comments on commit d8053c8

Please sign in to comment.