mirror of
https://github.com/SrIzan10/next-auth.git
synced 2026-05-01 10:55:20 +00:00
* rename `host` to `origin` internally * rename `userOptions` to `authOptions` internally * use object for `headers` internally * default `method` to GET * simplify `unstable_getServerSession` * allow optional headers * revert middleware * wip getURL * revert host detection * use old `detectHost` * fix/add some tests wip * move more to core, refactor getURL * better type auth actions * fix custom path support (w/ api/auth) * add `getURL` tests * fix email tests * fix assert tests * custom base without api/auth, with trailing slash * remove parseUrl from assert.ts * return 400 when wrong url * fix tests * refactor * fix protocol in dev * fix tests * fix custom url handling * add todo comments
173 lines
4.6 KiB
TypeScript
173 lines
4.6 KiB
TypeScript
import { createCSRF, handler, mockAdapter } from "./utils"
|
|
import EmailProvider from "../src/providers/email"
|
|
|
|
it("Send e-mail to the only address correctly", async () => {
|
|
const { secret, csrf } = await createCSRF()
|
|
|
|
const sendVerificationRequest = jest.fn()
|
|
const signIn = jest.fn(() => true)
|
|
|
|
const email = "email@example.com"
|
|
const { res } = await handler(
|
|
{
|
|
adapter: mockAdapter(),
|
|
providers: [EmailProvider({ sendVerificationRequest })],
|
|
callbacks: { signIn },
|
|
secret,
|
|
trustHost: true,
|
|
},
|
|
{
|
|
path: "signin/email",
|
|
requestInit: {
|
|
method: "POST",
|
|
headers: { cookie: csrf.cookie, "content-type": "application/json" },
|
|
body: JSON.stringify({ email: email, csrfToken: csrf.value }),
|
|
},
|
|
}
|
|
)
|
|
|
|
expect(res.redirect).toBe(
|
|
"http://localhost:3000/api/auth/verify-request?provider=email&type=email"
|
|
)
|
|
|
|
expect(signIn).toBeCalledTimes(1)
|
|
expect(signIn).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
user: expect.objectContaining({ email }),
|
|
})
|
|
)
|
|
|
|
expect(sendVerificationRequest).toHaveBeenCalledWith(
|
|
expect.objectContaining({ identifier: email })
|
|
)
|
|
})
|
|
|
|
it("Send e-mail to first address only", async () => {
|
|
const { secret, csrf } = await createCSRF()
|
|
const sendVerificationRequest = jest.fn()
|
|
const signIn = jest.fn(() => true)
|
|
|
|
const firstEmail = "email@email.com"
|
|
const email = `${firstEmail},email@email2.com`
|
|
const { res } = await handler(
|
|
{
|
|
adapter: mockAdapter(),
|
|
providers: [EmailProvider({ sendVerificationRequest })],
|
|
callbacks: { signIn },
|
|
secret,
|
|
trustHost: true,
|
|
},
|
|
{
|
|
path: "signin/email",
|
|
requestInit: {
|
|
method: "POST",
|
|
headers: { cookie: csrf.cookie, "content-type": "application/json" },
|
|
body: JSON.stringify({ email: email, csrfToken: csrf.value }),
|
|
},
|
|
}
|
|
)
|
|
|
|
expect(res.redirect).toBe(
|
|
"http://localhost:3000/api/auth/verify-request?provider=email&type=email"
|
|
)
|
|
|
|
expect(signIn).toBeCalledTimes(1)
|
|
expect(signIn).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
user: expect.objectContaining({ email: firstEmail }),
|
|
})
|
|
)
|
|
|
|
expect(sendVerificationRequest).toHaveBeenCalledWith(
|
|
expect.objectContaining({ identifier: firstEmail })
|
|
)
|
|
})
|
|
|
|
it("Send e-mail to address with first domain", async () => {
|
|
const { secret, csrf } = await createCSRF()
|
|
const sendVerificationRequest = jest.fn()
|
|
const signIn = jest.fn(() => true)
|
|
|
|
const firstEmail = "email@email.com"
|
|
const email = `${firstEmail},email2.com`
|
|
const { res } = await handler(
|
|
{
|
|
adapter: mockAdapter(),
|
|
providers: [EmailProvider({ sendVerificationRequest })],
|
|
callbacks: { signIn },
|
|
secret,
|
|
trustHost: true,
|
|
},
|
|
{
|
|
path: "signin/email",
|
|
requestInit: {
|
|
method: "POST",
|
|
headers: { cookie: csrf.cookie, "content-type": "application/json" },
|
|
body: JSON.stringify({ email: email, csrfToken: csrf.value }),
|
|
},
|
|
}
|
|
)
|
|
|
|
expect(res.redirect).toBe(
|
|
"http://localhost:3000/api/auth/verify-request?provider=email&type=email"
|
|
)
|
|
|
|
expect(signIn).toBeCalledTimes(1)
|
|
expect(signIn).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
user: expect.objectContaining({ email: firstEmail }),
|
|
})
|
|
)
|
|
|
|
expect(sendVerificationRequest).toHaveBeenCalledWith(
|
|
expect.objectContaining({ identifier: firstEmail })
|
|
)
|
|
})
|
|
|
|
it("Redirect to error page if multiple addresses aren't allowed", async () => {
|
|
const { secret, csrf } = await createCSRF()
|
|
const sendVerificationRequest = jest.fn()
|
|
const signIn = jest.fn()
|
|
const error = new Error("Only one email allowed")
|
|
const { res, log } = await handler(
|
|
{
|
|
adapter: mockAdapter(),
|
|
callbacks: { signIn },
|
|
providers: [
|
|
EmailProvider({
|
|
sendVerificationRequest,
|
|
normalizeIdentifier(identifier) {
|
|
if (identifier.split("@").length > 2) throw error
|
|
return identifier
|
|
},
|
|
}),
|
|
],
|
|
secret,
|
|
trustHost: true,
|
|
},
|
|
{
|
|
path: "signin/email",
|
|
requestInit: {
|
|
method: "POST",
|
|
headers: { cookie: csrf.cookie, "content-type": "application/json" },
|
|
body: JSON.stringify({
|
|
email: "email@email.com,email@email2.com",
|
|
csrfToken: csrf.value,
|
|
}),
|
|
},
|
|
}
|
|
)
|
|
|
|
expect(signIn).toBeCalledTimes(0)
|
|
expect(sendVerificationRequest).toBeCalledTimes(0)
|
|
|
|
expect(log.error.mock.calls[0]).toEqual([
|
|
"SIGNIN_EMAIL_ERROR",
|
|
{ error, providerId: "email" },
|
|
])
|
|
|
|
expect(res.redirect).toBe(
|
|
"http://localhost:3000/api/auth/error?error=EmailSignin"
|
|
)
|
|
})
|