|
| 1 | +import pytest |
| 2 | +from rest_framework.test import APIClient |
| 3 | + |
| 4 | +from users.models import User |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.django_db |
| 8 | +class TestUserAuthAPI: |
| 9 | + @pytest.fixture |
| 10 | + def client(self): |
| 11 | + return APIClient() |
| 12 | + |
| 13 | + def test_user_registration(self, client): |
| 14 | + url = "/api/register/user/" |
| 15 | + payload = { |
| 16 | + "fullname": "John Doe", |
| 17 | + "username": "johndoe", |
| 18 | + |
| 19 | + "mobile": "1234567890", |
| 20 | + "password": "password123", |
| 21 | + "confirm_password": "password123", |
| 22 | + } |
| 23 | + response = client.post(url, payload, format="json") |
| 24 | + assert response.status_code == 201 |
| 25 | + assert response.data["message"] == "User registered successfully." |
| 26 | + assert User.objects.filter(username="johndoe").exists() |
| 27 | + |
| 28 | + def test_technician_registration(self, client): |
| 29 | + url = "/api/register/technician/" |
| 30 | + payload = { |
| 31 | + "fullname": "Tech User", |
| 32 | + "username": "techuser", |
| 33 | + |
| 34 | + "mobile": "1234567891", |
| 35 | + "password": "password123", |
| 36 | + "confirm_password": "password123", |
| 37 | + } |
| 38 | + response = client.post(url, payload, format="json") |
| 39 | + assert response.status_code == 201 |
| 40 | + assert response.data["message"] == "Technician registered successfully." |
| 41 | + user = User.objects.get(username="techuser") |
| 42 | + assert user.role == "technician" |
| 43 | + |
| 44 | + def test_admin_registration(self, client): |
| 45 | + url = "/api/register/admin/" |
| 46 | + payload = { |
| 47 | + "fullname": "Admin User", |
| 48 | + "username": "adminuser", |
| 49 | + |
| 50 | + "mobile": "1234567892", |
| 51 | + "password": "password123", |
| 52 | + "confirm_password": "password123", |
| 53 | + } |
| 54 | + response = client.post(url, payload, format="json") |
| 55 | + assert response.status_code == 201 |
| 56 | + assert response.data["message"] == "Admin registered successfully." |
| 57 | + user = User.objects.get(username="adminuser") |
| 58 | + assert user.role == "admin" |
| 59 | + |
| 60 | + def test_login_with_username(self, client): |
| 61 | + user = User.objects.create_user( |
| 62 | + username="loginuser", |
| 63 | + |
| 64 | + mobile="1234567893", |
| 65 | + password="password123", |
| 66 | + fullname="Login User", |
| 67 | + role="user", |
| 68 | + ) |
| 69 | + url = "/api/login/" |
| 70 | + payload = {"identifier": "loginuser", "password": "password123"} |
| 71 | + response = client.post(url, payload, format="json") |
| 72 | + assert response.status_code == 200 |
| 73 | + assert response.data["username"] == user.username |
| 74 | + |
| 75 | + def test_login_with_email(self, client): |
| 76 | + user = User.objects.create_user( |
| 77 | + username="emailuser", |
| 78 | + |
| 79 | + mobile="1234567894", |
| 80 | + password="password123", |
| 81 | + fullname="Email User", |
| 82 | + role="user", |
| 83 | + ) |
| 84 | + url = "/api/login/" |
| 85 | + payload = { "identifier": "[email protected]", "password": "password123"} |
| 86 | + response = client.post(url, payload, format="json") |
| 87 | + assert response.status_code == 200 |
| 88 | + assert response.data["username"] == user.username |
| 89 | + |
| 90 | + def test_login_with_mobile(self, client): |
| 91 | + user = User.objects.create_user( |
| 92 | + username="mobileuser", |
| 93 | + |
| 94 | + mobile="1234567895", |
| 95 | + password="password123", |
| 96 | + fullname="Mobile User", |
| 97 | + role="user", |
| 98 | + ) |
| 99 | + url = "/api/login/" |
| 100 | + payload = {"identifier": "1234567895", "password": "password123"} |
| 101 | + response = client.post(url, payload, format="json") |
| 102 | + assert response.status_code == 200 |
| 103 | + assert response.data["username"] == user.username |
| 104 | + |
| 105 | + def test_login_invalid_credentials(self, client): |
| 106 | + url = "/api/login/" |
| 107 | + payload = {"identifier": "nonexistent", "password": "wrongpass"} |
| 108 | + response = client.post(url, payload, format="json") |
| 109 | + assert response.status_code == 401 |
| 110 | + assert "error" in response.data |
0 commit comments