Skip to content

Commit a4fd56a

Browse files
tests: adding test for process_individual_request
1 parent 58b93fe commit a4fd56a

File tree

2 files changed

+290
-0
lines changed

2 files changed

+290
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Scenario: No pending requests
2+
Given individual_pending_requests is empty
3+
When process_individual_requests is called
4+
Then individual_requests should remain unchanged
5+
6+
Scenario: Single pending request for a club
7+
Given individual_pending_requests contains a single pending request for a club
8+
And club id is "123"
9+
And access_tag is "access-tag-1"
10+
When process_individual_requests is called
11+
Then individual_requests should contain a single entry with module_tag "access-tag-1"
12+
And the entry should have club_id "123"
13+
And the entry should have userEmail, accessReason, accessType, requested_on, sla_breached, accessData
14+
15+
Scenario: Multiple pending requests for a club
16+
Given individual_pending_requests contains multiple pending requests for a club
17+
And club id is "123"
18+
And access_tag is "access-tag-1"
19+
When process_individual_requests is called
20+
Then individual_requests should contain a single entry with module_tag "access-tag-1"
21+
And the entry should have club_id "123"
22+
And the accessData should contain all the pending requests for the club
23+
24+
Scenario: Multiple pending requests for multiple clubs
25+
Given individual_pending_requests contains multiple pending requests for multiple clubs
26+
And access_tag is "access-tag-1"
27+
When process_individual_requests is called
28+
Then individual_requests should contain one entry for each club with module_tag "access-tag-1"
29+
And each entry should have club_id, userEmail, accessReason, accessType, requested_on, sla_breached, accessData
30+
And the accessData for each entry should contain all the pending requests for the corresponding club.
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
"""features/process_individual_request.feature feature tests."""
2+
3+
from pytest_bdd import (
4+
given,
5+
scenario,
6+
then,
7+
when,
8+
)
9+
10+
import pytest
11+
from .. import accessrequest_helper
12+
import Access
13+
from Access import helpers
14+
from Access.models import User
15+
from Access.models import UserAccessMapping
16+
from django.db.models.query import QuerySet
17+
from datetime import datetime, timedelta
18+
19+
@pytest.fixture
20+
def user(mocker):
21+
user = mocker.MagicMock()
22+
user.email = "[email protected]"
23+
user.user.username = "test-user"
24+
return user
25+
26+
@pytest.fixture
27+
def context(mocker):
28+
context = mocker.MagicMock()
29+
return context
30+
31+
@pytest.fixture
32+
def new_request(mocker):
33+
req = mocker.MagicMock()
34+
# req.user.user = mocker.MagicMock(spec=Access.models.User)
35+
# req.user.email = "[email protected]"
36+
return req
37+
38+
39+
40+
@scenario('./features/process_individual_request.feature', 'Multiple pending requests for a club')
41+
def test_multiple_pending_requests_for_a_club():
42+
"""Multiple pending requests for a club."""
43+
pass
44+
45+
46+
@scenario('./features/process_individual_request.feature', 'Multiple pending requests for multiple clubs')
47+
def test_multiple_pending_requests_for_multiple_clubs():
48+
"""Multiple pending requests for multiple clubs."""
49+
pass
50+
51+
52+
@scenario('./features/process_individual_request.feature', 'No pending requests')
53+
def test_no_pending_requests():
54+
"""No pending requests."""
55+
pass
56+
57+
58+
@scenario('./features/process_individual_request.feature', 'Single pending request for a club')
59+
def test_single_pending_request_for_a_club():
60+
"""Single pending request for a club."""
61+
pass
62+
63+
64+
@given('access_tag is "access-tag-1"')
65+
def step_impl(context):
66+
"""access_tag is "access-tag-1"."""
67+
context.access_tag = "access-tag-1"
68+
69+
70+
@given('club id is "123"')
71+
def step_impl(context):
72+
"""club id is "123"."""
73+
context.club_id = "123"
74+
75+
76+
@given('individual_pending_requests contains a single pending request for a club')
77+
def step_impl(context,mocker):
78+
"""individual_pending_requests contains a single pending request for a club."""
79+
context.individual_pending_requests = [{
80+
"requestId": "123_request_1",
81+
"club_id": "123",
82+
"userEmail": "[email protected]",
83+
"accessReason": "test_reason",
84+
"access_desc": "test_access_desc",
85+
"access_tag": "access-tag-1",
86+
"requested_on": datetime.now().isoformat(),
87+
"accessCategory": "test_access_category",
88+
"accessMeta": "test_access_meta"
89+
}]
90+
91+
92+
93+
94+
@given('individual_pending_requests contains multiple pending requests for a club')
95+
def step_impl(context):
96+
"""individual_pending_requests contains multiple pending requests for a club."""
97+
context.individual_pending_requests = [
98+
{
99+
"requestId": "123_1",
100+
"userEmail": "[email protected]",
101+
"accessReason": "reason 1",
102+
"access_desc": "desc 1",
103+
"access_tag": "tag 1",
104+
"requested_on": datetime(2023, 5, 1, 10, 0, 0),
105+
"accessCategory": "category 1",
106+
"accessMeta": "meta 1"
107+
},
108+
{
109+
"requestId": "123_2",
110+
"userEmail": "[email protected]",
111+
"accessReason": "reason 2",
112+
"access_desc": "desc 2",
113+
"access_tag": "tag 2",
114+
"requested_on": datetime(2023, 5, 1, 11, 0, 0),
115+
"accessCategory": "category 2",
116+
"accessMeta": "meta 2"
117+
}
118+
]
119+
120+
121+
@given('individual_pending_requests contains multiple pending requests for multiple clubs')
122+
def step_impl(context):
123+
"""individual_pending_requests contains multiple pending requests for multiple clubs."""
124+
context.individual_pending_requests = [
125+
{
126+
"requestId": "123_request1",
127+
"club_id": "123",
128+
"userEmail": "[email protected]",
129+
"accessReason": "reason1",
130+
"access_desc": "desc1",
131+
"access_tag": "access-tag-1",
132+
"requested_on": "2023-04-29T00:00:00Z",
133+
"accessCategory": "cat1",
134+
"accessMeta": "meta1"
135+
},
136+
{
137+
"requestId": "123_request2",
138+
"club_id": "123",
139+
"userEmail": "[email protected]",
140+
"accessReason": "reason2",
141+
"access_desc": "desc2",
142+
"access_tag": "access-tag-1",
143+
"requested_on": "2023-04-28T00:00:00Z",
144+
"accessCategory": "cat2",
145+
"accessMeta": "meta2"
146+
},
147+
{
148+
"requestId": "456_request1",
149+
"club_id": "456",
150+
"userEmail": "[email protected]",
151+
"accessReason": "reason3",
152+
"access_desc": "desc3",
153+
"access_tag": "access-tag-1",
154+
"requested_on": "2023-04-27T00:00:00Z",
155+
"accessCategory": "cat3",
156+
"accessMeta": "meta3"
157+
}
158+
]
159+
160+
161+
@given('individual_pending_requests is empty')
162+
def step_impl(context):
163+
"""individual_pending_requests is empty."""
164+
context.individual_pending_requests = []
165+
context.access_tag = "access-tag-1"
166+
167+
168+
@when('process_individual_requests is called')
169+
def step_impl(context,mocker):
170+
"""process_individual_requests is called."""
171+
context.individual_requests = []
172+
access_tag = context.access_tag
173+
mocker.patch("Access.helpers.sla_breached",return_value=True)
174+
accessrequest_helper.process_individual_requests(context.individual_pending_requests, context.individual_requests, access_tag)
175+
176+
177+
178+
@then('each entry should have club_id, userEmail, accessReason, accessType, requested_on, sla_breached, accessData')
179+
def step_impl(context):
180+
"""each entry should have club_id, userEmail, accessReason, accessType, requested_on, sla_breached, accessData."""
181+
for request in context.individual_requests:
182+
for club_request in request["requests"]:
183+
assert club_request["club_id"]
184+
assert club_request["userEmail"]
185+
assert club_request["accessReason"]
186+
assert club_request["accessType"]
187+
assert club_request["requested_on"]
188+
assert club_request["sla_breached"]
189+
190+
191+
@then('individual_requests should contain a single entry with module_tag "access-tag-1"')
192+
def step_impl(context):
193+
"""individual_requests should contain a single entry with module_tag "access-tag-1"."""
194+
assert len(context.individual_requests) == 1
195+
assert context.individual_requests[0]["module_tag"] == "access-tag-1"
196+
197+
198+
@then('individual_requests should contain one entry for each club with module_tag "access-tag-1"')
199+
def step_impl(context):
200+
"""individual_requests should contain one entry for each club with module_tag "access-tag-1"."""
201+
assert len(context.individual_requests[0]["requests"]) == 2
202+
club_ids = set(["123", "456"])
203+
for request in context.individual_requests:
204+
assert request["module_tag"] == "access-tag-1"
205+
assert request["requests"]
206+
club_id = request["requests"][0]["club_id"]
207+
assert club_id in club_ids
208+
club_ids.remove(club_id)
209+
210+
211+
@then('individual_requests should remain unchanged')
212+
def step_impl(context):
213+
"""individual_requests should remain unchanged."""
214+
assert len(context.individual_requests) == 0
215+
216+
217+
@then('the accessData for each entry should contain all the pending requests for the corresponding club.')
218+
def step_impl(context):
219+
"""the accessData for each entry should contain all the pending requests for the corresponding club.."""
220+
for request in context.individual_requests:
221+
for club_request in request["requests"]:
222+
club_id = club_request["club_id"]
223+
access_data = club_request["accessData"]
224+
for pending_request in context.individual_pending_requests:
225+
if pending_request["club_id"] == club_id:
226+
assert len(pending_request)== 9
227+
228+
assert len(context.individual_pending_requests) == 3
229+
230+
231+
@then('the accessData should contain all the pending requests for the club')
232+
def step_impl(context):
233+
"""the accessData should contain all the pending requests for the club."""
234+
assert len(context.individual_requests[0]["requests"][0]["accessData"]) == 2
235+
assert context.individual_requests[0]["requests"][0]["accessData"][0]["requestId"] == "123_1"
236+
assert context.individual_requests[0]["requests"][0]["accessData"][1]["requestId"] == "123_2"
237+
238+
239+
@then('the entry should have club_id "123"')
240+
def step_impl(context):
241+
"""the entry should have club_id "123"."""
242+
assert context.individual_requests[0]["requests"][0]["club_id"] == "123"
243+
244+
245+
246+
@then('the entry should have userEmail, accessReason, accessType, requested_on, sla_breached, accessData')
247+
def step_impl(context):
248+
"""the entry should have userEmail, accessReason, accessType, requested_on, sla_breached, accessData."""
249+
request = context.individual_requests[0]["requests"][0]
250+
assert "userEmail" in request
251+
assert "accessReason" in request
252+
assert "accessType" in request
253+
assert "requested_on" in request
254+
assert "sla_breached" in request
255+
assert "accessData" in request
256+
assert len(request["accessData"]) == 1
257+
assert request["accessData"][0]["accessCategory"] == "test_access_category"
258+
assert request["accessData"][0]["accessMeta"] == "test_access_meta"
259+
assert request["accessData"][0]["requestId"] == "123_request_1"
260+

0 commit comments

Comments
 (0)