forked from redwoodjs/redwood
-
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.
Also convert fetch handler test to vitest
- Loading branch information
Showing
1 changed file
with
24 additions
and
13 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 |
---|---|---|
@@ -1,6 +1,17 @@ | ||
import crypto from 'node:crypto' | ||
import path from 'node:path' | ||
|
||
import { | ||
vi, | ||
describe, | ||
it, | ||
expect, | ||
beforeAll, | ||
afterAll, | ||
beforeEach, | ||
afterEach, | ||
} from 'vitest' | ||
|
||
import { DbAuthHandler } from '../DbAuthHandler' | ||
import * as dbAuthError from '../errors' | ||
import { hashToken } from '../shared' | ||
|
@@ -136,7 +147,7 @@ let req, context, options | |
describe('dbAuth', () => { | ||
beforeEach(() => { | ||
// hide deprecation warnings during test | ||
jest.spyOn(console, 'warn').mockImplementation(() => {}) | ||
vi.spyOn(console, 'warn').mockImplementation(() => {}) | ||
// encryption key so results are consistent regardless of settings in .env | ||
process.env.SESSION_SECRET = SESSION_SECRET | ||
delete process.env.DBAUTH_COOKIE_DOMAIN | ||
|
@@ -221,7 +232,7 @@ describe('dbAuth', () => { | |
}) | ||
|
||
afterEach(async () => { | ||
jest.spyOn(console, 'warn').mockRestore() | ||
vi.spyOn(console, 'warn').mockRestore() | ||
await db.user.deleteMany({ | ||
where: { email: '[email protected]' }, | ||
}) | ||
|
@@ -665,7 +676,7 @@ describe('dbAuth', () => { | |
const dbAuth = new DbAuthHandler(fetchEvent, context, options) | ||
await dbAuth.init() | ||
|
||
dbAuth.logout = jest.fn(() => { | ||
dbAuth.logout = vi.fn(() => { | ||
throw Error('Logout error') | ||
}) | ||
const response = await dbAuth.invoke() | ||
|
@@ -687,7 +698,7 @@ describe('dbAuth', () => { | |
credentials: true, | ||
}, | ||
}) | ||
dbAuth.logout = jest.fn(() => { | ||
dbAuth.logout = vi.fn(() => { | ||
throw Error('Logout error') | ||
}) | ||
const response = await dbAuth.invoke() | ||
|
@@ -711,7 +722,7 @@ describe('dbAuth', () => { | |
|
||
const dbAuth = new DbAuthHandler(fetchEvent, context, options) | ||
await dbAuth.init() | ||
dbAuth.logout = jest.fn(() => ['body', { foo: 'bar' }]) | ||
dbAuth.logout = vi.fn(() => ['body', { foo: 'bar' }]) | ||
const response = await dbAuth.invoke() | ||
|
||
expect(dbAuth.logout).toHaveBeenCalled() | ||
|
@@ -1245,8 +1256,8 @@ describe('dbAuth', () => { | |
}) | ||
|
||
it('login db check is called with insensitive string when user has provided one in LoginFlowOptions', async () => { | ||
jest.clearAllMocks() | ||
const spy = jest.spyOn(db.user, 'findFirst') | ||
vi.clearAllMocks() | ||
const spy = vi.spyOn(db.user, 'findFirst') | ||
|
||
options.signup.usernameMatch = 'insensitive' | ||
options.login.usernameMatch = 'insensitive' | ||
|
@@ -1279,8 +1290,8 @@ describe('dbAuth', () => { | |
}) | ||
|
||
it('login db check is not called with insensitive string when user has not provided one in LoginFlowOptions', async () => { | ||
jest.clearAllMocks() | ||
const spy = jest.spyOn(db.user, 'findFirst') | ||
vi.clearAllMocks() | ||
const spy = vi.spyOn(db.user, 'findFirst') | ||
|
||
delete options.signup.usernameMatch | ||
delete options.login.usernameMatch | ||
|
@@ -2993,7 +3004,7 @@ describe('dbAuth', () => { | |
}) | ||
|
||
it('createUser db check is called with insensitive string when user has provided one in SignupFlowOptions', async () => { | ||
const spy = jest.spyOn(db.user, 'findFirst') | ||
const spy = vi.spyOn(db.user, 'findFirst') | ||
options.signup.usernameMatch = 'insensitive' | ||
|
||
const dbUser = await createDbUser() | ||
|
@@ -3018,11 +3029,11 @@ describe('dbAuth', () => { | |
}) | ||
|
||
it('createUser db check is not called with insensitive string when user has not provided one in SignupFlowOptions', async () => { | ||
jest.resetAllMocks() | ||
jest.clearAllMocks() | ||
vi.resetAllMocks() | ||
vi.clearAllMocks() | ||
|
||
const defaultMessage = options.signup.errors.usernameTaken | ||
const spy = jest.spyOn(db.user, 'findFirst') | ||
const spy = vi.spyOn(db.user, 'findFirst') | ||
delete options.signup.usernameMatch | ||
|
||
const dbUser = await createDbUser() | ||
|