Skip to content

Commit

Permalink
Add support for string single-valued Condition key (fixes #9)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdpauw committed Jan 31, 2022
1 parent 7355f9f commit 579567b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
21 changes: 1 addition & 20 deletions src/condition/deserialiser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Condition} from './condition';
import {parseArray} from '../arrays';

export class ConditionJSONDeserialiser {
static fromJSON(input: any): Condition[] {
Expand Down Expand Up @@ -49,25 +50,5 @@ export class ConditionJSONDeserialiser {
return new Condition(operator, key, parseArray(values));
});
}

function parseArray(obj: any) {
if (obj === undefined) {
return [];
}

if (Array.isArray(obj)) {
if (isArrayOfStrings(obj)) {
return obj as [];
} else {
throw new Error('Unsupported type: expecting an array of strings');
}
} else {
throw new Error('Unsupported type: expecting an array');
}
}

function isArrayOfStrings(obj: any[]) {
return obj.every((element) => typeof element === 'string');
}
};
}
9 changes: 8 additions & 1 deletion tests/condition/deserialiser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ describe('#ConditionJSONDeserialiser', function() {
expect(() => ConditionJSONDeserialiser.fromJSON(input)).to.throw(Error)
.with.property(
'message',
'Unsupported type: expecting an array');
'Unsupported type: expecting an array or a string');
});
});
describe('and its value is a string', function() {
it('should return a 1-length Condition array', function() {
const input = {operator: {key: 'value'}};
const expected = [new Condition('operator', 'key', ['value'])];
expect(ConditionJSONDeserialiser.fromJSON(input)).to.deep.equal(expected);
});
});
describe('and its value is an array', function() {
Expand Down
46 changes: 46 additions & 0 deletions tests/statement/deserialiser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect} from 'chai';
import {Condition} from '../../src/condition/condition';
import {AccountPrincipal} from '../../src/principals/account';
import {ArnPrincipal} from '../../src/principals/arn';
import {StatementJSONDeserialiser} from '../../src/statement/deserialiser';
Expand Down Expand Up @@ -131,4 +132,49 @@ describe('#StatementDeserialiser', function() {
});
});
});

describe('when JSON has a Condition', function() {
describe('with one operator', function() {
describe('and one key', function() {
describe('and its key value is a string', function() {
it('should return a Statement with conditions', function() {
const json = {
Condition: {StringEquals: {'aws:username': 'johndoe'}},
};
const actual = StatementJSONDeserialiser.fromJSON(json);
const expected = new Statement({
conditions: [new Condition('StringEquals', 'aws:username', ['johndoe'])],
});
expect(actual).to.deep.equal(expected);
});
});

describe('and its value is a 1-length array', function() {
it('should return a Statement with conditions', function() {
const json = {
Condition: {StringEquals: {'aws:username': ['johndoe']}},
};
const actual = StatementJSONDeserialiser.fromJSON(json);
const expected = new Statement({
conditions: [new Condition('StringEquals', 'aws:username', ['johndoe'])],
});
expect(actual).to.deep.equal(expected);
});
});

describe('and its value is a 2-length array', function() {
it('should return a Statement with principals', function() {
const json = {
Condition: {StringEquals: {'aws:username': ['johndoe', 'joesixpack']}},
};
const actual = StatementJSONDeserialiser.fromJSON(json);
const expected = new Statement({
conditions: [new Condition('StringEquals', 'aws:username', ['johndoe', 'joesixpack'])],
});
expect(actual).to.deep.equal(expected);
});
});
});
});
});
});

0 comments on commit 579567b

Please sign in to comment.