-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'nextjs' into feature-1137-terraform-ecs
- Loading branch information
Showing
14 changed files
with
2,539 additions
and
1,320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export default function parseJwt(token: string) { | ||
return JSON.parse(Buffer.from(token.split(".")[1], "base64").toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,4 @@ | |
"dependencies": { | ||
"uuid": "^9.0.1" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { APIGatewayProxyHandlerV2, APIGatewayProxyEventV2 } from "aws-lambda"; | ||
import parseJwt from "../../../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'; | ||
|
||
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 = { | ||
TableName: "UserTable", | ||
Item: | ||
{ | ||
user_id: user_id, | ||
name: eventBody['name'], | ||
email: eventBody['email'], | ||
phone: eventBody['phone'] | ||
} | ||
} | ||
|
||
if (putCommandInput == null) | ||
{ | ||
return { | ||
statusCode: 400, | ||
body: JSON.stringify({ message: "Invalid request body" }) | ||
} | ||
} | ||
|
||
const client = new DynamoDBClient({}); | ||
const docClient = DynamoDBDocumentClient.from(client); | ||
|
||
const command = new PutCommand(putCommandInput); | ||
const response = await docClient.send(command); | ||
|
||
if (response.$metadata.httpStatusCode != 200) { | ||
return { | ||
statusCode: 500, | ||
body: JSON.stringify({ message: "Internal server error."}) | ||
}; | ||
} | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ user_id: user_id, message: "Successfully created a new user."}) | ||
}; | ||
} | ||
return { | ||
statusCode: 404, | ||
body: JSON.stringify({ message: "Not Found" }), | ||
}; | ||
}; | ||
function removeUndefinedValues(obj: { [key: string]: any }) { | ||
const newObj: { [key: string]: any } = {}; | ||
for (const key in obj) { | ||
if (obj[key] !== undefined) { | ||
newObj[key] = obj[key]; | ||
} | ||
} | ||
return newObj; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { APIGatewayProxyHandlerV2, APIGatewayProxyEventV2 } from "aws-lambda"; | ||
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; | ||
import { DynamoDBDocumentClient, DeleteCommand } from '@aws-sdk/lib-dynamodb'; | ||
import parseJwt from "../../../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({ | ||
TableName : "UserTable", | ||
Key : | ||
{ | ||
user_id: user_id | ||
} | ||
}); | ||
|
||
const response = await docClient.send(command); | ||
|
||
if (response.$metadata.httpStatusCode == undefined || response.$metadata.httpStatusCode != 200) | ||
{ | ||
return { | ||
statusCode: 404, | ||
body: JSON.stringify({ message : "Delete operation failed" }) | ||
} | ||
} | ||
return { | ||
statusCode: 200, | ||
body: "Successfully deleted user with id " + user_id | ||
} | ||
} | ||
return { | ||
statusCode: 400, | ||
body: JSON.stringify({ message : "Malformed request content" }), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { APIGatewayProxyHandlerV2, APIGatewayProxyEventV2 } from "aws-lambda"; | ||
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; | ||
import { DynamoDBDocumentClient, GetCommand } from '@aws-sdk/lib-dynamodb'; | ||
import parseJwt from "../../../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({ | ||
TableName : "UserTable", | ||
Key : | ||
{ | ||
user_id : user_id | ||
} | ||
}); | ||
|
||
const response = await docClient.send(command); | ||
|
||
if (!response.Item) | ||
{ | ||
return { | ||
statusCode: 404, | ||
body: JSON.stringify({message: "Provided User ID does not exist"}) | ||
} | ||
} | ||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({message: "Successfully retrieved User data", user: response.Item}) | ||
} | ||
} | ||
return { | ||
statusCode: 400, | ||
body: JSON.stringify({message: "Malformed request content"}) | ||
}; | ||
} |
78 changes: 78 additions & 0 deletions
78
serverless/packages/functions/src/user/tests/create_user.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { APIGatewayProxyHandlerV2, APIGatewayProxyEventV2 } from "aws-lambda"; | ||
import { beforeEach, expect, it, vi} from "vitest"; | ||
import {DynamoDBClient} from '@aws-sdk/client-dynamodb'; | ||
import {DeleteCommand, DynamoDBDocumentClient, GetCommand, PutCommand } from '@aws-sdk/lib-dynamodb'; | ||
import {mockClient} from 'aws-sdk-client-mock'; | ||
import { handler } from '../create_user'; | ||
import 'aws-sdk-client-mock-vitest'; | ||
|
||
|
||
//mocks parseJwt so that the call just returns whatever the input is | ||
vi.mock('../../../../core/src/parseJwt', async () => { | ||
return { | ||
default: vi.fn().mockImplementation(input => input), | ||
} | ||
}) | ||
|
||
beforeEach(async () => { | ||
ddbMock.reset(); | ||
}) | ||
|
||
const ddbMock = mockClient(DynamoDBDocumentClient); | ||
|
||
const dynamodb = new DynamoDBClient({}); | ||
const ddb = DynamoDBDocumentClient.from(dynamodb); | ||
|
||
it("test successful create user call", async () => { | ||
ddbMock.on(PutCommand).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 | ||
const event: APIGatewayProxyEventV2 = { | ||
headers: { | ||
authorization: 'abcd', | ||
}, | ||
body: '{\n' + | ||
' "name": "SETH SHI BUT UPDATED",\n' + | ||
' "email": "[email protected]",\n' + | ||
' "phone": "123-456-7890"\n' + | ||
'}', | ||
} | ||
|
||
const result = await handler(event); | ||
expect(result.statusCode).toEqual(200); | ||
}); | ||
|
||
it("test internal service error", async () => { | ||
ddbMock.on(PutCommand).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 | ||
const event: APIGatewayProxyEventV2 = { | ||
headers: { | ||
authorization: 'abcd', | ||
}, | ||
body: '{\n' + | ||
' "name": "SETH SHI BUT UPDATED",\n' + | ||
' "email": "[email protected]",\n' + | ||
' "phone": "123-456-7890"\n' + | ||
'}', | ||
} | ||
|
||
const result = await handler(event); | ||
expect(result.statusCode).toEqual(500); | ||
}); | ||
|
||
it("test undefined event", async () => { | ||
ddbMock.on(PutCommand).resolves({ | ||
$metadata: { | ||
httpStatusCode: 400, | ||
} | ||
}) | ||
const result = await handler(undefined); | ||
expect(result.statusCode).toEqual(404); | ||
}); |
95 changes: 95 additions & 0 deletions
95
serverless/packages/functions/src/user/tests/delete_user.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { APIGatewayProxyHandlerV2, APIGatewayProxyEventV2 } from "aws-lambda"; | ||
import { beforeEach, expect, it, vi} from "vitest"; | ||
import {DynamoDBClient} from '@aws-sdk/client-dynamodb'; | ||
import {DeleteCommand, DynamoDBDocumentClient, GetCommand, PutCommand } from '@aws-sdk/lib-dynamodb'; | ||
import {mockClient} from 'aws-sdk-client-mock'; | ||
import { handler } from '../delete_user'; | ||
import 'aws-sdk-client-mock-vitest'; | ||
|
||
|
||
//mocks parseJwt so that the call just returns whatever the input is | ||
vi.mock('../../../../core/src/parseJwt', async () => { | ||
return { | ||
default: vi.fn().mockImplementation(input => input), | ||
} | ||
}) | ||
|
||
beforeEach(async () => { | ||
ddbMock.reset(); | ||
}) | ||
|
||
const ddbMock = mockClient(DynamoDBDocumentClient); | ||
|
||
const dynamodb = new DynamoDBClient({}); | ||
const ddb = DynamoDBDocumentClient.from(dynamodb); | ||
|
||
it("test successful delete user call", async () => { | ||
ddbMock.on(DeleteCommand).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 | ||
const event: APIGatewayProxyEventV2 = { | ||
headers: { | ||
authorization: 'abcd', | ||
}, | ||
body: '{\n' + | ||
' "name": "SETH SHI BUT UPDATED",\n' + | ||
' "email": "[email protected]",\n' + | ||
' "phone": "123-456-7890"\n' + | ||
'}', | ||
} | ||
|
||
const result = await handler(event); | ||
expect(result.statusCode).toEqual(200); | ||
}); | ||
|
||
it("test no response failed operation call", async () => { | ||
ddbMock.on(DeleteCommand).resolves({ | ||
$metadata: { | ||
httpStatusCode: undefined, | ||
} | ||
}) | ||
|
||
const event: APIGatewayProxyEventV2 = { | ||
headers: { | ||
authorization: 'abcd', | ||
}, | ||
body: '{\n' + | ||
' "name": "SETH SHI BUT UPDATED",\n' + | ||
' "email": "[email protected]",\n' + | ||
' "phone": "123-456-7890"\n' + | ||
'}', | ||
} | ||
|
||
const result = await handler(event); | ||
expect(result.statusCode).toEqual(404); | ||
}); | ||
|
||
it("test different status code failed operation call", async () => { | ||
ddbMock.on(DeleteCommand).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 | ||
const event: APIGatewayProxyEventV2 = { | ||
headers: { | ||
authorization: 'abcd', | ||
}, | ||
body: '{\n' + | ||
' "name": "SETH SHI BUT UPDATED",\n' + | ||
' "email": "[email protected]",\n' + | ||
' "phone": "123-456-7890"\n' + | ||
'}', | ||
} | ||
|
||
const result = await handler(event); | ||
expect(result.statusCode).toEqual(404); | ||
}); | ||
|
||
it("test malformed call", async () => { | ||
const result = await handler(undefined); | ||
expect(result.statusCode).toEqual(400); | ||
}); |
Oops, something went wrong.