OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.76k stars 6.56k forks source link

JWT authentication support for k6 Client code generator #19288

Open aakash-dev-maersk opened 3 months ago

aakash-dev-maersk commented 3 months ago

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

mostafa commented 2 months ago

@aakash-dev-maersk It isn't a supported feature at the moment. Feel free to implement it. Note that the bearer token can be a global variable like the BASE_URL.

aakash-dev-maersk commented 2 months ago

@aakash-dev-maersk It isn't a supported feature at the moment. Feel free to implement it. Note that the bearer token can be a global variable like the BASE_URL.

Sure, thanks