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

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

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

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

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

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

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

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

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

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

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

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

    expect(res.status).toBe(200);
    expect(res.body).toMatchObject({
      success: true,
      message: 'reset link has been sent to your email successfully',
    });
  });
});
