|
1 | 1 | from django.contrib.auth.models import User
|
| 2 | +from model_mommy import mommy |
2 | 3 | from rest_framework import status
|
3 | 4 | from rest_framework.test import APITestCase
|
4 | 5 | from . import const
|
5 | 6 |
|
6 | 7 | from .models import Cfp
|
7 | 8 |
|
8 |
| -CFP_URL = '/api/cfp/' |
| 9 | +CFP_URL = "/api/cfp/" |
9 | 10 |
|
10 | 11 |
|
11 | 12 | class CfpAuthApiTest(APITestCase):
|
12 | 13 | """Test that cfp service endpoint permissions are setup correctly."""
|
13 | 14 |
|
14 | 15 | def setUp(self):
|
15 | 16 | # Create CFP
|
16 |
| - self.cfp = Cfp.objects.create( |
17 |
| - name='test', |
18 |
| - |
19 |
| - title='Test', |
20 |
| - duration='30min', |
21 |
| - description='Test', |
22 |
| - type=const.TALK |
| 17 | + self.conference = mommy.make('conference.Conference') |
| 18 | + self.cfp = mommy.make( |
| 19 | + Cfp, |
| 20 | + name="test", |
| 21 | + |
| 22 | + title="Test", |
| 23 | + duration="30min", |
| 24 | + description="Test", |
| 25 | + conference=self.conference, |
| 26 | + type=const.TALK, |
23 | 27 | )
|
24 | 28 |
|
25 | 29 | # Create user
|
26 |
| - self. user = User. objects. create_superuser( username='test', password='1234', email='[email protected]') |
| 30 | + self.user = User.objects.create_superuser( |
| 31 | + username="test", password="1234", email="[email protected]" |
| 32 | + ) |
27 | 33 |
|
28 | 34 | def test_list_returns_error_for_unauthenticated_user(self):
|
29 | 35 | """Test that unauthenticated user can't use CFP API endpoint."""
|
30 | 36 |
|
31 | 37 | result = self.client.get(CFP_URL)
|
32 |
| - self.assertEqual(status.HTTP_403_FORBIDDEN, result.status_code, 'Status code should be 403 for list.') |
| 38 | + self.assertEqual( |
| 39 | + status.HTTP_403_FORBIDDEN, |
| 40 | + result.status_code, |
| 41 | + "Status code should be 403 for list.", |
| 42 | + ) |
33 | 43 |
|
34 | 44 | def test_create_returns_error_for_unauthenticated_user(self):
|
35 | 45 | """Test that unauthenticated user can't use CFP API endpoint."""
|
36 | 46 |
|
37 |
| - result = self.client.post(CFP_URL, { |
38 |
| - 'name': 'test1', |
39 |
| - |
40 |
| - 'title': 'Test1', |
41 |
| - 'duration': 'Test1', |
42 |
| - 'description': 'Test', |
43 |
| - 'type': const.TALK, |
44 |
| - }) |
45 |
| - self.assertEqual(status.HTTP_403_FORBIDDEN, result.status_code, 'Status code should be 403 for list.') |
| 47 | + result = self.client.post( |
| 48 | + CFP_URL, |
| 49 | + { |
| 50 | + "name": "test1", |
| 51 | + |
| 52 | + "title": "Test1", |
| 53 | + "duration": "Test1", |
| 54 | + "description": "Test", |
| 55 | + "type": const.TALK, |
| 56 | + }, |
| 57 | + ) |
| 58 | + self.assertEqual( |
| 59 | + status.HTTP_403_FORBIDDEN, |
| 60 | + result.status_code, |
| 61 | + "Status code should be 403 for list.", |
| 62 | + ) |
46 | 63 |
|
47 | 64 | def test_details_returns_error_for_unauthenticated_user(self):
|
48 | 65 | """Test that unauthenticated user can't use CFP API endpoint."""
|
49 | 66 |
|
50 |
| - result = self.client.get(CFP_URL + '{}/'.format(self.cfp.id)) |
51 |
| - self.assertEqual(status.HTTP_403_FORBIDDEN, result.status_code, 'Status code should be 403 for list.') |
| 67 | + result = self.client.get(CFP_URL + "{}/".format(self.cfp.id)) |
| 68 | + self.assertEqual( |
| 69 | + status.HTTP_403_FORBIDDEN, |
| 70 | + result.status_code, |
| 71 | + "Status code should be 403 for list.", |
| 72 | + ) |
52 | 73 |
|
53 | 74 | def test_update_returns_error_for_unauthenticated_user(self):
|
54 | 75 | """Test that unauthenticated user can't use CFP API endpoint."""
|
55 | 76 |
|
56 |
| - result = self.client.patch(CFP_URL + '{}/'.format(self.cfp.id)) |
57 |
| - self.assertEqual(status.HTTP_403_FORBIDDEN, result.status_code, 'Status code should be 403 for list.') |
| 77 | + result = self.client.patch(CFP_URL + "{}/".format(self.cfp.id)) |
| 78 | + self.assertEqual( |
| 79 | + status.HTTP_403_FORBIDDEN, |
| 80 | + result.status_code, |
| 81 | + "Status code should be 403 for list.", |
| 82 | + ) |
58 | 83 |
|
59 | 84 | def test_delete_returns_error_for_unauthenticated_user(self):
|
60 | 85 | """Test that unauthenticated user can't use CFP API endpoint."""
|
61 | 86 |
|
62 |
| - result = self.client.delete(CFP_URL + '{}/'.format(self.cfp.id)) |
63 |
| - self.assertEqual(status.HTTP_403_FORBIDDEN, result.status_code, 'Status code should be 403 for list.') |
| 87 | + result = self.client.delete(CFP_URL + "{}/".format(self.cfp.id)) |
| 88 | + self.assertEqual( |
| 89 | + status.HTTP_403_FORBIDDEN, |
| 90 | + result.status_code, |
| 91 | + "Status code should be 403 for list.", |
| 92 | + ) |
64 | 93 |
|
65 | 94 | def test_list_returns_success_for_unauthenticated_user(self):
|
66 | 95 | """Test that authenticated user can use CFP API endpoint."""
|
67 | 96 | self.client.force_login(self.user)
|
68 | 97 |
|
69 | 98 | result = self.client.get(CFP_URL)
|
70 |
| - self.assertEqual(status.HTTP_200_OK, result.status_code, 'Status code should be 200 for list.') |
| 99 | + self.assertEqual( |
| 100 | + status.HTTP_200_OK, |
| 101 | + result.status_code, |
| 102 | + "Status code should be 200 for list.", |
| 103 | + ) |
71 | 104 |
|
72 | 105 | def test_create_returns_success_for_authenticated_user(self):
|
73 | 106 | """Test that authenticated user can use CFP API endpoint."""
|
74 | 107 | self.client.force_login(self.user)
|
75 | 108 |
|
76 |
| - result = self.client.post(CFP_URL, { |
77 |
| - 'name': 'test1', |
78 |
| - |
79 |
| - 'title': 'Test1', |
80 |
| - 'duration': 'Test1', |
81 |
| - 'description': 'Test', |
82 |
| - 'type': const.TALK, |
83 |
| - }) |
84 |
| - self.assertEqual(status.HTTP_201_CREATED, result.status_code, 'Status code should be 201 for create.') |
| 109 | + result = self.client.post( |
| 110 | + CFP_URL, |
| 111 | + { |
| 112 | + "name": "test1", |
| 113 | + |
| 114 | + "title": "Test1", |
| 115 | + "duration": "Test1", |
| 116 | + "description": "Test", |
| 117 | + "type": const.TALK, |
| 118 | + }, |
| 119 | + ) |
| 120 | + self.assertEqual( |
| 121 | + status.HTTP_201_CREATED, |
| 122 | + result.status_code, |
| 123 | + "Status code should be 201 for create.", |
| 124 | + ) |
85 | 125 |
|
86 | 126 | def test_details_returns_success_for_authenticated_user(self):
|
87 | 127 | """Test that authenticated user can use CFP API endpoint."""
|
88 | 128 | self.client.force_login(self.user)
|
89 | 129 |
|
90 |
| - result = self.client.get(CFP_URL + '{}/'.format(self.cfp.id)) |
91 |
| - self.assertEqual(status.HTTP_200_OK, result.status_code, 'Status code should be 200 for details.') |
| 130 | + result = self.client.get(CFP_URL + "{}/".format(self.cfp.id)) |
| 131 | + self.assertEqual( |
| 132 | + status.HTTP_200_OK, |
| 133 | + result.status_code, |
| 134 | + "Status code should be 200 for details.", |
| 135 | + ) |
92 | 136 |
|
93 | 137 | def test_update_returns_success_for_authenticated_user(self):
|
94 | 138 | """Test that authenticated user can use CFP API endpoint."""
|
95 | 139 | self.client.force_login(self.user)
|
96 | 140 |
|
97 |
| - result = self.client.patch(CFP_URL + '{}/'.format(self.cfp.id)) |
98 |
| - self.assertEqual(status.HTTP_200_OK, result.status_code, 'Status code should be 200 for update.') |
| 141 | + result = self.client.patch(CFP_URL + "{}/".format(self.cfp.id)) |
| 142 | + self.assertEqual( |
| 143 | + status.HTTP_200_OK, |
| 144 | + result.status_code, |
| 145 | + "Status code should be 200 for update.", |
| 146 | + ) |
99 | 147 |
|
100 | 148 | def test_delete_returns_success_for_authenticated_user(self):
|
101 | 149 | """Test that authenticated user can use CFP API endpoint."""
|
102 | 150 | self.client.force_login(self.user)
|
103 | 151 |
|
104 |
| - result = self.client.delete(CFP_URL + '{}/'.format(self.cfp.id)) |
105 |
| - self.assertEqual(status.HTTP_204_NO_CONTENT, result.status_code, 'Status code should be 204 for delete.') |
| 152 | + result = self.client.delete(CFP_URL + "{}/".format(self.cfp.id)) |
| 153 | + self.assertEqual( |
| 154 | + status.HTTP_204_NO_CONTENT, |
| 155 | + result.status_code, |
| 156 | + "Status code should be 204 for delete.", |
| 157 | + ) |
0 commit comments