forked from jackcrane/slu-open-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Test cases for Non SSO login #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Muhammudy
wants to merge
7
commits into
password-auth
Choose a base branch
from
auth-test-cases-ym
base: password-auth
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
19ecb89
building test cases
Muhammudy ced7279
test cases
Muhammudy 8184e6e
most test cases are passing
Muhammudy 5bce431
all test cases passing
Muhammudy c2201f4
added backend test case and backend check for 8 chars password
Muhammudy 7a7c3c5
everything added
Muhammudy ff9a4de
small changes on admin side
Muhammudy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,36 @@ | ||
| // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
|
||
| exports[`/auth/forgotPassword/ > GET > returns a user's shop information to admins 1`] = ` | ||
| { | ||
| "accountTitle": null, | ||
| "accountType": "CUSTOMER", | ||
| "active": true, | ||
| "blacklisted": false, | ||
| "createdAt": Any<String>, | ||
| "id": Any<String>, | ||
| "shopId": Any<String>, | ||
| "updatedAt": Any<String>, | ||
| "user": { | ||
| "admin": true, | ||
| "balance": 0, | ||
| "createdAt": Any<String>, | ||
| "email": "[email protected]", | ||
| "firstName": "TestFirstName", | ||
| "id": Any<String>, | ||
| "isMe": true, | ||
| "jobCounts": { | ||
| "completedCount": 0, | ||
| "excludedCount": 0, | ||
| "inProgressCount": 0, | ||
| "notStartedCount": 0, | ||
| }, | ||
| "jobs": [], | ||
| "lastName": "TestLastName", | ||
| "simple": true, | ||
| "suspended": false, | ||
| "suspensionReason": null, | ||
| "updatedAt": Any<String>, | ||
| }, | ||
| "userId": Any<String>, | ||
| } | ||
| `; |
This file contains hidden or 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,117 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import prisma from "#prisma"; | ||
| import { LogType } from "@prisma/client"; | ||
| import request from "supertest"; | ||
| import { app } from "#index"; | ||
| import { tc } from "#setup"; | ||
| import postmark from "postmark"; | ||
| import jwt from "jsonwebtoken"; | ||
|
|
||
|
|
||
| const sendEmailMock = vi.fn().mockResolvedValue(true); | ||
|
|
||
| vi.mock("postmark", () => { | ||
| const ServerClient = vi.fn().mockImplementation(() => ({ | ||
| sendEmail: sendEmailMock, | ||
| })); | ||
|
|
||
| return { | ||
| default: { ServerClient }, | ||
| }; | ||
| }); | ||
|
|
||
|
|
||
| describe("/api/auth/forgotPassword", () => { | ||
| describe("POST", () => { | ||
| it("sends a user a reset link with postmark", async () => { | ||
| const res = await request(app) | ||
| .post("/api/auth/forgotPassword") | ||
| .send({ | ||
| email: tc.globalLocalUser.email, | ||
| }) | ||
|
|
||
| expect(res.status).toBe(200); | ||
| expect(res.body).toEqual({ success: true }); | ||
| expect(postmark.ServerClient).toHaveBeenCalled(); | ||
| expect(sendEmailMock).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
|
|
||
| it("returns status 404, user does not exist", async () => { //token is invalid | ||
| const res = await request(app) | ||
| .post("/api/auth/forgotPassword") | ||
| .send({ | ||
| email: "[email protected]", | ||
| }); | ||
|
|
||
| expect(res.status).toBe(404); | ||
| expect(res.body).toEqual({ error: "User does not exist." }); | ||
|
|
||
| }); | ||
|
|
||
|
|
||
| }); | ||
|
|
||
|
|
||
| describe("PUT", () => { //this route is very similar to the put request in login, but we are passing a token | ||
| it("resets a users password with postmark", async () => { | ||
| const res = await request(app) | ||
| .put("/api/auth/forgotPassword") | ||
| .send({ | ||
| token: tc.token, | ||
| newPassword: "newPassword", | ||
| }) | ||
|
|
||
| expect(res.status).toBe(200); | ||
| expect(res.body).toEqual({ success: true }); | ||
|
|
||
| const log = await prisma.logs.findFirst({ | ||
| where: { | ||
| type: LogType.USER_PASSWORD_CHANGE, | ||
| userId: tc.globalLocalUser.id, | ||
| }, | ||
| }); | ||
|
|
||
| expect(log).toBeDefined(); | ||
| expect(log.type).toBe(LogType.USER_PASSWORD_CHANGE); | ||
| }); | ||
|
|
||
|
|
||
|
|
||
| it("returns status 401, invalid token", async () => { //token is invalid | ||
| const res = await request(app) | ||
| .put("/api/auth/forgotPassword") | ||
| .send({ | ||
| newPassword: "newPassword", | ||
| token: "fake-token", | ||
| }); | ||
|
|
||
| expect(res.status).toBe(401); | ||
| expect(res.body).toEqual({ error: "Invalid or expired link." }); | ||
|
|
||
| }); | ||
|
|
||
|
|
||
| it("returns status 401, user not found", async () => { //token is valid, but the user doesnt exist with that token | ||
| const fakeUserId = "00000000-0000-0000-0000-000000000000"; | ||
|
|
||
| const token = jwt.sign( //we need the jwt token to verify successfully | ||
| { id: fakeUserId }, | ||
| process.env.JWT_SECRET | ||
| ); | ||
| const res = await request(app) | ||
| .put("/api/auth/forgotPassword") | ||
| .send({ | ||
| token: token, | ||
| newPassword: "newPassword", | ||
| }); | ||
|
|
||
| expect(res.status).toBe(401); | ||
| expect(res.body).toEqual({ error: "Invalid or expired link." }); | ||
|
|
||
| }); | ||
|
|
||
|
|
||
| }); | ||
| }); | ||
|
|
This file contains hidden or 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,166 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import prisma from "#prisma"; | ||
| import { LogType } from "@prisma/client"; | ||
| import request from "supertest"; | ||
| import { app } from "#index"; | ||
| import { tc } from "#setup"; | ||
|
|
||
| describe("/api/auth/login", () => { | ||
| describe("POST", () => { | ||
| it("enables a user to login", async () => { //test standard login | ||
| const res = await request(app) | ||
| .post("/api/auth/login") | ||
| .send({ | ||
| email: tc.globalLocalUser.email, | ||
| password: "TestPassword", | ||
| }); | ||
|
|
||
| expect(res.status).toBe(200); | ||
| expect(res.body).toHaveProperty("token"); | ||
| const log = await prisma.logs.findFirst({ | ||
| where: { | ||
| type: LogType.USER_LOGIN_LOCAL, | ||
| userId: tc.globalLocalUser.id, | ||
| }, | ||
| }); | ||
|
|
||
| expect(log).toBeDefined(); | ||
| }); | ||
| it("returns status 401, no password field exists in the db", async () => { //tests the fact that user doesnt have a password field i.e (SSO) | ||
| const res = await request(app) | ||
| .post("/api/auth/login") | ||
| .send({ | ||
| email: tc.globalLocalUser.email, //we can just use the globalUser because this has no password attribute and should throw an error. | ||
| password: "password", | ||
| }); | ||
|
|
||
|
|
||
| const log = await prisma.logs.findFirst({ | ||
| where: { | ||
| type: LogType.USER_LOGIN_FAILURE, | ||
| }, | ||
| orderBy: { createdAt: "desc" }, | ||
| }); | ||
|
|
||
| expect(log).toBeDefined(); | ||
| expect(log.type).toBe(LogType.USER_LOGIN_FAILURE); | ||
| expect(res.status).toBe(401); | ||
| expect(res.body).toEqual({ error: "Invalid credentials or SSO required" }); | ||
|
|
||
| }); | ||
| it("returns status 401, no user exists in the db", async () => { //tests a random email or person not in the db | ||
| const res = await request(app) | ||
| .post("/api/auth/login") | ||
| .send({ | ||
| email: "[email protected]", //user a random email | ||
| password: "password", | ||
| }); | ||
|
|
||
|
|
||
| const log = await prisma.logs.findFirst({ | ||
| where: { | ||
| type: LogType.USER_LOGIN_FAILURE, | ||
| }, | ||
| orderBy: { createdAt: "desc" }, | ||
| }); | ||
|
|
||
| expect(log).toBeDefined(); | ||
| expect(log.type).toBe(LogType.USER_LOGIN_FAILURE); | ||
| expect(res.status).toBe(401); | ||
| expect(res.body).toEqual({ error: "Invalid credentials or SSO required" }); | ||
|
|
||
| }); | ||
|
|
||
| it("returns status 401, passwords dont match", async () => { //tests a password doesnt match what is in the db | ||
| const res = await request(app) | ||
| .post("/api/auth/login") | ||
| .send({ | ||
| email: tc.globalLocalUser.email, | ||
| password: "passwordNotMatch", | ||
| }); | ||
|
|
||
|
|
||
| const log = await prisma.logs.findFirst({ | ||
| where: { | ||
| type: LogType.USER_LOGIN_FAILURE, | ||
| userId: tc.globalLocalUser.id, | ||
| }, | ||
| }); | ||
|
|
||
| expect(log).toBeDefined(); | ||
| expect(log.type).toBe(LogType.USER_LOGIN_FAILURE); | ||
| expect(res.status).toBe(401); | ||
| expect(res.body).toEqual({ error: "Invalid credentials or SSO required" }); | ||
|
|
||
| }); | ||
|
|
||
| }); | ||
|
|
||
|
|
||
|
|
||
| describe("PUT", () => { | ||
| it("resets a users password", async () => { //tests the fact that there is a password in the globalLocalUser | ||
| const res = await request(app) | ||
| .put("/api/auth/login") | ||
| .send({ | ||
| userId: tc.globalLocalUser.id, | ||
| password: "newPassword", | ||
| }); | ||
| expect(res.status).toBe(200); | ||
| expect(res.body).toEqual({ success: true }); | ||
|
|
||
| //get the user | ||
| const User = await prisma.user.findUnique({ | ||
| where: { id: tc.globalLocalUser.id }, | ||
| }); | ||
|
|
||
| expect(User.password).toBeDefined(); | ||
|
|
||
| //check the logs | ||
| const log = await prisma.logs.findFirst({ | ||
| where: { | ||
| type: LogType.USER_PASSWORD_CHANGE, | ||
| userId: tc.globalLocalUser.id, | ||
| }, | ||
| }); | ||
|
|
||
| expect(log).toBeDefined(); | ||
| expect(log.type).toBe(LogType.USER_PASSWORD_CHANGE); | ||
|
|
||
| }); | ||
|
|
||
| it("returns status 401, no user exists", async () => { //tests the fact that we dont have the user in our db | ||
| const res = await request(app) | ||
| .put("/api/auth/login") | ||
| .send({ | ||
| userId: "nonExistentUser", | ||
| password: "newPassword", | ||
| }); | ||
|
|
||
| expect(res.status).toBe(401); | ||
| expect(res.body).toEqual({ error: "Invalid credentials or SSO required" }); | ||
|
|
||
| }); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| it("returns status 401, no password field exisits in the db", async () => { //tests the fact that user doesnt have a password field i.e (SSO) | ||
| const res = await request(app) | ||
| .put("/api/auth/login") | ||
| .send({ | ||
| userId: tc.globalUser.id, //we can just use the globalUser because this has no password attribute and should throw an error. | ||
| password: "newPassword", | ||
| }); | ||
|
|
||
| expect(res.status).toBe(401); | ||
| expect(res.body).toEqual({ error: "Invalid credentials or SSO required" }); | ||
|
|
||
| }); | ||
|
|
||
| }); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.