Skip to content

Commit 4f393d3

Browse files
test(common): add comprehensive unit tests for auth helpers (hoppscotch#5211)
Co-authored-by: jamesgeorge007 <[email protected]>
1 parent ed8b85b commit 4f393d3

File tree

15 files changed

+1559
-51
lines changed

15 files changed

+1559
-51
lines changed

packages/hoppscotch-common/src/helpers/auth/auth-types.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@ export async function generateAuthHeaders(
3434
): Promise<HoppRESTHeader[]> {
3535
switch (auth.authType) {
3636
case "basic":
37-
return generateBasicAuthHeaders(auth, request, envVars, showKeyIfSecret)
37+
return generateBasicAuthHeaders(auth, envVars, showKeyIfSecret)
3838
case "bearer":
39-
return generateBearerAuthHeaders(auth, request, envVars, showKeyIfSecret)
39+
return generateBearerAuthHeaders(auth, envVars, showKeyIfSecret)
4040
case "api-key":
4141
return auth.addTo === "HEADERS"
42-
? generateApiKeyAuthHeaders(auth, request, envVars, showKeyIfSecret)
42+
? generateApiKeyAuthHeaders(auth, envVars, showKeyIfSecret)
4343
: []
4444
case "oauth-2":
45-
return generateOAuth2AuthHeaders(auth, request, envVars, showKeyIfSecret)
45+
return generateOAuth2AuthHeaders(auth, envVars, showKeyIfSecret)
4646
case "digest":
4747
return generateDigestAuthHeaders(auth, request, envVars, showKeyIfSecret)
4848
case "aws-signature":
4949
return generateAwsSignatureAuthHeaders(auth, request, envVars)
5050
case "hawk":
5151
return generateHawkAuthHeaders(auth, request, envVars, showKeyIfSecret)
5252
case "jwt":
53-
return generateJwtAuthHeaders(auth, request, envVars, showKeyIfSecret)
53+
return generateJwtAuthHeaders(auth, envVars, showKeyIfSecret)
5454
default:
5555
return []
5656
}
@@ -68,14 +68,14 @@ export async function generateAuthParams(
6868
switch (auth.authType) {
6969
case "api-key":
7070
return auth.addTo === "QUERY_PARAMS"
71-
? generateApiKeyAuthParams(auth, request, envVars, showKeyIfSecret)
71+
? generateApiKeyAuthParams(auth, envVars, showKeyIfSecret)
7272
: []
7373
case "oauth-2":
74-
return generateOAuth2AuthParams(auth, request, envVars, showKeyIfSecret)
74+
return generateOAuth2AuthParams(auth, envVars, showKeyIfSecret)
7575
case "aws-signature":
7676
return generateAwsSignatureAuthParams(auth, request, envVars)
7777
case "jwt":
78-
return generateJwtAuthParams(auth, request, envVars)
78+
return generateJwtAuthParams(auth, envVars)
7979
default:
8080
return []
8181
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { HoppRESTAuth } from "@hoppscotch/data"
2+
import { describe, expect, test } from "vitest"
3+
import { generateApiKeyAuthHeaders, generateApiKeyAuthParams } from "../api-key"
4+
import { mockEnvVars } from "./test-utils"
5+
6+
describe("API Key Auth", () => {
7+
describe("generateApiKeyAuthHeaders", () => {
8+
test("generates headers when addTo is HEADERS", async () => {
9+
const auth: HoppRESTAuth & { authType: "api-key" } = {
10+
authActive: true,
11+
authType: "api-key",
12+
addTo: "HEADERS",
13+
key: "X-API-Key",
14+
value: "<<API_VALUE>>",
15+
}
16+
17+
const headers = await generateApiKeyAuthHeaders(auth, mockEnvVars)
18+
19+
expect(headers).toHaveLength(1)
20+
expect(headers[0]).toEqual({
21+
active: true,
22+
key: "X-API-Key",
23+
value: "secret-value",
24+
description: "",
25+
})
26+
})
27+
28+
test("returns empty array when addTo is not HEADERS", async () => {
29+
const auth: HoppRESTAuth & { authType: "api-key" } = {
30+
authActive: true,
31+
authType: "api-key",
32+
addTo: "QUERY_PARAMS",
33+
key: "api_key",
34+
value: "test-value",
35+
}
36+
37+
const headers = await generateApiKeyAuthHeaders(auth, mockEnvVars)
38+
39+
expect(headers).toHaveLength(0)
40+
})
41+
42+
test("handles template strings in key and value", async () => {
43+
const auth: HoppRESTAuth & { authType: "api-key" } = {
44+
authActive: true,
45+
authType: "api-key",
46+
addTo: "HEADERS",
47+
key: "<<API_KEY>>",
48+
value: "<<API_VALUE>>",
49+
}
50+
51+
const headers = await generateApiKeyAuthHeaders(auth, mockEnvVars)
52+
53+
expect(headers[0].key).toBe("test-key-123")
54+
expect(headers[0].value).toBe("secret-value")
55+
})
56+
})
57+
58+
describe("generateApiKeyAuthParams", () => {
59+
test("generates params when addTo is QUERY_PARAMS", async () => {
60+
const auth: HoppRESTAuth & { authType: "api-key" } = {
61+
authActive: true,
62+
authType: "api-key",
63+
addTo: "QUERY_PARAMS",
64+
key: "api_key",
65+
value: "<<API_VALUE>>",
66+
}
67+
68+
const params = await generateApiKeyAuthParams(auth, mockEnvVars)
69+
70+
expect(params).toHaveLength(1)
71+
expect(params[0]).toEqual({
72+
active: true,
73+
key: "api_key",
74+
value: "secret-value",
75+
description: "",
76+
})
77+
})
78+
79+
test("returns empty array when addTo is not QUERY_PARAMS", async () => {
80+
const auth: HoppRESTAuth & { authType: "api-key" } = {
81+
authActive: true,
82+
authType: "api-key",
83+
addTo: "HEADERS",
84+
key: "X-API-Key",
85+
value: "test-value",
86+
}
87+
88+
const params = await generateApiKeyAuthParams(auth, mockEnvVars)
89+
90+
expect(params).toHaveLength(0)
91+
})
92+
})
93+
})

packages/hoppscotch-common/src/helpers/auth/types/__tests__/aws-signature.spec.ts

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { describe, expect, test, vi, beforeEach, afterEach } from "vitest"
2-
import { makeRESTRequest } from "@hoppscotch/data"
1+
import type { Environment, HoppRESTAuth } from "@hoppscotch/data"
2+
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"
33
import {
44
generateAwsSignatureAuthHeaders,
55
generateAwsSignatureAuthParams,
66
} from "../aws-signature"
7-
import type { HoppRESTAuth, Environment } from "@hoppscotch/data"
7+
import { createBaseRequest } from "./test-utils"
88

99
vi.mock("aws4fetch", () => ({
1010
AwsV4Signer: vi.fn().mockImplementation((config) => ({
@@ -69,33 +69,6 @@ describe("AWS Signature Auth", () => {
6969
...overrides,
7070
})
7171

72-
// Helper function to create base request
73-
const createBaseRequest = (
74-
overrides: Partial<Parameters<typeof makeRESTRequest>[0]> = {}
75-
) => {
76-
const baseRequest: Parameters<typeof makeRESTRequest>[0] = {
77-
method: "GET",
78-
endpoint: "https://s3.amazonaws.com/bucket/key",
79-
name: "Test Request",
80-
params: [],
81-
headers: [],
82-
preRequestScript: "",
83-
testScript: "",
84-
auth: {
85-
authType: "inherit",
86-
authActive: true,
87-
},
88-
body: {
89-
contentType: null,
90-
body: null,
91-
},
92-
requestVariables: [],
93-
responses: {},
94-
}
95-
96-
return makeRESTRequest({ ...baseRequest, ...overrides })
97-
}
98-
9972
beforeEach(() => {
10073
vi.clearAllMocks()
10174
vi.useFakeTimers()
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { HoppRESTAuth } from "@hoppscotch/data"
2+
import { describe, expect, test } from "vitest"
3+
import { generateBasicAuthHeaders } from "../basic"
4+
import { mockEnvVars } from "./test-utils"
5+
6+
describe("Basic Auth", () => {
7+
describe("generateBasicAuthHeaders", () => {
8+
test("generates basic auth header with credentials", async () => {
9+
const auth: HoppRESTAuth & { authType: "basic" } = {
10+
authActive: true,
11+
authType: "basic",
12+
username: "admin",
13+
password: "secret123",
14+
}
15+
16+
const headers = await generateBasicAuthHeaders(auth, mockEnvVars)
17+
18+
expect(headers).toHaveLength(1)
19+
expect(headers[0]).toEqual({
20+
active: true,
21+
key: "Authorization",
22+
value: `Basic ${btoa("admin:secret123")}`,
23+
description: "",
24+
})
25+
})
26+
27+
test("handles template strings in username and password", async () => {
28+
const auth: HoppRESTAuth & { authType: "basic" } = {
29+
authActive: true,
30+
authType: "basic",
31+
username: "<<USERNAME>>",
32+
password: "<<PASSWORD>>",
33+
}
34+
35+
const headers = await generateBasicAuthHeaders(auth, mockEnvVars)
36+
37+
expect(headers[0].value).toBe(`Basic ${btoa("testuser:testpass")}`)
38+
})
39+
40+
test("handles empty credentials", async () => {
41+
const auth: HoppRESTAuth & { authType: "basic" } = {
42+
authActive: true,
43+
authType: "basic",
44+
username: "",
45+
password: "",
46+
}
47+
48+
const headers = await generateBasicAuthHeaders(auth, mockEnvVars)
49+
50+
expect(headers[0].value).toBe(`Basic ${btoa(":")}`)
51+
})
52+
})
53+
})
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { describe, test, expect } from "vitest"
2+
import { generateBearerAuthHeaders } from "../bearer"
3+
import { createBaseRequest, mockEnvVars } from "./test-utils"
4+
import { HoppRESTAuth } from "@hoppscotch/data"
5+
6+
describe("Bearer Auth", () => {
7+
describe("generateBearerAuthHeaders", () => {
8+
test("generates bearer auth header with token", async () => {
9+
const auth: HoppRESTAuth & { authType: "bearer" } = {
10+
authActive: true,
11+
authType: "bearer",
12+
token: "abc123token",
13+
}
14+
15+
const headers = await generateBearerAuthHeaders(auth, mockEnvVars)
16+
17+
expect(headers).toHaveLength(1)
18+
expect(headers[0]).toEqual({
19+
active: true,
20+
key: "Authorization",
21+
value: "Bearer abc123token",
22+
description: "",
23+
})
24+
})
25+
26+
test("handles template strings in token", async () => {
27+
const auth: HoppRESTAuth & { authType: "bearer" } = {
28+
authActive: true,
29+
authType: "bearer",
30+
token: "<<ACCESS_TOKEN>>",
31+
}
32+
33+
const headers = await generateBearerAuthHeaders(auth, mockEnvVars)
34+
35+
expect(headers[0].value).toBe(
36+
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
37+
)
38+
})
39+
40+
test("handles empty token", async () => {
41+
const auth: HoppRESTAuth & { authType: "bearer" } = {
42+
authActive: true,
43+
authType: "bearer",
44+
token: "",
45+
}
46+
47+
const headers = await generateBearerAuthHeaders(auth, mockEnvVars)
48+
49+
expect(headers[0].value).toBe("Bearer ")
50+
})
51+
})
52+
})

0 commit comments

Comments
 (0)