Repository: freeCodeCamp/freeCodeCamp
File: api/src/plugins/auth0.test.ts (TypeScript)

Code before:
import bouncer from './bouncer.js';
import { newUser } from './__fixtures__/user.js';

const COOKIE_DOMAIN = 'test.com';

vi.mock('../utils/env', async importOriginal => ({
  ...(await importOriginal<typeof import('../utils/env.js')>()),
  COOKIE_DOMAIN: 'test.com'
}));

describe('auth0 plugin', () => {
  let fastify: FastifyInstance;

  beforeAll(async () => {
    fastify = Fastify();

    await fastify.register(cookies);
    await fastify.register(redirectWithMessage);
    await fastify.register(auth);
    await fastify.register(bouncer);
    await fastify.register(auth0Client);
    await fastify.register(prismaPlugin);
    // Mock the GrowthBook plugin
    fastify.decorate('gb', {
      isOn: () => false
    } as Partial<GrowthBook> as GrowthBook);
  });

  describe('GET /signin/google', () => {
    test('should redirect directly to Google via Auth0 with connection param', async () => {
      const res = await fastify.inject({
        method: 'GET',
        url: '/signin/google'
      });
      const redirectUrl = new URL(res.headers.location!);
      expect(redirectUrl.host).toMatch(AUTH0_DOMAIN);
      expect(redirectUrl.pathname).toBe('/authorize');
      expect(redirectUrl.searchParams.get('connection')).toBe('google-oauth2');
      expect(res.statusCode).toBe(302);
    });

    test('sets a login-returnto cookie', async () => {
      const returnTo = 'http://localhost:3000/learn';
      const res = await fastify.inject({
        method: 'GET',
        url: '/signin/google',
        headers: { referer: returnTo }
      });
      const cookie = res.cookies.find(c => c.name === 'login-returnto');
      expect(unsign(cookie!.value).value).toBe(returnTo);
      expect(cookie).toMatchObject({

Review comment (suggestion):
```suggestion
    await fastify.register(growthBook, {
      apiHost: GROWTHBOOK_FASTIFY_API_HOST,
      clientKey: GROWTHBOOK_FASTIFY_CLIENT_KEY
    });
```

Code after:
import cookies, { sign, unsign } from './cookies.js';
import { auth0Client } from './auth0.js';
import redirectWithMessage, { formatMessage } from './redirect-with-message.js';
import auth from './auth.js';
import bouncer from './bouncer.js';
import growthBook from './growth-book.js';
import { newUser } from './__fixtures__/user.js';

const COOKIE_DOMAIN = 'test.com';

vi.mock('../utils/env', async importOriginal => ({
  ...(await importOriginal<typeof import('../utils/env.js')>()),
  COOKIE_DOMAIN: 'test.com'
}));

describe('auth0 plugin', () => {
  let fastify: FastifyInstance;

  beforeAll(async () => {
    fastify = Fastify();

    await fastify.register(cookies);
    await fastify.register(redirectWithMessage);
    await fastify.register(auth);
    await fastify.register(bouncer);
    await fastify.register(auth0Client);
    await fastify.register(prismaPlugin);
    await fastify.register(growthBook, {
      apiHost: GROWTHBOOK_FASTIFY_API_HOST,
      clientKey: GROWTHBOOK_FASTIFY_CLIENT_KEY
    });
  });

  describe('GET /signin/google', () => {
    test('should redirect directly to Google via Auth0 with connection param', async () => {
      const res = await fastify.inject({
        method: 'GET',
        url: '/signin/google'
      });
      const redirectUrl = new URL(res.headers.location!);
      expect(redirectUrl.host).toMatch(AUTH0_DOMAIN);
      expect(redirectUrl.pathname).toBe('/authorize');
      expect(redirectUrl.searchParams.get('connection')).toBe('google-oauth2');
      expect(res.statusCode).toBe(302);
    });

    test('sets a login-returnto cookie', async () => {
      const returnTo = 'http://localhost:3000/learn';
      const res = await fastify.inject({
        method: 'GET',
        url: '/signin/google',