Spaces:
Running
Running
Download api/tests/test_projects_api.py from Dayozoe/harvestconnect-api: direct link, hf CLI and curl.
- Browser
- Download file 3.32 kB
-
https://huggingface.co/spaces/Dayozoe/harvestconnect-api/resolve/main/api/tests/test_projects_api.py
- Command line
-
hf download hf://spaces/Dayozoe/harvestconnect-api/api/tests/test_projects_api.py
-
curl -L -o test_projects_api.py https://huggingface.co/spaces/Dayozoe/harvestconnect-api/resolve/main/api/tests/test_projects_api.py
3.32 kB
| from django.contrib.auth import get_user_model | |
| from django.test import TestCase | |
| from rest_framework.test import APIClient | |
| from ..models import Project, UserProfile | |
| class ProjectAPITests(TestCase): | |
| def setUp(self): | |
| User = get_user_model() | |
| self.client_user = User.objects.create_user( | |
| username='project-client', email='project-client@example.test', password='test-pass' | |
| ) | |
| self.tradesman = User.objects.create_user( | |
| username='project-tradesman', email='project-tradesman@example.test', password='test-pass' | |
| ) | |
| self.other_user = User.objects.create_user( | |
| username='project-other', email='project-other@example.test', password='test-pass' | |
| ) | |
| UserProfile.objects.filter(user=self.tradesman).update(role='tradesman') | |
| self.api = APIClient() | |
| self.api.force_authenticate(self.client_user) | |
| def test_list_without_profile_returns_empty_list_instead_of_500(self): | |
| UserProfile.objects.filter(user=self.client_user).delete() | |
| response = self.api.get('/api/projects/') | |
| self.assertEqual(response.status_code, 200) | |
| self.assertEqual(response.data['results'], []) | |
| def test_client_can_create_project_for_selected_tradesman(self): | |
| response = self.api.post('/api/projects/', { | |
| 'tradesman_id': self.tradesman.pk, | |
| 'title': 'Kitchen renovation', | |
| 'description': 'Replace cabinets and worktops', | |
| 'budget': '1250.00', | |
| 'priority': 'medium', | |
| 'status': 'inquiry', | |
| }, format='json') | |
| self.assertEqual(response.status_code, 201, response.data) | |
| project = Project.objects.get(pk=response.data['id']) | |
| self.assertEqual(project.client_id, self.client_user.pk) | |
| self.assertEqual(project.tradesman_id, self.tradesman.pk) | |
| def test_client_without_selected_tradesman_gets_validation_error_not_500(self): | |
| response = self.api.post('/api/projects/', { | |
| 'title': 'Unassigned project', | |
| 'description': 'A project without a provider', | |
| 'budget': '100.00', | |
| 'priority': 'medium', | |
| 'status': 'inquiry', | |
| }, format='json') | |
| self.assertEqual(response.status_code, 400) | |
| self.assertIn('tradesman_id', response.data) | |
| def test_user_cannot_list_other_users_projects(self): | |
| Project.objects.create( | |
| tradesman=self.tradesman, | |
| client=self.other_user, | |
| title='Private project', | |
| description='Not visible to the current client', | |
| ) | |
| response = self.api.get('/api/projects/') | |
| self.assertEqual(response.status_code, 200) | |
| self.assertEqual(response.data['results'], []) | |
| def test_tradesman_can_create_project_under_own_account(self): | |
| self.api.force_authenticate(self.tradesman) | |
| response = self.api.post('/api/projects/', { | |
| 'title': 'Workshop repair', | |
| 'description': 'Repair workshop equipment', | |
| 'budget': '250.00', | |
| 'priority': 'high', | |
| 'status': 'inquiry', | |
| }, format='json') | |
| self.assertEqual(response.status_code, 201, response.data) | |
| self.assertEqual(Project.objects.get(pk=response.data['id']).tradesman_id, self.tradesman.pk) | |