OpenAPITools / OpenAPITools/openapi-generator
JWT authentication support for k6 Client code generator
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Is your feature request related to a problem? Please describe.
Authorization code not getting generated for bearer auth in k6 test script
openapi: 3.0.0
info:
title: Sample API
description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
version: 0.1.9
servers:
- url: http://api.example.com/v1
description: Optional server description, e.g. Main (production) server
- url: http://staging-api.example.com
description: Optional server description, e.g. Internal staging server for testing
security:
- bearerAuth: []
paths:
/users:
get:
summary: Returns a list of users.
description: Optional extended description in CommonMark or HTML.
responses:
"200": # status code
description: A JSON array of user names
content:
application/json:
schema:
type: array
items:
type: string
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT # Optional: specify the format (e.g., JWT) if applicable
The output for the above spec after running the test generator, I ran this with the latest pull from master which has the global auth support, no auth code is added to the test script
import http from "k6/http";
import { group, check, sleep } from "k6";
const BASE_URL = "http://api.example.com/v1";
// Sleep duration between successive requests.
// You might want to edit the value of this variable or remove calls to the sleep function on the script.
const SLEEP_DURATION = 0.1;
// Global variables should be initialized.
export default function() {
group("/users", () => {
// Request No. 1:
{
let url = BASE_URL + `/users`;
let request = http.get(url);
check(request, {
"A JSON array of user names": (r) => r.status === 200
});
}
});
}
Describe the solution you'd like
Expectation
Test script with auth code added as well
import http from "k6/http";
import { group, check, sleep } from "k6";
// Define the base URL for the API
const BASE_URL = "http://api.example.com/v1";
export default function(setupData) {
group("/v1/test_suites/{testSuiteId}/environments", () => {
// Retrieve the test suite ID from the setup data
let testSuiteId = setupData.testSuiteId;
// Request No. 1: getAllEnvironmentsForTestSuite
{
let url = `${BASE_URL}/v1/test_suites/${testSuiteId}/environments`;
let params = {
headers: {
"Authorization": `Bearer ${setupData.accessToken}`, // Use the access token from setupData
"Content-Type": "application/json" // Optional, depending on the API's requirements
}
};
let request = http.get(url, params);
// Check if the response status is 200 OK
check(request, {
"OK": (r) => r.status === 200
});
}
sleep(0.1); // Sleep between requests if needed
});
}
I am interested in working on a solution for this issue. Could you please confirm if it’s okay for me to start on it? Or there is something that I am missing here. Also If there are any guidelines or specific details you’d like me to follow, please let me know. @mostafa @wing328
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the k6 Client code generator and reproduce the supplied OpenAPI example with its test generator. Trace how the global bearerAuth security scheme is handled, then verify that the generated k6 script includes the expected bearer Authorization code and add or update the relevant generator test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, javascript
- Domain
- api, tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100