Skip to content

Commit d14f5ad

Browse files
committed
Write test file for accounts and tasks and Modify ci.yml
1 parent f426c8b commit d14f5ad

13 files changed

Lines changed: 216 additions & 46 deletions

File tree

.github/workflows/ci.yml

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
branches: [ main ]
88

99
jobs:
10-
build:
10+
test:
1111
runs-on: ubuntu-latest
1212

1313
services:
@@ -18,43 +18,56 @@ jobs:
1818
MYSQL_DATABASE: test_db
1919
ports:
2020
- 3306:3306
21-
options: --health-cmd="mysqladmin ping --silent" --health-interval=10s --health-timeout=5s --health-retries=3
21+
options: >-
22+
--health-cmd="mysqladmin ping -h 127.0.0.1 -u root -proot --silent"
23+
--health-interval=10s
24+
--health-timeout=5s
25+
--health-retries=5
26+
27+
env:
28+
SECRET_KEY: dummykey
29+
DEBUG: true
30+
DB_NAME: test_db
31+
DB_USER: root
32+
DB_PASSWORD: root
33+
DB_HOST: 127.0.0.1
34+
DB_PORT: 3306
35+
DJANGO_SETTINGS_MODULE: task_manager.settings
2236

2337
steps:
24-
- name: Checkout Code
25-
uses: actions/checkout@v3
26-
27-
- name: Set up Python
28-
uses: actions/setup-python@v4
29-
with:
30-
python-version: "3.10"
31-
32-
- name: Install Dependencies
33-
run: |
34-
python -m pip install --upgrade pip
35-
pip install -r requirements.txt
36-
37-
- name: Create .env file for CI
38-
run: |
39-
echo "SECRET_KEY=dummykey" >> .env
40-
echo "DEBUG=True" >> .env
41-
echo "DB_NAME=test_db" >> .env
42-
echo "DB_USER=root" >> .env
43-
echo "DB_PASSWORD=root" >> .env
44-
echo "DB_HOST=127.0.0.1" >> .env
45-
echo "DB_PORT=3306" >> .env
46-
47-
- name: Wait for MySQL to be Ready
48-
run: |
49-
until mysql -h 127.0.0.1 -u root -proot -e "SELECT 1"; do
50-
echo "Waiting for MySQL...";
51-
sleep 3;
52-
done
53-
54-
- name: Run Migrations
55-
run: |
56-
python manage.py migrate
57-
58-
- name: Run Tests
59-
run: |
60-
python manage.py test
38+
- name: Checkout Code
39+
uses: actions/checkout@v4
40+
41+
- name: Set up Python
42+
uses: actions/setup-python@v5
43+
with:
44+
python-version: '3.10'
45+
46+
- name: Cache pip dependencies
47+
uses: actions/cache@v4
48+
with:
49+
path: ~/.cache/pip
50+
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
51+
restore-keys: |
52+
${{ runner.os }}-pip-
53+
54+
- name: Install Dependencies
55+
run: |
56+
python -m pip install --upgrade pip
57+
pip install -r requirements.txt
58+
59+
- name: Wait for MySQL to be Ready
60+
run: |
61+
until mysql -h 127.0.0.1 -u root -proot -e "SELECT 1" > /dev/null 2>&1; do
62+
echo "⏳ Waiting for MySQL to start...";
63+
sleep 3;
64+
done
65+
echo "✅ MySQL is ready!"
66+
67+
- name: Run Migrations
68+
run: |
69+
python manage.py migrate --noinput
70+
71+
- name: Run Tests
72+
run: |
73+
python manage.py test
0 Bytes
Binary file not shown.
4.23 KB
Binary file not shown.

accounts/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from django.contrib.auth.models import AbstractUser
22

3+
# AbstractUser is a builtin user model which have username and password and other attributes for use
4+
# There is also AbstractBaseUser for advanced use.
35
class User(AbstractUser):
46
pass
57

accounts/tests.py

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
1-
from django.test import TestCase
1+
from rest_framework.test import APITestCase
2+
from django.urls import reverse
3+
from rest_framework import status
4+
from django.contrib.auth import get_user_model
5+
from accounts.serializers import user_serializer
6+
7+
User = get_user_model()
8+
9+
class TestUserRegistration(APITestCase):
10+
def setUp(self):
11+
self.url = reverse('reg_list_create')
12+
def test_valid_user_creation(self):
13+
data = {"username": "ahmed", "password": "Pass@123"}
14+
serializer = user_serializer(data= data)
15+
self.assertTrue(serializer.is_valid(), serializer.errors)
16+
17+
user = serializer.save()
18+
self.assertEqual(user.username, "ahmed")
19+
self.assertTrue(user.check_password("Pass@123"))
20+
21+
def test_username_not_lowercase(self):
22+
data = {"username": "Ahmed", "password": "Pass@123"}
23+
serializer = user_serializer(data= data)
24+
self.assertFalse(serializer.is_valid())
25+
self.assertIn("Username should be all lowercase characters", str(serializer.errors))
26+
27+
def test_password_too_short(self):
28+
data = {"username": "ahmed", "password": "abc"}
29+
serializer = user_serializer(data=data)
30+
self.assertFalse(serializer.is_valid())
31+
self.assertIn("Password must contain more than 6 characters", str(serializer.errors))
32+
33+
def test_password_missing_number(self):
34+
data = {"username": "ahmed", "password": "Password@"}
35+
serializer = user_serializer(data=data)
36+
self.assertFalse(serializer.is_valid())
37+
self.assertIn("Password must atleast contain a single number", str(serializer.errors))
38+
39+
def test_password_missing_special_char(self):
40+
data = {"username": "ahmed", "password": "Password1"}
41+
serializer = user_serializer(data=data)
42+
self.assertFalse(serializer.is_valid())
43+
self.assertIn("Password must atleast contain a special Character", str(serializer.errors))
44+
45+
def test_password_write_only(self):
46+
serializer = user_serializer()
47+
self.assertTrue(serializer.fields["password"].write_only)
48+
49+
def test_register_endpoint(self):
50+
data = {"username": "ahmed", "password": "Pass@123"}
51+
response = self.client.post(self.url, data, format='json')
52+
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
53+
254

3-
# Create your tests here.
4-
def test_placeholder():
5-
assert True == True

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ asgiref==3.10.0
22
Django==5.2.7
33
django-environ==0.12.0
44
djangorestframework==3.16.1
5+
djangorestframework_simplejwt==5.5.1
56
mysqlclient==2.2.7
7+
PyJWT==2.10.1
68
python-dotenv==1.1.1
79
sqlparse==0.5.3
810
tzdata==2025.2
0 Bytes
Binary file not shown.
7.02 KB
Binary file not shown.
0 Bytes
Binary file not shown.
187 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)