Back to Repositories

Testing Authentication Transformation Methods in Insomnia SDK

This test suite validates authentication functionality in the Insomnia SDK, focusing on various auth methods and their transformations. It ensures proper handling of different authentication types including Basic, Bearer, OAuth, AWS, and more.

Test Coverage Overview

The test suite provides comprehensive coverage of authentication mechanisms in the Insomnia SDK.

  • Tests RequestAuth class methods including validation, parameter handling, and state management
  • Verifies transformation logic between Insomnia’s internal auth format and SDK representation
  • Covers all supported auth types: Basic, Bearer, OAuth1/2, AWS IAM, HAWK, NTLM, etc.
  • Tests edge cases like disabled states and optional parameters

Implementation Analysis

The testing approach uses Jest’s describe/it blocks with focused unit tests for auth transformations. The implementation leverages TypeScript’s type system for auth options and employs utility functions for variable list conversions.

  • Modular test structure with separate concerns for methods and transformations
  • Strong type checking with TypeScript interfaces
  • Utility functions for data transformation and validation

Technical Details

  • Testing Framework: Jest/Vitest
  • Language: TypeScript
  • Key Classes: RequestAuth, AuthOptions
  • Helper Functions: varListToObject, toPreRequestAuth, fromPreRequestAuth
  • Type Definitions: Variable, VariableList interfaces

Best Practices Demonstrated

The test suite exemplifies strong testing practices with clear organization and thorough validation.

  • Comprehensive test cases for each auth type
  • Isolated test scenarios for different functionalities
  • Consistent assertion patterns
  • Type-safe testing approaches
  • Modular test organization

kong/insomnia

packages/insomnia-sdk/src/objects/__tests__/auth.test.ts

            
import { describe, expect, it } from 'vitest';

import { type AuthOptions, fromPreRequestAuth, RequestAuth, toPreRequestAuth } from '../auth';
import { Variable, VariableList } from '../variables';

const varListToObject = (obj: VariableList<Variable> | undefined) => {
    if (!obj) {
        return undefined;
    }

    return obj.map(
        (optVar: Variable) => ({
            // type: 'any', // TODO: fix type
            key: optVar.key,
            value: optVar.value,
        }),
        {}
    );
};

describe('test sdk objects', () => {
    it('test RequestAuth methods', () => {
        expect(RequestAuth.isValidType('noauth')).toBeTruthy();

        const basicAuthOptions = {
            type: 'basic',
            basic: [
                { key: 'username', value: 'user1' },
                { key: 'password', value: 'pwd1' },
            ],
        } as AuthOptions;

        const authObj = new RequestAuth(basicAuthOptions);

        const basicAuthOptsFromAuth = varListToObject(authObj.parameters());
        expect(basicAuthOptsFromAuth).toEqual(basicAuthOptions.basic);

        const basicAuthOptions2 = {
            type: 'basic',
            basic: [
                { key: 'username', value: 'user2' },
                { key: 'password', value: 'pwd2' },
            ],
        } as AuthOptions;
        const bearerAuthOptions = {
            type: 'bearer',
            bearer: [
                { key: 'token', value: 'mytoken' },
            ],
        } as AuthOptions;

        authObj.update(basicAuthOptions2);
        const basicAuthOpt2FromAuth = varListToObject(authObj.parameters());
        expect(basicAuthOpt2FromAuth).toEqual(basicAuthOptions2.basic);

        authObj.use('bearer', bearerAuthOptions);
        const beareerAuthOptFromAuth = varListToObject(authObj.parameters());
        expect(beareerAuthOptFromAuth).toEqual(bearerAuthOptions.bearer);

        authObj.clear('bearer');
        expect(authObj.parameters()).toBeUndefined();
    });
});

describe('test auth transforming', () => {
    it('transforming from script side to Insomnia and the reverse direction', () => {
        const basicAuth = {
            type: 'basic',
            useISO88591: true,
            disabled: false,
            username: 'uname',
            password: 'pwd',
        };
        const apikeyAuth = {
            type: 'apikey',
            disabled: false,
            key: 'key',
            value: 'value',
            addTo: 'addto',
        };
        const hawkAuth = {
            type: 'hawk',
            disabled: true,
            algorithm: 'sha256',
            id: 'id',
            key: 'key',
            ext: 'ext',
            validatePayload: true,
        };
        const oauth1Auth = {
            type: 'oauth1',
            disabled: true,
            signatureMethod: 'HMAC-SHA1',
            consumerKey: 'consumerKey',
            consumerSecret: 'consumerSecret',
            tokenKey: 'tokenKey',
            tokenSecret: 'tokenSecret',
            privateKey: 'privateKey',
            version: 'version',
            nonce: 'nonce',
            timestamp: 'timestamp',
            callback: 'callback',
            realm: 'realm',
            verifier: 'verifier',
            includeBodyHash: true,
        };
        const digestAuth = {
            type: 'digest',
            disabled: true,
            username: 'username',
            password: 'password',
        };
        const digestNtlm = {
            type: 'ntlm',
            disabled: true,
            username: 'username',
            password: 'password',
        };
        const bearerAuth = {
            type: 'bearer',
            disabled: true,
            token: 'token',
            prefix: 'prefix',
        };
        const awsv4Auth = {
            type: 'iam',
            disabled: true,
            accessKeyId: 'accessKeyId',
            secretAccessKey: 'secretAccessKey',
            sessionToken: 'sessionToken',
            region: 'region',
            service: 'service',
        };
        const asapAuth = {
            type: 'asap',
            disabled: true,
            issuer: 'issuer',
            subject: 'subject',
            audience: 'audience',
            additionalClaims: 'additionalClaims',
            keyId: 'keyId',
            privateKey: 'privateKey',
        };
        const noneAuth = {
            type: 'none',
            disabled: true,
        };
        const oauth2Auth = {
            type: 'oauth2',
            disabled: true,
            grantType: 'authorization_code',
            accessTokenUrl: 'accessTokenUrl',
            authorizationUrl: 'authorizationUrl',
            clientId: 'clientId',
            clientSecret: 'clientSecret',
            audience: 'audience',
            scope: 'scope',
            resource: 'resource',
            username: 'username',
            password: 'password',
            redirectUrl: 'redirectUrl',
            credentialsInBody: true,
            state: 'state',
            code: 'code',
            accessToken: 'accessToken',
            refreshToken: 'refreshToken',
            tokenPrefix: 'tokenPrefix',
            usePkce: true,
            pkceMethod: 'pkceMethod',
            responseType: 'id_token',
            origin: 'origin',
        };

        [
            basicAuth,
            apikeyAuth,
            hawkAuth,
            oauth1Auth,
            digestAuth,
            digestNtlm,
            bearerAuth,
            awsv4Auth,
            asapAuth,
            noneAuth,
            oauth2Auth,
        ].forEach(authMethod => {
            expect(fromPreRequestAuth(
                new RequestAuth(
                    toPreRequestAuth(authMethod)),
            )
            ).toEqual(authMethod);
        });
    });
});