@defra/grants-config-utils 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/.changeset/README.md +8 -0
  2. package/.changeset/config.json +14 -0
  3. package/.github/dependabot.yml +28 -0
  4. package/.github/workflows/check-pull-request.yml +44 -0
  5. package/.github/workflows/release.yml +60 -0
  6. package/.husky/pre-commit +1 -0
  7. package/.nvmrc +1 -0
  8. package/.prettierignore +4 -0
  9. package/.scannerwork/.sonar_lock +0 -0
  10. package/.scannerwork/architecture/js/src_broker_broker-auth-helper_js.udg +0 -0
  11. package/.scannerwork/architecture/js/src_broker_broker-auth-helper_test_js.udg +0 -0
  12. package/.scannerwork/architecture/js/src_config_config_js.udg +0 -0
  13. package/.scannerwork/architecture/js/src_s3_s3-client_js.udg +0 -0
  14. package/.scannerwork/architecture/js/src_s3_s3-client_test_js.udg +0 -0
  15. package/.scannerwork/architecture/js/src_s3_s3-interactions_js.udg +0 -0
  16. package/.scannerwork/architecture/js/src_s3_s3-interactions_test_js.udg +0 -0
  17. package/.scannerwork/architecture/js/src_service_config-store-and-inform_js.udg +0 -0
  18. package/.scannerwork/architecture/js/src_service_config-store-and-inform_test_js.udg +0 -0
  19. package/.scannerwork/report-task.txt +7 -0
  20. package/.vite/setup-files.js +15 -0
  21. package/CONTRIBUTING.md +26 -0
  22. package/LICENCE +8 -0
  23. package/README.md +32 -0
  24. package/babel.config.cjs +17 -0
  25. package/ci/release-it.sh +19 -0
  26. package/eslint.config.js +12 -0
  27. package/package.json +92 -0
  28. package/sonar-project.properties +15 -0
  29. package/src/broker/broker-auth-helper.js +74 -0
  30. package/src/broker/broker-auth-helper.test.js +262 -0
  31. package/src/config/config.js +146 -0
  32. package/src/s3/s3-client.js +20 -0
  33. package/src/s3/s3-client.test.js +56 -0
  34. package/src/s3/s3-interactions.js +35 -0
  35. package/src/s3/s3-interactions.test.js +82 -0
  36. package/src/service/config-store-and-inform.js +151 -0
  37. package/src/service/config-store-and-inform.test.js +168 -0
  38. package/vitest.config.js +18 -0
@@ -0,0 +1,262 @@
1
+ import { vi } from "vitest";
2
+
3
+ const TEST_ENCRYPTION_KEY = "test-encryption-key-for-unit-tests-32-chars";
4
+ const CONTENT_TYPE_JSON = "application/json";
5
+
6
+ const HEADER_OBJECTS = {
7
+ CONTENT_TYPE_JSON: { "Content-Type": CONTENT_TYPE_JSON },
8
+ EMPTY: {},
9
+ };
10
+
11
+ const CONFIG_AUTH_TOKEN = "configBroker.auth.token";
12
+ const CONFIG_ENCRYPTION_KEY = "configBroker.auth.encryptionKey";
13
+
14
+ const MOCK_TOKENS = {
15
+ DEFAULT: "test-token-123",
16
+ API: "api-test-token",
17
+ ALTERNATIVE: "test-token-456",
18
+ THIRD: "test-token-789",
19
+ MUTATION_TEST: "test-token-mutation",
20
+ SECRET: "my-secret-token",
21
+ };
22
+
23
+ const TEST_HEADERS = {
24
+ USER_AGENT: "test-agent",
25
+ CUSTOM_VALUE: "custom-value",
26
+ };
27
+
28
+ const mockConfigGet = vi.fn((key) => {
29
+ if (key === "configBroker.auth.token") {
30
+ return "default-token";
31
+ }
32
+ if (key === "configBroker.auth.encryptionKey") {
33
+ return TEST_ENCRYPTION_KEY;
34
+ }
35
+ return null;
36
+ });
37
+
38
+ vi.mock("../config/config.js", () => ({
39
+ config: {
40
+ get: mockConfigGet,
41
+ },
42
+ }));
43
+
44
+ async function importConfigBrokerHelper() {
45
+ return await import("./broker-auth-helper.js");
46
+ }
47
+
48
+ function setupMockConfig(value, encryptionKey = TEST_ENCRYPTION_KEY) {
49
+ const configValues = {
50
+ [CONFIG_AUTH_TOKEN]: value,
51
+ [CONFIG_ENCRYPTION_KEY]: encryptionKey,
52
+ };
53
+
54
+ mockConfigGet.mockImplementation((key) => configValues[key] || null);
55
+ }
56
+
57
+ describe("Broker Auth Helper", () => {
58
+ beforeEach(() => {
59
+ vi.clearAllMocks();
60
+ vi.resetModules();
61
+ });
62
+
63
+ describe("createApiHeadersForConfigBroker", () => {
64
+ describe("when no token is configured", () => {
65
+ it("should return base headers when no token is configured", async () => {
66
+ setupMockConfig("", TEST_ENCRYPTION_KEY);
67
+
68
+ const { createApiHeadersForConfigBroker } =
69
+ await importConfigBrokerHelper();
70
+ const headers = createApiHeadersForConfigBroker(
71
+ HEADER_OBJECTS.CONTENT_TYPE_JSON,
72
+ );
73
+
74
+ expect(mockConfigGet).toHaveBeenCalledWith(CONFIG_AUTH_TOKEN);
75
+ expect(headers).toEqual(HEADER_OBJECTS.CONTENT_TYPE_JSON);
76
+ expect(headers.Authorization).toBeUndefined();
77
+ });
78
+ });
79
+
80
+ describe("when token is configured", () => {
81
+ it("should add Authorization header when token is configured", async () => {
82
+ setupMockConfig(MOCK_TOKENS.DEFAULT);
83
+
84
+ const { createApiHeadersForConfigBroker } =
85
+ await importConfigBrokerHelper();
86
+ const headers = createApiHeadersForConfigBroker(
87
+ HEADER_OBJECTS.CONTENT_TYPE_JSON,
88
+ );
89
+
90
+ expect(mockConfigGet).toHaveBeenCalledWith(CONFIG_AUTH_TOKEN);
91
+ expect(headers).toHaveProperty("Content-Type", "application/json");
92
+ expect(headers).toHaveProperty("Authorization");
93
+ expect(headers.Authorization).toMatch(/^Bearer [A-Za-z0-9+/]+=*$/);
94
+
95
+ const base64Part = headers.Authorization.replace("Bearer ", "");
96
+ const decoded = Buffer.from(base64Part, "base64").toString("utf-8");
97
+ expect(decoded).toMatch(
98
+ /^([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*)$/,
99
+ );
100
+ });
101
+
102
+ it("should work with empty base headers", async () => {
103
+ setupMockConfig(MOCK_TOKENS.ALTERNATIVE);
104
+
105
+ const { createApiHeadersForConfigBroker } =
106
+ await importConfigBrokerHelper();
107
+ const headers = createApiHeadersForConfigBroker(undefined);
108
+
109
+ expect(mockConfigGet).toHaveBeenCalledWith(CONFIG_AUTH_TOKEN);
110
+ expect(headers).toHaveProperty("Authorization");
111
+ expect(headers.Authorization).toMatch(/^Bearer [A-Za-z0-9+/]+=*$/);
112
+
113
+ const base64Part = headers.Authorization.replace("Bearer ", "");
114
+ const decoded = Buffer.from(base64Part, "base64").toString("utf-8");
115
+ expect(decoded).toMatch(
116
+ /^([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*)$/,
117
+ );
118
+ });
119
+
120
+ it("should preserve all base headers when adding auth", async () => {
121
+ const baseHeaders = {
122
+ "Content-Type": CONTENT_TYPE_JSON,
123
+ "User-Agent": TEST_HEADERS.USER_AGENT,
124
+ "X-Custom-Header": TEST_HEADERS.CUSTOM_VALUE,
125
+ };
126
+
127
+ const { createAuthenticatedHeaders } = await importConfigBrokerHelper();
128
+ const headers = createAuthenticatedHeaders(
129
+ MOCK_TOKENS.THIRD,
130
+ TEST_ENCRYPTION_KEY,
131
+ baseHeaders,
132
+ );
133
+
134
+ expect(mockConfigGet).toHaveBeenCalledWith(CONFIG_AUTH_TOKEN);
135
+ expect(headers).toHaveProperty("Content-Type", CONTENT_TYPE_JSON);
136
+ expect(headers).toHaveProperty("User-Agent", TEST_HEADERS.USER_AGENT);
137
+ expect(headers).toHaveProperty(
138
+ "X-Custom-Header",
139
+ TEST_HEADERS.CUSTOM_VALUE,
140
+ );
141
+ expect(headers).toHaveProperty("Authorization");
142
+ expect(headers.Authorization).toMatch(/^Bearer [A-Za-z0-9+/]+=*$/);
143
+
144
+ const base64Part = headers.Authorization.replace("Bearer ", "");
145
+ const decoded = Buffer.from(base64Part, "base64").toString("utf-8");
146
+ expect(decoded).toMatch(
147
+ /^([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*)$/,
148
+ );
149
+ });
150
+ });
151
+
152
+ it("should not mutate original base headers object", async () => {
153
+ const baseHeaders = HEADER_OBJECTS.CONTENT_TYPE_JSON;
154
+
155
+ setupMockConfig(MOCK_TOKENS.MUTATION_TEST);
156
+
157
+ const { createApiHeadersForConfigBroker } =
158
+ await importConfigBrokerHelper();
159
+ const headers = createApiHeadersForConfigBroker(baseHeaders);
160
+
161
+ expect(baseHeaders).toEqual(HEADER_OBJECTS.CONTENT_TYPE_JSON);
162
+ expect(baseHeaders.Authorization).toBeUndefined();
163
+ expect(headers.Authorization).toBeDefined();
164
+ });
165
+
166
+ it("should handle null token correctly", async () => {
167
+ setupMockConfig(null, TEST_ENCRYPTION_KEY);
168
+
169
+ const { createApiHeadersForConfigBroker } =
170
+ await importConfigBrokerHelper();
171
+ const headers = createApiHeadersForConfigBroker({
172
+ "Content-Type": CONTENT_TYPE_JSON,
173
+ });
174
+
175
+ expect(headers).toEqual(HEADER_OBJECTS.CONTENT_TYPE_JSON);
176
+ expect(headers.Authorization).toBeUndefined();
177
+ });
178
+ });
179
+
180
+ describe("createApiHeaders", () => {
181
+ it("should return headers with Content-Type and no auth when no token configured", async () => {
182
+ setupMockConfig("", TEST_ENCRYPTION_KEY);
183
+
184
+ const { createApiHeadersForConfigBroker } =
185
+ await importConfigBrokerHelper();
186
+ const headers = createApiHeadersForConfigBroker();
187
+
188
+ expect(headers).toEqual(HEADER_OBJECTS.CONTENT_TYPE_JSON);
189
+ });
190
+
191
+ it("should return headers with Content-Type and Authorization when token configured", async () => {
192
+ setupMockConfig(MOCK_TOKENS.API);
193
+
194
+ const { createApiHeadersForConfigBroker } =
195
+ await importConfigBrokerHelper();
196
+ const headers = createApiHeadersForConfigBroker();
197
+
198
+ expect(headers).toHaveProperty("Content-Type", "application/json");
199
+ expect(headers).toHaveProperty("Authorization");
200
+ expect(headers.Authorization).toMatch(/^Bearer [A-Za-z0-9+/]+=*$/);
201
+
202
+ const base64Part = headers.Authorization.replace("Bearer ", "");
203
+ const decoded = Buffer.from(base64Part, "base64").toString("utf-8");
204
+ expect(decoded).toMatch(
205
+ /^([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*)$/,
206
+ );
207
+ });
208
+ });
209
+
210
+ describe("Base64 encoding", () => {
211
+ it("should encode encrypted token with blank username correctly", async () => {
212
+ setupMockConfig(MOCK_TOKENS.SECRET);
213
+
214
+ const { createApiHeadersForConfigBroker } =
215
+ await importConfigBrokerHelper();
216
+ const headers = createApiHeadersForConfigBroker();
217
+ const authHeader = headers.Authorization;
218
+
219
+ const base64Part = authHeader.replace("Bearer ", "");
220
+ const decoded = Buffer.from(base64Part, "base64").toString("utf-8");
221
+
222
+ expect(decoded).toMatch(
223
+ /^([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*):([A-Za-z0-9+/]+=*)$/,
224
+ );
225
+ });
226
+ });
227
+
228
+ describe("Token Encryption", () => {
229
+ beforeEach(() => {
230
+ vi.resetAllMocks();
231
+ // No longer needed with new mock structure
232
+ });
233
+
234
+ it("should encrypt token when encryption key is configured", async () => {
235
+ const configValues = {
236
+ [CONFIG_AUTH_TOKEN]: MOCK_TOKENS.DEFAULT,
237
+ [CONFIG_ENCRYPTION_KEY]: TEST_ENCRYPTION_KEY,
238
+ };
239
+
240
+ mockConfigGet.mockImplementation((key) => configValues[key] || null);
241
+
242
+ const { createApiHeadersForConfigBroker } =
243
+ await importConfigBrokerHelper();
244
+ const headers = createApiHeadersForConfigBroker(
245
+ HEADER_OBJECTS.CONTENT_TYPE_JSON,
246
+ );
247
+
248
+ expect(mockConfigGet).toHaveBeenCalledWith(CONFIG_AUTH_TOKEN);
249
+ expect(mockConfigGet).toHaveBeenCalledWith(CONFIG_ENCRYPTION_KEY);
250
+ expect(headers.Authorization).toBeDefined();
251
+ expect(headers.Authorization).toMatch(/^Bearer /);
252
+
253
+ const base64Part = headers.Authorization.replace("Bearer ", "");
254
+ const decoded = Buffer.from(base64Part, "base64").toString("utf-8");
255
+ const tokenPart = decoded.substring(1);
256
+
257
+ expect(tokenPart).toMatch(
258
+ /^[A-Za-z0-9+/]+=*:[A-Za-z0-9+/]+=*:[A-Za-z0-9+/]+=*$/,
259
+ );
260
+ });
261
+ });
262
+ });
@@ -0,0 +1,146 @@
1
+ import convict from "convict";
2
+ import convictFormatWithValidator from "convict-format-with-validator";
3
+
4
+ convict.addFormats(convictFormatWithValidator);
5
+
6
+ const isProduction = process.env.NODE_ENV === "production";
7
+ const isTest = process.env.NODE_ENV === "test";
8
+
9
+ export const config = convict({
10
+ serviceVersion: {
11
+ doc: "The service version, this variable is injected into your docker container in CDP environments",
12
+ format: String,
13
+ nullable: true,
14
+ default: null,
15
+ env: "SERVICE_VERSION",
16
+ },
17
+ host: {
18
+ doc: "The IP address to bind",
19
+ format: "ipaddress",
20
+ default: "0.0.0.0",
21
+ env: "HOST",
22
+ },
23
+ port: {
24
+ doc: "The port to bind",
25
+ format: "port",
26
+ default: 3001,
27
+ env: "PORT",
28
+ },
29
+ serviceName: {
30
+ doc: "Api Service Name",
31
+ format: String,
32
+ default: "grants-config-playground",
33
+ },
34
+ cdpEnvironment: {
35
+ doc: 'The CDP environment the app is running in. With the addition of "local" for local development',
36
+ format: [
37
+ "local",
38
+ "infra-dev",
39
+ "management",
40
+ "dev",
41
+ "test",
42
+ "perf-test",
43
+ "ext-test",
44
+ "prod",
45
+ ],
46
+ default: "local",
47
+ env: "ENVIRONMENT",
48
+ },
49
+ log: {
50
+ isEnabled: {
51
+ doc: "Is logging enabled",
52
+ format: Boolean,
53
+ default: !isTest,
54
+ env: "LOG_ENABLED",
55
+ },
56
+ level: {
57
+ doc: "Logging level",
58
+ format: ["fatal", "error", "warn", "info", "debug", "trace", "silent"],
59
+ default: "info",
60
+ env: "LOG_LEVEL",
61
+ },
62
+ format: {
63
+ doc: "Format to output logs in",
64
+ format: ["ecs", "pino-pretty"],
65
+ default: isProduction ? "ecs" : "pino-pretty",
66
+ env: "LOG_FORMAT",
67
+ },
68
+ redact: {
69
+ doc: "Log paths to redact",
70
+ format: Array,
71
+ default: isProduction
72
+ ? ["req.headers.authorization", "req.headers.cookie", "res.headers"]
73
+ : ["req", "res", "responseTime"],
74
+ },
75
+ },
76
+ httpProxy: {
77
+ doc: "HTTP Proxy URL",
78
+ format: String,
79
+ nullable: true,
80
+ default: null,
81
+ env: "HTTP_PROXY",
82
+ },
83
+ tracing: {
84
+ header: {
85
+ doc: "CDP tracing header name",
86
+ format: String,
87
+ default: "x-cdp-request-id",
88
+ env: "TRACING_HEADER",
89
+ },
90
+ },
91
+ aws: {
92
+ endpointUrl: {
93
+ doc: "AWS Endpoint URL used for LocalStack",
94
+ format: String,
95
+ nullable: true,
96
+ default: null,
97
+ env: "AWS_ENDPOINT_URL",
98
+ },
99
+ region: {
100
+ doc: "AWS Region",
101
+ format: String,
102
+ default: "eu-west-2",
103
+ env: "AWS_REGION",
104
+ },
105
+ s3: {
106
+ bucketName: {
107
+ doc: "Configs S3 bucket name",
108
+ format: String,
109
+ default: "configs-bucket",
110
+ env: "CONFIG_BUCKET_NAME",
111
+ },
112
+ forcePathStyle: {
113
+ doc: "Force path style on S3 bucket",
114
+ format: Boolean,
115
+ default: true,
116
+ env: "FORCE_PATH_STYLE",
117
+ },
118
+ },
119
+ },
120
+ configBroker: {
121
+ auth: {
122
+ token: {
123
+ doc: "Bearer token for service-to-service authentication",
124
+ format: String,
125
+ default: "",
126
+ env: "GRANTS_CONFIG_BROKER_AUTH_TOKEN",
127
+ sensitive: true,
128
+ },
129
+ encryptionKey: {
130
+ doc: "Encryption key for decrypting bearer token",
131
+ format: String,
132
+ default: "",
133
+ env: "GRANTS_CONFIG_BROKER_ENCRYPTION_KEY",
134
+ sensitive: true,
135
+ },
136
+ },
137
+ apiEndpoint: {
138
+ doc: "Endpoint for the config broker API",
139
+ format: String,
140
+ default: "http://localhost:3001",
141
+ env: "GRANTS_CONFIG_BROKER_API_ENDPOINT",
142
+ },
143
+ },
144
+ });
145
+
146
+ config.validate({ allowed: "strict" });
@@ -0,0 +1,20 @@
1
+ import { S3Client } from "@aws-sdk/client-s3";
2
+
3
+ export function createS3Client({
4
+ region,
5
+ endpoint,
6
+ forcePathStyle,
7
+ credentials = undefined,
8
+ }) {
9
+ const config = {
10
+ region,
11
+ endpoint,
12
+ forcePathStyle,
13
+ };
14
+
15
+ if (credentials) {
16
+ config.credentials = credentials;
17
+ }
18
+
19
+ return new S3Client(config);
20
+ }
@@ -0,0 +1,56 @@
1
+ import { S3Client } from "@aws-sdk/client-s3";
2
+
3
+ import { createS3Client } from "./s3-client.js";
4
+
5
+ vi.mock("@aws-sdk/client-s3");
6
+
7
+ const region = "eu-west-2";
8
+ const endpoint = "http://localhost:4566";
9
+ const forcePathStyle = true;
10
+ const credentials = { accessKeyId: "test", secretAccessKey: "test" };
11
+
12
+ describe("createS3Client", () => {
13
+ let s3Client;
14
+
15
+ beforeEach(() => {
16
+ s3Client = { config: { region } };
17
+
18
+ vi.mocked(S3Client).mockImplementation(function (config) {
19
+ return s3Client;
20
+ });
21
+ });
22
+
23
+ afterEach(() => {
24
+ vi.resetAllMocks();
25
+ });
26
+
27
+ it("should create client with expected configuration", () => {
28
+ const result = createS3Client({ region, endpoint, forcePathStyle });
29
+
30
+ expect(S3Client).toHaveBeenCalledWith({
31
+ region,
32
+ endpoint,
33
+ forcePathStyle,
34
+ });
35
+
36
+ expect(result).toBe(s3Client);
37
+ });
38
+
39
+ it("should create client with specific credentials", () => {
40
+ const result = createS3Client({
41
+ region,
42
+ endpoint,
43
+ forcePathStyle,
44
+ credentials,
45
+ });
46
+
47
+ expect(S3Client).toHaveBeenCalledWith({
48
+ region,
49
+ endpoint,
50
+ forcePathStyle,
51
+ credentials,
52
+ });
53
+
54
+ expect(result).toBe(s3Client);
55
+ });
56
+ });
@@ -0,0 +1,35 @@
1
+ import { config } from "../config/config.js";
2
+ import { createS3Client } from "./s3-client.js";
3
+ import { PutObjectCommand } from "@aws-sdk/client-s3";
4
+
5
+ let s3client;
6
+
7
+ const bucketName = config.get("aws.s3.bucketName");
8
+
9
+ const initialiseClient = () => {
10
+ if (!s3client) {
11
+ s3client = createS3Client({
12
+ region: config.get("aws.region"),
13
+ endpoint: config.get("aws.endpointUrl"),
14
+ forcePathStyle: config.get("aws.s3.forcePathStyle"),
15
+ });
16
+ }
17
+ return s3client;
18
+ };
19
+
20
+ export const uploadBlob = async (logger, filename, contents) => {
21
+ const client = initialiseClient();
22
+
23
+ const params = {
24
+ Bucket: bucketName,
25
+ Key: filename,
26
+ Body: contents,
27
+ };
28
+
29
+ const result = await client.send(new PutObjectCommand(params));
30
+ logger.info(`Uploaded document: ${filename}, ETag: ${result.ETag}`);
31
+
32
+ return result;
33
+ };
34
+
35
+ export const getBucketName = () => bucketName;
@@ -0,0 +1,82 @@
1
+ import { getBucketName, uploadBlob } from "./s3-interactions.js";
2
+ import { createS3Client } from "./s3-client.js";
3
+
4
+ vi.mock("./s3-client.js");
5
+
6
+ const bucketName = "configs-bucket";
7
+
8
+ describe("s3-interactions", () => {
9
+ const mockLogger = {
10
+ info: vi.fn(),
11
+ warn: vi.fn(),
12
+ error: vi.fn(),
13
+ };
14
+ beforeEach(() => {
15
+ createS3Client.mockReturnValueOnce(mockS3Client);
16
+ });
17
+
18
+ afterEach(() => {
19
+ vi.clearAllMocks();
20
+ });
21
+
22
+ const mockPutObjectResponse = { ETag: '"mock-etag"' };
23
+ const mockS3Client = {
24
+ send: vi.fn(() => Promise.resolve(mockPutObjectResponse)),
25
+ };
26
+
27
+ describe("uploadBlob", () => {
28
+ it("should upload a blob to the S3 bucket with specified key", async () => {
29
+ const key = "test-key";
30
+ const body = "test-data";
31
+
32
+ const result = await uploadBlob(mockLogger, key, body);
33
+
34
+ expect(createS3Client).toHaveBeenCalledTimes(1);
35
+ expect(mockS3Client.send).toHaveBeenCalledWith(
36
+ expect.objectContaining({
37
+ input: {
38
+ Bucket: bucketName,
39
+ Key: key,
40
+ Body: body,
41
+ },
42
+ }),
43
+ );
44
+ expect(result).toEqual(mockPutObjectResponse);
45
+ expect(mockLogger.info).toHaveBeenCalledWith(
46
+ `Uploaded document: ${key}, ETag: ${mockPutObjectResponse.ETag}`,
47
+ );
48
+ });
49
+
50
+ it("should throw an error if the upload fails", async () => {
51
+ const key = "test-key";
52
+ const body = "test-data";
53
+ const mockError = new Error("Upload failed");
54
+
55
+ mockS3Client.send.mockRejectedValueOnce(mockError);
56
+
57
+ createS3Client.mockReturnValueOnce(mockS3Client);
58
+
59
+ await expect(uploadBlob(mockLogger, key, body)).rejects.toThrow(
60
+ "Upload failed",
61
+ );
62
+
63
+ expect(createS3Client).not.toHaveBeenCalled();
64
+ expect(mockS3Client.send).toHaveBeenCalledWith(
65
+ expect.objectContaining({
66
+ input: {
67
+ Bucket: bucketName,
68
+ Key: key,
69
+ Body: body,
70
+ },
71
+ }),
72
+ );
73
+ });
74
+ });
75
+
76
+ describe("getBucketName", () => {
77
+ it("should return the bucket name", async () => {
78
+ const result = getBucketName();
79
+ expect(result).toEqual("configs-bucket");
80
+ });
81
+ });
82
+ });