Skip to content

Commit

Permalink
tests: complete requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
thenicekat committed Apr 25, 2024
1 parent dd7e53d commit 45e1ae5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
2 changes: 0 additions & 2 deletions backend/src/service/posts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,10 @@ export const completePost = async (postId: string, authorEmail: string): Promise
}

// You cannot mark other's post as completed
console.log(post.author.email, authorEmail, post.author.email != authorEmail)
if (post.author.email != authorEmail) return {
error: true,
data: "You are not the author of this post."
}
console.log("Hello")

// If post is completed or no request was accepted then throw error
if (post.status == "completed") return {
Expand Down
51 changes: 51 additions & 0 deletions backend/tests/posts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ const post: Post & {
}
}

const closedPost: Post & {
authorEmail: string,
author: {
email: string
}
} = {
id: "1",
authorId: "1",
authorEmail: "[email protected]",
source: "source",
destination: "destination",
costInPoints: 10,
service: "service",
status: "closed",
author: {
email: "[email protected]"
}
}

const completedPost: Post & {
authorEmail: string,
author: {
Expand All @@ -78,6 +97,13 @@ const request: Request = {
status: "open"
}

const completedRequest: Request = {
id: "1",
postId: "1",
senderEmail: "[email protected]",
status: "completed"
}

describe("Create a new post", () => {
it("should create a new post", () => {

Expand Down Expand Up @@ -392,6 +418,31 @@ describe("Complete post", () => {
data: "Some error occurred while completing the post"
});
});

it("should throw error if user does not have enough karma", () => {
prismaMock.user.findUnique.mockResolvedValue(userWith0KarmaPoints);
prismaMock.post.findUnique.mockResolvedValue(closedPost);
prismaMock.request.findMany.mockResolvedValue([completedRequest]);
prismaMock.$transaction.mockResolvedValue([[completedRequest], closedPost]);

expect(completePost(closedPost.id, userWith0KarmaPoints.email)).resolves.toEqual({
error: true,
data: "Not enough karma to complete this post."
});
})

it("should complete successfully if user does have enough karma", () => {
prismaMock.user.findUnique.mockResolvedValue(userWith10KarmaPoints);
prismaMock.post.findUnique.mockResolvedValue(closedPost);
prismaMock.request.findMany.mockResolvedValue([completedRequest]);
prismaMock.$transaction.mockResolvedValue([[completedRequest], closedPost]);
prismaMock.post.update.mockResolvedValue(completedPost);

expect(completePost(closedPost.id, userWith10KarmaPoints.email)).resolves.toEqual({
error: false,
data: completedPost
});
})
})

describe("Delete post", () => {
Expand Down

0 comments on commit 45e1ae5

Please sign in to comment.