Skip to content

Commit

Permalink
Feature 1124 user table (#1145)
Browse files Browse the repository at this point in the history
* added create user function

* get and delete endpoints added

* added sst appstack test for users

* fixed sst bug and readded working appstack unit test

* uncommented additional test which was accidentally removed

* set up framework for unit tests - get_user test doesn't work yet

* added user test

* added all user tests

* actually added user tests

* cleaned up imports and added some comments

* removed some redundant code

* removed an additional redundant import

* finished docs and found some more redundency

* fixed vitest relative path issue

* removed random space

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

* remove more whitespace

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

---------

Co-authored-by: ssh51117 <ssh51117@users.noreply.github.com>
  • Loading branch information
ssh51117 and ssh51117 authored Mar 15, 2024
1 parent 265834b commit f7ad5af
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 34 deletions.
3 changes: 2 additions & 1 deletion serverless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"eslint": "^8.53.0",
"sst": "2.40.1",
"typescript": "^5.2.2",
"vitest": "^0.34.6"
"vitest": "^0.34.6",
"vite-tsconfig-paths": "4.3.1"
},
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.418.0",
Expand Down
3 changes: 2 additions & 1 deletion serverless/packages/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"@types/aws-lambda": "^8.10.119",
"@types/node": "^20.5.0",
"sst": "^2.24.3",
"vitest": "^0.34.1"
"vitest": "^0.34.1",
"vite-tsconfig-paths": "4.3.1"
},
"dependencies": {
"uuid": "^9.0.1"
Expand Down
4 changes: 2 additions & 2 deletions serverless/packages/functions/src/user/create_user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { APIGatewayProxyHandlerV2, APIGatewayProxyEventV2 } from "aws-lambda";
import parseJwt from "../../../core/src/parseJwt";
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';
Expand Down Expand Up @@ -32,7 +32,7 @@ export async function handler<APIGatewayProxyHandlerV2>(event : APIGatewayProxyE

const command = new PutCommand(putCommandInput);
const response = await docClient.send(command);

if (response.$metadata.httpStatusCode != 200) {
return {
statusCode: 500,
Expand Down
2 changes: 1 addition & 1 deletion serverless/packages/functions/src/user/delete_user.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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";
import parseJwt from "@dlp-sst-app/core/src/parseJwt";

export async function handler<APIGatewayProxyHandlerV2>(event : APIGatewayProxyEventV2) {
if (event) {
Expand Down
2 changes: 1 addition & 1 deletion serverless/packages/functions/src/user/get_user.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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";
import parseJwt from "@dlp-sst-app/core/src/parseJwt";

export async function handler<APIGatewayProxyHandlerV2>(event : APIGatewayProxyEventV2) {
if (event)
Expand Down
14 changes: 4 additions & 10 deletions serverless/packages/functions/src/user/tests/create_user.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { APIGatewayProxyHandlerV2, APIGatewayProxyEventV2 } from "aws-lambda";
import { 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 { DynamoDBDocumentClient, 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 () => {
vi.mock('@dlp-sst-app/core/src/parseJwt', async () => {
return {
default: vi.fn().mockImplementation(input => input),
}
Expand All @@ -20,9 +18,6 @@ beforeEach(async () => {

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: {
Expand All @@ -40,7 +35,6 @@ it("test successful create user call", async () => {
' "phone": "123-456-7890"\n' +
'}',
}

const result = await handler(event);
expect(result.statusCode).toEqual(200);
});
Expand Down
13 changes: 4 additions & 9 deletions serverless/packages/functions/src/user/tests/delete_user.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { APIGatewayProxyHandlerV2, APIGatewayProxyEventV2 } from "aws-lambda";
import { 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 { DeleteCommand, DynamoDBDocumentClient } 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 () => {
vi.mock('@dlp-sst-app/core/src/parseJwt', async () => {
return {
default: vi.fn().mockImplementation(input => input),
}
Expand All @@ -20,9 +18,6 @@ beforeEach(async () => {

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: {
Expand Down
13 changes: 4 additions & 9 deletions serverless/packages/functions/src/user/tests/get_user.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { APIGatewayProxyHandlerV2, APIGatewayProxyEventV2 } from "aws-lambda";
import { 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 { DynamoDBDocumentClient, GetCommand } from '@aws-sdk/lib-dynamodb';
import { mockClient } from 'aws-sdk-client-mock';
import { handler } from '../get_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 () => {
vi.mock('@dlp-sst-app/core/src/parseJwt', async () => {
return {
default: vi.fn().mockImplementation(input => input),
}
Expand All @@ -19,9 +17,6 @@ beforeEach(async () => {

const ddbMock = mockClient(DynamoDBDocumentClient);

const dynamodb = new DynamoDBClient({});
const ddb = DynamoDBDocumentClient.from(dynamodb);

it("test successful get user call", async () => {
ddbMock.on(GetCommand).resolves({
Item: { user_id: { S: 'UID' } }
Expand Down
10 changes: 10 additions & 0 deletions serverless/packages/functions/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'vite'
import path from 'path';

export default defineConfig({
resolve: {
alias: {
'@dlp-sst-app': path.resolve(__dirname, './Deep-Learning-Playground/serverless')
}
}
})

0 comments on commit f7ad5af

Please sign in to comment.