Spaces:
Running
Running
File size: 3,323 Bytes
a6be783 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | 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)
|