|
1 | 1 | """ |
2 | | -Tests for authentication views: login, register, me, token refresh, org switch, |
| 2 | +Tests for authentication views: me, token refresh, org switch, |
3 | 3 | Google OAuth callback, Google ID token, and token refresh edge cases. |
4 | 4 |
|
5 | 5 | Run with: pytest common/tests/test_auth.py -v |
|
18 | 18 | from common.serializer import OrgAwareRefreshToken |
19 | 19 |
|
20 | 20 |
|
21 | | -@pytest.mark.django_db |
22 | | -class TestLoginView: |
23 | | - """Tests for POST /api/auth/login/""" |
24 | | - |
25 | | - url = "/api/auth/login/" |
26 | | - |
27 | | - def test_login_success(self, unauthenticated_client, admin_user, admin_profile): |
28 | | - response = unauthenticated_client.post( |
29 | | - self.url, |
30 | | - { "email": "[email protected]", "password": "testpass123"}, |
31 | | - format="json", |
32 | | - ) |
33 | | - assert response.status_code == status.HTTP_200_OK |
34 | | - assert "access_token" in response.data |
35 | | - assert "refresh_token" in response.data |
36 | | - assert "current_org" in response.data |
37 | | - |
38 | | - def test_login_wrong_password(self, unauthenticated_client, admin_user, admin_profile): |
39 | | - response = unauthenticated_client.post( |
40 | | - self.url, |
41 | | - { "email": "[email protected]", "password": "wrongpassword"}, |
42 | | - format="json", |
43 | | - ) |
44 | | - assert response.status_code == status.HTTP_400_BAD_REQUEST |
45 | | - |
46 | | - def test_login_nonexistent_user(self, unauthenticated_client): |
47 | | - response = unauthenticated_client.post( |
48 | | - self.url, |
49 | | - { "email": "[email protected]", "password": "testpass123"}, |
50 | | - format="json", |
51 | | - ) |
52 | | - assert response.status_code == status.HTTP_400_BAD_REQUEST |
53 | | - |
54 | | - def test_login_returns_tokens_and_org( |
55 | | - self, unauthenticated_client, admin_user, admin_profile, org_a |
56 | | - ): |
57 | | - response = unauthenticated_client.post( |
58 | | - self.url, |
59 | | - { "email": "[email protected]", "password": "testpass123"}, |
60 | | - format="json", |
61 | | - ) |
62 | | - assert response.status_code == status.HTTP_200_OK |
63 | | - data = response.data |
64 | | - assert "access_token" in data |
65 | | - assert "refresh_token" in data |
66 | | - assert "user" in data |
67 | | - assert "current_org" in data |
68 | | - assert data["current_org"]["id"] == str(org_a.id) |
69 | | - |
70 | | - def test_login_with_specific_org(self, unauthenticated_client, admin_user, org_b): |
71 | | - # Give admin_user access to org_b |
72 | | - Profile.objects.create( |
73 | | - user=admin_user, org=org_b, role="USER", is_active=True |
74 | | - ) |
75 | | - response = unauthenticated_client.post( |
76 | | - self.url, |
77 | | - { |
78 | | - |
79 | | - "password": "testpass123", |
80 | | - "org_id": str(org_b.id), |
81 | | - }, |
82 | | - format="json", |
83 | | - ) |
84 | | - assert response.status_code == status.HTTP_200_OK |
85 | | - assert response.data["current_org"]["id"] == str(org_b.id) |
86 | | - |
87 | | - def test_login_unauthorized_org( |
88 | | - self, unauthenticated_client, admin_user, admin_profile, org_b |
89 | | - ): |
90 | | - # admin_user does NOT have a profile in org_b |
91 | | - response = unauthenticated_client.post( |
92 | | - self.url, |
93 | | - { |
94 | | - |
95 | | - "password": "testpass123", |
96 | | - "org_id": str(org_b.id), |
97 | | - }, |
98 | | - format="json", |
99 | | - ) |
100 | | - assert response.status_code == status.HTTP_403_FORBIDDEN |
101 | | - |
102 | | - def test_login_missing_email(self, unauthenticated_client): |
103 | | - """Login without email should fail.""" |
104 | | - response = unauthenticated_client.post( |
105 | | - self.url, |
106 | | - {"password": "testpass123"}, |
107 | | - format="json", |
108 | | - ) |
109 | | - assert response.status_code == status.HTTP_400_BAD_REQUEST |
110 | | - |
111 | | - def test_login_missing_password(self, unauthenticated_client, admin_user): |
112 | | - """Login without password should fail.""" |
113 | | - response = unauthenticated_client.post( |
114 | | - self.url, |
115 | | - |
116 | | - format="json", |
117 | | - ) |
118 | | - assert response.status_code == status.HTTP_400_BAD_REQUEST |
119 | | - |
120 | | - def test_login_empty_body(self, unauthenticated_client): |
121 | | - """Login with empty body should fail.""" |
122 | | - response = unauthenticated_client.post( |
123 | | - self.url, |
124 | | - {}, |
125 | | - format="json", |
126 | | - ) |
127 | | - assert response.status_code == status.HTTP_400_BAD_REQUEST |
128 | | - |
129 | | - def test_login_user_data_returned( |
130 | | - self, unauthenticated_client, admin_user, admin_profile |
131 | | - ): |
132 | | - """Login should return user details.""" |
133 | | - response = unauthenticated_client.post( |
134 | | - self.url, |
135 | | - { "email": "[email protected]", "password": "testpass123"}, |
136 | | - format="json", |
137 | | - ) |
138 | | - assert response.status_code == status.HTTP_200_OK |
139 | | - user_data = response.data["user"] |
140 | | - assert "email" in user_data |
141 | | - assert user_data[ "email"] == "[email protected]" |
142 | | - |
143 | | - def test_login_user_no_org(self, unauthenticated_client): |
144 | | - """Login for user without any org should return tokens without current_org.""" |
145 | | - User. objects. create_user( email="[email protected]", password="testpass123") |
146 | | - response = unauthenticated_client.post( |
147 | | - self.url, |
148 | | - { "email": "[email protected]", "password": "testpass123"}, |
149 | | - format="json", |
150 | | - ) |
151 | | - assert response.status_code == status.HTTP_200_OK |
152 | | - assert "access_token" in response.data |
153 | | - assert "current_org" not in response.data |
154 | | - |
155 | | - |
156 | 21 | @pytest.mark.django_db |
157 | 22 | class TestMeView: |
158 | 23 | """Tests for GET /api/auth/me/""" |
|
0 commit comments