Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
jrivals committed Dec 16, 2024
1 parent 52008a3 commit 94c5a9b
Showing 1 changed file with 12 additions and 59 deletions.
71 changes: 12 additions & 59 deletions server/routers/test/auth.router.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { fakerFR } from '@faker-js/faker';
import bcrypt from 'bcryptjs';
import { constants } from 'http2';
import randomstring from 'randomstring';
import request from 'supertest';
import { genUser, genValidPassword } from '../../../shared/test/userFixtures';
import { genUser } from '../../../shared/test/userFixtures';
import { Users } from '../../repositories/userRepository';
import { createServer } from '../../server';

import { describe, test, expect, beforeAll, afterAll } from 'vitest';
const { app } = createServer();
import { afterAll, beforeAll, describe, expect, test } from 'vitest';

describe('Auth routes', () => {
const { app } = createServer();

const user = genUser();

beforeAll(async () => {
Expand All @@ -25,62 +24,16 @@ describe('Auth routes', () => {
await Users().delete().where('email', user.email);
});

describe('POST /auth/sign-in', () => {
const testRoute = '/api/auth/sign-in';

test('should receive valid email and password', async () => {
await request(app)
.post(testRoute)
.send({
email: randomstring.generate(),
password: genValidPassword()
})
.expect(constants.HTTP_STATUS_BAD_REQUEST);

await request(app)
.post(testRoute)
.send({
email: fakerFR.internet.email(),
password: ' '
})
.expect(constants.HTTP_STATUS_BAD_REQUEST);
});

test('should fail if the user is missing', async () => {
await request(app)
.post(testRoute)
.send({
email: fakerFR.internet.email(),
password: genValidPassword()
})
.expect(constants.HTTP_STATUS_UNAUTHORIZED);
});

test('should fail if the password is wrong', async () => {
await request(app)
.post(testRoute)
.send({
email: user.email,
password: genValidPassword()
})
.expect(constants.HTTP_STATUS_UNAUTHORIZED);
});
describe('GET /auth/redirect-url', () => {
const testRoute = '/api/auth/redirect-url';

test('should succeed if the user is found and the password is correct', async () => {
const res = await request(app)
.post(testRoute)
.send({
email: user.email,
password: user.password
})
.expect(constants.HTTP_STATUS_OK);
test('should return a redirect url', async () => {
const response = await request(app).get(testRoute);

expect(res.body).toMatchObject(
expect.objectContaining({
userId: user.id,
accessToken: expect.any(String)
})
);
expect(response.status).toBe(constants.HTTP_STATUS_OK);
expect(response.body).toHaveProperty('url');
expect(response.body).toHaveProperty('state');
expect(response.body).toHaveProperty('nonce');
});
});
});

0 comments on commit 94c5a9b

Please sign in to comment.