From f69cdf403cdd6cda87c96f31fa7f23785961da3d Mon Sep 17 00:00:00 2001 From: ssh51117 <116283570+ssh51117@users.noreply.github.com> Date: Sat, 18 May 2024 01:38:33 -0400 Subject: [PATCH] Bug 1159-user endpoint updates (#1163) * almost working endpoints * working delete all endpoint * updated tests and renamed delete_all to follow formatting * minor updates to user endpoints and tests * unduplicated code in comments * trying to pass duplicated code * trying to pass duplicated code check * :art: Auto-generated directory tree for repository in Architecture.md * trying to fix duplications * attempt again * another attempt --------- Co-authored-by: ssh51117 --- .../tests/create_trainspace.test.ts | 8 ++++--- .../tests/delete_all_trainspace.test.ts | 11 +++++----- .../tests/delete_trainspace.test.ts | 10 ++++----- .../tests/get_all_trainspace.test.ts | 6 +++--- .../trainspace/tests/get_trainspace.test.ts | 8 +++---- .../functions/src/user/create_user.ts | 18 +++++++--------- .../functions/src/user/delete_user.ts | 12 +++++------ .../packages/functions/src/user/get_user.ts | 12 +++++------ .../src/user/tests/create_user.test.ts | 18 +++++++++------- .../src/user/tests/delete_user.test.ts | 21 ++++++++++--------- .../functions/src/user/tests/get_user.test.ts | 14 ++++++------- 11 files changed, 68 insertions(+), 70 deletions(-) diff --git a/serverless/packages/functions/src/trainspace/tests/create_trainspace.test.ts b/serverless/packages/functions/src/trainspace/tests/create_trainspace.test.ts index 5eab2c16..ed03877c 100644 --- a/serverless/packages/functions/src/trainspace/tests/create_trainspace.test.ts +++ b/serverless/packages/functions/src/trainspace/tests/create_trainspace.test.ts @@ -4,6 +4,8 @@ import { DynamoDBClient, PutItemCommand} from '@aws-sdk/client-dynamodb'; import { mockClient } from 'aws-sdk-client-mock'; import { handler } from '../create_trainspace'; +//note: event declaration errors are supressed becasue we only need +// certain parts of them for that specific function //mocks parseJwt so that the call just returns whatever the input is vi.mock('@dlp-sst-app/core/src/parseJwt', async () => { return { @@ -23,7 +25,7 @@ it("test successful create trainspace call", async () => { httpStatusCode: 200, } }) - // @ts-expect-error : error doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error : we only need authorization and body const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -48,7 +50,7 @@ it("test internal service error", async () => { httpStatusCode: 456, } }) - // @ts-expect-error : error doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -73,7 +75,7 @@ it("test undefined event", async () => { httpStatusCode: 400, } }) - // @ts-expect-error : we are trying to cause an error + //@ts-expect-error : see test name const result = await handler(undefined); expect(result.statusCode).toEqual(404); }); \ No newline at end of file diff --git a/serverless/packages/functions/src/trainspace/tests/delete_all_trainspace.test.ts b/serverless/packages/functions/src/trainspace/tests/delete_all_trainspace.test.ts index d137ee3f..8fd16a11 100644 --- a/serverless/packages/functions/src/trainspace/tests/delete_all_trainspace.test.ts +++ b/serverless/packages/functions/src/trainspace/tests/delete_all_trainspace.test.ts @@ -4,7 +4,6 @@ import { DynamoDBClient, QueryCommand, BatchExecuteStatementCommand } from '@aws import { mockClient } from 'aws-sdk-client-mock'; import { handler } from '../delete_all_trainspace'; - //mocks parseJwt so that the call just returns whatever the input is vi.mock('@dlp-sst-app/core/src/parseJwt', async () => { return { @@ -31,7 +30,7 @@ it("test successful delete all trainspace call", async () => { } }) - // @ts-expect-error : error doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error : we only need the authorization const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -56,7 +55,7 @@ it("test no batch delete response call", async () => { } }) - // @ts-expect-error : error doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error : see above note const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -81,7 +80,7 @@ it("test incorrect batch delete response failed call", async () => { } }) - // @ts-expect-error : error doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -104,7 +103,7 @@ it("test delete all on no existing trainspaces call", async () => { } }) - // @ts-expect-error : error doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -117,7 +116,7 @@ it("test delete all on no existing trainspaces call", async () => { it("test malformed call", async () => { - // @ts-expect-error : we are trying to cause an error + //@ts-expect-error : testing call with undefined parameters const result = await handler(undefined); expect(result.statusCode).toEqual(400); }); \ No newline at end of file diff --git a/serverless/packages/functions/src/trainspace/tests/delete_trainspace.test.ts b/serverless/packages/functions/src/trainspace/tests/delete_trainspace.test.ts index e0d840a7..53cb7b7d 100644 --- a/serverless/packages/functions/src/trainspace/tests/delete_trainspace.test.ts +++ b/serverless/packages/functions/src/trainspace/tests/delete_trainspace.test.ts @@ -23,7 +23,7 @@ it("test successful delete trainspace call", async () => { httpStatusCode: 200, } }) - // @ts-expect-error : error doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error : we only need authorization, id, and a body const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -50,7 +50,7 @@ it("test no response failed operation call", async () => { } }) - // @ts-expect-error : error doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error : same as above const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -76,7 +76,7 @@ it("test different status code failed operation call", async () => { httpStatusCode: 267, } }) - // @ts-expect-error : error doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error : see above const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -102,7 +102,7 @@ it("test no trainspace id given", async () => { } }) - // @ts-expect-error : error doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error : we are giving an empty event const event: APIGatewayProxyEventV2 = { pathParameters: { } @@ -114,7 +114,7 @@ it("test no trainspace id given", async () => { it("test malformed call", async () => { - // @ts-expect-error : we are trying to cause an error + // @ts-expect-error : testing for null parameter response const result = await handler(undefined); expect(result.statusCode).toEqual(400); }); \ No newline at end of file diff --git a/serverless/packages/functions/src/trainspace/tests/get_all_trainspace.test.ts b/serverless/packages/functions/src/trainspace/tests/get_all_trainspace.test.ts index 00ba8dfc..894da762 100644 --- a/serverless/packages/functions/src/trainspace/tests/get_all_trainspace.test.ts +++ b/serverless/packages/functions/src/trainspace/tests/get_all_trainspace.test.ts @@ -25,7 +25,7 @@ it("test successful get all trainspace call", async () => { }], "Count": 4 }); - // @ts-expect-error : error doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -42,7 +42,7 @@ it("test no existing trainspaces for user id", async () => { Items: undefined }) - // @ts-expect-error : error doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -54,7 +54,7 @@ it("test no existing trainspaces for user id", async () => { }); it("test malformed request", async () => { - // @ts-expect-error : we are trying to cause an error + //@ts-expect-error const result = await handler(undefined); expect(result.statusCode).toEqual(400); }); \ No newline at end of file diff --git a/serverless/packages/functions/src/trainspace/tests/get_trainspace.test.ts b/serverless/packages/functions/src/trainspace/tests/get_trainspace.test.ts index fe8f7e2c..6aead77d 100644 --- a/serverless/packages/functions/src/trainspace/tests/get_trainspace.test.ts +++ b/serverless/packages/functions/src/trainspace/tests/get_trainspace.test.ts @@ -23,7 +23,7 @@ it("test successful get trainspace call", async () => { Item: { trainspaceID: { S: 'sample trainspace id' } } }) - // @ts-expect-error : error doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error const event: APIGatewayProxyEventV2 = { pathParameters: { id: "some trainspace_id" @@ -40,7 +40,7 @@ it("test no existing trainspace id", async () => { Item: undefined }) - // @ts-expect-error : error doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error const event: APIGatewayProxyEventV2 = { pathParameters: { id: "some trainspace_id" @@ -56,7 +56,7 @@ it("test no trainspace id given", async () => { Item: { trainspaceID: { S: 'sample trainspace id' } } }) - // @ts-expect-error : error doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error const event: APIGatewayProxyEventV2 = { pathParameters: { } @@ -67,7 +67,7 @@ it("test no trainspace id given", async () => { }); it("test malformed request", async () => { - // @ts-expect-error : we are trying to cause an error + //@ts-expect-error const result = await handler(undefined); expect(result.statusCode).toEqual(400); }); \ No newline at end of file diff --git a/serverless/packages/functions/src/user/create_user.ts b/serverless/packages/functions/src/user/create_user.ts index 3072563c..5419cc67 100644 --- a/serverless/packages/functions/src/user/create_user.ts +++ b/serverless/packages/functions/src/user/create_user.ts @@ -1,21 +1,20 @@ import { APIGatewayProxyHandlerV2, APIGatewayProxyEventV2 } from "aws-lambda"; import parseJwt from "@dlp-sst-app/core/src/parseJwt"; import { v4 as uuidv4 } from 'uuid'; -import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; -import { DynamoDBDocumentClient, PutCommand, PutCommandInput } from '@aws-sdk/lib-dynamodb'; +import { DynamoDBClient, PutItemCommand, PutItemCommandInput } from '@aws-sdk/client-dynamodb'; export async function handler(event : APIGatewayProxyEventV2) { if (event) { const user_id: string = parseJwt(event.headers.authorization ?? "")["user_id"]; const eventBody = JSON.parse(event.body? event.body : ""); - let putCommandInput: PutCommandInput = { + let putCommandInput: PutItemCommandInput = { TableName: "UserTable", Item: { - user_id: user_id, - name: eventBody['name'], - email: eventBody['email'], - phone: eventBody['phone'] + user_id: {"S": user_id}, + name: {"S": eventBody['name']}, + email: {"S": eventBody['email']}, + phone: {"S": eventBody['phone']} } } @@ -28,10 +27,9 @@ export async function handler(event : APIGatewayProxyE } const client = new DynamoDBClient({}); - const docClient = DynamoDBDocumentClient.from(client); - const command = new PutCommand(putCommandInput); - const response = await docClient.send(command); + const command = new PutItemCommand(putCommandInput); + const response = await client.send(command); if (response.$metadata.httpStatusCode != 200) { return { diff --git a/serverless/packages/functions/src/user/delete_user.ts b/serverless/packages/functions/src/user/delete_user.ts index fdcb216f..315678ea 100644 --- a/serverless/packages/functions/src/user/delete_user.ts +++ b/serverless/packages/functions/src/user/delete_user.ts @@ -1,6 +1,5 @@ -import { APIGatewayProxyHandlerV2, APIGatewayProxyEventV2 } from "aws-lambda"; -import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; -import { DynamoDBDocumentClient, DeleteCommand } from '@aws-sdk/lib-dynamodb'; +import { APIGatewayProxyEventV2 } from "aws-lambda"; +import { DynamoDBClient, DeleteItemCommand } from '@aws-sdk/client-dynamodb'; import parseJwt from "@dlp-sst-app/core/src/parseJwt"; export async function handler(event : APIGatewayProxyEventV2) { @@ -8,17 +7,16 @@ export async function handler(event : APIGatewayProxyE const user_id: string = parseJwt(event.headers.authorization ?? "")["user_id"]; const client = new DynamoDBClient({}); - const docClient = DynamoDBDocumentClient.from(client); - const command = new DeleteCommand({ + const command = new DeleteItemCommand({ TableName : "UserTable", Key : { - user_id: user_id + user_id: {"S": user_id} } }); - const response = await docClient.send(command); + const response = await client.send(command); if (response.$metadata.httpStatusCode == undefined || response.$metadata.httpStatusCode != 200) { diff --git a/serverless/packages/functions/src/user/get_user.ts b/serverless/packages/functions/src/user/get_user.ts index ada5d2f4..fa2886c6 100644 --- a/serverless/packages/functions/src/user/get_user.ts +++ b/serverless/packages/functions/src/user/get_user.ts @@ -1,6 +1,5 @@ -import { APIGatewayProxyHandlerV2, APIGatewayProxyEventV2 } from "aws-lambda"; -import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; -import { DynamoDBDocumentClient, GetCommand } from '@aws-sdk/lib-dynamodb'; +import { APIGatewayProxyEventV2 } from "aws-lambda"; +import { DynamoDBClient, GetItemCommand } from '@aws-sdk/client-dynamodb'; import parseJwt from "@dlp-sst-app/core/src/parseJwt"; export async function handler(event : APIGatewayProxyEventV2) { @@ -8,17 +7,16 @@ export async function handler(event : APIGatewayProxyE { const user_id: string = parseJwt(event.headers.authorization ?? "")["user_id"]; const client: DynamoDBClient = new DynamoDBClient({}); - const docClient = DynamoDBDocumentClient.from(client); - const command : GetCommand = new GetCommand({ + const command : GetItemCommand = new GetItemCommand({ TableName : "UserTable", Key : { - user_id : user_id + user_id : {"S": user_id} } }); - const response = await docClient.send(command); + const response = await client.send(command); if (!response.Item) { diff --git a/serverless/packages/functions/src/user/tests/create_user.test.ts b/serverless/packages/functions/src/user/tests/create_user.test.ts index 215a43c4..4ea3b845 100644 --- a/serverless/packages/functions/src/user/tests/create_user.test.ts +++ b/serverless/packages/functions/src/user/tests/create_user.test.ts @@ -1,10 +1,11 @@ import { APIGatewayProxyEventV2 } from "aws-lambda"; import { beforeEach, expect, it, vi} from "vitest"; -import { DynamoDBDocumentClient, PutCommand } from '@aws-sdk/lib-dynamodb'; +import { DynamoDBClient, PutItemCommand } from '@aws-sdk/client-dynamodb'; import { mockClient } from 'aws-sdk-client-mock'; import { handler } from '../create_user'; - +//note: event declaration errors are supressed becasue we only need +//certain parts of them for that specific function //mocks parseJwt so that the call just returns whatever the input is vi.mock('@dlp-sst-app/core/src/parseJwt', async () => { return { @@ -16,15 +17,15 @@ beforeEach(async () => { ddbMock.reset(); }) -const ddbMock = mockClient(DynamoDBDocumentClient); +const ddbMock = mockClient(DynamoDBClient); it("test successful create user call", async () => { - ddbMock.on(PutCommand).resolves({ + ddbMock.on(PutItemCommand).resolves({ $metadata: { httpStatusCode: 200, } }) - //error is fine, doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -40,12 +41,12 @@ it("test successful create user call", async () => { }); it("test internal service error", async () => { - ddbMock.on(PutCommand).resolves({ + ddbMock.on(PutItemCommand).resolves({ $metadata: { httpStatusCode: 456, } }) - //error is fine, doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -62,11 +63,12 @@ it("test internal service error", async () => { }); it("test undefined event", async () => { - ddbMock.on(PutCommand).resolves({ + ddbMock.on(PutItemCommand).resolves({ $metadata: { httpStatusCode: 400, } }) + //@ts-expect-error const result = await handler(undefined); expect(result.statusCode).toEqual(404); }); \ No newline at end of file diff --git a/serverless/packages/functions/src/user/tests/delete_user.test.ts b/serverless/packages/functions/src/user/tests/delete_user.test.ts index 5809e35e..709ed395 100644 --- a/serverless/packages/functions/src/user/tests/delete_user.test.ts +++ b/serverless/packages/functions/src/user/tests/delete_user.test.ts @@ -1,10 +1,9 @@ import { APIGatewayProxyEventV2 } from "aws-lambda"; import { beforeEach, expect, it, vi} from "vitest"; -import { DeleteCommand, DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'; +import { DynamoDBClient, DeleteItemCommand } from '@aws-sdk/client-dynamodb'; import { mockClient } from 'aws-sdk-client-mock'; import { handler } from '../delete_user'; - //mocks parseJwt so that the call just returns whatever the input is vi.mock('@dlp-sst-app/core/src/parseJwt', async () => { return { @@ -16,15 +15,15 @@ beforeEach(async () => { ddbMock.reset(); }) -const ddbMock = mockClient(DynamoDBDocumentClient); +const ddbMock = mockClient(DynamoDBClient); it("test successful delete user call", async () => { - ddbMock.on(DeleteCommand).resolves({ + ddbMock.on(DeleteItemCommand).resolves({ $metadata: { httpStatusCode: 200, } }) - //error is fine, doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -41,12 +40,13 @@ it("test successful delete user call", async () => { }); it("test no response failed operation call", async () => { - ddbMock.on(DeleteCommand).resolves({ + ddbMock.on(DeleteItemCommand).resolves({ $metadata: { httpStatusCode: undefined, } }) + //@ts-expect-error const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -63,12 +63,12 @@ it("test no response failed operation call", async () => { }); it("test different status code failed operation call", async () => { - ddbMock.on(DeleteCommand).resolves({ + ddbMock.on(DeleteItemCommand).resolves({ $metadata: { httpStatusCode: 267, } }) - //error is fine, doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -85,6 +85,7 @@ it("test different status code failed operation call", async () => { }); it("test malformed call", async () => { - const result = await handler(undefined); - expect(result.statusCode).toEqual(400); + //@ts-expect-error + const result = await handler(undefined); + expect(result.statusCode).toEqual(400); }); \ No newline at end of file diff --git a/serverless/packages/functions/src/user/tests/get_user.test.ts b/serverless/packages/functions/src/user/tests/get_user.test.ts index 661b122b..1107017c 100644 --- a/serverless/packages/functions/src/user/tests/get_user.test.ts +++ b/serverless/packages/functions/src/user/tests/get_user.test.ts @@ -1,6 +1,6 @@ import { APIGatewayProxyEventV2 } from "aws-lambda"; import { beforeEach, expect, it, vi} from "vitest"; -import { DynamoDBDocumentClient, GetCommand } from '@aws-sdk/lib-dynamodb'; +import { DynamoDBClient, GetItemCommand } from '@aws-sdk/client-dynamodb'; import { mockClient } from 'aws-sdk-client-mock'; import { handler } from '../get_user'; @@ -15,14 +15,14 @@ beforeEach(async () => { ddbMock.reset(); }) -const ddbMock = mockClient(DynamoDBDocumentClient); +const ddbMock = mockClient(DynamoDBClient); it("test successful get user call", async () => { - ddbMock.on(GetCommand).resolves({ + ddbMock.on(GetItemCommand).resolves({ Item: { user_id: { S: 'UID' } } }) - //error is fine, doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -34,11 +34,11 @@ it("test successful get user call", async () => { }); it("test no existing user id", async () => { - ddbMock.on(GetCommand).resolves({ + ddbMock.on(GetItemCommand).resolves({ Item: undefined }) - //error is fine, doesn't affect functionality. We don't need the rest of the event, and it's really long for no reason + //@ts-expect-error const event: APIGatewayProxyEventV2 = { headers: { authorization: 'abcd', @@ -50,7 +50,7 @@ it("test no existing user id", async () => { }); it("test malformed request", async () => { - + //@ts-expect-error const result = await handler(undefined); expect(result.statusCode).toEqual(400); }); \ No newline at end of file