mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-03-02 20:02:00 -05:00
refactor(api): sync dev and auth0 plugins (#57136)
This commit is contained in:
committed by
GitHub
parent
c69e9c6fff
commit
2f4e6ae8f5
139
api/src/plugins/auth-dev.test.ts
Normal file
139
api/src/plugins/auth-dev.test.ts
Normal file
@@ -0,0 +1,139 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||
import Fastify, { FastifyInstance } from 'fastify';
|
||||
|
||||
import { defaultUserEmail } from '../../jest.utils';
|
||||
import { HOME_LOCATION } from '../utils/env';
|
||||
import { devAuth } from '../plugins/auth-dev';
|
||||
import prismaPlugin from '../db/prisma';
|
||||
import auth from './auth';
|
||||
import cookies from './cookies';
|
||||
|
||||
import { newUser } from './__fixtures__/user';
|
||||
|
||||
describe('dev login', () => {
|
||||
let fastify: FastifyInstance;
|
||||
|
||||
beforeAll(async () => {
|
||||
fastify = Fastify();
|
||||
|
||||
await fastify.register(cookies);
|
||||
await fastify.register(auth);
|
||||
await fastify.register(devAuth);
|
||||
await fastify.register(prismaPlugin);
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await fastify.prisma.user.deleteMany({
|
||||
where: { email: defaultUserEmail }
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await fastify.prisma.user.deleteMany({
|
||||
where: { email: defaultUserEmail }
|
||||
});
|
||||
await fastify.close();
|
||||
});
|
||||
|
||||
describe('GET /signin', () => {
|
||||
it('should create an account if one does not exist', async () => {
|
||||
const before = await fastify.prisma.user.count({});
|
||||
await fastify.inject({
|
||||
method: 'GET',
|
||||
url: '/signin'
|
||||
});
|
||||
|
||||
const after = await fastify.prisma.user.count({});
|
||||
|
||||
expect(before).toBe(0);
|
||||
expect(after).toBe(before + 1);
|
||||
});
|
||||
|
||||
it('should populate the user with the correct data', async () => {
|
||||
await fastify.inject({
|
||||
method: 'GET',
|
||||
url: '/signin'
|
||||
});
|
||||
|
||||
const user = await fastify.prisma.user.findFirstOrThrow({
|
||||
where: { email: defaultUserEmail }
|
||||
});
|
||||
|
||||
expect(user).toEqual(newUser(defaultUserEmail));
|
||||
expect(user.username).toBe(user.usernameDisplay);
|
||||
});
|
||||
|
||||
it('should set the jwt_access_token cookie', async () => {
|
||||
const res = await fastify.inject({
|
||||
method: 'GET',
|
||||
url: '/signin'
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(302);
|
||||
|
||||
expect(res.cookies).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ name: 'jwt_access_token' })
|
||||
])
|
||||
);
|
||||
});
|
||||
|
||||
it.todo('should create a session');
|
||||
|
||||
it('should redirect to the Referer (if it is a valid origin)', async () => {
|
||||
const res = await fastify.inject({
|
||||
method: 'GET',
|
||||
url: '/signin',
|
||||
headers: {
|
||||
referer: 'https://www.freecodecamp.org/some-path/or/other'
|
||||
}
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(302);
|
||||
expect(res.headers.location).toBe(
|
||||
'https://www.freecodecamp.org/some-path/or/other'
|
||||
);
|
||||
});
|
||||
|
||||
it('should redirect to /valid-language/learn when signing in from /valid-language', async () => {
|
||||
const res = await fastify.inject({
|
||||
method: 'GET',
|
||||
url: '/signin',
|
||||
headers: {
|
||||
referer: 'https://www.freecodecamp.org/espanol'
|
||||
}
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(302);
|
||||
expect(res.headers.location).toBe(
|
||||
'https://www.freecodecamp.org/espanol/learn'
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle referers with trailing slahes', async () => {
|
||||
const res = await fastify.inject({
|
||||
method: 'GET',
|
||||
url: '/signin',
|
||||
headers: {
|
||||
referer: 'https://www.freecodecamp.org/espanol/'
|
||||
}
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(302);
|
||||
expect(res.headers.location).toBe(
|
||||
'https://www.freecodecamp.org/espanol/learn'
|
||||
);
|
||||
});
|
||||
|
||||
it('should redirect to /learn by default', async () => {
|
||||
const res = await fastify.inject({
|
||||
method: 'GET',
|
||||
url: '/signin'
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(302);
|
||||
expect(res.headers.location).toBe(`${HOME_LOCATION}/learn`);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user