-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GET and DELETE endpoint implementation for generator and generatorWri…
…ters
- Loading branch information
1 parent
a15339e
commit 95c1f42
Showing
7 changed files
with
775 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** | ||
* Copyright (c) 2016, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or | ||
* https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
/** | ||
* tests/api/v1/generators/delete.js | ||
*/ | ||
'use strict'; | ||
|
||
const supertest = require('supertest'); | ||
const api = supertest(require('../../../../index').app); | ||
const constants = require('../../../../api/v1/constants'); | ||
const tu = require('../../../testUtils'); | ||
const u = require('./utils'); | ||
const Generator = tu.db.Generator; | ||
const path = '/v1/generators'; | ||
const expect = require('chai').expect; | ||
const ZERO = 0; | ||
const ONE = 1; | ||
|
||
describe(`api: DELETE ${path}`, () => { | ||
let generatorId; | ||
let token; | ||
const generatorToCreate = u.getGenerator(); | ||
|
||
/** | ||
* Throws error if response object's | ||
* isDeleted value <= 0 | ||
* @param {Object} res THe response object | ||
*/ | ||
function bodyCheckIfDeleted(res) { | ||
expect(res.body.isDeleted).to.be.above(ZERO); | ||
} | ||
|
||
/** | ||
* Throws error if aspect created for test | ||
* was returned. | ||
*/ | ||
function notFound() { | ||
Generator.findById(generatorId) | ||
.then((aspect) => { | ||
expect(aspect).to.equal(null); | ||
}); | ||
} | ||
|
||
before((done) => { | ||
tu.createToken() | ||
.then((returnedToken) => { | ||
token = returnedToken; | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
|
||
beforeEach((done) => { | ||
Generator.create(generatorToCreate) | ||
.then((gen) => { | ||
generatorId = gen.id; | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
|
||
afterEach(u.forceDelete); | ||
after(tu.forceDeleteUser); | ||
|
||
it('delete by id is ok', (done) => { | ||
api.delete(`${path}/${generatorId}`) | ||
.set('Authorization', token) | ||
.expect(constants.httpStatus.OK) | ||
.expect(bodyCheckIfDeleted) | ||
.expect(notFound) | ||
.end((err) => err ? done(err) : done()); | ||
}); | ||
|
||
it('delete by name is ok', (done) => { | ||
api.delete(`${path}/${generatorToCreate.name}`) | ||
.set('Authorization', token) | ||
.expect(constants.httpStatus.OK) | ||
.expect(bodyCheckIfDeleted) | ||
.expect(notFound) | ||
.end((err) => err ? done(err) : done()); | ||
}); | ||
|
||
it('delete with case insensitive name succeeds', (done) => { | ||
api.delete(`${path}/${generatorToCreate.name.toLowerCase()}`) | ||
.set('Authorization', token) | ||
.expect(constants.httpStatus.OK) | ||
.expect(bodyCheckIfDeleted) | ||
.expect(notFound) | ||
.end((err) => err ? done(err) : done()); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Copyright (c) 2017, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or | ||
* https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
/** | ||
* tests/api/v1/generators/deleteWithoutPerms.js | ||
*/ | ||
'use strict'; | ||
|
||
const supertest = require('supertest'); | ||
const api = supertest(require('../../../../index').app); | ||
const constants = require('../../../../api/v1/constants'); | ||
const tu = require('../../../testUtils'); | ||
const u = require('./utils'); | ||
const Generator = tu.db.Generator; | ||
const User = tu.db.User; | ||
const path = '/v1/generators/{key}'; | ||
const expect = require('chai').expect; | ||
|
||
describe('api: generators: delete without permission', () => { | ||
let generator; | ||
let otherValidToken; | ||
|
||
const generatorToCreate = u.getGenerator(); | ||
|
||
before((done) => { | ||
tu.createToken() | ||
.then(() => { | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
|
||
before((done) => { | ||
Generator.create(generatorToCreate) | ||
.then((gen) => { | ||
generator = gen; | ||
}) | ||
.then(() => | ||
|
||
/** | ||
* tu.createToken creates an user and an admin user is already created, | ||
* so one use of these. | ||
*/ | ||
User.findOne({ where: { name: tu.userName } })) | ||
.then((usr) => { | ||
return generator.addWriter(usr); | ||
}) | ||
.then(() => tu.createUser('myUNiqueUser')) | ||
.then((_usr) => tu.createTokenFromUserName(_usr.name)) | ||
.then((tkn) => { | ||
otherValidToken = tkn; | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
after(u.forceDelete); | ||
after(tu.forceDeleteUser); | ||
|
||
it('should return 403 when deleting the generator' + | ||
' without permission', (done) => { | ||
api.delete(path.replace('{key}', generator.id)) | ||
.set('Authorization', otherValidToken) | ||
.expect(constants.httpStatus.FORBIDDEN) | ||
.end((err, res) => { | ||
if (err) { | ||
done(err); | ||
} | ||
expect(res.body.errors[0].message).to.equal('Forbidden') | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
Oops, something went wrong.