@@ -12,6 +12,8 @@ import {
1212} from "@aws-sdk/client-dynamodb" ;
1313import { AppRoles } from "../../src/common/roles.js" ;
1414import { createApiKey } from "../../src/api/functions/apiKey.js" ;
15+ import { randomUUID } from "crypto" ;
16+ import { SendMessageCommand , SQSClient } from "@aws-sdk/client-sqs" ;
1517
1618// Mock the createApiKey function
1719vi . mock ( "../../src/api/functions/apiKey.js" , ( ) => {
@@ -28,6 +30,7 @@ vi.mock("../../src/api/functions/apiKey.js", () => {
2830
2931// Mock DynamoDB client
3032const dynamoMock = mockClient ( DynamoDBClient ) ;
33+ const sqsMock = mockClient ( SQSClient ) ;
3134const jwt_secret = secretObject [ "jwt_key" ] ;
3235
3336vi . stubEnv ( "JwtSigningKey" , jwt_secret ) ;
@@ -37,6 +40,7 @@ const app = await init();
3740describe ( "API Key Route Tests" , ( ) => {
3841 beforeEach ( ( ) => {
3942 dynamoMock . reset ( ) ;
43+ sqsMock . reset ( ) ;
4044 vi . clearAllMocks ( ) ;
4145
4246 dynamoMock . on ( TransactWriteItemsCommand ) . resolves ( { } ) ;
@@ -61,6 +65,8 @@ describe("API Key Route Tests", () => {
6165
6266 describe ( "Create API Key" , ( ) => {
6367 test ( "Should create an API key successfully" , async ( ) => {
68+ const queueId = randomUUID ( ) ;
69+ sqsMock . on ( SendMessageCommand ) . resolves ( { MessageId : queueId } ) ;
6470 const testJwt = createJwt ( ) ;
6571 await app . ready ( ) ;
6672
@@ -79,10 +85,13 @@ describe("API Key Route Tests", () => {
7985 expect ( response . body . apiKey ) . toBe ( "acmuiuc_test123_abcdefg12345" ) ;
8086 expect ( createApiKey ) . toHaveBeenCalledTimes ( 1 ) ;
8187 expect ( dynamoMock . calls ( ) ) . toHaveLength ( 1 ) ;
88+ expect ( sqsMock . calls ( ) ) . toHaveLength ( 1 ) ;
8289 } ) ;
8390
8491 test ( "Should create an API key with expiration" , async ( ) => {
8592 const testJwt = createJwt ( ) ;
93+ const queueId = randomUUID ( ) ;
94+ sqsMock . on ( SendMessageCommand ) . resolves ( { MessageId : queueId } ) ;
8695 await app . ready ( ) ;
8796
8897 const expiryTime = Math . floor ( Date . now ( ) / 1000 ) + 3600 ; // 1 hour from now
@@ -104,9 +113,11 @@ describe("API Key Route Tests", () => {
104113 expect ( response . body . expiresAt ) . toBe ( expiryTime ) ;
105114 expect ( createApiKey ) . toHaveBeenCalledTimes ( 1 ) ;
106115 expect ( dynamoMock . calls ( ) ) . toHaveLength ( 1 ) ;
116+ expect ( sqsMock . calls ( ) ) . toHaveLength ( 1 ) ;
107117 } ) ;
108118
109119 test ( "Should not create an API key for invalid API key roles" , async ( ) => {
120+ sqsMock . on ( SendMessageCommand ) . rejects ( { } ) ;
110121 const testJwt = createJwt ( ) ;
111122 await app . ready ( ) ;
112123
@@ -131,14 +142,15 @@ describe("API Key Route Tests", () => {
131142
132143 expect ( createApiKey ) . toHaveBeenCalledTimes ( 0 ) ;
133144 expect ( dynamoMock . calls ( ) ) . toHaveLength ( 0 ) ;
145+ expect ( sqsMock . calls ( ) ) . toHaveLength ( 0 ) ;
134146 } ) ;
135147
136148 test ( "Should handle DynamoDB insertion error" , async ( ) => {
137149 // Mock the DynamoDB client to throw an error
138150 dynamoMock
139151 . on ( TransactWriteItemsCommand )
140152 . rejects ( new Error ( "DynamoDB error" ) ) ;
141-
153+ sqsMock . on ( SendMessageCommand ) . rejects ( { } ) ;
142154 const testJwt = createJwt ( ) ;
143155 await app . ready ( ) ;
144156
@@ -157,6 +169,7 @@ describe("API Key Route Tests", () => {
157169 expect ( response . body . message ) . toBe ( "Could not create API key." ) ;
158170 expect ( createApiKey ) . toHaveBeenCalledTimes ( 1 ) ;
159171 expect ( dynamoMock . calls ( ) ) . toHaveLength ( 1 ) ;
172+ expect ( sqsMock . calls ( ) ) . toHaveLength ( 0 ) ;
160173 } ) ;
161174
162175 test ( "Should require authorization" , async ( ) => {
@@ -177,6 +190,8 @@ describe("API Key Route Tests", () => {
177190
178191 describe ( "Delete API Key" , ( ) => {
179192 test ( "Should delete an API key successfully" , async ( ) => {
193+ const queueId = randomUUID ( ) ;
194+ sqsMock . on ( SendMessageCommand ) . resolves ( { MessageId : queueId } ) ;
180195 const testJwt = createJwt ( ) ;
181196 await app . ready ( ) ;
182197
@@ -212,10 +227,12 @@ describe("API Key Route Tests", () => {
212227 expect ( response . body ) . toHaveProperty ( "message" ) ;
213228 expect ( response . body . message ) . toBe ( "Key does not exist." ) ;
214229 expect ( dynamoMock . calls ( ) ) . toHaveLength ( 1 ) ;
230+ expect ( sqsMock . calls ( ) ) . toHaveLength ( 0 ) ;
215231 } ) ;
216232
217233 test ( "Should handle DynamoDB deletion error" , async ( ) => {
218234 // Mock the DynamoDB client to throw a generic error
235+ sqsMock . on ( SendMessageCommand ) . rejects ( ) ;
219236 dynamoMock
220237 . on ( TransactWriteItemsCommand )
221238 . rejects ( new Error ( "DynamoDB error" ) ) ;
@@ -233,6 +250,7 @@ describe("API Key Route Tests", () => {
233250 expect ( response . body ) . toHaveProperty ( "message" ) ;
234251 expect ( response . body . message ) . toBe ( "Could not delete API key." ) ;
235252 expect ( dynamoMock . calls ( ) ) . toHaveLength ( 1 ) ;
253+ expect ( sqsMock . calls ( ) ) . toHaveLength ( 0 ) ;
236254 } ) ;
237255
238256 test ( "Should require authentication" , async ( ) => {
0 commit comments