|
2 | 2 | import os |
3 | 3 | import shutil |
4 | 4 | import sys |
5 | | - |
| 5 | +import uuid |
6 | 6 | from behave.model import Scenario |
7 | 7 | from dotenv import load_dotenv |
8 | 8 | from playwright.sync_api import sync_playwright, expect |
9 | 9 | from methods.api import eps_api_methods |
10 | 10 | import allure |
| 11 | +import requests |
| 12 | +import json |
11 | 13 |
|
12 | 14 | load_dotenv(override=True) |
13 | 15 | global _page |
|
118 | 120 | # this is not currently used |
119 | 121 | MOCK_CIS2_LOGIN_ID_NO_ROLES = "555073103101" |
120 | 122 |
|
| 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 | + |
121 | 132 | REPOS = { |
122 | 133 | "CPTS-UI": "https://github.com/NHSDigital/eps-prescription-tracker-ui", |
123 | 134 | "CPTS-FHIR": "https://github.com/NHSDigital/electronic-prescription-service-clinical-prescription-tracker", |
|
146 | 157 | PSU_SUFFIX = "prescription-status-update" |
147 | 158 |
|
148 | 159 |
|
| 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 | + |
149 | 202 | def count_of_scenarios_to_run(context): |
150 | 203 | tags = context.config.tags |
151 | 204 | total_scenarios = sum( |
@@ -183,6 +236,8 @@ def before_scenario(context, scenario): |
183 | 236 | return |
184 | 237 | product = context.config.userdata["product"].upper() |
185 | 238 | if product == "CPTS-UI": |
| 239 | + clear_scenario_user_sessions(context, scenario.effective_tags) |
| 240 | + |
186 | 241 | global _playwright # noqa: F824 |
187 | 242 | global _page # noqa: |
188 | 243 | expect.set_options(timeout=10_000) |
@@ -262,7 +317,7 @@ def before_all(context): |
262 | 317 | else: |
263 | 318 | print("no tests to run. Check your tags and try again") |
264 | 319 | sys.exit(0) |
265 | | - print(f"arm64: {context.config.userdata["arm64"]}") |
| 320 | + print(f"Run using arm64 version of Chromium: {context.config.userdata['arm64']}") |
266 | 321 | if product == "CPTS-UI": |
267 | 322 | global _playwright |
268 | 323 | _playwright = sync_playwright().start() |
|
0 commit comments