|
| 1 | +import { |
| 2 | + RequestContext, |
| 3 | + HttpMethod, |
| 4 | +} from "../../packages/datadog-api-client/src"; |
| 5 | +import { |
| 6 | + BearerAuthAuthentication, |
| 7 | + configureAuthMethods, |
| 8 | +} from "../../packages/datadog-api-client/src/auth"; |
| 9 | +import { |
| 10 | + createConfiguration, |
| 11 | + applySecurityAuthentication, |
| 12 | +} from "../../packages/datadog-api-client/src/configuration"; |
| 13 | + |
| 14 | +describe("BearerAuthAuthentication", () => { |
| 15 | + it("should set Authorization Bearer header", () => { |
| 16 | + const auth = new BearerAuthAuthentication("ddpat_test_token_123"); |
| 17 | + const ctx = new RequestContext("https://example.com", HttpMethod.GET); |
| 18 | + auth.applySecurityAuthentication(ctx); |
| 19 | + expect(ctx.getHeaders()["Authorization"]).toBe( |
| 20 | + "Bearer ddpat_test_token_123", |
| 21 | + ); |
| 22 | + }); |
| 23 | + |
| 24 | + it("should return correct name", () => { |
| 25 | + const auth = new BearerAuthAuthentication("token"); |
| 26 | + expect(auth.getName()).toBe("bearerAuth"); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +describe("configureAuthMethods", () => { |
| 31 | + it("should configure bearerAuth when provided", () => { |
| 32 | + const methods = configureAuthMethods({ |
| 33 | + bearerAuth: "ddpat_my_pat", |
| 34 | + }); |
| 35 | + expect(methods.bearerAuth).toBeDefined(); |
| 36 | + expect(methods.bearerAuth!.getName()).toBe("bearerAuth"); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should configure all auth methods together", () => { |
| 40 | + const methods = configureAuthMethods({ |
| 41 | + apiKeyAuth: "api_key", |
| 42 | + appKeyAuth: "app_key", |
| 43 | + bearerAuth: "ddpat_pat", |
| 44 | + }); |
| 45 | + expect(methods.apiKeyAuth).toBeDefined(); |
| 46 | + expect(methods.appKeyAuth).toBeDefined(); |
| 47 | + expect(methods.bearerAuth).toBeDefined(); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +describe("createConfiguration with bearer auth", () => { |
| 52 | + const originalEnv = process.env; |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + process.env = { ...originalEnv }; |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + process.env = originalEnv; |
| 60 | + }); |
| 61 | + |
| 62 | + it("should read DD_BEARER_TOKEN env var", () => { |
| 63 | + process.env.DD_BEARER_TOKEN = "ddpat_env_pat"; |
| 64 | + const config = createConfiguration(); |
| 65 | + expect(config.authMethods.bearerAuth).toBeDefined(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should not override explicit bearerAuth config", () => { |
| 69 | + process.env.DD_BEARER_TOKEN = "ddpat_env_pat"; |
| 70 | + const config = createConfiguration({ |
| 71 | + authMethods: { |
| 72 | + bearerAuth: "ddpat_explicit_pat", |
| 73 | + }, |
| 74 | + }); |
| 75 | + const ctx = new RequestContext("https://example.com", HttpMethod.GET); |
| 76 | + config.authMethods.bearerAuth!.applySecurityAuthentication(ctx); |
| 77 | + expect(ctx.getHeaders()["Authorization"]).toBe( |
| 78 | + "Bearer ddpat_explicit_pat", |
| 79 | + ); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe("applySecurityAuthentication with bearer auth", () => { |
| 84 | + it("should send Bearer header when only bearerAuth is configured", () => { |
| 85 | + const config = createConfiguration({ |
| 86 | + authMethods: { |
| 87 | + bearerAuth: "ddpat_test_pat", |
| 88 | + }, |
| 89 | + }); |
| 90 | + const ctx = new RequestContext("https://example.com", HttpMethod.GET); |
| 91 | + applySecurityAuthentication(config, ctx, [ |
| 92 | + "apiKeyAuth", |
| 93 | + "appKeyAuth", |
| 94 | + "AuthZ", |
| 95 | + ]); |
| 96 | + expect(ctx.getHeaders()["Authorization"]).toBe("Bearer ddpat_test_pat"); |
| 97 | + expect(ctx.getHeaders()["DD-API-KEY"]).toBeUndefined(); |
| 98 | + expect(ctx.getHeaders()["DD-APPLICATION-KEY"]).toBeUndefined(); |
| 99 | + }); |
| 100 | + |
| 101 | + it("should send all auth headers when all are configured", () => { |
| 102 | + const config = createConfiguration({ |
| 103 | + authMethods: { |
| 104 | + apiKeyAuth: "test_api_key", |
| 105 | + appKeyAuth: "test_app_key", |
| 106 | + bearerAuth: "ddpat_test_pat", |
| 107 | + }, |
| 108 | + }); |
| 109 | + const ctx = new RequestContext("https://example.com", HttpMethod.GET); |
| 110 | + applySecurityAuthentication(config, ctx, [ |
| 111 | + "apiKeyAuth", |
| 112 | + "appKeyAuth", |
| 113 | + "AuthZ", |
| 114 | + ]); |
| 115 | + expect(ctx.getHeaders()["Authorization"]).toBe("Bearer ddpat_test_pat"); |
| 116 | + expect(ctx.getHeaders()["DD-API-KEY"]).toBe("test_api_key"); |
| 117 | + expect(ctx.getHeaders()["DD-APPLICATION-KEY"]).toBe("test_app_key"); |
| 118 | + }); |
| 119 | + |
| 120 | + it("should use API key + app key when bearerAuth is not configured", () => { |
| 121 | + const config = createConfiguration({ |
| 122 | + authMethods: { |
| 123 | + apiKeyAuth: "test_api_key", |
| 124 | + appKeyAuth: "test_app_key", |
| 125 | + }, |
| 126 | + }); |
| 127 | + const ctx = new RequestContext("https://example.com", HttpMethod.GET); |
| 128 | + applySecurityAuthentication(config, ctx, [ |
| 129 | + "apiKeyAuth", |
| 130 | + "appKeyAuth", |
| 131 | + "AuthZ", |
| 132 | + ]); |
| 133 | + expect(ctx.getHeaders()["DD-API-KEY"]).toBe("test_api_key"); |
| 134 | + expect(ctx.getHeaders()["DD-APPLICATION-KEY"]).toBe("test_app_key"); |
| 135 | + expect(ctx.getHeaders()["Authorization"]).toBeUndefined(); |
| 136 | + }); |
| 137 | + |
| 138 | + it("should use only API key when only apiKeyAuth is required and no bearer auth", () => { |
| 139 | + const config = createConfiguration({ |
| 140 | + authMethods: { |
| 141 | + apiKeyAuth: "test_api_key", |
| 142 | + }, |
| 143 | + }); |
| 144 | + const ctx = new RequestContext("https://example.com", HttpMethod.GET); |
| 145 | + applySecurityAuthentication(config, ctx, ["apiKeyAuth"]); |
| 146 | + expect(ctx.getHeaders()["DD-API-KEY"]).toBe("test_api_key"); |
| 147 | + expect(ctx.getHeaders()["Authorization"]).toBeUndefined(); |
| 148 | + }); |
| 149 | +}); |
0 commit comments