Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete Function and Test #198

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion farmdata2_modules/fd2_tabs/resources/FarmOSAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,52 @@ function getAllPages(url, arr) {
})
}

function getSessionToken() {
return new Promise((resolve, reject) => {
axios
.get('/restws/session/token')
.then(response => {
return response.data
})
.then(function(token) {
resolve(token)
})
.catch(function(error){
reject(error)
})
})

}

function deleteLog(url, deleteID, sessionToken) {
// Need to retrive the session token when logged in and
// use that in any requests that modify the database
// (i.e. PUT, POST, DELETE).
//let id = parseInt(deleteID)
return new Promise((resolve, reject) => {
url = url + deleteID
axios
.delete(url, {
headers: {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-TOKEN' : sessionToken,
}
})

.then((response) => {
resolve(response)
})
.catch((error) => {
reject(error)
})
})
}

try {
module.exports = {
getAllPages: getAllPages
getAllPages: getAllPages,
getSessionToken: getSessionToken,
deleteLog: deleteLog
}
}
catch {}
155 changes: 108 additions & 47 deletions farmdata2_modules/fd2_tabs/resources/FarmOSAPI.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var FarmOSAPI = require("./FarmOSAPI.js")
var getAllPages = FarmOSAPI.getAllPages
var getSessionToken = FarmOSAPI.getSessionToken
var deleteLog = FarmOSAPI.deleteLog

describe('API Request Function', () => {
var testArray
Expand All @@ -9,66 +11,125 @@ describe('API Request Function', () => {
testArray = []
})

it('Test on a request with one page.', () => {
cy.intercept("GET",/log\?type=farm_seeding/).as('onlyone')
context('getAllPages API request function', () => {
it('Test on a request with one page.', () => {
cy.intercept("GET",/log\?type=farm_seeding/).as('onlyone')

let calls=0
let calls=0

// This wait is a bit of a hack.
// Without it the first request is not intercepted.
// There may be a more correct way to wait?
cy.wait(1)
.then(() => {
cy.wait('@onlyone').should(() => {
cy.wrap(testArray).should('have.length.gt',0)
calls++
})
// This wait is a bit of a hack.
// Without it the first request is not intercepted.
// There may be a more correct way to wait?
cy.wait(1)
.then(() => {
cy.wait('@onlyone').should(() => {
cy.wrap(testArray).should('have.length.gt',0)
calls++
})

getAllPages('/log?type=farm_seeding&id[lt]=1500', testArray)
getAllPages('/log?type=farm_seeding&id[lt]=1500', testArray)
})
.then(() => {
expect(calls).to.equal(1)
})
})
.then(() => {
expect(calls).to.equal(1)

it('Test on a request with multiple pages', () => {
cy.intercept("GET",/log\?type=farm_seeding$/).as('first')
cy.intercept("GET","/log?type=farm_seeding&page=1").as('second')
cy.intercept("GET","/log?type=farm_seeding&page=2").as('third')

let firstCalls=0
let secondCalls=0
let thirdCalls=0

cy.wait(1)
.then(() => {
cy.wait('@first').should(() => {
cy.wrap(testArray).should('have.length.gt',0)
firstCalls++
})

cy.wait('@second').should(() => {
// Just by getting here we know the second page was requested.

// Check that data made it into testArray.
// Note: depending on timing this may not run until after any
// subsequent calls.
cy.wrap(testArray).should('have.length.gt',100)
secondCalls++
})

cy.wait('@third').should(() => {
cy.wrap(testArray).should('have.length.gt',200)
thirdCalls++
})

getAllPages("/log?type=farm_seeding", testArray)

})
.then(() => {
expect(firstCalls).to.equal(1)
expect(secondCalls).to.equal(1)
expect(thirdCalls).to.equal(1)
})
})
})

it('Test on a request with multiple pages', () => {
cy.intercept("GET",/log\?type=farm_seeding$/).as('first')
cy.intercept("GET","/log?type=farm_seeding&page=1").as('second')
cy.intercept("GET","/log?type=farm_seeding&page=2").as('third')

let firstCalls=0
let secondCalls=0
let thirdCalls=0

cy.wait(1)
.then(() => {
cy.wait('@first').should(() => {
cy.wrap(testArray).should('have.length.gt',0)
firstCalls++
context('getSessionToken API request function', () => {
it('returns a token when it resolves', () => {
getSessionToken().then(token => {
expect(token).to.not.be.null
})
})
it('returns a token of length 43', () => {
getSessionToken().then(token => {
expect(token.length).to.equal(43)
})
})
})

cy.wait('@second').should(() => {
// Just by getting here we know the second page was requested.
context.only('deleteLog API request function', () => {
beforeEach(() => {

})
it('deletes a log based on log ID', () => {
getSessionToken()
.then(function(token) {
console.log('making log')

// Check that data made it into testArray.
// Note: depending on timing this may not run until after any
// subsequent calls.
cy.wrap(testArray).should('have.length.gt',100)
secondCalls++
})
req = {
url: '/log.json?type=farm_observation',
method: 'POST',
headers: {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-TOKEN' : token,
},
body: {
"name": "pleasegoaway",
"type": "farm_observation",
"timestamp": "1526584271",
}
}

cy.wait('@third').should(() => {
cy.wrap(testArray).should('have.length.gt',200)
thirdCalls++
})
logID = -1
cy.request(req).as('created')
cy.get('@created').should((response) => {
expect(response.status).to.equal(201)
logID = response.body.id
})

getAllPages("/log?type=farm_seeding", testArray)
cy.wrap(deleteLog('/log.json?type=farm_observation', logID, token)).as('delete')
cy.get('@delete').should((response) => {
expect(response.status).to.equal(200)
})

})
.then(() => {
expect(firstCalls).to.equal(1)
expect(secondCalls).to.equal(1)
expect(thirdCalls).to.equal(1)
url = '/log.json?type=farm_observation&id=' + logID
cy.request(url).as('check')
cy.get('@check').should((response) => {
expect(response.body.list.length).to.equal(0)
})
})
})
})
})