Skip to content

Commit

Permalink
feat(endpoints): Inbound authorizations handler (#16)
Browse files Browse the repository at this point in the history
* feat(swagger): Add new `POST .../authorizations` to swagger spec

* feat(authorizations): Implementing POST authorizations domain methods

feat(authorizations): Basic handler tests

feat(authorizations): add outbound call tests

* fix(authorizations): Parsing of request id

* fix(modules): Disable the fancy modules for now

* fix(paths): rename thirdpartyRequests dir to thirdpartyrequest

* fix(paths): revert to tpr path until we have sorted out swagger issues

* chore(linting): Fix linting errors

* feat(swagger): Add `POST /thirdpartyRequests/transactions/{ID}/authorizations` to api-template

* chore(repo): Refactor repo structure to line up with Sridhar's changes

* chore(test): Fixing unit tests

* chore(auth): refactor our setimmediate

* feat(histogram): Extracted histogram into generic wrapper for handlers

* feat(handlers): Update authorizations handler to add span tags

* chore(ambient.d.ts): Updated central-services-shared along with definitions

* fix(config): Rename `SWITCH_ENDPOINT` to more specific `ENDPOINT_SERVICE_URL`

* feat(transactions): Minor logging improvements

* clean up template and tests

* feature(docs): Update docs about testing

* fixing tests and removing logs

* feature(tests): Fixing broken unit tests after switch from `setImmediate`

* feat(authorization.test.ts): Expanding test coverage

* feat(coverage): Improving test coverage for span

* test(start.ts): Add tests to start.ts

* feat(coverage): Improving test coverage domain

* chore(lint): fix linting issues

* fix(transactions.test.ts): Fixed invalid test mock value

* test(bdd): Add bdd tests

- rename `template` bdd test to `health`

* chore(docs): Add Runtime configuration section to the Readme

* fix(tests): Fix tests and incorporate PR feedback

fix(errors): Improve error logging for unhandled promise rejections

feat(test): Expanding histogram error handling tests

feat(docs): Improve jsdoc for util.ts

Clean up test timers

Clean up test timers

fix(mocks): Improve response toolkit mocking

fix(mocks): Improve response toolkit mocking

* fix(coverage): Fix missing test coverage

* fix(vulns): update audit commands to scan production modules only

* fix(ci): Fix ci/cd build-local step

* fix(ci): remove docker specific build step

* fix(ci): fix sed escape character hell

* fix(ambient.d.ts): fix jsdoc optional param references

* chore: rename Bill & Melinda Gates Foundation > Copyright © 2020 Mojaloop Foundation

* fix(license): header references to old foundation
  • Loading branch information
lewisdaly authored Aug 13, 2020
1 parent f06f618 commit b47f4ba
Show file tree
Hide file tree
Showing 52 changed files with 2,130 additions and 2,632 deletions.
4 changes: 2 additions & 2 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# LICENSE

Copyright © 2017 Bill & Melinda Gates Foundation
Copyright © 2020 Mojaloop Foundation

The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0
The Mojaloop files are made available by the Mojaloop Foundation under the Apache License, Version 2.0
(the "License") and you may not use these files except in compliance with the [License](http://www.apache.org/licenses/LICENSE-2.0). You may obtain a copy of the License at

[http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)
Expand Down
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# thirdparty-api-adapter (Work in Progress)
[![Git Commit](https://img.shields.io/github/last-commit/mojaloop/thirdparty-api-adapter.svg?style=flat)](https://github.com/mojaloop/thirdparty-api-adapter/commits/master)
[![Git Releases](https://img.shields.io/github/release/mojaloop/thirdparty-api-adapter.svg?style=flat)](https://github.com/mojaloop/thirdparty-api-adapter/releases)
[![Npm Version](https://img.shields.io/npm/v/@mojaloop/thirdparty-api-adapter.svg?style=flat)](https://www.npmjs.com/package/@mojaloop/thirdparty-api-adapter)
[![NPM Vulnerabilities](https://img.shields.io/snyk/vulnerabilities/npm/@mojaloop/thirdparty-api-adapter.svg?style=flat)](https://www.npmjs.com/package/@mojaloop/thirdparty-api-adapter)
[![CircleCI](https://circleci.com/gh/mojaloop/thirdparty-api-adapter.svg?style=svg)](https://circleci.com/gh/mojaloop/thirdparty-api-adapter)

The thirdparty-api-adapter service is used to handle HTTP requests from third parties.
Expand All @@ -11,6 +9,25 @@ The thirdparty-api-adapter service is used to handle HTTP requests from third pa

- [Documentation](./docs/README.md)

## Runtime Configuration

Runtime configuration is handled by `rc`, and can be specified using either Environment Variables, or a `.json` file.

See [`./config/default.json`](./config/default.json) for an example config file.

When setting configuration using environment variables, the `THIRD_PARTY` environment variable prefix is required. See [`src/shared/config.ts`](src/shared/config.ts) to understand how these variables are configured.

### Key Config Options

> ***Note:** See [`./config/default.json`](./config/default.json) for all available config options, and their default values.*
| Name | Env Var | jsonPath | Description |
| ---- | ------- | -------- | ----------- |
| `PORT` | `THIRD_PARTY_PORT` | `.PORT` | The TCP port the Hapi server should start on |
| `HOST` | `THIRD_PARTY_HOST` | `.HOST` | The hostname the Hapi server should bind to |
| `ENDPOINT_SERVICE_URL` | `ENDPOINT_SERVICE_URL` | `.ENDPOINT_SERVICE_URL` | The internal service used to retrieve endpoints for Mojaloop Participants. Currently this is the `central-ledger`. |


## Setup

### Clone repo
Expand Down
93 changes: 88 additions & 5 deletions ambient.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*****
License
--------------
Copyright © 2017 Bill & Melinda Gates Foundation
The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
Copyright © 2020 Mojaloop Foundation
The Mojaloop files are made available by the Mojaloop Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Contributors
Expand All @@ -22,9 +22,51 @@
--------------
******/


// for mojaloop there is lack for @types files
// to stop typescript complains, we have to declare some modules here
declare module '@mojaloop/central-services-logger'
declare module '@mojaloop/central-services-metrics' {
import { Histogram } from 'prom-client'

interface metricOptionsType {
prefix: string
timeout: number
}
interface Metrics {

/**
* @function getHistogram
* @description Get the histogram values for given name
* @param {string} name - The name of the histogram to get. If the name doesn't exist, it creates a new histogram
* @param {string} [help] - (Optional) Help description of the histogram (only used with creating a new histogram)
* @param {Array<string>} [labelNames] - (Optional) Keys of the label to attach to the histogram
* @param {Array<number>} [buckets] - (Optional) Buckets used in the histogram
* @returns {Histogram} - The Prometheus Histogram object
* @throws {Error} -
*/
getHistogram: (name: string, help?: string, labelNames?: string[], buckets?: number[]) => Histogram

/**
* @function getMetricsForPrometheus
* @description Gets the metrics
*/
getMetricsForPrometheus: () => string

/**
* @function setup
* @description Setup the prom client for collecting metrics using the options passed
* @param {metricOptionsType} - Config option for Metrics setup
* @returns boolean
*/
setup: (options: metricOptionsType) => boolean
}

// `@mojaloop/central-services/metrics` exports a new class
// i.e. `new metrics.Metrics()`
const defaultMetrics: Metrics;
export default defaultMetrics
}
declare module '@mojaloop/central-services-shared' {
interface ReturnCode {
CODE: number;
Expand Down Expand Up @@ -93,7 +135,20 @@ declare module '@mojaloop/central-services-shared' {
THIRDPARTY_TRANSACTIONS_AUTHORIZATIONS_POST = 'FSPIOP_THIRDPARTY_TRANSACTIONS_AUTHORIZATIONS_POST',
THIRDPARTY_TRANSACTIONS_AUTHORIZATIONS_PUT = 'FSPIOP_THIRDPARTY_TRANSACTIONS_AUTHORIZATIONS_PUT',
THIRDPARTY_TRANSACTIONS_AUTHORIZATIONS_ERROR = 'FSPIOP_THIRDPARTY_TRANSACTIONS_AUTHORIZATIONS_PUT_ERROR',
THIRDPARTY_CALLBACK_URL_TRX_REQ_POST = 'THIRDPARTY_CALLBACK_URL_TRX_REQ_POST'
THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_POST = 'THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_POST',
THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_PUT = 'THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_PUT',
THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_PUT_ERROR = 'THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_PUT_ERROR',
THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_AUTHORIZATIONS_POST = 'THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_AUTHORIZATIONS_POST',
THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_AUTHORIZATIONS_PUT = 'THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_AUTHORIZATIONS_PUT',
THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_AUTHORIZATIONS_PUT_ERROR = 'THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_AUTHORIZATIONS_PUT_ERROR',
THIRDPARTY_CALLBACK_URL_CONSENT_REQUEST_POST = 'THIRDPARTY_CALLBACK_URL_CONSENT_REQUEST_POST',
THIRDPARTY_CALLBACK_URL_CONSENT_REQUEST_PUT = 'THIRDPARTY_CALLBACK_URL_CONSENT_REQUEST_PUT',
THIRDPARTY_CALLBACK_URL_CONSENT_REQUEST_PUT_ERROR = 'THIRDPARTY_CALLBACK_URL_CONSENT_REQUEST_PUT_ERROR',
THIRDPARTY_CALLBACK_URL_CREATE_CREDENTIAL_POST = 'THIRDPARTY_CALLBACK_URL_CREATE_CREDENTIAL_POST',
THIRDPARTY_CALLBACK_URL_CONSENT_POST = 'THIRDPARTY_CALLBACK_URL_CONSENT_POST',
THIRDPARTY_CALLBACK_URL_CONSENT_GET = 'THIRDPARTY_CALLBACK_URL_CONSENT_GET',
THIRDPARTY_CALLBACK_URL_CONSENT_PUT = 'THIRDPARTY_CALLBACK_URL_CONSENT_PUT',
THIRDPARTY_CALLBACK_URL_CONSENT_PUT_ERROR = 'THIRDPARTY_CALLBACK_URL_CONSENT_PUT_ERROR',
}
interface EndPointsEnum {
EndpointType: {
Expand Down Expand Up @@ -136,7 +191,20 @@ declare module '@mojaloop/central-services-shared' {
THIRDPARTY_TRANSACTIONS_AUTHORIZATIONS_POST: FspEndpointTypesEnum.THIRDPARTY_TRANSACTIONS_AUTHORIZATIONS_POST;
THIRDPARTY_TRANSACTIONS_AUTHORIZATIONS_PUT: FspEndpointTypesEnum.THIRDPARTY_TRANSACTIONS_AUTHORIZATIONS_PUT;
THIRDPARTY_TRANSACTIONS_AUTHORIZATIONS_ERROR: FspEndpointTypesEnum.THIRDPARTY_TRANSACTIONS_AUTHORIZATIONS_ERROR;
THIRDPARTY_CALLBACK_URL_TRX_REQ_POST: FspEndpointTypesEnum.THIRDPARTY_CALLBACK_URL_TRX_REQ_POST;
THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_POST: FspEndpointTypesEnum.THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_POST;
THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_PUT: FspEndpointTypesEnum.THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_PUT;
THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_PUT_ERROR: FspEndpointTypesEnum.THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_PUT_ERROR;
THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_AUTHORIZATIONS_POST: FspEndpointTypesEnum.THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_AUTHORIZATIONS_POST;
THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_AUTHORIZATIONS_PUT: FspEndpointTypesEnum.THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_AUTHORIZATIONS_PUT;
THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_AUTHORIZATIONS_PUT_ERROR: FspEndpointTypesEnum.THIRDPARTY_CALLBACK_URL_TRANSACTION_REQUEST_AUTHORIZATIONS_PUT_ERROR;
THIRDPARTY_CALLBACK_URL_CONSENT_REQUEST_POST: FspEndpointTypesEnum.THIRDPARTY_CALLBACK_URL_CONSENT_REQUEST_POST;
THIRDPARTY_CALLBACK_URL_CONSENT_REQUEST_PUT: FspEndpointTypesEnum.THIRDPARTY_CALLBACK_URL_CONSENT_REQUEST_PUT;
THIRDPARTY_CALLBACK_URL_CONSENT_REQUEST_PUT_ERROR: FspEndpointTypesEnum.THIRDPARTY_CALLBACK_URL_CONSENT_REQUEST_PUT_ERROR;
THIRDPARTY_CALLBACK_URL_CREATE_CREDENTIAL_POST: FspEndpointTypesEnum.THIRDPARTY_CALLBACK_URL_CREATE_CREDENTIAL_POST;
THIRDPARTY_CALLBACK_URL_CONSENT_POST: FspEndpointTypesEnum.THIRDPARTY_CALLBACK_URL_CONSENT_POST;
THIRDPARTY_CALLBACK_URL_CONSENT_GET: FspEndpointTypesEnum.THIRDPARTY_CALLBACK_URL_CONSENT_GET;
THIRDPARTY_CALLBACK_URL_CONSENT_PUT: FspEndpointTypesEnum.THIRDPARTY_CALLBACK_URL_CONSENT_PUT;
THIRDPARTY_CALLBACK_URL_CONSENT_PUT_ERROR: FspEndpointTypesEnum.THIRDPARTY_CALLBACK_URL_CONSENT_PUT_ERROR;
};
FspEndpointTemplates: {
TRANSACTION_REQUEST_POST: string;
Expand All @@ -159,8 +227,22 @@ declare module '@mojaloop/central-services-shared' {
BULK_TRANSFERS_POST: string;
BULK_TRANSFERS_PUT: string;
BULK_TRANSFERS_PUT_ERROR: string;
THIRDPARTY_TRANSACTION_REQUEST_PUT_ERROR: string;
THIRDPARTY_TRANSACTION_REQUEST_POST: string;
THIRDPARTY_TRANSACTION_REQUEST_PUT: string;
THIRDPARTY_TRANSACTION_REQUEST_PUT_ERROR: string;
THIRDPARTY_AUTHORIZATIONS_POST: string;
THIRDPARTY_AUTHORIZATIONS_PUT: string;
THIRDPARTY_TRANSACTION_REQUEST_AUTHORIZATIONS_POST: string;
THIRDPARTY_TRANSACTION_REQUEST_AUTHORIZATIONS_PUT: string;
THIRDPARTY_TRANSACTION_REQUEST_AUTHORIZATIONS_PUT_ERROR: string;
THIRDPARTY_CONSENT_REQUEST_POST: string;
THIRDPARTY_CONSENT_REQUEST_PUT: string;
THIRDPARTY_CONSENT_REQUEST_PUT_ERROR: string;
THIRDPARTY_CONSENT_CREATE_CREDENTIAL_POST: string;
THIRDPARTY_CONSENT_POST: string;
THIRDPARTY_CONSENT_GET: string;
THIRDPARTY_CONSENT_PUT: string;
THIRDPARTY_CONSENT_PUT_ERROR: string;
};
}
interface Enum {
Expand All @@ -172,6 +254,7 @@ declare module '@mojaloop/central-services-shared' {
POST: string;
};
Type: {
AUTHORIZATION: string;
TRANSACTION_REQUEST: string;
};
};
Expand Down
4 changes: 2 additions & 2 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"expiresIn": 180000,
"generateTimeout": 30000
},
"SWITCH_ENDPOINT": "http://central-ledger.local:3001",
"ENDPOINT_SERVICE_URL": "http://central-ledger.local:3001",
"ERROR_HANDLING": {
"includeCauseExtension": true,
"truncateExtensions": true
Expand All @@ -30,4 +30,4 @@
}
}
}
}
}
44 changes: 41 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,32 @@ npm run test:junit

There is `mojaloop` convention to use `test/unit` path to keep tests. The placement of test folder should be similar to placement of tested code in `src` folder

### Jest Tips

The [Jest CLI](https://jestjs.io/docs/en/cli) has some powerful commands for filtering, watching and running tests

```bash
# run tests on just 1 file:
npm run test:unit -- -- test/unit/index.test.ts

# run tests on file change:
npm run test:unit -- --watch

# After the tests are run, you can configure the watch settings like so:
Ran all test suites related to changed files.

Watch Usage
› Press a to run all tests.
› Press f to run only failed tests.
› Press p to filter by a filename regex pattern.
› Press t to filter by a test name regex pattern.
› Press q to quit watch mode.
› Press Enter to trigger a test run.
```

## linting

[eslint]() setup compatible with javascript [standard](https://standardjs.com/) and dedicated for TypeScript [typescript-eslint](https://github.com/typescript-eslint/typescript-eslint).
[eslint](https://eslint.org/) setup compatible with javascript [standard](https://standardjs.com/) and dedicated for TypeScript [typescript-eslint](https://github.com/typescript-eslint/typescript-eslint).
- it is much more flexible
- has good support for editors to visualize linting problem during typing.

Expand Down Expand Up @@ -95,7 +118,7 @@ Generate the named "alpha" pre-release
npm run release -- --prerelase alpha
```

### Docker setup
## Docker setup
Minimal working Docker image you can find in [Dockerfile](../Dockerfile).

To build the image
Expand All @@ -113,9 +136,24 @@ When the image is run you should be able to reach the dockerized _thirdparty-api
If you already added the `127.0.0.1 thirdparty-api-adapter.local` entry in your `/etc/hosts` then the _thirdparty-api-adapter_ is reachable on `http://thirdparty-api-adapter.local:3008`.


### external links
## External Links

- [about conventional commits](https://www.conventionalcommits.org/en/v1.0.0/)
- [standard-version](https://github.com/conventional-changelog/standard-version)
- [conventional-changelog-config-spec](https://github.com/conventional-changelog/conventional-changelog-config-spec/tree/master/versions/2.1.0)



## Tips + Tricks:

### 1. How do I disable the event-sdk logging in my unit tests?

You can use the `EVENT_SDK_LOG_FILTER` environment variable. This setting is for configuring the internal config of the event-sdk, which isn't currently exposed to us in the `@mojaloop/central-services-shared` library.

e.g.
```bash
export EVENT_SDK_LOG_FILTER=""
npm run test:unit -- --watch

```

Loading

0 comments on commit b47f4ba

Please sign in to comment.