import supertest from 'supertest';
import { server, createTestUser, logInUser, id, createRole } from '../helpers';

describe('POST /auth/users', () => {
  it('should return 403 if user is not logged in', async () => {
    const res = await supertest(server).post('/api/v1/auth/users').send();

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

  it('should return 400 if user does not pass the necessary details', async () => {
    const res = await supertest(server)
      .post('/api/v1/auth/users')
      .set('Authorization', await logInUser())
      .send();

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

  it('should return 403 if user does not have permission', async () => {
    const roleId = id();
    const res = await supertest(server)
      .post('/api/v1/auth/users')
      .set('Authorization', await logInUser({ role_id: roleId }))
      .send({
        firstName: 'firstName',
        lastName: 'lastName',
        userType: 'staff',
        email: 'abc@email.com',
        phoneNumber: '+2348012345690',
        password: 'password',
        confirmPassword: 'password',
        role_id: id(),
      });

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

  it('should return 201 if has permissions', async () => {
    const roleId = id();
    const companyId = id();
    await createRole({
      _id: roleId,
      company_id: companyId,
      permissions: ['create_users'],
    });

    await createTestUser({
      company_id: companyId,
    });

    const res = await supertest(server)
      .post('/api/v1/auth/users')
      .set(
        'Authorization',
        await logInUser({ role_id: roleId, company_id: companyId }),
      )
      .send({
        firstName: 'firstName',
        lastName: 'lastName',
        userType: 'staff',
        email: 'abcd@email.com',
        phoneNumber: '+2348012345690',
        password: 'password',
        confirmPassword: 'password',
        role_id: id(),
      });

    expect(res.status).toBe(201);
    expect(res.body).toMatchObject({
      success: true,
      message: 'staff created successfully',
    });
  });

  it('should return 403 if user does not have permission', async () => {
    const roleId = id();
    const res = await supertest(server)
      .post('/api/v1/auth/users')
      .set('Authorization', await logInUser({ role_id: roleId }))
      .send({
        firstName: 'firstName',
        lastName: 'lastName',
        userType: 'rider',
        email: 'abc@email.com',
        phoneNumber: '+2348012345690',
        password: 'password',
        confirmPassword: 'password',
        address: 'address',
      });

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

  it('should return 400 if additional rider details are not entered', async () => {
    const roleId = id();
    const companyId = id();
    await createRole({
      _id: roleId,
      company_id: companyId,
      permissions: ['create_dispatchers'],
    });

    await createTestUser({
      company_id: companyId,
    });

    const res = await supertest(server)
      .post('/api/v1/auth/users')
      .set(
        'Authorization',
        await logInUser({ role_id: roleId, company_id: companyId }),
      )
      .send({
        firstName: 'firstName',
        lastName: 'lastName',
        userType: 'rider',
        email: 'abcd@email.com',
        phoneNumber: '+2348012345690',
        password: 'password',
        confirmPassword: 'password',
      });

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

  it('should return 201 if has permissions to create rider', async () => {
    const roleId = id();
    const companyId = id();
    await createRole({
      _id: roleId,
      company_id: companyId,
      permissions: ['create_dispatchers'],
    });

    await createTestUser({
      company_id: companyId,
    });

    const res = await supertest(server)
      .post('/api/v1/auth/users')
      .set(
        'Authorization',
        await logInUser({ role_id: roleId, company_id: companyId }),
      )
      .send({
        firstName: 'firstName',
        lastName: 'lastName',
        userType: 'rider',
        email: 'abcd@email.com',
        phoneNumber: '+2348012345690',
        password: 'password',
        confirmPassword: 'password',
        address: 'address',
      });

    expect(res.status).toBe(201);
    expect(res.body).toMatchObject({
      success: true,
      message: 'rider created successfully',
    });
  });
});
