Skip to content

Commit

Permalink
clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matte22 committed Sep 19, 2024
1 parent 2622a4f commit ddd4fdf
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 78 deletions.
10 changes: 5 additions & 5 deletions test/api/mocha/data/appdata/op.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const config = require('../../testConfig.json')
const utils = require('../../utils/testUtils.js')
const iterations = require('../../iterations.js')

describe('Op', () => {
describe('GET - Op', () => {
before(async function () {
this.timeout(4000)
await utils.uploadTestStigs()
Expand All @@ -16,7 +16,7 @@ describe('Op', () => {

for(const iteration of iterations){
describe(`iteration:${iteration.name}`, () => {
describe('GET - getAppData - /op/appdata', () => {
describe('getAppData - /op/appdata', () => {
it('Export application data', async () => {
const res = await chai.request(config.baseUrl)
.get(`/op/appdata?elevate=true`)
Expand All @@ -28,7 +28,7 @@ describe('Op', () => {
expect(res).to.have.status(200)
})
})
describe('GET - getConfiguration - /op/configuration', () => {
describe('getConfiguration - /op/configuration', () => {
it('Return API version and configuration information', async () => {
const res = await chai.request(config.baseUrl)
.get(`/op/configuration`)
Expand All @@ -42,7 +42,7 @@ describe('Op', () => {
expect(res).to.have.status(200)
})
})
describe('GET - getDetails - /op/details', () => {
describe('getDetails - /op/details', () => {
it('Return API Deployment Details', async () => {
const res = await chai.request(config.baseUrl)
.get(`/op/details?elevate=true`)
Expand All @@ -59,7 +59,7 @@ describe('Op', () => {

})
})
describe('GET - getDefinition - /op/definition', () => {
describe('getDefinition - /op/definition', () => {
it('Return API Deployment Definition', async () => {
const res = await chai.request(config.baseUrl)
.get(`/op/definition`)
Expand Down
6 changes: 1 addition & 5 deletions test/api/mocha/data/collection/collectionGet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,7 @@ describe('GET - Collection', function () {
expect(res.body.collectionId).to.equal(reference.testCollection.collectionId)
const regex = new RegExp(reference.testCollection.name)
expect(res.body.name).to.match(regex)

// this is a bug
// expect(res.body.grants).to.have.lengthOf(distinct.grantCnt)
// grants projection
// todo: lvl1 iteration seems to be getting all grants, but should only see owners and themselves (or grants projection is invalid for lvl1 iterations)

for(const grant of res.body.grants){
const userIds = reference.testCollection.grantsProjected.map(grant => grant.user.userId)
expect(userIds).to.include(grant.user.userId)
Expand Down
139 changes: 77 additions & 62 deletions test/api/mocha/data/collection/collectionPost.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,68 +57,71 @@ describe('POST - Collection - not all tests run for all iterations', function ()

describe("createCollection - /collections", function () {

const random = Math.floor(Math.random() * 100) + "-" + Math.floor(Math.random() * 100)

it("Create a Collection and test projections",async function () {
const post = requestBodies.createCollection
const res = await chai
.request(config.baseUrl)
.post(
`/collections?elevate=${distinct.canElevate}&projection=grants&projection=labels&projection=assets&projection=owners&projection=statistics&projection=stigs`
)
.set("Authorization", `Bearer ${iteration.token}`)
.send(post)
if(distinct.canCreateCollection === false){
expect(res).to.have.status(403)
return
}
expect(res).to.have.status(201)
if (distinct.grant === 'none') {
// grant = none iteration can create a collection, but does not give itself access to the collection
// TODO: Should eventually be changed to respond with empty object
return
}
expect(res.body.description).to.equal("Collection TEST description")
expect(res.body.name).to.equal("TEST")
expect(res.body.settings.fields.detail.enabled).to.equal(post.settings.fields.detail.enabled)
expect(res.body.settings.fields.detail.required).to.equal(post.settings.fields.detail.required)
expect(res.body.settings.fields.comment.enabled).to.equal(post.settings.fields.comment.enabled)
expect(res.body.settings.fields.comment.required).to.equal(post.settings.fields.comment.required)
expect(res.body.settings.status.canAccept).to.equal(post.settings.status.canAccept)
expect(res.body.settings.status.minAcceptGrant).to.equal(post.settings.status.minAcceptGrant)
expect(res.body.settings.status.resetCriteria).to.equal(post.settings.status.resetCriteria)
expect(res.body.settings.history.maxReviews).to.equal(post.settings.history.maxReviews)
expect(res.body.metadata.pocName).to.equal(post.metadata.pocName)
expect(res.body.metadata.pocEmail).to.equal(post.metadata.pocEmail)
expect(res.body.metadata.pocPhone).to.equal(post.metadata.pocPhone)
expect(res.body.metadata.reqRar).to.equal(post.metadata.reqRar)

// grants projection
expect(res.body.grants).to.have.lengthOf(1)
expect(res.body.grants[0].user.userId).to.equal("1")
expect(res.body.grants[0].accessLevel).to.equal(4)

// labels projection
expect(res.body.labels).to.have.lengthOf(1)
expect(res.body.labels[0].name).to.equal("TEST")
expect(res.body.labels[0].description).to.equal("Collection label description")
expect(res.body.labels[0].color).to.equal("ffffff")

// assets projection
expect(res.body.assets).to.have.lengthOf(0)

// owners projection
expect(res.body.owners).to.have.lengthOf(1)
expect(res.body.owners[0].userId).to.equal("1")

// statistics projection
expect(res.body.statistics.assetCount).to.equal(0)
expect(res.body.statistics.checklistCount).to.equal(0)
expect(res.body.statistics.grantCount).to.equal(1)

// stigs projection
expect(res.body.stigs).to.have.lengthOf(0)
const post = JSON.parse(JSON.stringify(requestBodies.createCollection))
post.name = "testCollection" + random
const res = await chai
.request(config.baseUrl)
.post(
`/collections?elevate=${distinct.canElevate}&projection=grants&projection=labels&projection=assets&projection=owners&projection=statistics&projection=stigs`
)
.set("Authorization", `Bearer ${iteration.token}`)
.send(post)
if(distinct.canCreateCollection === false){
expect(res).to.have.status(403)
return
}
expect(res).to.have.status(201)
if (distinct.grant === 'none') {
// grant = none iteration can create a collection, but does not give itself access to the collection
// TODO: Should eventually be changed to respond with empty object
return
}
expect(res.body.description).to.equal("Collection TEST description")
expect(res.body.name).to.equal(post.name)
expect(res.body.settings.fields.detail.enabled).to.equal(post.settings.fields.detail.enabled)
expect(res.body.settings.fields.detail.required).to.equal(post.settings.fields.detail.required)
expect(res.body.settings.fields.comment.enabled).to.equal(post.settings.fields.comment.enabled)
expect(res.body.settings.fields.comment.required).to.equal(post.settings.fields.comment.required)
expect(res.body.settings.status.canAccept).to.equal(post.settings.status.canAccept)
expect(res.body.settings.status.minAcceptGrant).to.equal(post.settings.status.minAcceptGrant)
expect(res.body.settings.status.resetCriteria).to.equal(post.settings.status.resetCriteria)
expect(res.body.settings.history.maxReviews).to.equal(post.settings.history.maxReviews)
expect(res.body.metadata.pocName).to.equal(post.metadata.pocName)
expect(res.body.metadata.pocEmail).to.equal(post.metadata.pocEmail)
expect(res.body.metadata.pocPhone).to.equal(post.metadata.pocPhone)
expect(res.body.metadata.reqRar).to.equal(post.metadata.reqRar)

// grants projection
expect(res.body.grants).to.have.lengthOf(1)
expect(res.body.grants[0].user.userId).to.equal("1")
expect(res.body.grants[0].accessLevel).to.equal(4)

// labels projection
expect(res.body.labels).to.have.lengthOf(1)
expect(res.body.labels[0].name).to.equal("TEST")
expect(res.body.labels[0].description).to.equal("Collection label description")
expect(res.body.labels[0].color).to.equal("ffffff")

// assets projection
expect(res.body.assets).to.have.lengthOf(0)

// owners projection
expect(res.body.owners).to.have.lengthOf(1)
expect(res.body.owners[0].userId).to.equal("1")

// statistics projection
expect(res.body.statistics.assetCount).to.equal(0)
expect(res.body.statistics.checklistCount).to.equal(0)
expect(res.body.statistics.grantCount).to.equal(1)

// stigs projection
expect(res.body.stigs).to.have.lengthOf(0)

// just an extra check to make sure the collection was created
const createdCollection = await utils.getCollection(res.body.collectionId)
// just an extra check to make sure the collection was created
const createdCollection = await utils.getCollection(res.body.collectionId)
expect(createdCollection).to.exist
})
it("should throw SmError.UnprocessableError due to duplicate user in grant array.",async function () {
Expand All @@ -140,7 +143,8 @@ describe('POST - Collection - not all tests run for all iterations', function ()
expect(res.body.detail).to.equal("Duplicate user in grant array")
})
it("should throw SmError.UnprocessableError due to duplicate name exists ",async function () {
const post = requestBodies.createCollection
const post = JSON.parse(JSON.stringify(requestBodies.createCollection))
post.name = "testCollection" + random
const res = await chai
.request(config.baseUrl)
.post(`/collections?elevate=${distinct.canElevate}&projection=grants&projection=labels&projection=assets&projection=owners&projection=statistics&projection=stigs`)
Expand Down Expand Up @@ -334,6 +338,8 @@ describe('POST - Collection - not all tests run for all iterations', function ()

describe("createCollectionLabel - /collections/{collectionId}/labels", function () {

let label = null

it("Create Label in a Collection",async function () {

const request = {
Expand All @@ -351,13 +357,22 @@ describe('POST - Collection - not all tests run for all iterations', function ()
expect(res).to.have.status(403)
return
}

label = res.body
expect(res).to.have.status(201)
expect(res.body.name).to.equal(request.name)
expect(res.body.description).to.equal(request.description)
expect(res.body.color).to.equal(request.color)
expect(res.body.uses).to.equal(0)
})
it("Clean up - delete label",async function () {
if(label){
const res = await chai
.request(config.baseUrl)
.delete(`/collections/${reference.scrapCollection.collectionId}/labels/${label.labelId}`)
.set("Authorization", `Bearer ${iteration.token}`)
expect(res).to.have.status(204)
}
})
})

describe("writeStigPropsByCollectionStig - /collections/{collectionId}/stigs/{benchmarkId}", function () {
Expand Down
10 changes: 5 additions & 5 deletions test/api/mocha/data/review/reviewGet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('GET - Review', () => {

before(async function () {
this.timeout(4000)
//await utils.uploadTestStigs()
await utils.uploadTestStigs()
await utils.loadAppData()
//await utils.createDisabledCollectionsandAssets()
})
Expand All @@ -25,6 +25,8 @@ describe('GET - Review', () => {
describe(`iteration:${iteration.name}`, () => {
const distinct = expectations[iteration.name]
describe('GET - getReviewsByCollection - /collections/{collectionId}/reviews', () => {


it('Return a list of reviews accessible to the requester', async () => {
const res = await chai.request(config.baseUrl)
.get(`/collections/${reference.testCollection.collectionId}/reviews?projection=rule&projection=stigs&projection=metadata`)
Expand Down Expand Up @@ -249,7 +251,7 @@ describe('GET - Review', () => {
expect(review.assetId).to.be.oneOf(reference.testCollection.assetIds)
}
})
// this test has some odd behavior .
// this test has some odd behavior . will occational see 0 reviews returned sometimes 11. I think 0 is correct?
it('Return a list of reviews accessible to the requester, rules=not-default', async () => {
const res = await chai.request(config.baseUrl)
.get(`/collections/${reference.testCollection.collectionId}/reviews?rules=not-default`)
Expand Down Expand Up @@ -511,8 +513,6 @@ describe('GET - Review', () => {
expect(res.body.error).to.be.equal("Resource not found.")
})
})

})
}
})

})
2 changes: 1 addition & 1 deletion test/api/runMocha.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ usage() {
echo " -f file Run specific test file."
echo " -d directory Run tests in specific directory."
echo " -i iteration Run tests for specific iteration.name (see iterations.js)"
echo -e " -h help examples: \n ./runMocha.sh -p \"the name of my test\" \n ./runMocha.sh -i lvl1 -i lvl2 -p getCollections \n ./runMocha.sh -f collectionGet.test.js \n ./runMocha.sh -d mocha/data/collection"
echo -e " -h help examples: \n ./runMocha.sh \n ./runMocha.sh -p \"the name of my test\" \n ./runMocha.sh -p \"getCollections|getAsset\" \n ./runMocha.sh -p getCollections \n ./runMocha.sh -i lvl1 -i lvl2 -p getCollections \n ./runMocha.sh -f collectionGet.test.js \n ./runMocha.sh -d mocha/data/collection"
exit
}

Expand Down

0 comments on commit ddd4fdf

Please sign in to comment.