Config ci cd (#5)

* feat: Add GitHub Actions workflow for Amplify deployment and configuration

* feat: Update Amplify deployment workflow and add Serverless deployment workflow

* feat: Set environment variables for Amplify monorepo deployment

* feat: Refactor Amplify deployment configuration for monorepo structure

* feat: Update environment variable naming and refactor image URL handling across the application

* feat: Add Jest configuration and tests for project and user handlers
This commit is contained in:
2024-11-26 16:37:33 +07:00
parent 11e61829f1
commit e293ab3c2c
31 changed files with 4864 additions and 75 deletions

110
.github/workflows/amplify-deployment.yml vendored Normal file
View File

@@ -0,0 +1,110 @@
name: Set Amplify Environment Variables and Trigger Deployment
permissions:
id-token: write
contents: read
on:
push:
branches:
- main
paths:
- "tasker-client/**"
workflow_dispatch: # Manual trigger
env:
APPLICATION_NAME: tasker
jobs:
deploy-amplify:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Verify AWS CLI Installation
run: aws --version
- name: Install Amplify CLI
run: npm install -g @aws-amplify/cli
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v3
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Fetch Environment Variables from AWS SSM
run: |
export NEXT_PUBLIC_API_BASE_URL=$(aws ssm get-parameter --name "/tasker/api/base-url" --query "Parameter.Value" --output text || exit 1)
export NEXT_PUBLIC_COGNITO_USER_POOL_ID=$(aws ssm get-parameter --name "/tasker/cognito/user-pool-id" --query "Parameter.Value" --output text || exit 1)
export NEXT_PUBLIC_COGNITO_USER_POOL_CLIENT_ID=$(aws ssm get-parameter --name "/tasker/cognito/client-id" --query "Parameter.Value" --output text || exit 1)
export NEXT_PUBLIC_S3_PUBLIC_IMAGE_URL=$(aws ssm get-parameter --name "/tasker/s3/public-images-url" --query "Parameter.Value" --output text || exit 1)
echo "NEXT_PUBLIC_API_BASE_URL=$NEXT_PUBLIC_API_BASE_URL" >> $GITHUB_ENV
echo "NEXT_PUBLIC_COGNITO_USER_POOL_ID=$NEXT_PUBLIC_COGNITO_USER_POOL_ID" >> $GITHUB_ENV
echo "NEXT_PUBLIC_COGNITO_USER_POOL_CLIENT_ID=$NEXT_PUBLIC_COGNITO_USER_POOL_CLIENT_ID" >> $GITHUB_ENV
echo "NEXT_PUBLIC_S3_PUBLIC_IMAGE_URL=$NEXT_PUBLIC_S3_PUBLIC_IMAGE_URL" >> $GITHUB_ENV
- name: Load Build Spec from `tasker-client/amplify.json`
run: |
build_spec=$(jq -c . tasker-client/amplify.json)
echo "build_spec=$build_spec" >> $GITHUB_ENV
- name: Initialize or Update Amplify App
id: amplify
run: |
echo "Fetching Amplify apps in region ${{ secrets.AWS_REGION }}..."
app_list=$(aws amplify list-apps --region ${{ secrets.AWS_REGION }})
app_id=$(echo "$app_list" | jq -r '.apps[] | select(.name == "${{ env.APPLICATION_NAME }}") | .appId')
if [ -n "$app_id" ]; then
echo "Amplify App already exists. Updating..."
echo "app_id=$app_id" >> $GITHUB_ENV
aws amplify update-app \
--app-id "$app_id" \
--environment-variables "NEXT_PUBLIC_API_BASE_URL=$NEXT_PUBLIC_API_BASE_URL,NEXT_PUBLIC_COGNITO_USER_POOL_ID=$NEXT_PUBLIC_COGNITO_USER_POOL_ID,NEXT_PUBLIC_COGNITO_USER_POOL_CLIENT_ID=$NEXT_PUBLIC_COGNITO_USER_POOL_CLIENT_ID,NEXT_PUBLIC_S3_PUBLIC_IMAGE_URL=$NEXT_PUBLIC_S3_PUBLIC_IMAGE_URL,AMPLIFY_MONOREPO_APP_ROOT=tasker-client,AMPLIFY_DIFF_DEPLOY=false" \
--build-spec "$build_spec"
else
echo "Creating a new Amplify app..."
formatted_repo_url=$(echo "${{ github.repositoryUrl }}" | sed 's|git://|https://|')
create_app=$(aws amplify create-app \
--name "${{ env.APPLICATION_NAME }}" \
--platform WEB_COMPUTE \
--repository "$formatted_repo_url" \
--environment-variables "NEXT_PUBLIC_API_BASE_URL=$NEXT_PUBLIC_API_BASE_URL,NEXT_PUBLIC_COGNITO_USER_POOL_ID=$NEXT_PUBLIC_COGNITO_USER_POOL_ID,NEXT_PUBLIC_COGNITO_USER_POOL_CLIENT_ID=$NEXT_PUBLIC_COGNITO_USER_POOL_CLIENT_ID,NEXT_PUBLIC_S3_PUBLIC_IMAGE_URL=$NEXT_PUBLIC_S3_PUBLIC_IMAGE_URL,AMPLIFY_MONOREPO_APP_ROOT=tasker-client,AMPLIFY_DIFF_DEPLOY=false" \
--iam-service-role-arn "${{ secrets.AWS_ROLE_TO_ASSUME }}" \
--region "${{ secrets.AWS_REGION }}" \
--access-token "${{ secrets.PAT_TOKEN }}" \
--build-spec "$build_spec" \
--output "json"
)
app_id=$(echo $create_app | jq -r '.app.appId')
echo "app_id=$app_id" >> $GITHUB_ENV
fi
- name: Create or Update Amplify Branch
run: |
echo "Checking if branch ${{ github.ref_name}} exists..."
branch_exists=$(aws amplify list-branches \
--app-id "$app_id" \
--query "branches[?branchName=='${{ github.ref_name}}'] | length(@)" \
--output text)
if [ "$branch_exists" -gt 0 ]; then
echo "Branch ${{ github.ref_name}} already exists. Skipping..."
else
echo "Branch ${{ github.ref_name}} does not exist. Creating it..."
aws amplify create-branch \
--app-id "$app_id" \
--branch-name "${{ github.ref_name}}"
fi
- name: Trigger Amplify Deployment
run: |
echo "Triggering deployment for branch ${{ github.ref_name}}..."
aws amplify start-job \
--app-id "$app_id" \
--branch-name "${{ github.ref_name}}" \
--job-type RELEASE

View File

@@ -0,0 +1,45 @@
name: Deploy with Serverless Framework
permissions:
id-token: write
contents: read
on:
push:
branches:
- main
paths:
- "tasker-server/**"
workflow_dispatch: # This is a manual trigger
jobs:
deploy-serverless:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Install Dependencies
working-directory: tasker-server
run: npm ci
- name: Run Tests
working-directory: tasker-server
run: npm run test:ci
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Deploy with Serverless GitHub Action
uses: serverless/github-action@v3.2
with:
args: -c "cd ./tasker-server && serverless deploy"
entrypoint: /bin/sh

View File

@@ -0,0 +1,35 @@
{
"version": 1,
"applications": [
{
"appRoot": "tasker-client",
"frontend": {
"phases": {
"preBuild": {
"commands": [
"npm ci"
]
},
"build": {
"commands": [
"npm run build"
]
}
},
"artifacts": {
"baseDirectory": ".next",
"files": [
"**/*"
]
},
"cache": {
"paths": [
"node_modules/**/*",
".next/cache/**/*",
".npm/**/*"
]
}
}
}
]
}

View File

@@ -1,7 +1,17 @@
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
/* config options here */
images: {
remotePatterns: [
{
protocol: "https",
hostname: new URL(process.env.NEXT_PUBLIC_S3_PUBLIC_IMAGE_URL || "")
.hostname,
port: "",
pathname: "/**",
},
],
},
};
export default nextConfig;

View File

@@ -1,6 +1,11 @@
import Modal from "@/app/components/Modal";
import { Priority, Status, useCreateTaskMutation } from "@/state/api";
import React, { useState } from "react";
import {
Priority,
Status,
useCreateTaskMutation,
useGetAuthUserQuery,
} from "@/state/api";
import React, { useEffect, useState } from "react";
import { formatISO } from "date-fns";
type Props = {
@@ -22,22 +27,28 @@ const ModalNewTask = ({ isOpen, onClose, id = null }: Props) => {
const [assignedUserId, setAssignedUserId] = useState("");
const [projectId, setProjectId] = useState("");
const { data: currentUser } = useGetAuthUserQuery({});
const userId = currentUser?.userDetails?.userId ?? null;
useEffect(() => {
setAuthorUserId(userId || "");
}, [userId]);
const handleSubmit = async () => {
console.log(title, authorUserId, id, projectId);
if (!(title && authorUserId && (id !== null || projectId))) return;
console.log("Creating task 1..");
if (
!(title && authorUserId && assignedUserId && (id !== null || projectId))
)
return;
console.log("Creating task 2...");
const finalAssignedUserId = assignedUserId.trim() || authorUserId;
const formattedStartDate = formatISO(new Date(startDate), {
representation: "complete",
});
const formattedDueDate = formatISO(new Date(dueDate), {
representation: "complete",
});
const formattedStartDate =
startDate ??
formatISO(new Date(startDate), {
representation: "complete",
});
const formattedDueDate =
dueDate ??
formatISO(new Date(dueDate), {
representation: "complete",
});
await createTask({
title,
@@ -48,16 +59,15 @@ const ModalNewTask = ({ isOpen, onClose, id = null }: Props) => {
startDate: formattedStartDate,
dueDate: formattedDueDate,
authorUserId: authorUserId,
assignedUserId: assignedUserId,
assignedUserId: finalAssignedUserId,
projectId: id !== null ? id : projectId,
});
onClose();
};
const isFormValid = () => {
console.log(title, authorUserId, id, projectId);
return (
title && authorUserId && assignedUserId && (id !== null || projectId)
);
return title && authorUserId && (id !== null || projectId);
};
const selectStyles =
@@ -92,9 +102,13 @@ const ModalNewTask = ({ isOpen, onClose, id = null }: Props) => {
<select
className={selectStyles}
value={status}
onChange={(e) =>
setStatus(Status[e.target.value as keyof typeof Status])
}
onChange={(e) => {
const selectedStatus = Object.entries(Status).find(
([, value]) => value === e.target.value,
)?.[0] as keyof typeof Status;
setStatus(selectedStatus ? Status[selectedStatus] : Status.ToDo);
}}
>
<option value="">Select Status</option>
<option value={Status.ToDo}>To Do</option>
@@ -139,13 +153,15 @@ const ModalNewTask = ({ isOpen, onClose, id = null }: Props) => {
onChange={(e) => setDueDate(e.target.value)}
/>
</div>
<input
type="text"
className={inputStyles}
placeholder="Author User ID"
value={authorUserId}
onChange={(e) => setAuthorUserId(e.target.value)}
/>
{authorUserId === "" && (
<input
type="text"
className={inputStyles}
placeholder="Author User ID"
value={authorUserId}
onChange={(e) => setAuthorUserId(e.target.value)}
/>
)}
<input
type="text"
className={inputStyles}

View File

@@ -77,7 +77,7 @@ const Navbar = () => {
<div className="align-center flex h-9 w-9 justify-center">
{!!currentUserDetails?.profilePictureUrl ? (
<Image
src={`${process.env.S3_PUBLIC_IMAGE_URL}/${currentUserDetails?.profilePictureUrl}`}
src={`${process.env.NEXT_PUBLIC_S3_PUBLIC_IMAGE_URL}/${currentUserDetails?.profilePictureUrl}`}
alt={currentUserDetails?.username || "User Profile Picture"}
width={100}
height={50}

View File

@@ -71,7 +71,7 @@ const Sidebar = () => {
</div>
<div className="flex items-center gap-5 border-y-[1.5px] border-gray-200 px-8 py-4 dark:border-gray-700">
<Image
src={`${process.env.S3_PUBLIC_IMAGE_URL}/logo.png`}
src={`${process.env.NEXT_PUBLIC_S3_PUBLIC_IMAGE_URL}/logo.png`}
alt="logo"
width={40}
height={40}
@@ -162,7 +162,7 @@ const Sidebar = () => {
<div className="align-center flex h-9 w-9 justify-center">
{!!currentUserDetails?.profilePictureUrl ? (
<Image
src={`${process.env.S3_PUBLIC_IMAGE_URL}/${currentUserDetails?.profilePictureUrl}`}
src={`${process.env.NEXT_PUBLIC_S3_PUBLIC_IMAGE_URL}/${currentUserDetails?.profilePictureUrl}`}
alt={currentUserDetails?.username || "User Profile Picture"}
width={100}
height={50}

View File

@@ -16,7 +16,7 @@ const TaskCard = ({ task }: Props) => {
<div className="flex flex-wrap">
{task.attachments && task.attachments.length > 0 && (
<Image
src={`${process.env.S3_PUBLIC_IMAGE_URL}/${task.attachments[0].fileURL}`}
src={`${process.env.NEXT_PUBLIC_S3_PUBLIC_IMAGE_URL}/${task.attachments[0].fileURL}`}
alt={task.attachments[0].fileName}
width={400}
height={200}

View File

@@ -11,7 +11,7 @@ const UserCard = ({ user }: Props) => {
<div className="flex items-center rounded border p-4 shadow">
{user.profilePictureUrl && (
<Image
src={`${process.env.S3_PUBLIC_IMAGE_URL}/${user.profilePictureUrl}`}
src={`${process.env.NEXT_PUBLIC_S3_PUBLIC_IMAGE_URL}/${user.profilePictureUrl}`}
alt="profile picture"
width={32}
height={32}

View File

@@ -24,6 +24,7 @@ const BoardView = ({ projectId, setIsModalNewTaskOpen }: BoardProps) => {
data: fetchedTasks,
isLoading,
error,
refetch,
} = useGetTasksQuery({ projectId });
const [updateTaskStatus] = useUpdateTaskStatusMutation();
const [tasks, setTasks] = useState<TaskType[]>([]);
@@ -43,6 +44,7 @@ const BoardView = ({ projectId, setIsModalNewTaskOpen }: BoardProps) => {
try {
await updateTaskStatus({ taskId, status: toStatus });
await refetch();
} catch (error) {
console.error("Failed to update task status:", error);
setTasks(fetchedTasks || []);
@@ -197,7 +199,7 @@ const Task = ({ task }: TaskProps) => {
>
{task.attachments && task.attachments.length > 0 && (
<Image
src={`${process.env.S3_PUBLIC_IMAGE_URL}/${task.attachments[0].fileURL}`}
src={`${process.env.NEXT_PUBLIC_S3_PUBLIC_IMAGE_URL}/${task.attachments[0].fileURL}`}
alt={task.attachments[0].fileName}
width={400}
height={200}
@@ -248,8 +250,8 @@ const Task = ({ task }: TaskProps) => {
<div className="flex -space-x-[6px] overflow-hidden">
{task.assignee && (
<Image
key={task.assignee.userId}
src={`${process.env.S3_PUBLIC_IMAGE_URL}/${task.assignee.profilePictureUrl!}`}
key={`assignee#${task.assignee.userId}`}
src={`${process.env.NEXT_PUBLIC_S3_PUBLIC_IMAGE_URL}/${task.assignee.profilePictureUrl!}`}
alt={task.assignee.username}
width={30}
height={30}
@@ -258,8 +260,8 @@ const Task = ({ task }: TaskProps) => {
)}
{task.author && (
<Image
key={task.author.userId}
src={`${process.env.S3_PUBLIC_IMAGE_URL}/${task.author.profilePictureUrl!}`}
key={`author#${task.author.userId}`}
src={`${process.env.NEXT_PUBLIC_S3_PUBLIC_IMAGE_URL}/${task.author.profilePictureUrl!}`}
alt={task.author.username}
width={30}
height={30}

View File

@@ -21,16 +21,19 @@ const Timeline = ({ projectId, setIsModalNewTaskOpen }: Props) => {
});
const ganttTasks = useMemo(() => {
if (!tasks || tasks.length === 0) return [];
return (
tasks?.map((task) => ({
start: new Date(task.startDate as string),
end: new Date(task.dueDate as string),
name: task.title,
id: `Task-${task.taskId}`,
type: "task" as TaskTypeItems,
progress: task.points ? (task.points / 10) * 100 : 0,
isDisabled: false,
})) || []
tasks
?.filter((task) => task.startDate && task.dueDate)
.map((task) => ({
start: new Date(task.startDate as string),
end: new Date(task.dueDate as string),
name: task.title,
id: `Task-${task.taskId}`,
type: "task" as TaskTypeItems,
progress: task.points ? (task.points / 10) * 100 : 0,
isDisabled: false,
})) || []
);
}, [tasks]);
@@ -66,16 +69,20 @@ const Timeline = ({ projectId, setIsModalNewTaskOpen }: Props) => {
</div>
<div className="overflow-hidden rounded-md bg-white shadow dark:bg-dark-secondary dark:text-white">
<div className="timeline">
<Gantt
tasks={ganttTasks}
{...displayOptions}
columnWidth={displayOptions.viewMode === ViewMode.Month ? 150 : 100}
listCellWidth="100px"
barBackgroundColor={isDarkMode ? "#101214" : "#aeb8c2"}
barBackgroundSelectedColor={isDarkMode ? "#000" : "#9ba1a6"}
/>
</div>
{ganttTasks.length > 0 && (
<div className="timeline">
<Gantt
tasks={ganttTasks}
{...displayOptions}
columnWidth={
displayOptions.viewMode === ViewMode.Month ? 150 : 100
}
listCellWidth="100px"
barBackgroundColor={isDarkMode ? "#101214" : "#aeb8c2"}
barBackgroundSelectedColor={isDarkMode ? "#000" : "#9ba1a6"}
/>
</div>
)}
<div className="px-4 pb-5 pt-1">
<button
className="flex items-center rounded bg-blue-primary px-3 py-2 text-white hover:bg-blue-600"

View File

@@ -31,7 +31,7 @@ const columns: GridColDef[] = [
<div className="flex h-full w-full items-center justify-center">
<div className="h-9 w-9">
<Image
src={`${process.env.S3_PUBLIC_IMAGE_URL}/${params.value}`}
src={`${process.env.NEXT_PUBLIC_S3_PUBLIC_IMAGE_URL}/${params.value}`}
alt={params.row.username}
width={100}
height={50}

View File

@@ -0,0 +1,13 @@
{
"preset": "ts-jest",
"testEnvironment": "node",
"setupFiles": ["<rootDir>/jest.env.js"],
"moduleFileExtensions": ["ts", "js"],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testMatch": ["**/tests/**/*.test.ts"],
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/src/$1"
}
}

View File

@@ -0,0 +1,6 @@
process.env.SLS_REGION = "mock-region";
process.env.TASKER_TASK_TABLE_NAME = "mock-task-table";
process.env.TASKER_PROJECT_TABLE_NAME = "mock-project-table";
process.env.TASKER_USER_TABLE_NAME = "mock-user-table";
process.env.TASKER_TASK_EXTRA_TABLE_NAME = "mock-task-extra-table";
process.env.TASKER_TEAM_TABLE_NAME = "mock-team-table";

File diff suppressed because it is too large Load Diff

View File

@@ -3,13 +3,15 @@
"version": "1.0.0",
"private": true,
"scripts": {
"infra:init": "cd terraform && AWS_PROFILE=default terraform init",
"infra:plan": "cd terraform && AWS_PROFILE=default terraform plan",
"infra:apply": "cd terraform && AWS_PROFILE=default terraform apply",
"infra:destroy": "cd terraform && AWS_PROFILE=default terraform destroy",
"sls:package": "AWS_PROFILE=default sls package",
"sls:deploy": "AWS_PROFILE=default sls deploy",
"sls:remove": "AWS_PROFILE=default sls remove"
"infra:init": "cd terraform && terraform init",
"infra:plan": "cd terraform && terraform plan",
"infra:apply": "cd terraform && terraform apply",
"infra:destroy": "cd terraform && terraform destroy",
"sls:package": "sls package",
"sls:deploy": "sls deploy",
"sls:remove": "sls remove",
"test": "jest --coverage",
"test:ci": "jest --no-coverage --ci"
},
"author": "",
"license": "ISC",
@@ -22,10 +24,13 @@
"uuid": "^11.0.3"
},
"devDependencies": {
"@types/jest": "^29.5.14",
"@types/node": "^22.9.1",
"eslint": "^9.15.0",
"jest": "^29.7.0",
"prettier": "^3.3.3",
"serverless-prune-plugin": "^2.1.0",
"ts-jest": "^29.2.5",
"typescript": "^5.6.3"
}
}

View File

@@ -1174,7 +1174,7 @@ export const handler = async () => {
console.info(`Data population for table ${tableName} complete.`);
}
console.log("Data population complete.");
console.info("Data population complete.");
} catch (error) {
console.error("Failed to populate data:", error);
}

View File

@@ -10,7 +10,6 @@ provider:
runtime: nodejs20.x
environment:
SLS_REGION: ${self:provider.region}
API_BASE_URL: ${ssm:/tasker/api/base-url}
TASKER_TASK_TABLE_NAME: ${ssm:/tasker/dynamodb/task-table-name}
TASKER_PROJECT_TABLE_NAME: ${ssm:/tasker/dynamodb/project-table-name}
TASKER_USER_TABLE_NAME: ${ssm:/tasker/dynamodb/user-table-name}

View File

@@ -10,10 +10,9 @@ const client = new DynamoDBClient({ region: SLS_REGION });
const docClient = DynamoDBDocument.from(client);
export const handler = async (event: any): Promise<any> => {
console.info(`Event: ${JSON.stringify(event)}`);
const username =
event.request.userAttributes["preferred_username"] || event.userName;
const cognitoId = event.userName;
const cognitoId = event.request.userAttributes["sub"];
const teamId = await fetchRandomTeamId();
try {

View File

@@ -17,13 +17,17 @@ export const handler = async (event: any): Promise<any> => {
const userTasks = [...authorTasks, ...assigneeTasks];
const uniqueTasks = Array.from(
new Map(userTasks.map((task) => [task.taskId, task])).values()
);
return {
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(userTasks),
body: JSON.stringify(uniqueTasks),
};
} catch (error: any) {
return {

View File

@@ -0,0 +1,94 @@
const mockPut = jest.fn();
import { handler } from "../src/handlers/createProject";
import { v4 as uuidv4 } from "uuid";
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
put: mockPut,
})),
},
}));
jest.mock("uuid", () => ({
v4: jest.fn(),
}));
describe("handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should create a new project and return 201 response", async () => {
const mockUUID = "mock-uuid";
(uuidv4 as jest.Mock).mockReturnValue(mockUUID);
const event = {
body: JSON.stringify({
name: "Test Project",
description: "This is a test project.",
startDate: "2024-01-01",
endDate: "2024-12-31",
}),
};
mockPut.mockResolvedValue({});
const response = await handler(event);
expect(mockPut).toHaveBeenCalledWith({
TableName: "mock-project-table",
Item: {
category: "projects",
projectId: `project_${mockUUID}`,
name: "Test Project",
description: "This is a test project.",
startDate: "2024-01-01",
endDate: "2024-12-31",
},
});
expect(response).toEqual({
statusCode: 201,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
category: "projects",
projectId: `project_${mockUUID}`,
name: "Test Project",
description: "This is a test project.",
startDate: "2024-01-01",
endDate: "2024-12-31",
}),
});
});
it("should return 500 response on error", async () => {
const event = {
body: JSON.stringify({
name: "Test Project",
description: "This is a test project.",
startDate: "2024-01-01",
endDate: "2024-12-31",
}),
};
const mockError = new Error("DynamoDB error");
mockPut.mockRejectedValue(mockError);
const response = await handler(event);
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error creating project: ${mockError.message}`,
}),
});
});
});

View File

@@ -0,0 +1,122 @@
const mockPut = jest.fn();
import { handler } from "../src/handlers/createTask";
import { v4 as uuidv4 } from "uuid";
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
put: mockPut,
})),
},
}));
jest.mock("uuid", () => ({
v4: jest.fn(),
}));
describe("createTask handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should create a new task and return 201 response", async () => {
const mockUUID = "mock-uuid";
(uuidv4 as jest.Mock).mockReturnValue(mockUUID);
const event = {
body: JSON.stringify({
title: "Test Task",
description: "This is a test task.",
status: "In Progress",
priority: "High",
tags: ["test", "jest"],
startDate: "2024-01-01",
dueDate: "2024-01-15",
points: 5,
projectId: "project_12345",
authorUserId: "user_abc",
assignedUserId: "user_xyz",
}),
};
mockPut.mockResolvedValue({});
const response = await handler(event);
expect(mockPut).toHaveBeenCalledWith({
TableName: "mock-task-table",
Item: {
category: "tasks",
taskId: `task_${mockUUID}`,
title: "Test Task",
description: "This is a test task.",
status: "In Progress",
priority: "High",
tags: ["test", "jest"],
startDate: "2024-01-01",
dueDate: "2024-01-15",
points: 5,
projectId: "project_12345",
authorUserId: "user_abc",
assignedUserId: "user_xyz",
},
});
expect(response).toEqual({
statusCode: 201,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
category: "tasks",
taskId: `task_${mockUUID}`,
title: "Test Task",
description: "This is a test task.",
status: "In Progress",
priority: "High",
tags: ["test", "jest"],
startDate: "2024-01-01",
dueDate: "2024-01-15",
points: 5,
projectId: "project_12345",
authorUserId: "user_abc",
assignedUserId: "user_xyz",
}),
});
});
it("should return 500 response on error", async () => {
const event = {
body: JSON.stringify({
title: "Test Task",
description: "This is a test task.",
status: "In Progress",
priority: "High",
tags: ["test", "jest"],
startDate: "2024-01-01",
dueDate: "2024-01-15",
points: 5,
projectId: "project_12345",
authorUserId: "user_abc",
assignedUserId: "user_xyz",
}),
};
const mockError = new Error("DynamoDB error");
mockPut.mockRejectedValue(mockError);
const response = await handler(event);
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error creating task: ${mockError.message}`,
}),
});
});
});

View File

@@ -0,0 +1,104 @@
const mockPut = jest.fn();
import { handler } from "../src/handlers/createUser";
import { v4 as uuidv4 } from "uuid";
import { fetchRandomTeamId } from "@/lib/util";
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
put: mockPut,
})),
},
}));
jest.mock("uuid", () => ({
v4: jest.fn(),
}));
jest.mock("@/lib/util", () => ({
fetchRandomTeamId: jest.fn(),
}));
describe("createUser handler", () => {
beforeAll(() => {
jest.spyOn(console, "info").mockImplementation(jest.fn());
});
afterAll(() => {
jest.restoreAllMocks();
});
it("should create a new user and log a success message", async () => {
const mockUUID = "mock-uuid";
const mockTeamId = "team-123";
(uuidv4 as jest.Mock).mockReturnValue(mockUUID);
(fetchRandomTeamId as jest.Mock).mockResolvedValue(mockTeamId);
const event = {
request: {
userAttributes: {
preferred_username: "testUser",
sub: "cognito-123",
},
},
userName: "fallbackUser",
};
mockPut.mockResolvedValue({});
const response = await handler(event);
expect(fetchRandomTeamId).toHaveBeenCalled();
expect(mockPut).toHaveBeenCalledWith({
TableName: "mock-user-table",
Item: {
category: "users",
cognitoId: "cognito-123",
userId: `user_${mockUUID}`,
username: "testUser",
profilePictureUrl: "p0.jpeg",
teamId: mockTeamId,
},
});
expect(response).toEqual(event);
});
it("should throw an error if DynamoDB operation fails", async () => {
const mockUUID = "mock-uuid";
const mockTeamId = "team-123";
(uuidv4 as jest.Mock).mockReturnValue(mockUUID);
(fetchRandomTeamId as jest.Mock).mockResolvedValue(mockTeamId);
const event = {
request: {
userAttributes: {
preferred_username: "testUser",
sub: "cognito-123",
},
},
userName: "fallbackUser",
};
const mockError = new Error("DynamoDB error");
mockPut.mockRejectedValue(mockError);
await expect(handler(event)).rejects.toThrow(
"Error creating user: DynamoDB error"
);
expect(fetchRandomTeamId).toHaveBeenCalled();
expect(mockPut).toHaveBeenCalledWith({
TableName: "mock-user-table",
Item: {
category: "users",
cognitoId: "cognito-123",
userId: `user_${mockUUID}`,
username: "testUser",
profilePictureUrl: "p0.jpeg",
teamId: mockTeamId,
},
});
});
});

View File

@@ -0,0 +1,73 @@
const mockQuery = jest.fn();
import { handler } from "../src/handlers/getProjects";
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
query: mockQuery,
})),
},
}));
describe("getProjects handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should retrieve projects and return a 200 response", async () => {
const mockProjects = [
{ projectId: "project_1", name: "Project 1" },
{ projectId: "project_2", name: "Project 2" },
];
mockQuery.mockResolvedValue({
Items: mockProjects,
});
const response = await handler({});
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-project-table",
KeyConditionExpression: "category = :category",
ExpressionAttributeValues: {
":category": "projects",
},
});
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(mockProjects),
});
});
it("should return a 500 response if the query fails", async () => {
const mockError = new Error("DynamoDB query error");
mockQuery.mockRejectedValue(mockError);
const response = await handler({});
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-project-table",
KeyConditionExpression: "category = :category",
ExpressionAttributeValues: {
":category": "projects",
},
});
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving projects: ${mockError.message}`,
}),
});
});
});

View File

@@ -0,0 +1,131 @@
const mockQuery = jest.fn();
const mockFetchUserWithUserId = jest.fn();
const mockFetchComments = jest.fn();
const mockFetchAttachments = jest.fn();
import { handler } from "../src/handlers/getTasks";
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
query: mockQuery,
})),
},
}));
jest.mock("@/lib/util", () => ({
fetchUserWithUserId: mockFetchUserWithUserId,
fetchComments: mockFetchComments,
fetchAttachments: mockFetchAttachments,
}));
describe("getTasks handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should retrieve tasks with details and return a 200 response", async () => {
const mockTasks = [
{
taskId: "task_1",
title: "Task 1",
authorUserId: "user_1",
assignedUserId: "user_2",
},
{
taskId: "task_2",
title: "Task 2",
},
];
const mockAuthor = { userId: "user_1", username: "AuthorUser" };
const mockAssignee = { userId: "user_2", username: "AssigneeUser" };
const mockComments = [{ commentId: "comment_1", content: "Nice work!" }];
const mockAttachments = [
{ attachmentId: "attachment_1", name: "file.pdf" },
];
mockQuery.mockResolvedValue({ Items: mockTasks });
mockFetchUserWithUserId.mockResolvedValueOnce(mockAuthor);
mockFetchUserWithUserId.mockResolvedValueOnce(mockAssignee);
mockFetchComments.mockResolvedValue(mockComments);
mockFetchAttachments.mockResolvedValue(mockAttachments);
const event = {
queryStringParameters: { projectId: "project_123" },
};
const response = await handler(event);
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-task-table",
KeyConditionExpression: "category = :category AND projectId = :projectId",
IndexName: "GSI-project-id",
ExpressionAttributeValues: {
":category": "tasks",
":projectId": "project_123",
},
});
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify([
{
taskId: "task_1",
title: "Task 1",
authorUserId: "user_1",
assignedUserId: "user_2",
author: mockAuthor,
assignee: mockAssignee,
comments: mockComments,
attachments: mockAttachments,
},
{
taskId: "task_2",
title: "Task 2",
author: null,
assignee: null,
comments: mockComments,
attachments: mockAttachments,
},
]),
});
});
it("should return a 500 response if there is an error", async () => {
const mockError = new Error("DynamoDB query error");
mockQuery.mockRejectedValue(mockError);
const event = {
queryStringParameters: { projectId: "project_123" },
};
const response = await handler(event);
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-task-table",
KeyConditionExpression: "category = :category AND projectId = :projectId",
IndexName: "GSI-project-id",
ExpressionAttributeValues: {
":category": "tasks",
":projectId": "project_123",
},
});
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving tasks: ${mockError.message}`,
}),
});
});
});

View File

@@ -0,0 +1,113 @@
const mockQuery = jest.fn();
const mockFetchUserWithUserId = jest.fn();
import { handler } from "../src/handlers/getTeams";
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
query: mockQuery,
})),
},
}));
jest.mock("@/lib/util", () => ({
fetchUserWithUserId: mockFetchUserWithUserId,
}));
describe("getTeams handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should retrieve teams with usernames and return a 200 response", async () => {
const mockTeams = [
{
teamId: "team_1",
name: "Team A",
productOwnerUserId: "user_1",
projectManagerUserId: "user_2",
},
{
teamId: "team_2",
name: "Team B",
productOwnerUserId: "user_3",
},
];
const mockProductOwner1 = { userId: "user_1", username: "POUser1" };
const mockProjectManager1 = { userId: "user_2", username: "PMUser2" };
const mockProductOwner2 = { userId: "user_3", username: "POUser3" };
mockQuery.mockResolvedValue({ Items: mockTeams });
mockFetchUserWithUserId.mockImplementation((userId) => {
if (userId === "user_1") return Promise.resolve(mockProductOwner1);
if (userId === "user_2") return Promise.resolve(mockProjectManager1);
if (userId === "user_3") return Promise.resolve(mockProductOwner2);
return Promise.resolve(null);
});
const response = await handler({});
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-team-table",
KeyConditionExpression: "category = :category",
ExpressionAttributeValues: {
":category": "teams",
},
});
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify([
{
teamId: "team_1",
name: "Team A",
productOwnerUserId: "user_1",
projectManagerUserId: "user_2",
productOwnerUsername: "POUser1",
projectManagerUsername: "PMUser2",
},
{
teamId: "team_2",
name: "Team B",
productOwnerUserId: "user_3",
projectManagerUserId: undefined,
productOwnerUsername: "POUser3",
projectManagerUsername: null,
},
]),
});
});
it("should return a 500 response if there is an error", async () => {
const mockError = new Error("DynamoDB query error");
mockQuery.mockRejectedValue(mockError);
const response = await handler({});
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-team-table",
KeyConditionExpression: "category = :category",
ExpressionAttributeValues: {
":category": "teams",
},
});
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving teams: ${mockError.message}`,
}),
});
});
});

View File

@@ -0,0 +1,115 @@
const mockQuery = jest.fn();
import { handler } from "../src/handlers/getUser";
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
query: mockQuery,
})),
},
}));
describe("getUser handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should retrieve a user and return a 200 response", async () => {
const mockUser = {
cognitoId: "cognito_123",
userId: "user_1",
username: "TestUser",
category: "users",
};
mockQuery.mockResolvedValue({
Items: [mockUser],
});
const event = {
pathParameters: { cognitoId: "cognito_123" },
};
const response = await handler(event);
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-user-table",
KeyConditionExpression: "category = :category AND cognitoId = :cognitoId",
ExpressionAttributeValues: {
":category": "users",
":cognitoId": "cognito_123",
},
});
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(mockUser),
});
});
it("should return an empty object if no user is found", async () => {
mockQuery.mockResolvedValue({
Items: [],
});
const event = {
pathParameters: { cognitoId: "cognito_456" },
};
const response = await handler(event);
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-user-table",
KeyConditionExpression: "category = :category AND cognitoId = :cognitoId",
ExpressionAttributeValues: {
":category": "users",
":cognitoId": "cognito_456",
},
});
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({}),
});
});
it("should return a 500 response if there is an error", async () => {
const mockError = new Error("DynamoDB query error");
mockQuery.mockRejectedValue(mockError);
const event = {
pathParameters: { cognitoId: "cognito_123" },
};
const response = await handler(event);
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-user-table",
KeyConditionExpression: "category = :category AND cognitoId = :cognitoId",
ExpressionAttributeValues: {
":category": "users",
":cognitoId": "cognito_123",
},
});
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving user: ${mockError.message}`,
}),
});
});
});

View File

@@ -0,0 +1,120 @@
const mockQueryTasks = jest.fn();
import { handler } from "../src/handlers/getUserTasks";
import { queryTasks } from "@/lib/util";
jest.mock("@/lib/util", () => ({
queryTasks: mockQueryTasks,
}));
describe("getUserTasks handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should retrieve tasks authored and assigned to the user and return a 200 response", async () => {
const mockAuthorTasks = [
{ taskId: "task_1", title: "Authored Task 1" },
{ taskId: "task_2", title: "Authored Task 2" },
];
const mockAssigneeTasks = [
{ taskId: "task_2", title: "Authored Task 2" }, // Duplicate task
{ taskId: "task_3", title: "Assigned Task 3" },
];
const mockUniqueTasks = [
{ taskId: "task_1", title: "Authored Task 1" },
{ taskId: "task_2", title: "Authored Task 2" },
{ taskId: "task_3", title: "Assigned Task 3" },
];
mockQueryTasks
.mockResolvedValueOnce(mockAuthorTasks)
.mockResolvedValueOnce(mockAssigneeTasks);
const event = {
pathParameters: { userId: "user_123" },
};
const response = await handler(event);
expect(queryTasks).toHaveBeenCalledWith(
"user_123",
"GSI-author-user-id",
"authorUserId"
);
expect(queryTasks).toHaveBeenCalledWith(
"user_123",
"GSI-assigned-user-id",
"assignedUserId"
);
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(mockUniqueTasks),
});
});
it("should return a 500 response if queryTasks fails", async () => {
const mockError = new Error("Query failed");
mockQueryTasks.mockRejectedValueOnce(mockError);
const event = {
pathParameters: { userId: "user_123" },
};
const response = await handler(event);
expect(queryTasks).toHaveBeenCalledWith(
"user_123",
"GSI-author-user-id",
"authorUserId"
);
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving tasks for user: ${mockError.message}`,
}),
});
});
it("should handle empty author and assignee tasks gracefully", async () => {
mockQueryTasks.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
const event = {
pathParameters: { userId: "user_123" },
};
const response = await handler(event);
expect(queryTasks).toHaveBeenCalledWith(
"user_123",
"GSI-author-user-id",
"authorUserId"
);
expect(queryTasks).toHaveBeenCalledWith(
"user_123",
"GSI-assigned-user-id",
"assignedUserId"
);
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify([]),
});
});
});

View File

@@ -0,0 +1,98 @@
const mockQuery = jest.fn();
import { handler } from "../src/handlers/getUsers";
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
query: mockQuery,
})),
},
}));
describe("getUsers handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should retrieve all users and return a 200 response", async () => {
const mockUsers = [
{ userId: "user_1", username: "User1", category: "users" },
{ userId: "user_2", username: "User2", category: "users" },
];
mockQuery.mockResolvedValue({
Items: mockUsers,
});
const response = await handler({});
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-user-table",
KeyConditionExpression: "category = :category",
ExpressionAttributeValues: {
":category": "users",
},
});
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(mockUsers),
});
});
it("should return an empty array if no users are found", async () => {
mockQuery.mockResolvedValue({
Items: [],
});
const response = await handler({});
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-user-table",
KeyConditionExpression: "category = :category",
ExpressionAttributeValues: {
":category": "users",
},
});
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify([]),
});
});
it("should return a 500 response if there is an error", async () => {
const mockError = new Error("DynamoDB query error");
mockQuery.mockRejectedValue(mockError);
const response = await handler({});
expect(mockQuery).toHaveBeenCalledWith({
TableName: "mock-user-table",
KeyConditionExpression: "category = :category",
ExpressionAttributeValues: {
":category": "users",
},
});
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error retrieving users: ${mockError.message}`,
}),
});
});
});

View File

@@ -0,0 +1,120 @@
const mockUpdate = jest.fn();
import { handler } from "../src/handlers/updateTaskStatus"; // Adjust the path as needed
jest.mock("@aws-sdk/lib-dynamodb", () => ({
DynamoDBDocument: {
from: jest.fn(() => ({
update: mockUpdate,
})),
},
}));
describe("updateTaskStatus handler", () => {
afterAll(() => {
jest.restoreAllMocks();
});
it("should update the task status and return the updated task", async () => {
const mockUpdatedTask = {
category: "tasks",
taskId: "task_123",
status: "Completed",
};
mockUpdate.mockResolvedValue({
Attributes: mockUpdatedTask,
});
const event = {
pathParameters: { taskId: "task_123" },
body: JSON.stringify({ status: "Completed" }),
};
const response = await handler(event);
expect(mockUpdate).toHaveBeenCalledWith({
TableName: "mock-task-table",
Key: {
category: "tasks",
taskId: "task_123",
},
UpdateExpression: "set #status = :status",
ExpressionAttributeNames: {
"#status": "status",
},
ExpressionAttributeValues: {
":status": "Completed",
},
ReturnValues: "ALL_NEW",
});
expect(response).toEqual({
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(mockUpdatedTask),
});
});
it("should return a 500 response if the update fails", async () => {
const mockError = new Error("DynamoDB update error");
mockUpdate.mockRejectedValue(mockError);
const event = {
pathParameters: { taskId: "task_123" },
body: JSON.stringify({ status: "Completed" }),
};
const response = await handler(event);
expect(mockUpdate).toHaveBeenCalledWith({
TableName: "mock-task-table",
Key: {
category: "tasks",
taskId: "task_123",
},
UpdateExpression: "set #status = :status",
ExpressionAttributeNames: {
"#status": "status",
},
ExpressionAttributeValues: {
":status": "Completed",
},
ReturnValues: "ALL_NEW",
});
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error updating task: ${mockError.message}`,
}),
});
});
it("should handle invalid input gracefully", async () => {
const event = {
pathParameters: { taskId: "task_123" },
body: JSON.stringify({}),
};
const response = await handler(event);
expect(response).toEqual({
statusCode: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: `Error updating task: DynamoDB update error`,
}),
});
});
});

View File

@@ -22,6 +22,6 @@
"@/*": ["./src/*"]
}
},
"include": ["**/*.ts", "**/*.tsx"],
"include": ["**/*.ts", "**/*.tsx", "jest.config.js"],
"exclude": ["node_modules"]
}