Skip to content

Commit

Permalink
Bug 1159-user endpoint updates (#1163)
Browse files Browse the repository at this point in the history
* 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

* 🎨 Auto-generated directory tree for repository in Architecture.md

* trying to fix duplications

* attempt again

* another attempt

---------

Co-authored-by: ssh51117 <[email protected]>
  • Loading branch information
ssh51117 and ssh51117 authored May 18, 2024
1 parent bed1107 commit f69cdf4
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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: {
}
Expand All @@ -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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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: {
}
Expand All @@ -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);
});
18 changes: 8 additions & 10 deletions serverless/packages/functions/src/user/create_user.ts
Original file line number Diff line number Diff line change
@@ -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<APIGatewayProxyHandlerV2>(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']}
}
}

Expand All @@ -28,10 +27,9 @@ export async function handler<APIGatewayProxyHandlerV2>(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 {
Expand Down
12 changes: 5 additions & 7 deletions serverless/packages/functions/src/user/delete_user.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
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<APIGatewayProxyHandlerV2>(event : APIGatewayProxyEventV2) {
if (event) {
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)
{
Expand Down
12 changes: 5 additions & 7 deletions serverless/packages/functions/src/user/get_user.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
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<APIGatewayProxyHandlerV2>(event : APIGatewayProxyEventV2) {
if (event)
{
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)
{
Expand Down
18 changes: 10 additions & 8 deletions serverless/packages/functions/src/user/tests/create_user.test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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);
});
Loading

0 comments on commit f69cdf4

Please sign in to comment.