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

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

describe('PATCH /auth/reset-password', () => {
  it('should return 400 no token or passwords is passed', async () => {
    const res = await supertest(server)
      .patch('/api/v1/auth/reset-password')
      .send();

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

  it('should return 404 if user is not found', async () => {
    const token = getToken(user);

    const res = await supertest(server)
      .patch('/api/v1/auth/reset-password')
      .query({ token })
      .send({ newPassword: 'password', confirmPassword: 'password' });

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

  it('should return 201 and correct data', async () => {
    const createdUser = await createTestUser();
    const token = getToken(createdUser.toObject());

    const res = await supertest(server)
      .patch('/api/v1/auth/reset-password')
      .query({ token })
      .send({ newPassword: 'password', confirmPassword: 'password' });

    expect(res.status).toBe(201);
    expect(res.body).toMatchObject({
      success: true,
      message: 'password reset successful',
    });
  });
});
