-
Notifications
You must be signed in to change notification settings - Fork 51
added e2e tests for authorized endpoint #485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| name: Lightspeed Core Service (LCS) | ||
| service: | ||
| host: 0.0.0.0 | ||
| port: 8080 | ||
| auth_enabled: false | ||
| workers: 1 | ||
| color_log: true | ||
| access_log: true | ||
| llama_stack: | ||
| # Uses a remote llama-stack service | ||
| # The instance would have already been started with a llama-stack-run.yaml file | ||
| use_as_library_client: false | ||
| # Alternative for "as library use" | ||
| # use_as_library_client: true | ||
| # library_client_config_path: <path-to-llama-stack-run.yaml-file> | ||
| url: http://llama-stack:8321 | ||
| api_key: xyzzy | ||
| user_data_collection: | ||
| feedback_enabled: true | ||
| feedback_storage: "/tmp/data/feedback" | ||
| transcripts_enabled: true | ||
| transcripts_storage: "/tmp/data/transcripts" | ||
|
|
||
| authentication: | ||
| module: "noop-with-token" |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| Feature: Authorized endpoint API tests for the noop authentication module | ||
|
|
||
| Background: | ||
| Given The service is started locally | ||
| And REST API service hostname is localhost | ||
| And REST API service port is 8080 | ||
| And REST API service prefix is /v1 | ||
|
|
||
| Scenario: Check if the authorized endpoint works fine when user_id and auth header are not provided | ||
| Given The system is in default state | ||
| When I access endpoint "authorized" using HTTP POST method | ||
radofuchs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| {"placeholder":"abc"} | ||
| """ | ||
| Then The status code of the response is 200 | ||
| And The body of the response is the following | ||
| """ | ||
| {"user_id": "00000000-0000-0000-0000-000","username": "lightspeed-user"} | ||
radofuchs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
|
|
||
| Scenario: Check if the authorized endpoint works when auth token is not provided | ||
| Given The system is in default state | ||
| When I access endpoint "authorized" using HTTP POST method with user_id "test_user" | ||
| Then The status code of the response is 200 | ||
| And The body of the response is the following | ||
| """ | ||
| {"user_id": "test_user","username": "lightspeed-user"} | ||
| """ | ||
|
|
||
| Scenario: Check if the authorized endpoint works when user_id is not provided | ||
| Given The system is in default state | ||
| And I set the Authorization header to Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva | ||
| When I access endpoint "authorized" using HTTP POST method without user_id | ||
| Then The status code of the response is 200 | ||
| And The body of the response is the following | ||
| """ | ||
| {"user_id": "00000000-0000-0000-0000-000","username": "lightspeed-user"} | ||
| """ | ||
|
|
||
| Scenario: Check if the authorized endpoint works when providing empty user_id | ||
| Given The system is in default state | ||
| And I set the Authorization header to Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva | ||
| When I access endpoint "authorized" using HTTP POST method with user_id "" | ||
| Then The status code of the response is 200 | ||
| And The body of the response is the following | ||
| """ | ||
| {"user_id": "","username": "lightspeed-user"} | ||
| """ | ||
|
|
||
| Scenario: Check if the authorized endpoint works when providing proper user_id | ||
| Given The system is in default state | ||
| And I set the Authorization header to Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva | ||
| When I access endpoint "authorized" using HTTP POST method with user_id "test_user" | ||
| Then The status code of the response is 200 | ||
| And The body of the response is the following | ||
| """ | ||
| {"user_id": "test_user","username": "lightspeed-user"} | ||
| """ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| @Authorized | ||
| Feature: Authorized endpoint API tests for the noop-with-token | ||
|
|
||
| Background: | ||
| Given The service is started locally | ||
| And REST API service hostname is localhost | ||
| And REST API service port is 8080 | ||
| And REST API service prefix is /v1 | ||
|
|
||
| Scenario: Check if the authorized endpoint fails when user_id and auth header are not provided | ||
| Given The system is in default state | ||
| When I access endpoint "authorized" using HTTP POST method | ||
| """ | ||
| {"placeholder":"abc"} | ||
| """ | ||
| Then The status code of the response is 400 | ||
| And The body of the response is the following | ||
| """ | ||
| {"detail": "No Authorization header found"} | ||
| """ | ||
|
|
||
| Scenario: Check if the authorized endpoint works when user_id is not provided | ||
| Given The system is in default state | ||
| And I set the Authorization header to Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva | ||
| When I access endpoint "authorized" using HTTP POST method without user_id | ||
| Then The status code of the response is 200 | ||
| And The body of the response is the following | ||
| """ | ||
| {"user_id": "00000000-0000-0000-0000-000","username": "lightspeed-user"} | ||
| """ | ||
|
|
||
| Scenario: Check if the authorized endpoint works when providing empty user_id | ||
| Given The system is in default state | ||
| And I set the Authorization header to Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva | ||
| When I access endpoint "authorized" using HTTP POST method with user_id "" | ||
| Then The status code of the response is 200 | ||
| And The body of the response is the following | ||
| """ | ||
| {"user_id": "","username": "lightspeed-user"} | ||
| """ | ||
|
|
||
| Scenario: Check if the authorized endpoint works when providing proper user_id | ||
| Given The system is in default state | ||
| And I set the Authorization header to Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva | ||
| When I access endpoint "authorized" using HTTP POST method with user_id "test_user" | ||
| Then The status code of the response is 200 | ||
| And The body of the response is the following | ||
| """ | ||
| {"user_id": "test_user","username": "lightspeed-user"} | ||
| """ | ||
|
|
||
| Scenario: Check if the authorized endpoint works with proper user_id but bearer token is not present | ||
| Given The system is in default state | ||
| When I access endpoint "authorized" using HTTP POST method with user_id "test_user" | ||
| Then The status code of the response is 400 | ||
| And The body of the response is the following | ||
| """ | ||
| {"detail": "No Authorization header found"} | ||
| """ | ||
|
|
||
| Scenario: Check if the authorized endpoint works when auth token is malformed | ||
| Given The system is in default state | ||
| And I set the Authorization header to BearereyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva | ||
| When I access endpoint "authorized" using HTTP POST method with user_id "test_user" | ||
| Then The status code of the response is 400 | ||
| And The body of the response is the following | ||
| """ | ||
| {"detail": "No token found in Authorization header"} | ||
| """ |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,25 +1,60 @@ | ||||||||||||||||||||||||||||||||
| """Implementation of common test steps.""" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| from behave import given, then # pyright: ignore[reportAttributeAccessIssue] | ||||||||||||||||||||||||||||||||
| import requests | ||||||||||||||||||||||||||||||||
| from behave import given, when # pyright: ignore[reportAttributeAccessIssue] | ||||||||||||||||||||||||||||||||
| from behave.runner import Context | ||||||||||||||||||||||||||||||||
| from tests.e2e.utils.utils import normalize_endpoint | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @then("The body of the response has proper username") | ||||||||||||||||||||||||||||||||
| def check_body_username(context: Context) -> None: | ||||||||||||||||||||||||||||||||
| """Check that the username is correct in response.""" | ||||||||||||||||||||||||||||||||
| # TODO: add step implementation | ||||||||||||||||||||||||||||||||
| assert context is not None | ||||||||||||||||||||||||||||||||
| @given("I set the Authorization header to {header_value}") | ||||||||||||||||||||||||||||||||
| def set_authorization_header_custom(context: Context, header_value: str) -> None: | ||||||||||||||||||||||||||||||||
| """Set a custom Authorization header value.""" | ||||||||||||||||||||||||||||||||
| if not hasattr(context, "auth_headers"): | ||||||||||||||||||||||||||||||||
| context.auth_headers = {} | ||||||||||||||||||||||||||||||||
| context.auth_headers["Authorization"] = header_value | ||||||||||||||||||||||||||||||||
| print(f"🔑 Set Authorization header to: {header_value}") | ||||||||||||||||||||||||||||||||
|
Comment on lines
+9
to
+15
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Don’t log secrets; optionally strip quotes from header value. Avoid printing Authorization values to CI logs; also consider stripping wrapping quotes. Apply: - context.auth_headers["Authorization"] = header_value
- print(f"🔑 Set Authorization header to: {header_value}")
+ header_value = header_value.strip('"')
+ context.auth_headers["Authorization"] = header_value
+ print("🔑 Authorization header set.")📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @given("I remove the auth header") | ||||||||||||||||||||||||||||||||
| def remove_auth_header(context: Context) -> None: | ||||||||||||||||||||||||||||||||
| """Remove the auth header.""" | ||||||||||||||||||||||||||||||||
| # TODO: add step implementation | ||||||||||||||||||||||||||||||||
| assert context is not None | ||||||||||||||||||||||||||||||||
| @when("I access endpoint {endpoint} using HTTP POST method with user_id {user_id}") | ||||||||||||||||||||||||||||||||
| def access_rest_api_endpoint_post( | ||||||||||||||||||||||||||||||||
| context: Context, endpoint: str, user_id: str | ||||||||||||||||||||||||||||||||
| ) -> None: | ||||||||||||||||||||||||||||||||
| """Send POST HTTP request with payload in the endpoint as parameter to tested service. | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| The response is stored in `context.response` attribute. | ||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
| endpoint = normalize_endpoint(endpoint) | ||||||||||||||||||||||||||||||||
| user_id = user_id.replace('"', "") | ||||||||||||||||||||||||||||||||
| base = f"http://{context.hostname}:{context.port}" | ||||||||||||||||||||||||||||||||
| path = f"{endpoint}?user_id={user_id}".replace("//", "/") | ||||||||||||||||||||||||||||||||
| url = base + path | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @given("I modify the auth header so that the user is it authorized") | ||||||||||||||||||||||||||||||||
| def modify_auth_header(context: Context) -> None: | ||||||||||||||||||||||||||||||||
| """Modify the auth header making the user unauthorized.""" | ||||||||||||||||||||||||||||||||
| # TODO: add step implementation | ||||||||||||||||||||||||||||||||
| assert context is not None | ||||||||||||||||||||||||||||||||
| if not hasattr(context, "auth_headers"): | ||||||||||||||||||||||||||||||||
| context.auth_headers = {} | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # perform REST API call | ||||||||||||||||||||||||||||||||
| context.response = requests.post( | ||||||||||||||||||||||||||||||||
| url, json="", headers=context.auth_headers, timeout=10 | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @when("I access endpoint {endpoint} using HTTP POST method without user_id") | ||||||||||||||||||||||||||||||||
| def access_rest_api_endpoint_post_without_param( | ||||||||||||||||||||||||||||||||
| context: Context, endpoint: str | ||||||||||||||||||||||||||||||||
| ) -> None: | ||||||||||||||||||||||||||||||||
| """Send POST HTTP request without user_id payload. | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| The response is stored in `context.response` attribute. | ||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
| endpoint = normalize_endpoint(endpoint) | ||||||||||||||||||||||||||||||||
| base = f"http://{context.hostname}:{context.port}" | ||||||||||||||||||||||||||||||||
| path = f"{endpoint}".replace("//", "/") | ||||||||||||||||||||||||||||||||
| url = base + path | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if not hasattr(context, "auth_headers"): | ||||||||||||||||||||||||||||||||
| context.auth_headers = {} | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # perform REST API call | ||||||||||||||||||||||||||||||||
| context.response = requests.post( | ||||||||||||||||||||||||||||||||
| url, json="", headers=context.auth_headers, timeout=10 | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
radofuchs marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.