Skip to content

Commit

Permalink
GET and DELETE endpoint implementation for generator and generatorWri…
Browse files Browse the repository at this point in the history
…ters
  • Loading branch information
shriramshankar committed Jul 27, 2017
1 parent a15339e commit 95c1f42
Show file tree
Hide file tree
Showing 7 changed files with 775 additions and 5 deletions.
14 changes: 14 additions & 0 deletions api/v1/controllers/generators.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const doFind = require('../helpers/verbs/doFind');
const doGet = require('../helpers/verbs/doGet');
const doPatch = require('../helpers/verbs/doPatch');
const doPost = require('../helpers/verbs/doPost');
const doDelete = require('../helpers/verbs/doDelete');
const doPut = require('../helpers/verbs/doPut');
const u = require('../helpers/verbs/utils');
const httpStatus = require('../constants').httpStatus;
Expand Down Expand Up @@ -92,6 +93,19 @@ module.exports = {
doPut(req, res, next, helper);
},

/**
* DELETE /generators/{key}
*
* Delete the generator and sends it back in the response.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
deleteGenerator(req, res, next) {
doDelete(req, res, next, helper);
},

/**
* GET /generators/{key}/writers
*
Expand Down
43 changes: 38 additions & 5 deletions api/v1/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1996,6 +1996,38 @@ paths:

/generators/{key}:
x-swagger-router-controller: generators
delete:
security:
- jwt: []
summary: Delete the specified generator
tags: [ generators ]
description: >-
Delete the specified generator. If the Refocus configuration parameter `useAccessToken` is set to `true`, you must include an `Authorization` request header with your [JSON Web Token](https://tools.ietf.org/html/rfc7519) (JWT) as the value. You can get a token using `POST /v1/register` or `POST /v1/tokens`.
operationId: deleteGenerator
parameters:
-
name: key
in: path
description: >-
The id or name of the generator to delete.
required: true
type: string
responses:
200:
description: >-
Success, returns the deleted generator.
schema:
$ref: "#/definitions/GeneratorResponse"
400:
$ref: "#/responses/400"
401:
$ref: "#/responses/401"
403:
$ref: "#/responses/403"
404:
$ref: "#/responses/404"
default:
$ref: "#/responses/genericError"
get:
security:
- jwt: []
Expand Down Expand Up @@ -2279,6 +2311,7 @@ paths:
# ---------------------------------------------------------------------------

/generators/{key}/writers/{userNameOrId}:
x-swagger-router-controller: generators
get:
security:
- jwt: []
Expand Down Expand Up @@ -2310,7 +2343,11 @@ paths:
description: >-
Success, returns requested user.
schema:
$ref: "#/definitions/UsersResponse"
type: array
items:
$ref: "#/definitions/UsersResponse"
description: >-
The users having write permission to the aspect
400:
$ref: "#/responses/400"
404:
Expand Down Expand Up @@ -9646,10 +9683,6 @@ definitions:
readOnly: true
description: >
The collector id.
isDeleted:
type: string
readOnly: true
description: 0 if this generator is NOT deleted. Timestamp of the deletion otherwise.
isActive:
type: boolean
description: Whether or not the generator is running on a collector.
Expand Down
97 changes: 97 additions & 0 deletions tests/api/v1/generators/delete.js
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());
});
});
78 changes: 78 additions & 0 deletions tests/api/v1/generators/deleteWithoutPerms.js
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();
});
});
});

0 comments on commit 95c1f42

Please sign in to comment.