Skip to content

Commit 9dc09e2

Browse files
New: [AEA-5519] - Add test support step to logout of all sessions, incl. concurrent before running (#404)
## Summary - ✨ New Feature ### Details Add test support step to logout of all sessions, incl. concurrent before running. This is to ensure that subsequent tests in the linear running, are not still logged in and affected by any previous session state or concurrency conditions. --------- Signed-off-by: Connor Avery <[email protected]> Co-authored-by: MatthewPopat-NHS <[email protected]>
1 parent eedca2c commit 9dc09e2

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

features/environment.py

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
import os
33
import shutil
44
import sys
5-
5+
import uuid
66
from behave.model import Scenario
77
from dotenv import load_dotenv
88
from playwright.sync_api import sync_playwright, expect
99
from methods.api import eps_api_methods
1010
import allure
11+
import requests
12+
import json
1113

1214
load_dotenv(override=True)
1315
global _page
@@ -118,6 +120,15 @@
118120
# this is not currently used
119121
MOCK_CIS2_LOGIN_ID_NO_ROLES = "555073103101"
120122

123+
account_scenario_tags = {
124+
"multiple_access": MOCK_CIS2_LOGIN_ID_MULTIPLE_ACCESS_ROLES,
125+
"multiple_access_pre_selected": MOCK_CIS2_LOGIN_ID_MULTIPLE_ACCESS_ROLES_WITH_SELECTED_ROLE,
126+
"single_access": MOCK_CIS2_LOGIN_ID_SINGLE_ACCESS_ROLE,
127+
"multiple_roles_single_access": MOCK_CIS2_LOGIN_ID_SINGLE_ROLE_WITH_ACCESS_MULTIPLE_WITHOUT,
128+
"multiple_roles_no_access": MOCK_CIS2_LOGIN_ID_NO_ACCESS_ROLE,
129+
"no_roles_no_access": MOCK_CIS2_LOGIN_ID_NO_ROLES,
130+
}
131+
121132
REPOS = {
122133
"CPTS-UI": "https://github.com/NHSDigital/eps-prescription-tracker-ui",
123134
"CPTS-FHIR": "https://github.com/NHSDigital/electronic-prescription-service-clinical-prescription-tracker",
@@ -146,6 +157,48 @@
146157
PSU_SUFFIX = "prescription-status-update"
147158

148159

160+
class ConflictException(Exception):
161+
pass
162+
163+
164+
class TestingSupportFailure(Exception):
165+
pass
166+
167+
168+
def clear_scenario_user_sessions(context, scenario_tags):
169+
conflict_tags = set(account_scenario_tags)
170+
171+
conflict = conflict_tags.intersection(scenario_tags)
172+
if len(conflict) > 1:
173+
raise ConflictException(
174+
f"You're attempting to use conflicting account credential tags in scenario {context.scenario.name}"
175+
)
176+
177+
for tag in scenario_tags:
178+
for key, value in account_scenario_tags.items():
179+
if tag == key:
180+
request_id = str(uuid.uuid4())
181+
print(
182+
f"Logging out all sessions for Mock_{value} ahead of running {context.scenario.name}.\
183+
Request ID: {request_id}"
184+
)
185+
payload = json.dumps(
186+
{"username": "Mock_" + value, "request_id": request_id}
187+
)
188+
# Not catching any exceptions, we want this to raise a stack if it doesn't work
189+
response = requests.post(
190+
f"{context.cpts_ui_base_url}/api/test-support-clear-active-session",
191+
data=payload,
192+
headers={
193+
"Source": f"{context.scenario.name}",
194+
},
195+
timeout=60,
196+
)
197+
if response.json()["message"] != "Success":
198+
print(response)
199+
raise TestingSupportFailure("Failed to clear active sessions")
200+
201+
149202
def count_of_scenarios_to_run(context):
150203
tags = context.config.tags
151204
total_scenarios = sum(
@@ -183,6 +236,8 @@ def before_scenario(context, scenario):
183236
return
184237
product = context.config.userdata["product"].upper()
185238
if product == "CPTS-UI":
239+
clear_scenario_user_sessions(context, scenario.effective_tags)
240+
186241
global _playwright # noqa: F824
187242
global _page # noqa:
188243
expect.set_options(timeout=10_000)
@@ -262,7 +317,7 @@ def before_all(context):
262317
else:
263318
print("no tests to run. Check your tags and try again")
264319
sys.exit(0)
265-
print(f"arm64: {context.config.userdata["arm64"]}")
320+
print(f"Run using arm64 version of Chromium: {context.config.userdata['arm64']}")
266321
if product == "CPTS-UI":
267322
global _playwright
268323
_playwright = sync_playwright().start()

methods/api/common_api_methods.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77

88
def get(context, **kwargs):
9-
logging.debug(f"Request Body: {kwargs.get("data")}")
9+
logging.debug(f"Request Body: {kwargs.get('data')}")
1010
context.response = api_get_request(**kwargs)
1111
logging.debug(f"Response Body: {context.response.content}")
1212
common.attach_api_information(context)
1313
return context.response
1414

1515

1616
def post(context, **kwargs):
17-
logging.debug(f"Request Body: {kwargs.get("data")}")
17+
logging.debug(f"Request Body: {kwargs.get('data')}")
1818
context.response = api_post_request(**kwargs)
1919
logging.debug(f"Response Body: {context.response.content}")
2020
common.attach_api_information(context)

0 commit comments

Comments
 (0)