|
1 | 1 | """Implementation of common test steps.""" |
2 | 2 |
|
3 | | -from behave import given, then # pyright: ignore[reportAttributeAccessIssue] |
| 3 | +import requests |
| 4 | +from behave import given, when # pyright: ignore[reportAttributeAccessIssue] |
4 | 5 | from behave.runner import Context |
| 6 | +from tests.e2e.utils.utils import normalize_endpoint |
5 | 7 |
|
6 | 8 |
|
7 | | -@then("The body of the response has proper username") |
8 | | -def check_body_username(context: Context) -> None: |
9 | | - """Check that the username is correct in response.""" |
10 | | - # TODO: add step implementation |
11 | | - assert context is not None |
| 9 | +@given("I set the Authorization header to {header_value}") |
| 10 | +def set_authorization_header_custom(context: Context, header_value: str) -> None: |
| 11 | + """Set a custom Authorization header value.""" |
| 12 | + if not hasattr(context, "auth_headers"): |
| 13 | + context.auth_headers = {} |
| 14 | + context.auth_headers["Authorization"] = header_value |
| 15 | + print(f"🔑 Set Authorization header to: {header_value}") |
12 | 16 |
|
13 | 17 |
|
14 | | -@given("I remove the auth header") |
15 | | -def remove_auth_header(context: Context) -> None: |
16 | | - """Remove the auth header.""" |
17 | | - # TODO: add step implementation |
18 | | - assert context is not None |
| 18 | +@when("I access endpoint {endpoint} using HTTP POST method with user_id {user_id}") |
| 19 | +def access_rest_api_endpoint_post( |
| 20 | + context: Context, endpoint: str, user_id: str |
| 21 | +) -> None: |
| 22 | + """Send POST HTTP request with payload in the endpoint as parameter to tested service. |
19 | 23 |
|
| 24 | + The response is stored in `context.response` attribute. |
| 25 | + """ |
| 26 | + endpoint = normalize_endpoint(endpoint) |
| 27 | + user_id = user_id.replace('"', "") |
| 28 | + base = f"http://{context.hostname}:{context.port}" |
| 29 | + path = f"{endpoint}?user_id={user_id}".replace("//", "/") |
| 30 | + url = base + path |
20 | 31 |
|
21 | | -@given("I modify the auth header so that the user is it authorized") |
22 | | -def modify_auth_header(context: Context) -> None: |
23 | | - """Modify the auth header making the user unauthorized.""" |
24 | | - # TODO: add step implementation |
25 | | - assert context is not None |
| 32 | + if not hasattr(context, "auth_headers"): |
| 33 | + context.auth_headers = {} |
| 34 | + |
| 35 | + # perform REST API call |
| 36 | + context.response = requests.post( |
| 37 | + url, json="", headers=context.auth_headers, timeout=10 |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +@when("I access endpoint {endpoint} using HTTP POST method without user_id") |
| 42 | +def access_rest_api_endpoint_post_without_param( |
| 43 | + context: Context, endpoint: str |
| 44 | +) -> None: |
| 45 | + """Send POST HTTP request without user_id payload. |
| 46 | +
|
| 47 | + The response is stored in `context.response` attribute. |
| 48 | + """ |
| 49 | + endpoint = normalize_endpoint(endpoint) |
| 50 | + base = f"http://{context.hostname}:{context.port}" |
| 51 | + path = f"{endpoint}".replace("//", "/") |
| 52 | + url = base + path |
| 53 | + |
| 54 | + if not hasattr(context, "auth_headers"): |
| 55 | + context.auth_headers = {} |
| 56 | + |
| 57 | + # perform REST API call |
| 58 | + context.response = requests.post( |
| 59 | + url, json="", headers=context.auth_headers, timeout=10 |
| 60 | + ) |
0 commit comments