import supertest from 'supertest';
import { server, createCompanyUser, createTestUser } from '../helpers';

const user = {
  email: 'abc@email.com',
  password: '12345678',
  companyName: 'Dre',
  rcNumber: 'bn 12345',
  address: 'lekki, lagos',
  phoneNumber: '+2348012345678',
};

describe('POST /auth/signin', () => {
  it('should return 400 if email &/or password is not passed', async () => {
    const res = await supertest(server).post('/api/v1/auth/signin').send();

    expect(res.status).toBe(400);
  });

  it('should return 404 if email passed not registered', async () => {
    const res = await supertest(server)
      .post('/api/v1/auth/signin')
      .send({ email: user.email, password: user.password });

    expect(res.status).toBe(404);
    expect(res.body).toMatchObject({
      success: false,
      message: 'Invalid email address',
    });
  });

  it('should return 403 if password is not a match', async () => {
    await createTestUser({ email: user.email, password: user.password });

    const res = await supertest(server)
      .post('/api/v1/auth/signin')
      .send({ email: user.email, password: 'invalid-password' });

    expect(res.status).toBe(403);
    expect(res.body).toMatchObject({
      success: false,
      message: 'Invalid password',
    });
  });

  it('should return 403 if account is not active', async () => {
    await createCompanyUser({
      email: user.email,
      password: user.password,
      isActive: false,
    });

    const res = await supertest(server)
      .post('/api/v1/auth/signin')
      .send({ email: user.email, password: user.password });

    expect(res.status).toBe(403);
    expect(res.body).toMatchObject({
      success: false,
      message: 'Account is not active',
    });
  });

  it('should return 200 and correct response', async () => {
    await createCompanyUser({ email: user.email, password: user.password });

    const res = await supertest(server)
      .post('/api/v1/auth/signin')
      .send({ email: user.email, password: user.password });

    expect(res.status).toBe(200);
    expect(res.body).toMatchObject({
      success: true,
      message: 'Sign in successful',
      data: expect.any(Object),
    });
  });
});
