2026-02-27 12:12:26 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
from rest_framework import status
|
|
|
|
|
from rest_framework.test import APIClient
|
|
|
|
|
|
2026-02-27 15:21:46 +00:00
|
|
|
from apps.accounts.models import Invite, Organization, Role, User
|
2026-02-27 12:12:26 +00:00
|
|
|
|
|
|
|
|
class AccountsApiTests(TestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.client: APIClient = APIClient()
|
2026-02-27 15:21:46 +00:00
|
|
|
self.manager: User = User.objects.create_user(
|
2026-02-27 12:12:26 +00:00
|
|
|
email_address='manager@example.com',
|
|
|
|
|
password='pass1234',
|
|
|
|
|
first_name='Manager',
|
|
|
|
|
last_name='User',
|
|
|
|
|
date_of_birth='1990-01-01',
|
|
|
|
|
is_manager=True,
|
|
|
|
|
)
|
2026-02-27 15:21:46 +00:00
|
|
|
self.member: User = User.objects.create_user(
|
2026-02-27 12:12:26 +00:00
|
|
|
email_address='member@example.com',
|
|
|
|
|
password='pass1234',
|
|
|
|
|
first_name='Member',
|
|
|
|
|
last_name='User',
|
|
|
|
|
date_of_birth='1992-02-02',
|
|
|
|
|
)
|
2026-02-27 15:21:46 +00:00
|
|
|
self.other: User = User.objects.create_user(
|
2026-02-27 12:12:26 +00:00
|
|
|
email_address='other@example.com',
|
|
|
|
|
password='pass1234',
|
|
|
|
|
first_name='Other',
|
|
|
|
|
last_name='User',
|
|
|
|
|
date_of_birth='1993-03-03',
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.organization = Organization.objects.create(
|
|
|
|
|
name='Team Alpha',
|
|
|
|
|
description='Main team',
|
|
|
|
|
owner=self.manager,
|
|
|
|
|
)
|
|
|
|
|
self.organization.members.add(self.manager, self.member)
|
|
|
|
|
self.role = Role.objects.create(name='Developer', organization=self.organization)
|
|
|
|
|
|
|
|
|
|
def test_user_list_path(self):
|
|
|
|
|
response = self.client.get('/api/user/')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
def test_user_retrieve_path(self):
|
|
|
|
|
response = self.client.get(f'/api/user/{self.manager.uuid}/')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
def test_user_login_path(self):
|
|
|
|
|
response = self.client.post('/api/user/login/', {
|
|
|
|
|
'email_address': 'manager@example.com',
|
|
|
|
|
'password': 'pass1234',
|
|
|
|
|
})
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
self.assertTrue(response.json().get('success'))
|
|
|
|
|
|
|
|
|
|
def test_user_logout_path(self):
|
|
|
|
|
self.client.force_authenticate(self.manager)
|
|
|
|
|
response = self.client.post('/api/user/logout/')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
def test_user_me_path(self):
|
|
|
|
|
self.client.force_authenticate(self.member)
|
|
|
|
|
response = self.client.get('/api/user/me/')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
self.assertEqual(response.json()['email_address'], 'member@example.com')
|
|
|
|
|
|
|
|
|
|
def test_user_session_path(self):
|
|
|
|
|
response = self.client.get('/api/user/session/')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
self.assertIn('isAuthenticated', response.json())
|
|
|
|
|
|
|
|
|
|
def test_user_signup_path(self):
|
|
|
|
|
response = self.client.post('/api/user/signup/', {
|
|
|
|
|
'email_address': 'signup@example.com',
|
|
|
|
|
'password': 'newpass123',
|
|
|
|
|
'confirm_password': 'newpass123',
|
|
|
|
|
'first_name': 'Sign',
|
|
|
|
|
'last_name': 'Up',
|
|
|
|
|
'date_of_birth': '1995-05-05',
|
|
|
|
|
'manager': False,
|
|
|
|
|
}, format='json')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
|
|
|
|
|
|
|
|
|
def test_user_change_password_path(self):
|
|
|
|
|
self.client.force_authenticate(self.member)
|
|
|
|
|
response = self.client.post('/api/user/change_password/', {
|
|
|
|
|
'old_password': 'pass1234',
|
|
|
|
|
'password': 'newpass123',
|
|
|
|
|
'confirm_password': 'newpass123',
|
|
|
|
|
}, format='json')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
def test_organization_list_path(self):
|
|
|
|
|
self.client.force_authenticate(self.manager)
|
|
|
|
|
response = self.client.get('/api/organization/')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
def test_organization_create_path(self):
|
|
|
|
|
self.client.force_authenticate(self.manager)
|
|
|
|
|
response = self.client.post('/api/organization/', {
|
|
|
|
|
'name': 'Team Beta',
|
|
|
|
|
'description': 'Second team',
|
|
|
|
|
}, format='json')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
|
|
|
|
|
|
|
|
|
def test_organization_retrieve_path(self):
|
|
|
|
|
self.client.force_authenticate(self.member)
|
|
|
|
|
response = self.client.get(f'/api/organization/{self.organization.uuid}/')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
def test_organization_update_path(self):
|
|
|
|
|
self.client.force_authenticate(self.manager)
|
|
|
|
|
response = self.client.put(
|
|
|
|
|
f'/api/organization/{self.organization.uuid}/',
|
|
|
|
|
{'name': 'Team Alpha Updated', 'description': 'Updated'},
|
|
|
|
|
format='json',
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
def test_organization_partial_update_path(self):
|
|
|
|
|
self.client.force_authenticate(self.manager)
|
|
|
|
|
response = self.client.patch(
|
|
|
|
|
f'/api/organization/{self.organization.uuid}/',
|
|
|
|
|
{'description': 'Patched'},
|
|
|
|
|
format='json',
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
def test_organization_delete_path(self):
|
|
|
|
|
self.client.force_authenticate(self.manager)
|
|
|
|
|
org = Organization.objects.create(name='Delete Me', owner=self.manager)
|
|
|
|
|
org.members.add(self.manager)
|
|
|
|
|
response = self.client.delete(f'/api/organization/{org.uuid}/')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
|
|
|
|
|
|
|
|
|
|
def test_organization_invite_list_path(self):
|
|
|
|
|
self.client.force_authenticate(self.manager)
|
|
|
|
|
Invite.objects.create(organization=self.organization, created_by=self.manager)
|
|
|
|
|
response = self.client.get(f'/api/organization/{self.organization.uuid}/invite/')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
def test_organization_create_invite_path(self):
|
|
|
|
|
self.client.force_authenticate(self.manager)
|
2026-02-27 15:21:46 +00:00
|
|
|
response = self.client.post(f'/api/organization/{self.organization.uuid}/create-invite/?max_uses=2', {}, format='json')
|
2026-02-27 12:12:26 +00:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
2026-02-27 15:21:46 +00:00
|
|
|
self.assertIn('uuid', response.json())
|
2026-02-27 12:12:26 +00:00
|
|
|
|
|
|
|
|
def test_organization_revoke_invite_path(self):
|
|
|
|
|
self.client.force_authenticate(self.manager)
|
|
|
|
|
invite = Invite.objects.create(organization=self.organization, created_by=self.manager)
|
2026-02-27 15:21:46 +00:00
|
|
|
response = self.client.delete(f'/api/organization/{self.organization.uuid}/revoke-invite/{invite.uuid}/')
|
2026-02-27 12:12:26 +00:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
def test_organization_join_path(self):
|
|
|
|
|
self.client.force_authenticate(self.other)
|
|
|
|
|
invite = Invite.objects.create(organization=self.organization, created_by=self.manager)
|
2026-02-27 15:21:46 +00:00
|
|
|
response = self.client.post(f'/api/organization/join/{invite.uuid}/')
|
2026-02-27 12:12:26 +00:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
def test_organization_leave_path(self):
|
|
|
|
|
self.client.force_authenticate(self.member)
|
|
|
|
|
response = self.client.post(f'/api/organization/{self.organization.uuid}/leave/')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
def test_organization_members_path(self):
|
|
|
|
|
self.client.force_authenticate(self.manager)
|
|
|
|
|
response = self.client.get(f'/api/organization/{self.organization.uuid}/members/')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
def test_organization_remove_member_path(self):
|
|
|
|
|
self.client.force_authenticate(self.manager)
|
2026-02-27 15:21:46 +00:00
|
|
|
response = self.client.post(f'/api/organization/{self.organization.uuid}/member/{self.member.uuid}/remove/')
|
2026-02-27 12:12:26 +00:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
def test_organization_roles_get_path(self):
|
|
|
|
|
self.client.force_authenticate(self.manager)
|
|
|
|
|
response = self.client.get(f'/api/organization/{self.organization.uuid}/role/')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
def test_organization_roles_post_path(self):
|
|
|
|
|
self.client.force_authenticate(self.manager)
|
|
|
|
|
response = self.client.post(
|
|
|
|
|
f'/api/organization/{self.organization.uuid}/role/',
|
|
|
|
|
{'name': 'Designer', 'description': 'Design role'},
|
|
|
|
|
format='json',
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
|
|
|
|
|
|
|
|
|
def test_organization_my_roles_path(self):
|
|
|
|
|
self.client.force_authenticate(self.member)
|
|
|
|
|
self.role.members.add(self.member)
|
|
|
|
|
response = self.client.get('/api/organization/role/mine/')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
def test_organization_delete_role_path(self):
|
|
|
|
|
self.client.force_authenticate(self.manager)
|
|
|
|
|
delete_role = Role.objects.create(name='DeleteRole', organization=self.organization)
|
|
|
|
|
response = self.client.delete(f'/api/organization/{self.organization.uuid}/role/{delete_role.uuid}/')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
|
|
|
|
|
|
|
|
|
|
def test_organization_join_role_path(self):
|
|
|
|
|
self.client.force_authenticate(self.member)
|
|
|
|
|
response = self.client.post(f'/api/organization/{self.organization.uuid}/role/{self.role.uuid}/join/')
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|