-
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.
fix register tests, and start login tests not working yet. Also impro…
…ve test report
- Loading branch information
Showing
8 changed files
with
271 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,4 +35,7 @@ yarn-error.log* | |
*.tsbuildinfo | ||
next-env.d.ts | ||
|
||
.env | ||
.env | ||
|
||
# tests | ||
test-report.html |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
import { testApiHandler } from "next-test-api-route-handler"; | ||
import * as loginHandler from "@/app/api/auth/[...nextauth]/route"; | ||
|
||
describe("NextAuth API", () => { | ||
it("signs in with valid credentials", async () => { | ||
await testApiHandler({ | ||
appHandler: loginHandler, // Use appHandler for Next.js API routes | ||
params: { nextauth: ["callback", "credentials"] }, | ||
test: async ({ fetch }) => { | ||
const res = await fetch({ | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
identifier: "[email protected]", | ||
password: "alicePassword", | ||
}), | ||
}); | ||
|
||
console.log("Response status:", res.status); | ||
|
||
if (res.status === 302) { | ||
console.log("Redirect location:", res.headers.get("location")); | ||
} | ||
|
||
// Handle the 302 redirect or other responses as needed | ||
expect(res.status).toBe(200); // Adjust this expectation based on your actual response | ||
|
||
// Only parse JSON if status is not 302 | ||
if (res.status !== 302) { | ||
const data = await res.json(); | ||
console.log("Response data:", data); | ||
|
||
expect(data).toHaveProperty("user"); | ||
expect(data.user).toEqual( | ||
expect.objectContaining({ | ||
email: "[email protected]", | ||
}), | ||
); | ||
} | ||
}, | ||
}); | ||
}); | ||
|
||
it("fails to sign in with invalid credentials", async () => { | ||
await testApiHandler({ | ||
appHandler: loginHandler, // Use appHandler for Next.js API routes | ||
params: { nextauth: ["callback", "credentials"] }, | ||
test: async ({ fetch }) => { | ||
const res = await fetch({ | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
identifier: "[email protected]", | ||
password: "invalid-password", | ||
}), | ||
}); | ||
|
||
console.log("Response status:", res.status); | ||
|
||
if (res.status === 302) { | ||
console.log("Redirect location:", res.headers.get("location")); | ||
} | ||
|
||
// Handle the 302 redirect or other responses as needed | ||
expect(res.status).toBe(401); // Adjust this expectation based on your actual response | ||
|
||
// Only parse JSON if status is not 302 | ||
if (res.status !== 302) { | ||
const data = await res.json(); | ||
console.log("Response data:", data); | ||
|
||
expect(data).toEqual( | ||
expect.objectContaining({ | ||
error: "CredentialsSignin", | ||
}), | ||
); | ||
} | ||
}, | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -7,20 +7,21 @@ describe("registerSchema", () => { | |
name: "John", | ||
lastName: "Doe", | ||
username: "johndoe", | ||
dateOfBirth: new Date("1990-01-01"), | ||
dateOfBirth: new Date("1990-01-01").toISOString(), | ||
email: "[email protected]", | ||
password: "Password@123", | ||
confirmPassword: "Password@123", | ||
}; | ||
|
||
expect(() => registerSchema.parse(input)).not.toThrow(); | ||
}); | ||
|
||
it("fails validation with invalid email format", () => { | ||
const input = { | ||
name: "John", | ||
lastName: "Doe", | ||
username: "johndoe", | ||
dateOfBirth: "1990-01-01", | ||
dateOfBirth: new Date("1990-01-01").toISOString(), | ||
email: "invalid-email", | ||
password: "Password@123", | ||
confirmPassword: "Password@123", | ||
|
@@ -34,7 +35,7 @@ describe("registerSchema", () => { | |
name: "J".repeat(256), | ||
lastName: "Doe", | ||
username: "johndoe", | ||
dateOfBirth: new Date("1990-01-01"), | ||
dateOfBirth: new Date("1990-01-01").toISOString(), | ||
email: "[email protected]", | ||
password: "Password@123", | ||
confirmPassword: "Password@123", | ||
|
@@ -50,7 +51,7 @@ describe("registerSchema", () => { | |
name: "John", | ||
lastName: "Doe", | ||
username: "johndoe", | ||
dateOfBirth: new Date("1990-01-01"), | ||
dateOfBirth: new Date("1990-01-01").toISOString(), | ||
email: "[email protected]", | ||
password: "password@123", | ||
confirmPassword: "password@123", | ||
|
@@ -66,7 +67,7 @@ describe("registerSchema", () => { | |
name: "John", | ||
lastName: "Doe", | ||
username: "johndoe", | ||
dateOfBirth: new Date("1990-01-01"), | ||
dateOfBirth: new Date("1990-01-01").toISOString(), | ||
email: "[email protected]", | ||
password: "Password@", | ||
confirmPassword: "Password@", | ||
|
@@ -82,7 +83,7 @@ describe("registerSchema", () => { | |
name: "John", | ||
lastName: "Doe", | ||
username: "johndoe", | ||
dateOfBirth: new Date("1990-01-01"), | ||
dateOfBirth: new Date("1990-01-01").toISOString(), | ||
email: "[email protected]", | ||
password: "Password123", | ||
confirmPassword: "Password123", | ||
|
@@ -98,7 +99,7 @@ describe("registerSchema", () => { | |
name: "John", | ||
lastName: "Doe", | ||
username: "johndoe", | ||
dateOfBirth: new Date("1990-01-01"), | ||
dateOfBirth: new Date("1990-01-01").toISOString(), | ||
email: "[email protected]", | ||
password: "Pass@1", | ||
confirmPassword: "Pass@1", | ||
|
@@ -114,7 +115,7 @@ describe("registerSchema", () => { | |
name: "John", | ||
lastName: "Doe", | ||
username: "johndoe", | ||
dateOfBirth: new Date("1990-01-01"), | ||
dateOfBirth: new Date("1990-01-01").toISOString(), | ||
email: "[email protected]", | ||
password: "Password@123", | ||
confirmPassword: "Password@124", | ||
|
@@ -140,23 +141,21 @@ describe("registerSchema", () => { | |
name: "John", | ||
lastName: "Doe", | ||
username: "johndoe", | ||
dateOfBirth: "invalid-date", // This should be a string to trigger the invalid date error | ||
dateOfBirth: "invalid-date", | ||
email: "[email protected]", | ||
password: "Password@123", | ||
confirmPassword: "Password@123", | ||
}; | ||
|
||
expect(() => registerSchema.parse(input)).toThrow( | ||
"Expected date, received string", | ||
); | ||
expect(() => registerSchema.parse(input)).toThrow("Date invalide"); | ||
}); | ||
|
||
it("fails validation with date of birth in the future", () => { | ||
const input = { | ||
name: "John", | ||
lastName: "Doe", | ||
username: "johndoe", | ||
dateOfBirth: new Date(Date.now() + 86400000), // Future date | ||
dateOfBirth: new Date(Date.now() + 86400000).toISOString(), | ||
email: "[email protected]", | ||
password: "Password@123", | ||
confirmPassword: "Password@123", | ||
|
@@ -172,7 +171,7 @@ describe("registerSchema", () => { | |
name: " ", | ||
lastName: " ", | ||
username: " ", | ||
dateOfBirth: new Date("1990-01-01"), | ||
dateOfBirth: new Date("1990-01-01").toISOString(), | ||
email: "[email protected]", | ||
password: "Password@123", | ||
confirmPassword: "Password@123", | ||
|
@@ -186,7 +185,7 @@ describe("registerSchema", () => { | |
name: "John", | ||
lastName: "Doe", | ||
username: "johndoe", | ||
dateOfBirth: new Date("1990-01-01"), | ||
dateOfBirth: new Date("1990-01-01").toISOString(), | ||
email: "[email protected]", | ||
password: "Abc12345", | ||
confirmPassword: "Abc12345", | ||
|
@@ -202,7 +201,7 @@ describe("registerSchema", () => { | |
name: "John", | ||
lastName: "Doe", | ||
username: "johndoe", | ||
dateOfBirth: new Date("1990-01-01"), | ||
dateOfBirth: new Date("1990-01-01").toISOString(), | ||
email: "[email protected]", | ||
password: "Password 123", | ||
confirmPassword: "Password 123", | ||
|
@@ -218,7 +217,7 @@ describe("registerSchema", () => { | |
name: "John", | ||
lastName: "Doe", | ||
username: "JohnDoe", | ||
dateOfBirth: new Date("1990-01-01"), | ||
dateOfBirth: new Date("1990-01-01").toISOString(), | ||
email: "[email protected]", | ||
password: "Password@123", | ||
confirmPassword: "Password@123", | ||
|
@@ -232,7 +231,7 @@ describe("registerSchema", () => { | |
name: "John", | ||
lastName: "Doe", | ||
username: "johndoe", | ||
dateOfBirth: new Date("1990-01-01"), | ||
dateOfBirth: new Date("1990-01-01").toISOString(), | ||
email: "[email protected]", | ||
password: "Password@123", | ||
confirmPassword: "Password@123", | ||
|
@@ -246,7 +245,7 @@ describe("registerSchema", () => { | |
name: "John", | ||
lastName: "Doe", | ||
username: "johndoe", | ||
dateOfBirth: new Date("1990-01-01"), | ||
dateOfBirth: new Date("1990-01-01").toISOString(), | ||
email: " [email protected] ", | ||
password: "Password@123", | ||
confirmPassword: "Password@123", | ||
|
@@ -262,7 +261,7 @@ describe("registerSchema", () => { | |
name: 123, | ||
lastName: {}, | ||
username: [], | ||
dateOfBirth: new Date("1990-01-01"), | ||
dateOfBirth: new Date("1990-01-01").toISOString(), | ||
email: 456, | ||
password: true, | ||
confirmPassword: false, | ||
|
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
Oops, something went wrong.