This repository was archived by the owner on Nov 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_consumer.py
62 lines (54 loc) · 2.07 KB
/
test_consumer.py
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
from django.contrib.auth import get_user_model
from rest_framework import status
from rest_framework.test import APITestCase
from api_bouncer.models import Consumer
User = get_user_model()
class ConsumerTests(APITestCase):
def setUp(self):
self.superuser = User.objects.create_superuser(
'john',
'john@localhost.local',
'john123john'
)
self.user = User.objects.create_user(
'jane',
'jane@localhost.local',
'jane123jane'
)
self.url = '/consumers/'
def test_create_consumer_ok(self):
"""
Ensure we can create a new consumer object.
"""
data = {
'username': 'django',
}
self.client.login(username='john', password='john123john')
response = self.client.post(self.url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Consumer.objects.count(), 1)
self.assertEqual(Consumer.objects.get().username, 'django')
def test_create_consumer_403(self):
"""
Ensure we can create a new consumer object only as superuser.
"""
data = {
'username': 'django',
}
self.client.login(username='jane', password='jane123jane')
response = self.client.post(self.url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_duplicate_consumer(self):
"""
Ensure we can't duplicate consumer object.
"""
data = {
'username': 'django',
}
self.client.login(username='john', password='john123john')
response = self.client.post(self.url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Consumer.objects.count(), 1)
self.assertEqual(Consumer.objects.get().username, 'django')
response = self.client.post(self.url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)