/* 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')

const requestLength = 10

describe('Testing Reserve CVE-ID Endpoints', () => {
  // beforeEach(() => { })
  context('Positive Tests', () => {
    it('Should return 200 and have correct number of cve-id results for a successful non-squential cve-id reservation ', (done) => {
      chai.request(app)
        .post(`/api/cve-id?amount=${requestLength}&cve_year=2023&short_name=mitre&batch_type=non-sequential`)
        .set(constants.headers)
        .end((err, res) => {
          expect(err).to.be.null
          expect(res.body).to.have.property('cve_ids')
          expect(res.body.cve_ids).to.have.length(requestLength)
          expect(res).to.have.status(200)
          done()
        })
    })
    it('Should return 200 and have correct number of cve-id results in sequential order for a successful sequential cve-id reserveration ', (done) => {
      chai.request(app)
        .post('/api/cve-id?amount=10&cve_year=2023&short_name=mitre&batch_type=sequential')
        .set(constants.headers)
        .end((err, res) => {
          expect(err).to.be.null
          expect(res.body.cve_ids).to.have.length(10)
          expect(res).to.have.status(200)
          const onlyCveIdNumbers = res.body.cve_ids.map((element) => { return parseInt(element.cve_id.split('-')[2]) })
          const cveNumbersInOrder = onlyCveIdNumbers.every((value, index) => {
            if (index === 0) {
              return true
            } else {
              return value >= onlyCveIdNumbers[index - 1]
            }
          })
          expect(cveNumbersInOrder).to.be.true
          done()
        })
    })
  })
  context('Negative Tests', () => {
    it('Should return 403 when a request is by a user to reserve cve-ids outside of their organization ', (done) => {
      chai.request(app)
        .post('/api/cve-id?amount=10&cve_year=2023&short_name=mitre&batch_type=sequential')
        .set(constants.nonSecretariatUserHeaders)
        .end((err, res) => {
          expect(err).to.be.null
          expect(res).to.have.status(403)
          done()
        })
    })
  })

  // afterEach(() => { })
})
