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_middleware_acl.py
150 lines (133 loc) · 5.02 KB
/
test_middleware_acl.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from django.contrib.auth import get_user_model
from rest_framework import status
from rest_framework.test import APITestCase
from api_bouncer.models import Api, Consumer, ConsumerACL
User = get_user_model()
class ACLMiddlewareTests(APITestCase):
def setUp(self):
self.superuser = User.objects.create_superuser(
'john',
'john@localhost.local',
'john123john'
)
self.example_api = Api.objects.create(
name='httpbin',
hosts=['httpbin.org'],
upstream_url='https://httpbin.org'
)
self.url = '/apis/{}/plugins/'.format(self.example_api.name)
self.consumer = Consumer.objects.create(username='django')
self.acl = ConsumerACL.objects.create(
consumer=self.consumer,
group='group1'
)
def test_cant_set_whitelist_and_blacklist(self):
"""
Ensure we can't set blacklist and whitelist on the same configuration
"""
self.client.login(username='john', password='john123john')
data = {
'name': 'acl',
'config': {
'whitelist': ['group1', 'group2'],
'blacklist': ['group3', ],
}
}
response = self.client.post(self.url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
response.data['config'],
['Whitelist and blacklist are mutually exclusive']
)
def test_whitelist_block_request(self):
"""
Ensure we block requests from groups not included in whitelist
"""
self.client.login(username='john', password='john123john')
data_ok = {
'name': 'acl',
'config': {
'whitelist': ['nogroup'],
}
}
response = self.client.post(self.url, data_ok, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.client.credentials(
HTTP_HOST='httpbin.org',
HTTP_CONSUMER_ID=str(self.consumer.id)
)
response = self.client.get('/get')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_whitelist_allow_request(self):
"""
Ensure we allow requests from groups included in whitelist
"""
self.client.login(username='john', password='john123john')
data_ok = {
'name': 'acl',
'config': {
'whitelist': ['group1', 'group2', 'anyone', ],
}
}
response = self.client.post(self.url, data_ok, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.client.credentials(
HTTP_HOST='httpbin.org',
HTTP_CONSUMER_ID=str(self.consumer.id)
)
response = self.client.get('/get')
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_blacklist_block_request(self):
"""
Ensure we block requests from groups included in blacklist
"""
self.client.login(username='john', password='john123john')
data_ok = {
'name': 'acl',
'config': {
'blacklist': ['group1', 'anyone'],
}
}
response = self.client.post(self.url, data_ok, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.client.credentials(
HTTP_HOST='httpbin.org',
HTTP_CONSUMER_ID=str(self.consumer.id)
)
response = self.client.get('/get')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_blacklist_allow_request(self):
"""
Ensure we allow requests from groups not included in blacklist
"""
self.client.login(username='john', password='john123john')
data_ok = {
'name': 'acl',
'config': {
'blacklist': ['noone', 'another_group'],
}
}
response = self.client.post(self.url, data_ok, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.client.credentials(
HTTP_HOST='httpbin.org',
HTTP_CONSUMER_ID=str(self.consumer.id)
)
response = self.client.get('/get')
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_block_request_without_consumer(self):
"""
Ensure we block requests from groups included in blacklist
"""
self.client.login(username='john', password='john123john')
data_ok = {
'name': 'acl',
'config': {
'whitelist': ['group1', 'anyone'],
}
}
response = self.client.post(self.url, data_ok, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.client.credentials(HTTP_HOST='httpbin.org')
response = self.client.get('/get')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)