Skip to content

Commit

Permalink
uses less loadappdata in favor of api useage
Browse files Browse the repository at this point in the history
  • Loading branch information
Matte22 committed Sep 11, 2024
1 parent d626c9b commit a2647fe
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 132 deletions.
52 changes: 19 additions & 33 deletions test/api/mocha/data/asset/assetDelete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,21 @@ const expectations = require('./expectations.js')
const reference = require('../../referenceData.js')
const requestBodies = require('./requestBodies.js')

const resetTestAsset = async () => {
const put = JSON.parse(JSON.stringify(requestBodies.testAssetPut))
const res = await utils.putAsset(reference.testAsset.assetId, put)

const createTempAsset = async () => {
const res = await utils.createTempAsset(requestBodies.tempAssetPost)
return res.data
}

describe('DELETE - Asset', function () {

let localTestAsset = null
let localScrapAsset = null

before(async function () {
this.timeout(4000)
// await utils.uploadTestStigs()
//await utils.loadAppData()
// await utils.createDisabledCollectionsandAssets()
// const post = JSON.parse(JSON.stringify(requestBodies.tempAssetPost))
// post.name = 'tempAsset' + Math.floor(Math.random() * 1000)
// localTestAsset = await utils.createTempAsset(post)
})

afterEach(async function () {
beforeEach(async function () {
this.timeout(4000)
await resetTestAsset()
await utils.resetTestAsset()
})

for(const iteration of iterations){
if (expectations[iteration.name] === undefined){
it(`No expectations for this iteration scenario: ${iteration.name}`, async function () {})
Expand All @@ -48,7 +39,6 @@ describe('DELETE - Asset', function () {
.delete(`/assets/${reference.testAsset.assetId}/metadata/keys/${reference.testAsset.metadataKey}`)
.set('Content-Type', 'application/json')
.set('Authorization', 'Bearer ' + iteration.token)
// .send(`${JSON.stringify(requestBodies.tempAssetPost.metadata.testKey)}`)

if(!distinct.canModifyCollection){
expect(res).to.have.status(403)
Expand All @@ -64,65 +54,61 @@ describe('DELETE - Asset', function () {
it('Delete a STIG assignment to an Asset', async function () {
const res = await chai
.request(config.baseUrl)
.delete(`/assets/${localTestAsset.data.assetId}/stigs/${reference.benchmark}`)
.delete(`/assets/${reference.testAsset.assetId}/stigs/${reference.benchmark}`)
.set('Authorization', 'Bearer ' + iteration.token)
if(!distinct.canModifyCollection){
expect(res).to.have.status(403)
return
}
expect(res).to.have.status(200)

const asset = await utils.getAsset(localTestAsset.data.assetId)
const asset = await utils.getAsset(reference.testAsset.assetId)
expect(asset.stigs).to.not.include(reference.benchmark)
})
})
describe(`removeStigsFromAsset -/assets/{assetId}/stigs`, function () {
it('Delete all STIG assignments to an Asset', async function () {
const res = await chai
.request(config.baseUrl)
.delete(`/assets/${localTestAsset.data.assetId}/stigs`)
.delete(`/assets/${reference.testAsset.assetId}/stigs`)
.set('Authorization', 'Bearer ' + iteration.token)
if(!distinct.canModifyCollection){
expect(res).to.have.status(403)
return
}
expect(res).to.have.status(200)
expect(res.body).to.be.an('array')
const asset = await utils.getAsset(localTestAsset.data.assetId)
const asset = await utils.getAsset(reference.testAsset.assetId)
expect(asset.stigs).to.be.an('array').that.is.empty

})
})

describe(`deleteAsset - /assets/{assetId}`, function () {

before(async function () {
this.timeout(4000)
// await utils.loadAppData()
// await utils.uploadTestStigs()
if(distinct.canModifyCollection){
localTestAsset = await createTempAsset()
}
})
it('Delete test Asset', async function () {

it('Delete scrap Asset', async function () {
const res = await chai
.request(config.baseUrl)
.delete(`/assets/${localTestAsset.data.assetId}?projection=statusStats&projection=stigs&projection=stigGrants`)
.delete(`/assets/${localTestAsset.assetId}?projection=statusStats&projection=stigs&projection=stigGrants`)
.set('Authorization', 'Bearer ' + iteration.token)
if(!distinct.canModifyCollection){
expect(res).to.have.status(403)
return
}
expect(res).to.have.status(200)
expect(res.body.assetId).to.equal(localTestAsset.data.assetId)
expect(res.body.assetId).to.equal(localTestAsset.assetId)
expect(res.body.statusStats.ruleCount).to.equal(reference.testAsset.stats.ruleCount)


expect(res.body.stigs).to.be.an('array').of.length(reference.testAsset.validStigs.length)
for(const stig of res.body.stigs){
expect(stig.benchmarkId).to.be.oneOf(reference.testAsset.validStigs)
}

expect(res.body.stigGrants).to.be.an('array').of.length(reference.testAsset.usersWithGrant.length)
for(const user of res.body.stigGrants){
expect(user.users[0].userId).to.be.oneOf(reference.testAsset.usersWithGrant)
}
})
})
})
Expand Down
48 changes: 23 additions & 25 deletions test/api/mocha/data/asset/assetPatch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,12 @@ const utils = require('../../utils/testUtils')
const iterations = require('../../iterations.js')
const expectations = require('./expectations.js')
const reference = require('../../referenceData.js')
const requestBodies = require('./requestBodies.js')

const resetTestAsset = async () => {

const res = await utils.putAsset(reference.testAsset.assetId, requestBodies.testAssetPut)

}

const resetScrapAsset = async () => {

const res = await utils.putAsset(reference.scrapAsset.assetId, requestBodies.scrapAssetPut)
}

describe('PATCH - Asset', function () {
before(async function () {
await utils.createDisabledCollectionsandAssets()
// await utils.loadAppData()

after(async () => {
await utils.resetTestAsset()
await utils.resetScrapAsset()
})

for(const iteration of iterations){
Expand All @@ -34,13 +23,15 @@ describe('PATCH - Asset', function () {

describe(`iteration:${iteration.name}`, function () {
const distinct = expectations[iteration.name]

beforeEach(async function () {
this.timeout(4000)
await resetTestAsset()
await resetScrapAsset()
await utils.resetTestAsset()
await utils.resetScrapAsset()
})

describe(`updateAsset - /assets/{assetId}`, function () {

it('Merge provided properties with an Asset - Change Collection - Fail for all iterations', async function () {
const res = await chai
.request(config.baseUrl)
Expand Down Expand Up @@ -177,12 +168,13 @@ describe('PATCH - Asset', function () {
}
})
})
describe(`patchAssetMetadata - /assets/{assetId}/metadata`, function () {

describe(`patchAssetMetadata - /assets/{assetId}/metadata`, function () {

it('Merge provided properties with an Asset - Change metadata', async function () {
const res = await chai
.request(config.baseUrl)
.patch(`/assets/${reference.scrapAsset.assetId}/metadata`)
.patch(`/assets/${reference.testAsset.assetId}/metadata`)
.set('Authorization', 'Bearer ' + iteration.token)
.send({
"testkey":"poc2Patched"
Expand All @@ -195,7 +187,7 @@ describe('PATCH - Asset', function () {
expect(res.body, "expect new metadata to take effect").to.deep.equal({
"testkey": "poc2Patched",
})
const effectedAsset = await utils.getAsset(reference.scrapAsset.assetId)
const effectedAsset = await utils.getAsset(reference.testAsset.assetId)
expect(effectedAsset.metadata, "getting asset for double checking").to.deep.equal({
"testkey": "poc2Patched"
})
Expand All @@ -222,14 +214,20 @@ describe('PATCH - Asset', function () {
})
})
})

describe(`patchAssets - /assets`, function () {

let asset1 = null
let asset2 = null

before(async function () {
asset1 = await utils.createTempAsset()
asset2 = await utils.createTempAsset()

})

it('Delete Assets - expect success for valid iterations', async function () {

// create 2 assets to be deleted
const asset1 = await utils.createTempAsset()
const asset2 = await utils.createTempAsset()

const assetIds = [asset1.data.assetId, asset2.data.assetId]

const res = await chai
Expand Down
13 changes: 2 additions & 11 deletions test/api/mocha/data/asset/assetPost.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ const expectations = require('./expectations.js')
const reference = require('../../referenceData.js')

describe('POST - Asset', function () {
before(async function () {
this.timeout(4000)
await utils.uploadTestStigs()
await utils.loadAppData()
// await utils.createDisabledCollectionsandAssets()
})

for (const iteration of iterations) {
if (expectations[iteration.name] === undefined){
it(`No expectations for this iteration scenario: ${iteration.name}`, async function () {})
Expand All @@ -26,14 +19,13 @@ describe('POST - Asset', function () {
describe(`iteration:${iteration.name}`, function () {
const distinct = expectations[iteration.name]
describe(`createAsset - /assets`, function () {

it('Create an Asset (with statusStats and stigs projection', async function () {
const res = await chai
.request(config.baseUrl)
.post('/assets?projection=statusStats&projection=stigs')
.set('Authorization', 'Bearer ' + iteration.token)
.send({
name: 'TestAsset' + Math.floor(Math.random() * 1000),
name: 'TestAsset' + Date.now(),
collectionId: reference.testCollection.collectionId,
description: 'test',
ip: '1.1.1.1',
Expand Down Expand Up @@ -78,7 +70,6 @@ describe('POST - Asset', function () {
expect(effectedAsset.statusStats.ruleCount).to.equal(reference.testAsset.stats.ruleCount)

})

it('should fail, duplicate asset name', async function () {

const res = await chai
Expand Down Expand Up @@ -109,7 +100,7 @@ describe('POST - Asset', function () {
.post('/assets?projection=stigGrants')
.set('Authorization', 'Bearer ' + iteration.token)
.send({
name: 'TestAsset' + Math.floor(Math.random() * 1000),
name: 'TestAsset' + Date.now(),
collectionId: reference.testCollection.collectionId,
description: 'test',
ip: '1.1.1.1',
Expand Down
8 changes: 4 additions & 4 deletions test/api/mocha/data/asset/assetPut.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ const { v4: uuidv4 } = require('uuid')
describe('PUT - Asset', function () {

before(async function () {
this.timeout(4000)
await utils.uploadTestStigs()
await utils.loadAppData()
// await utils.createDisabledCollectionsandAssets()
await utils.resetTestAsset()
await utils.resetScrapAsset()
})

for (const iteration of iterations) {
if (expectations[iteration.name] === undefined){
it(`No expectations for this iteration scenario: ${iteration.name}`, async function () {})
continue
}
describe(`iteration:${iteration.name}`, function () {
const distinct = expectations[iteration.name]

describe(`replaceAsset -/assets/{assetId}`, function () {

it('Set all properties of an Asset', async function () {
Expand Down
35 changes: 5 additions & 30 deletions test/api/mocha/data/asset/requestBodies.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,7 @@

const requestBodies = {
tempAssetPost: {
name: 'tempAsset',
collectionId: "21",
description: 'temp',
ip: '1.1.1.1',
fqdn: null,
noncomputing: true,
mac: null,
labelIds: [],
metadata: {
testKey: 'testValue'
},
stigs: ["VPN_SRG_TEST", "Windows_10_STIG_TEST"],
},
testAssetPut: {
name: "Collection_X_lvl1_asset-1",
name: "TempAsset",
collectionId: "21",
description: "",
ip: "",
Expand All @@ -30,19 +16,8 @@ const requestBodies = {
testkey: "testvalue"
},
stigs: ["VPN_SRG_TEST", "Windows_10_STIG_TEST"],
},
scrapAssetPut: {
name: "test asset stigmanadmin",
collectionId: "1",
description: "test desc",
ip: "1.1.1.1",
fqdn: null,
noncomputing: true,
mac: null,
labelIds: [],
metadata: {},
stigs: ["VPN_SRG_TEST", "Windows_10_STIG_TEST","RHEL_7_STIG_TEST"],
},
}
},
}
module.exports = requestBodies



3 changes: 3 additions & 0 deletions test/api/mocha/referenceData.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ const reference = {
scrapBenchmark: "RHEL_7_STIG_TEST",
metadataKey: "testkey",
metadataValue: "testvalue",
ruleCount: 612,
validStigs: ["VPN_SRG_TEST", "Windows_10_STIG_TEST", "RHEL_7_STIG_TEST"],

},
testAssetNoStigs: {
name: "ACHERNAR_Collection_X_asset",
Expand Down
Loading

0 comments on commit a2647fe

Please sign in to comment.