-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from Divyateja04/NoWay
Admin inc/dec user karma
- Loading branch information
Showing
5 changed files
with
180 additions
and
3 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
Empty file.
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,7 +1,7 @@ | ||
import { describe, expect } from "@jest/globals"; | ||
import { prismaMock } from "./_mockdb"; | ||
import { User } from ".prisma/client"; | ||
import { getAllUsers, promoteUser, demoteUser, banUser } from "../src/service/admin.service"; | ||
import { getAllUsers, promoteUser, demoteUser, banUser, deltaKarma} from "../src/service/admin.service"; | ||
|
||
const user: User = { | ||
id: "1", | ||
|
@@ -33,7 +33,17 @@ const notadmin: User = { | |
isPublic: true | ||
} | ||
|
||
const bu: User = { | ||
const na2: User = { | ||
id: "3", | ||
name: "ben", | ||
email: "[email protected]", | ||
phoneNumber: "9898989898", | ||
karmaPoints: 0, | ||
role: "user", | ||
isPublic: true | ||
} | ||
|
||
const bu: User= { | ||
id: "2", | ||
name: "ben", | ||
email: "[email protected]", | ||
|
@@ -43,6 +53,16 @@ const bu: User = { | |
isPublic: true | ||
} | ||
|
||
const iku: User = { | ||
id: "2", | ||
name: "ben", | ||
email: "[email protected]", | ||
phoneNumber: "9898989898", | ||
karmaPoints: 12, | ||
role: "user", | ||
isPublic: true | ||
} | ||
|
||
describe("Get all users", () => { | ||
it("should get all users", () => { | ||
prismaMock.user.findMany.mockResolvedValue([user]); | ||
|
@@ -90,6 +110,7 @@ describe("demote admin to user", () => { | |
prismaMock.user.findUnique.mockResolvedValue(admin); | ||
prismaMock.user.update.mockResolvedValue(notadmin); | ||
|
||
|
||
expect(demoteUser(user.email, admin.email)).resolves.toEqual({ | ||
error: false, | ||
data: notadmin | ||
|
@@ -100,6 +121,7 @@ describe("demote admin to user", () => { | |
prismaMock.user.findUnique.mockResolvedValue(admin); | ||
prismaMock.user.update.mockRejectedValue(new Error("Some error occurred")); | ||
|
||
|
||
expect(demoteUser(user.email, admin.email)).resolves.toEqual({ | ||
error: true, | ||
data: "Some error occurred while demoting admin to user role" | ||
|
@@ -145,4 +167,62 @@ describe("ban user", () => { | |
data: "Some error occurred while banning user." | ||
}) | ||
}) | ||
}) | ||
|
||
describe("increase/decrease user karma", () => { | ||
it("should increase karma", () => { | ||
prismaMock.user.findUnique.mockResolvedValue(na); | ||
prismaMock.user.update.mockResolvedValue(iku); | ||
|
||
expect(deltaKarma(na.email,12,true)).resolves.toEqual({ | ||
error: false, | ||
data: iku | ||
}) | ||
}) | ||
|
||
it("should decrease karma", () => { | ||
prismaMock.user.findUnique.mockResolvedValue(iku); | ||
prismaMock.user.update.mockResolvedValue(na); | ||
|
||
expect(deltaKarma(iku.email,12,false)).resolves.toEqual({ | ||
error: false, | ||
data: na | ||
}) | ||
}) | ||
|
||
it("should return error if target karma is negative", () => { | ||
prismaMock.user.findUnique.mockResolvedValue(na); | ||
|
||
expect(deltaKarma(na.email,12,false)).resolves.toEqual({ | ||
error: true, | ||
data: "Stop, Stop! he is already dead." | ||
}) | ||
}) | ||
|
||
it("should return an error if user does not exist", () => { | ||
|
||
expect(banUser("[email protected]")).resolves.toEqual({ | ||
error: true, | ||
data: "User does not exist." | ||
}) | ||
}) | ||
|
||
it("should return error if user to be updated is an admin", () => { | ||
prismaMock.user.findUnique.mockResolvedValue(a); | ||
|
||
expect(deltaKarma(a.email,12,true)).resolves.toEqual({ | ||
error: true, | ||
data: "Admin karma cannot be updated." | ||
}) | ||
}) | ||
|
||
it("should return error if any error occured", () => { | ||
prismaMock.user.findUnique.mockResolvedValue(na); | ||
prismaMock.user.update.mockRejectedValue(new Error("Some error occurred")); | ||
|
||
expect(deltaKarma(na.email,12,true)).resolves.toEqual({ | ||
error: true, | ||
data: "Some error occurred while changing karma." | ||
}) | ||
}) | ||
}) |
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