/* eslint-disable no-unused-expressions */

const chai = require('chai')
chai.use(require('chai-http'))
const _ = require('lodash')

const expect = chai.expect

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

describe('Testing Get CVE-ID endpoint', () => {
  // TODO: Update this test to dynamically calculate reserved count.
  const RESESRVED_COUNT = 120
  const YEAR_COUNT = 10
  const PUB_YEAR_COUNT = 4
  const TIME_WINDOW_COUNT = 40

  context('Positive Tests', () => {
    it('Get CVE-ID should return everything when no parameters are specifed', async () => {
      await chai.request(app)
        .get('/api/cve-id')
        .set(constants.headers)
        .then((res, err) => {
          expect(err).to.be.undefined
          expect(res).to.have.status(200)
        })
    })
    it('Get CVE-ID should return an empty array when time modified is set to a very far future date', async () => {
      await chai.request(app)
        .get('/api/cve-id?time_modified.gt=2100-01-01T00:00:00')
        .set(constants.headers)
        .then((res, err) => {
          expect(err).to.be.undefined
          expect(res).to.have.status(200)
          expect(res.body.cve_ids).to.have.length(0)
        })
    })
    // Need a better way to test each individual cve-id's time_modified
    it('Get all CVE-IDs modified within a given timeframe', async () => {
      await chai.request(app)
        .get('/api/cve-id?time_modified.gt=2021-05-11T15:05:20.093Z&time_modified.lt=2021-05-11T15:07:00.093Z')
        .set(constants.headers)
        .then((res, err) => {
          expect(err).to.be.undefined
          expect(res).to.have.status(200)
          expect(res.body.cve_ids).to.have.length(TIME_WINDOW_COUNT)
        })
    })
    it('Get all CVE-IDs in the RESERVED state', async () => {
      await chai.request(app)
        .get('/api/cve-id?state=RESERVED')
        .set(constants.headers)
        .then((res, err) => {
          expect(err).to.be.undefined
          expect(res).to.have.status(200)
          expect(_.every(res.body.cve_ids, { state: 'RESERVED' })).to.be.true
          expect(res.body.cve_ids).to.have.length(RESESRVED_COUNT)
        })
    })
    it('Get all CVE-IDs in the PUBLISHED state', async () => {
      await chai.request(app)
        .get('/api/cve-id?state=PUBLISHED')
        .set(constants.headers)
        .then((res, err) => {
          expect(err).to.be.undefined
          expect(res).to.have.status(200)
          expect(_.every(res.body.cve_ids, { state: 'PUBLISHED' })).to.be.true
        })
    })
    it('Get all CVE-IDs in the REJECTED state', async () => {
      await chai.request(app)
        .get('/api/cve-id?state=REJECTED')
        .set(constants.headers)
        .then((res, err) => {
          expect(err).to.be.undefined
          expect(res).to.have.status(200)
          expect(_.every(res.body.cve_ids, { state: 'REJECTED' })).to.be.true
        })
    })
    it('Get all CVE-IDs with cve_id_year 1999', async () => {
      await chai.request(app)
        .get('/api/cve-id?cve_id_year=1999')
        .set(constants.headers)
        .then((res, err) => {
          expect(err).to.be.undefined
          expect(res).to.have.status(200)
          expect(_.every(res.body.cve_ids, { cve_year: '1999' })).to.be.true
          expect(res.body.cve_ids).to.have.length(YEAR_COUNT)
        })
    })
    it('Get all CVE-IDs with cve_id_year 1999 with state PUBLISHED', async () => {
      await chai.request(app)
        .get('/api/cve-id?cve_id_year=1999&state=PUBLISHED')
        .set(constants.headers)
        .then((res, err) => {
          expect(err).to.be.undefined
          expect(res).to.have.status(200)
          expect(_.every(res.body.cve_ids, { cve_year: '1999', state: 'PUBLISHED' })).to.be.true
          expect(res.body.cve_ids).to.have.length(PUB_YEAR_COUNT)
        })
    })
  })
})
