Skip to content

Commit

Permalink
Merge branch 'master' into JCN-464-validate-invoker-discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Vilche committed Jan 12, 2024
2 parents 54dc277 + 4761c37 commit 07d41ad
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [5.1.0] - 2024-01-02
### Added
- Add method `setUserId()` to add `userId` in header _janis-api-key_

## [5.0.1] - 2023-09-21
### Changed
- Update dependencies: `@janiscommerce/aws-secrets-manager` and `@janiscommerce/lambda`
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ _Since 4.0.0_

> :warning: **After version 4.0.0, `get`, `post`, `put`, `path`, `delete` are *REMOVED*** :warning:
_Since 5.1.0_

* `setUserId(userId)`

Function for add user id in api-key header

Params: `userId` `{String}`

Returns a `MicroServiceCallInstance`.

## Parameters

The Parameters used in the API functions.
Expand Down
22 changes: 21 additions & 1 deletion lib/microservice-call.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,13 @@ module.exports = class MicroServiceCall {
* @returns {CredentialHeaders}
*/
get credentialsHeaders() {

const servicePart = `service-${process.env.JANIS_SERVICE_NAME}`;

const userPart = this.apiKeyUser ? `_user-${this.apiKeyUser}` : '';

return {
'janis-api-key': `service-${process.env.JANIS_SERVICE_NAME}`,
'janis-api-key': `${servicePart}${userPart}`,
'janis-api-secret': SecretFetcher.secretValue
};
}
Expand All @@ -92,6 +97,18 @@ module.exports = class MicroServiceCall {
return sessionHeaders;
}

/**
* Set userId for make api-key header
* @param {string} userId User identifier
* @returns {MicroServiceCall}
*/
setUserId(userId) {

this.apiKeyUser = userId;

return this;
}

/**
* Get the basic headers of that will be set in the request to the ms.
*
Expand All @@ -105,6 +122,9 @@ module.exports = class MicroServiceCall {
...this.sessionHeaders
};

// clear user id for api key
this.apiKeyUser = null;

return basic;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@janiscommerce/microservice-call",
"version": "5.0.1",
"version": "5.1.0",
"description": "Allows communication between services.",
"main": "lib/microservice-call.js",
"directories": {
Expand Down
82 changes: 82 additions & 0 deletions tests/microservice-call.js
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,88 @@ describe('MicroService call', () => {
});
});

describe('Using setUserId function', () => {

beforeEach(() => {
process.env.JANIS_SERVICE_SECRET = 'insecure-secret';

getEndpointStub({
baseUrl: 'https://sample-service.janis-test.in',
path: '/api/alarms/{alarmName}/state',
method: 'POST'
});
});

const baseRequestHeaders = {
'content-type': 'application/json',
'janis-api-secret': 'insecure-secret'
};

const requestArgs = ['sample-service', 'alarms', 'list', { foo: 'bar' }, null, { alarmName: 'foo' }];

const mockRequest = reqheaders => {

const replyArgs = [200, { name: 'foo' }, { 'content-type': 'application/json' }];

nock('https://sample-service.janis-test.in', { reqheaders })
.post('/api/alarms/foo/state', { foo: 'bar' })
.reply(...replyArgs);
};

it('Should set user id in janis-api-key header', async () => {

const reqheaders = {
...baseRequestHeaders,
'janis-api-key': 'service-dummy-service_user-5f4adc8f9c4ae13ea8000000'
};

mockRequest(reqheaders);

await ms
.setUserId('5f4adc8f9c4ae13ea8000000')
.call(...requestArgs);

secretsNotCalled(sinon);
});

it('Should not set user id in janis-api-key header if value is falsy', async () => {

const reqheaders = {
...baseRequestHeaders,
'janis-api-key': 'service-dummy-service'
};

mockRequest(reqheaders);

await ms
.setUserId(null)
.call(...requestArgs);

secretsNotCalled(sinon);
});

it('Should set and clear user id in janis-api-key header', async () => {

const reqheaders = {
...baseRequestHeaders,
'janis-api-key': 'service-dummy-service_user-5f4adc8f9c4ae13ea8000000'
};

mockRequest(reqheaders);

await ms
.setUserId('5f4adc8f9c4ae13ea8000000')
.call(...requestArgs);

mockRequest({ ...reqheaders, 'janis-api-key': 'service-dummy-service' });

await ms.call(...requestArgs);

secretsNotCalled(sinon);
});

});

describe('Discovery service fails', () => {

it('Should throw an error if it has an error message', async () => {
Expand Down

0 comments on commit 07d41ad

Please sign in to comment.