import supertest from 'supertest';
import nock from 'nock';
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/request-email-verification', () => {
  it('should return 400 if no email is not passed', async () => {
    const res = await supertest(server)
      .post('/api/v1/auth/request-email-verification')
      .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/request-email-verification')
      .send({ email: user.email });

    expect(res.status).toBe(404);
    expect(res.body).toMatchObject({
      success: false,
      message: 'User account does not exist',
    });
  });

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

    const res = await supertest(server)
      .post('/api/v1/auth/request-email-verification')
      .send({ email: user.email });

    expect(res.status).toBe(403);
    expect(res.body).toMatchObject({
      success: false,
      message: 'kindly contact organization Admin to activate account',
    });
  });

  it('should return 500 if email service is down', async () => {
    await createCompanyUser({ email: user.email });

    const res = await supertest(server)
      .post('/api/v1/auth/request-email-verification')
      .send({ email: user.email });

    expect(res.status).toBe(500);
    expect(res.body).toMatchObject({
      success: false,
      message: 'Sending Email verification link failed',
    });
  });

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

    nock(process.env.NOTIFICATION_SERVICE_URL!)
      .post('/send-email')
      .reply(200, {});

    const res = await supertest(server)
      .post('/api/v1/auth/request-email-verification')
      .send({ email: user.email });

    expect(res.status).toBe(200);
    expect(res.body).toMatchObject({
      success: true,
      message:
        'Link successfully sent. please check your email for email verification',
    });
  });
});
