Skip to content

Commit 8eeb5e4

Browse files
authored
Merge pull request #138 from browserstack/test_individual_pending_requests
tests: adding test for process_individual_request
2 parents 10a0b94 + d875ff9 commit 8eeb5e4

File tree

2 files changed

+318
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)