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

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

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

  it('should return 403 if user does not have permission', async () => {
    const res = await supertest(server)
      .get(`/api/v1/auth/roles`)
      .set('Authorization', await logInUser())
      .send();

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

  it('should return 200 and an Object if roleID is passed', async () => {
    const roleId = id();
    const companyId = id();
    await createRole({
      _id: roleId,
      company_id: companyId,
      permissions: ['view_roles'],
    });

    const res = await supertest(server)
      .get(`/api/v1/auth/roles`)
      .set(
        'Authorization',
        await logInUser({ role_id: roleId, company_id: companyId }),
      )
      .query({ roleID: roleId })
      .send();

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

  it('should return 200 and an array if no roleID is provided', async () => {
    const roleId = id();
    const companyId = id();

    await createRole({
      _id: roleId,
      company_id: companyId,
      permissions: ['view_roles'],
    });

    const res = await supertest(server)
      .get(`/api/v1/auth/roles`)
      .set(
        'Authorization',
        await logInUser({ role_id: roleId, company_id: companyId }),
      )
      .send();

    expect(res.status).toBe(200);
    expect(res.body).toMatchObject({
      success: true,
      message: 'Roles request successful',
      data: {
        role: expect.any(Array),
      },
    });
  });
});
