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

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

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

  it('should return 403 if has no permissions', async () => {
    const res = await supertest(server)
      .get('/api/v1/auth/permissions')
      .set('Authorization', await logInUser())
      .send();

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

  it('should return 200 and correct data', async () => {
    const roleId = id();
    const companyId = id();
    await createRole({
      _id: roleId,
      company_id: companyId,
      permissions: ['create_roles'],
    });

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

    expect(res.status).toBe(200);
    expect(res.body).toMatchObject({
      success: true,
      message: 'List of all available Permissions',
      data: expect.any(Array),
    });
  });
});
