Skip to content

Commit

Permalink
Merge branch 'reject_revoked_tokens' of https://github.com/salesforce…
Browse files Browse the repository at this point in the history
…/refocus into reject_revoked_tokens
  • Loading branch information
iamigo committed Feb 17, 2017
2 parents 5e463f3 + 7dd8794 commit 7480022
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 7 deletions.
10 changes: 10 additions & 0 deletions api/v1/apiErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ apiErrors.create({
' an empty array. This is not a valid filter combination ',
});

apiErrors.create({
code: 11101,
status: 400,
name: 'InvalidFilterParameterError',
parent: apiErrors.ValidationError,
fields: [],
defaultMessage: 'Filter should be passed in query parameter as ' +
'an include filter or an exclude filter, but not the combination of both.',
});

// ----------------------------------------------------------------------------
// Not Found
// ----------------------------------------------------------------------------
Expand Down
26 changes: 26 additions & 0 deletions api/v1/controllers/subjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ function checkDuplicates(tagsArr) {
return false;
}

/**
* Validates the correct filter parameter
* passed in query parameters
* @param {Array} filterParams Filter Tags Array
*/
function validateFilterParams(filterParams) {
let subjectTagsCounter = 0;
const EXCLUDE_SYMBOL = '-';

subjectTagsCounter = filterParams
.filter((i) => i.startsWith(EXCLUDE_SYMBOL)).length;

if (subjectTagsCounter !== ZERO &&
filterParams.length !== subjectTagsCounter) {
throw new apiErrors.InvalidFilterParameterError();
}
}

/**
* Validates the given fields from request body or url.
* If fails, throws a corresponding error.
Expand Down Expand Up @@ -197,6 +215,14 @@ module.exports = {
const resultObj = { reqStartTime: new Date() };
const params = req.swagger.params;
const depth = Number(params.depth.value);
const filterParams = ['subjectTags', 'aspectTags', 'aspect', 'status'];

// Filter Parameter Validation
for (let i = 0; i < filterParams.length; i++) {
if (params[filterParams[i]].value) {
validateFilterParams(params[filterParams[i]].value.split(','));
}
}

u.findByKey(helper, params, ['hierarchy', 'samples'])
.then((o) => {
Expand Down
2 changes: 2 additions & 0 deletions api/v1/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4359,6 +4359,8 @@ paths:
sign to indicate that the subject having tags with those names will
be excluded. For example, ?subjectTags=-TAG3,-TAG4 will return the
subject hierarchy without subjects having tags - TAG3 and TAG4.
Tags should be passed as an include filter or as an exclude filter
but not the combination of both.
type: string
-
$ref: "#/parameters/SubjectsFieldsParam"
Expand Down
55 changes: 48 additions & 7 deletions tests/api/v1/subjects/getHierarchyAspectAndTagsFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,48 @@ describe(`api: GET ${path}:`, () => {
done();
});
});

it('Multiple Query Params: Tags should be passed as include filter' +
'or exclude filter not the combination of both', (done) => {
const endpoint = path.replace('{key}', gp.id) +
'?subjectTags=-cold,ea,-verycold';
api.get(endpoint)
.set('Authorization', token)
.expect(constants.httpStatus.BAD_REQUEST)
.expect((res) => {
expect(res.body.errors[0].type).to
.equal('InvalidFilterParameterError');
})
.end((err /* , res */) => {
if (err) {
return done(err);
}

done();
});
});

it('Multiple Query Params: Tags should be passed as include filter' +
'or exclude filter not the combination of both', (done) => {
const endpoint = path.replace('{key}', gp.id) +
'?subjectTags=cold,-ea,verycold';
api.get(endpoint)
.set('Authorization', token)
.expect(constants.httpStatus.BAD_REQUEST)
.expect((res) => {
expect(res.body.errors[0].type).to
.equal('InvalidFilterParameterError');
})
.end((err /* , res */) => {
if (err) {
return done(err);
}

done();
});
});
});

describe('Aspect Filter on Hierarchy', () => {
it('should return samples with temperature and humidity aspects',
(done) => {
Expand Down Expand Up @@ -329,20 +370,20 @@ describe(`api: GET ${path}:`, () => {

it('filter should apply to all levels of hierarchy', (done) => {
const endpoint2 = path.replace('{key}', par.id) +
'?aspect=-humidity,temperature';
'?aspect=humidity,temperature';
api.get(endpoint2)
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.expect((res) => {
expect(res.body).to.not.equal(null);
expect(res.body.samples).to.have.length(1);
expect(res.body.samples[0]).to.have.deep
expect(res.body.samples).to.have.length(2);
expect(res.body.samples[1]).to.have.deep
.property('aspect.name', 'temperature');
expect(res.body.children).to.have.length(1);
expect(res.body.children[0].samples).to.have.length(0);
expect(res.body.children[0].children).to.have.length(1);
expect(res.body.children[0].children[0].samples[0]).to.have.deep
.property('aspect.name', 'wind-speed');
expect(res.body.children[0].samples).to.have.length(1);
expect(res.body.children[0].children).to.have.length(0);
expect(res.body.children[0].samples[0]).to.have.deep
.property('aspect.name', 'humidity');
})
.end((err /* , res */) => {
if (err) {
Expand Down
20 changes: 20 additions & 0 deletions tests/api/v1/subjects/getHierarchyStatusAndCombinedFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,26 @@ describe(`api: GET ${path}:`, () => {
});
});

it('filter :: subjectTags=na, -ea and aspect=humidity,wind-speed&,' +
'aspectTags=hum gives error because of mismatch of filter', (done) => {
const endpoint = path.replace('{key}', gp.id) +
'?subjectTags=na,-ea&aspect=temperature&aspectTags=temp,hum';
api.get(endpoint)
.set('Authorization', token)
.expect(constants.httpStatus.BAD_REQUEST)
.expect((res) => {
expect(res.body.errors[0].type).to
.equal('InvalidFilterParameterError');
})
.end((err /* , res */) => {
if (err) {
done(err);
}

done();
});
});

it('filter :: subjectTags=na and aspect=humidity,wind-speed and ' +
'aspectTags=hum and status = Critical', (done) => {
const endpoint = path.replace('{key}', gp.id) +
Expand Down

0 comments on commit 7480022

Please sign in to comment.