/* eslint-disable no-unused-expressions */
const chai = require('chai')
chai.use(require('chai-http'))
const expect = chai.expect

const constants = require('../constants.js')
const app = require('../../../src/index.js')

describe('Testing that a user maintains their quota if the cna role is removed', () => {
  context('Positive Test', () => {
    it('User should still have quota after cna role removal', async () => {
      // Get the org ID since it is remade every run
      let orgUuid, quota
      await chai.request(app)
        .get('/api/org/win_5/users')
        .set(constants.headers)
        .then((res, err) => {
          expect(err).to.be.undefined
          expect(res).to.have.status(200)
          orgUuid = res.body.users[0].org_UUID
        })
      // Check to make sure ORG has the role.
      await chai.request(app)
        .get(`/api/org/${orgUuid}`)
        .set(constants.headers)
        .then((res, err) => {
          expect(err).to.be.undefined
          expect(res).to.have.status(200)
          expect(res.body.authority.active_roles).to.include('ADP')
          expect(res.body.policies.id_quota).to.exist
          quota = res.body.policies.id_quota
        })
      // Remove the role from the user. & check the quota and active roles
      await chai.request(app)
        .put('/api/org/win_5?active_roles.remove=CNA')
        .set(constants.headers)
        .then((res, err) => {
          expect(err).to.be.undefined
          expect(res).to.have.status(200)
          expect(res.body.updated.authority.active_roles).to.not.include('CNA')
          expect(res.body.updated.policies.id_quota).to.be.equal(quota)
        })
    })
  })
  // Give the user the role back incase it is used by another test
  afterEach(async () => {
    await chai.request(app)
      .put('/api/org/win_5?active_roles.add=CNA')
      .set(constants.headers)
      .then((res, err) => {
        expect(err).to.be.undefined
        expect(res).to.have.status(200)
        expect(res.body.updated.authority.active_roles).to.include('CNA')
      })
  })
})
