Skip to content

Commit

Permalink
feat: support prefix in VariableScope
Browse files Browse the repository at this point in the history
This change allows us to use the current VariableScope
with VaultVariables which have a fixed 'vault' prefix
  • Loading branch information
Pranav Joglekar committed Jul 15, 2024
1 parent e1dfe7a commit 300538d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
28 changes: 21 additions & 7 deletions lib/collection/variable-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ _.inherit((
if (mutations) {
this.mutations = new MutationTracker(mutations);
}

if (definition && definition.prefix) {
this.prefix = definition.prefix;
}
}), Property);

/**
Expand All @@ -136,6 +140,16 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {
*/
_postman_propertyRequiresId: true,

/**
* Defines the prefix associated with the VariableList variables for internal use.
*
* @private
* @type {String}
*
*/
prefix: '',


/**
* @private
* @deprecated discontinued in v4.0
Expand Down Expand Up @@ -176,7 +190,7 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {
* false otherwise
*/
has: function (key) {
var variable = this.values.oneNormalizedVariable(key),
var variable = this.values.oneNormalizedVariable(this.prefix + key),
i,
ii;

Expand All @@ -199,7 +213,7 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {
* @returns {*} The value of the specified variable across scopes.
*/
get: function (key) {
var variable = this.values.oneNormalizedVariable(key),
var variable = this.values.oneNormalizedVariable(this.prefix + key),
i,
ii;

Expand All @@ -222,10 +236,10 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {
* @param {Variable.types} [type] - Optionally, the value of the variable can be set to a type
*/
set: function (key, value, type) {
var variable = this.values.oneNormalizedVariable(key),
var variable = this.values.oneNormalizedVariable(this.prefix + key),

// create an object that will be used as setter
update = { key, value };
update = { key: this.prefix + key, value: value };

_.isString(type) && (update.type = type);

Expand All @@ -250,9 +264,9 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {
unset: function (key) {
var lastDisabledVariable;

this.values.remove(function (variable) {
this.values.remove((variable) => {
// bail out if variable name didn't match
if (variable.key !== key) {
if (variable.key !== (this.prefix + key)) {
return false;
}

Expand All @@ -269,7 +283,7 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {

// restore the reference with the last disabled variable
if (lastDisabledVariable) {
this.values.reference[key] = lastDisabledVariable;
this.values.reference[this.prefix + key] = lastDisabledVariable;
}

// track the change if mutation tracking is enabled
Expand Down
56 changes: 56 additions & 0 deletions test/unit/variable-scope.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,17 @@ describe('VariableScope', function () {
expect(scope.get('random')).to.be.undefined;
});

it('should get variables with prefix', function () {
var scope = new VariableScope({
prefix: 'vault:',
values: [
{ key: 'vault:var-1', value: 'var-1-value' },
{ key: 'vault:var-2', value: 'var-2-value' }
] });

expect(scope.get('var-2')).to.equal('var-2-value');
});

describe('multi layer search', function () {
it('should get from parent scope', function () {
var scope = new VariableScope([
Expand Down Expand Up @@ -506,6 +517,19 @@ describe('VariableScope', function () {
scope.set('var-4', 3.142, 'boolean');
expect(scope.get('var-4')).to.be.true;
});

it('should correctly update an existing value with prefix', function () {
var scope = new VariableScope({
prefix: 'vault:',
values: [{
key: 'vault:var-1',
value: 'var-1-value'
}]
});

scope.set('var-1', 'new-var-1-value');
expect(scope.get('var-1')).to.equal('new-var-1-value');
});
});

describe('unset', function () {
Expand Down Expand Up @@ -607,6 +631,24 @@ describe('VariableScope', function () {
// check reference list
expect(scope.values.reference).to.have.property('var-2');
});

it('should correctly remove an existing variable with prefix', function () {
var scope = new VariableScope({
prefix: 'vault:',
values: [{
key: 'vault:var-1',
value: 'var-1-value'
}, {
key: 'vault:var-2',
value: 'var-2-value'
}]
});

scope.unset('var-1');

expect(scope.values.count()).to.equal(1);
expect(scope.get('var-1')).to.be.undefined;
});
});

describe('clear', function () {
Expand Down Expand Up @@ -1233,6 +1275,20 @@ describe('VariableScope', function () {

expect(scope.has('alpha')).to.be.true;
});

it('should find variable from current scope with prefix', function () {
var scope = new VariableScope({
prefix: 'vault:',
values: [
{ key: 'vault:alpha', value: 'foo' },
{ key: 'vault:gamma', value: 'baz', disabled: true }
]
});

expect(scope.has('alpha')).to.be.true;
expect(scope.has('gamma')).to.be.false;
expect(scope.has('random')).to.be.false;
});
});

describe('disabled variable', function () {
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2327,6 +2327,7 @@ declare module "postman-collection" {
type definition = {
id?: string;
name?: string;
prefix?: string;
values?: Variable.definition[];
};
}
Expand Down

0 comments on commit 300538d

Please sign in to comment.