-
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.
- Loading branch information
Showing
1 changed file
with
128 additions
and
0 deletions.
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
tests/unit/app/profile/mainDetails/updateUserDetailsAction.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,128 @@ | ||
import { db } from "@/drizzle/db"; | ||
import { Moderator, Reviewer, User, Wave } from "@/drizzle/schema"; | ||
import { eq } from "drizzle-orm"; | ||
import { mockUserSession } from "tests/helpers/mockUserSession"; | ||
import { | ||
createBlocked, | ||
createModerator, | ||
createReviewer, | ||
createUser, | ||
} from "tests/helpers/queries"; | ||
import { afterEach, describe, expect, it, vi } from "vitest"; | ||
|
||
import { updateUserDetailsAction } from "@/app/profile/mainDetails/updateUserDetailsAction"; | ||
|
||
const userId = "0"; | ||
|
||
const userDetails = { | ||
nickname: "Default user", | ||
}; | ||
|
||
describe("app/profile/mainDetails/updateUserDetailsAction", () => { | ||
afterEach(async () => { | ||
await db.delete(Moderator); | ||
await db.delete(User); | ||
await db.delete(Reviewer); | ||
await db.delete(Wave); | ||
|
||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe("permissions", () => { | ||
it("visitor", async () => { | ||
expect(() => updateUserDetailsAction(userDetails)).rejects.toThrowError( | ||
"Unauthenticated", | ||
); | ||
}); | ||
|
||
it("blocked", async () => { | ||
await createBlocked(userId); | ||
mockUserSession({ userId, credentialType: "orb" }); | ||
|
||
await updateUserDetailsAction(userDetails); | ||
|
||
const users = await db.query.User.findMany(); | ||
expect(users).toHaveLength(1); | ||
expect(users[0].name).toBe(userDetails.nickname); | ||
}); | ||
|
||
it("device verified", async () => { | ||
await createUser(userId); | ||
mockUserSession({ userId, credentialType: "device" }); | ||
|
||
await updateUserDetailsAction(userDetails); | ||
|
||
const users = await db.query.User.findMany(); | ||
expect(users).toHaveLength(1); | ||
expect(users[0].name).toBe(userDetails.nickname); | ||
}); | ||
|
||
it("orb verified", async () => { | ||
await createUser(userId); | ||
mockUserSession({ userId, credentialType: "orb" }); | ||
|
||
await updateUserDetailsAction(userDetails); | ||
|
||
const users = await db.query.User.findMany(); | ||
expect(users).toHaveLength(1); | ||
expect(users[0].name).toBe(userDetails.nickname); | ||
}); | ||
|
||
it("reviewer", async () => { | ||
await createReviewer(userId); | ||
mockUserSession({ userId, credentialType: "orb" }); | ||
|
||
await updateUserDetailsAction(userDetails); | ||
|
||
const users = await db.query.User.findMany(); | ||
expect(users).toHaveLength(1); | ||
expect(users[0].name).toBe(userDetails.nickname); | ||
}); | ||
|
||
it("moderator", async () => { | ||
await createModerator(userId); | ||
mockUserSession({ userId, credentialType: "orb" }); | ||
|
||
await updateUserDetailsAction(userDetails); | ||
|
||
const users = await db.query.User.findMany(); | ||
expect(users).toHaveLength(1); | ||
expect(users[0].name).toBe(userDetails.nickname); | ||
}); | ||
}); | ||
|
||
it("can update user details", async () => { | ||
await createUser(userId); | ||
mockUserSession({ userId, credentialType: "device" }); | ||
|
||
await updateUserDetailsAction({ | ||
nickname: "random user", | ||
avatar: { | ||
id: "8e60662c-e935-48dd-9493-76139d5bf950", | ||
height: 120, | ||
width: 120, | ||
placeholder: "placeholder", | ||
}, | ||
}); | ||
|
||
const users = await db.query.User.findMany(); | ||
expect(users).toHaveLength(1); | ||
expect(users[0].name).toBe("random user"); | ||
expect(users[0].imageId).toBe("8e60662c-e935-48dd-9493-76139d5bf950"); | ||
}); | ||
|
||
it("can't set name if duplicate", async () => { | ||
const name = "username"; | ||
const firstUserId = "1"; | ||
const secondUserId = "2"; | ||
|
||
mockUserSession({ userId: secondUserId, credentialType: "device" }); | ||
await createUser(firstUserId); | ||
await createUser(secondUserId); | ||
await db.update(User).set({ name }).where(eq(User.id, firstUserId)); | ||
|
||
expect(await updateUserDetailsAction({ nickname: name })).toStrictEqual({ | ||
error: "Nickname is already taken", | ||
}); | ||
}); | ||
}); |