Skip to content

Commit

Permalink
fix(payment-service): fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Surbhi-sharma1 committed Aug 22, 2024
1 parent f5b0018 commit c314b6c
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 426 deletions.
449 changes: 172 additions & 277 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions services/payment-service/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ node_modules/
dist/
coverage/
.eslintrc.js
migrations/

migration.js
4 changes: 2 additions & 2 deletions services/payment-service/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module.exports = {
'no-extra-boolean-cast': 'off',
'@typescript-eslint/interface-name-prefix': 'off',
'no-prototype-builtins': 'off',
'no-await-in-loop': 'error'
'no-await-in-loop': 'error',
},
parserOptions: {
project: './tsconfig.json',
tsconfigRootDir: __dirname,
},
};
};
2 changes: 2 additions & 0 deletions services/payment-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ This application is generated using [LoopBack 4 CLI](https://loopback.io/doc/en/
By default, dependencies were installed when this application was generated.
Whenever dependencies in `package.json` are changed, run the following command:



```sh
npm install
```
Expand Down

This file was deleted.

This file was deleted.

15 changes: 6 additions & 9 deletions services/payment-service/src/__tests__/acceptance/test-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@ export async function setupApplication(): Promise<AppWithClient> {
rest: restConfig,
});

app.bind('datasources.config.db').to({
name: 'db',
connector: 'memory',
});




app.bind('datasources.config.db').to({
name: 'db',
connector: 'memory',
});

await app.boot();
await app.start();

Expand All @@ -39,7 +36,7 @@ function setUpEnv() {
process.env.NODE_ENV = 'test';
process.env.ENABLE_TRACING = '0';
process.env.ENABLE_OBF = '0';
}
}

export interface AppWithClient {
app: PaymentServiceApplication;
Expand Down
63 changes: 11 additions & 52 deletions services/payment-service/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,89 +4,49 @@ import {
RestExplorerBindings,
RestExplorerComponent,
} from '@loopback/rest-explorer';
import * as dotenv from 'dotenv';
import * as dotenvExt from 'dotenv-extended';
import {AuthenticationComponent} from 'loopback4-authentication';
import {
AuthorizationBindings,
AuthorizationComponent,
} from 'loopback4-authorization';
import {
ServiceSequence,
SFCoreBindings,
BearerVerifierBindings,
BearerVerifierComponent,
BearerVerifierConfig,
BearerVerifierType,
SECURITY_SCHEME_SPEC,
TenantUtilitiesBindings,
} from '@sourceloop/core';
import {PayPalBindings, PaymentServiceComponent, PaypalProvider} from '@sourceloop/payment-service'
import {
PayPalBindings,
PaymentServiceComponent,
PaypalProvider,
} from '@sourceloop/payment-service';
import {RepositoryMixin} from '@loopback/repository';
import {RestApplication} from '@loopback/rest';
import {ServiceMixin} from '@loopback/service-proxy';
import path from 'path';
import * as openapi from './openapi.json';

export {ApplicationConfig};

export class PaymentServiceApplication extends BootMixin(
ServiceMixin(RepositoryMixin(RestApplication)),
) {
constructor(options: ApplicationConfig = {}) {
const port = 3000;
dotenv.config();
dotenvExt.load({
schema: '.env.example',
errorOnMissing: process.env.NODE_ENV !== 'test',
includeProcessEnv: true,
});
options.rest = options.rest ?? {};
options.rest.basePath = process.env.BASE_PATH ?? '';
options.rest.port = +(process.env.PORT ?? port);
options.rest.host = process.env.HOST;
options.rest.openApiSpec = {
endpointMapping: {
[`${options.rest.basePath}/openapi.json`]: {
version: '3.0.0',
format: 'json',
},
},
};

super(options);

// To check if monitoring is enabled from env or not
const enableObf = !!+(process.env.ENABLE_OBF ?? 0);
// To check if authorization is enabled for swagger stats or not
const authentication =
process.env.SWAGGER_USER && process.env.SWAGGER_PASSWORD ? true : false;
const obj={
enableObf,
obfPath: process.env.OBF_PATH ?? '/obf',
openapiSpec: openapi,
authentication: authentication,
swaggerUsername: process.env.SWAGGER_USER,
swaggerPassword: process.env.SWAGGER_PASSWORD,

}
this.bind(SFCoreBindings.config).to(obj);

// Set up the custom sequence
this.sequence(ServiceSequence);

// Add authentication component
this.component(AuthenticationComponent);
// this.bind(TenantUtilitiesBindings.Config).to({
// useSingleTenant:true
// })
this.bind(PayPalBindings.PayPalHelper.key).toProvider(PaypalProvider);
this.bind(PayPalBindings.PayPalConfig).to({
clientId: process.env.PAYPAL_CLIENT_ID ?? '',
clientSecret: process.env.PAYPAL_CLIENT_SECRET ?? '',
});
this.component(PaymentServiceComponent);

this.bind(PayPalBindings.PayPalConfig).to({
clientId: process.env.PAYPAL_CLIENT_ID ?? '',
clientSecret: process.env.PAYPAL_CLIENT_SECRET ?? '',
});
this.component(PaymentServiceComponent);
this.bind(PayPalBindings.PayPalHelper.key).toProvider(PaypalProvider);
// Add bearer verifier component
this.bind(BearerVerifierBindings.Config).to({
type: BearerVerifierType.service,
Expand All @@ -108,7 +68,6 @@ export class PaymentServiceApplication extends BootMixin(

this.component(RestExplorerComponent);


this.projectRoot = __dirname;
// Customize @loopback/boot Booter Conventions here
this.bootOptions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class HomePageController {
);
}

@authorize({permissions: ['*']})
@authorize({permissions: ['*']})
@get('/', {
responses: {
[STATUS_CODE.OK]: {
Expand All @@ -33,10 +33,7 @@ export class HomePageController {
},
})
homePage() {
this.response
.status(STATUS_CODE.OK)
.contentType('html')
.send(this.html);
this.response.status(STATUS_CODE.OK).contentType('html').send(this.html);
return this.response;
}
}
}
11 changes: 4 additions & 7 deletions services/payment-service/src/controllers/ping.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import {inject} from '@loopback/core';
import {
Request,
RestBindings,
get,
ResponseObject,
} from '@loopback/rest';
import {Request, RestBindings, get, ResponseObject} from '@loopback/rest';
import {authorize} from 'loopback4-authorization';
import {STATUS_CODE} from '@sourceloop/core';

Expand Down Expand Up @@ -39,7 +34,9 @@ const PING_RESPONSE: ResponseObject = {
* A simple controller to bounce back http requests
*/
export class PingController {
constructor(@inject(RestBindings.Http.REQUEST) private readonly req: Request) {}
constructor(
@inject(RestBindings.Http.REQUEST) private readonly req: Request,
) {}

// Map to `GET /ping`
@authorize({permissions: ['*']})
Expand Down
20 changes: 10 additions & 10 deletions services/payment-service/src/datasources/payment.datasource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core';
import {juggler} from '@loopback/repository';
import {PaymentDatasourceName} from '@sourceloop/payment-service'
import {PaymentDatasourceName} from '@sourceloop/payment-service';

const DEFAULT_MAX_CONNECTIONS = 25;
const DEFAULT_DB_IDLE_TIMEOUT_MILLIS = 60000;
Expand All @@ -12,21 +12,21 @@ const config = {
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
schema: process.env.DB_SCHEMA,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE
schema: process.env.DB_SCHEMA,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
};

// Observe application's life cycle to disconnect the datasource when
// application is stopped. This allows the application to be shut down
// gracefully. The `stop()` method is inherited from `juggler.DataSource`.
// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html
@lifeCycleObserver('datasource')
export class PaymentDataSource extends juggler.DataSource
implements LifeCycleObserver {

static dataSourceName = PaymentDatasourceName;
export class PaymentDataSource
extends juggler.DataSource
implements LifeCycleObserver
{
static readonly dataSourceName = PaymentDatasourceName;

static readonly defaultConfig = config;

Expand All @@ -51,4 +51,4 @@ export class PaymentDataSource extends juggler.DataSource

super(dsConfig);
}
}
}
5 changes: 1 addition & 4 deletions services/payment-service/src/openapi-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ exportOpenApiSpec()
process.exit(0);
})
.catch(err => {
console.error(
'Fail to export OpenAPI spec from the application.',
err,
);
console.error('Fail to export OpenAPI spec from the application.', err);
process.exit(1);
});
10 changes: 3 additions & 7 deletions services/payment-service/src/opentelemetry-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ if (!!+(process.env.ENABLE_TRACING ?? 0)) {
tags: [],
// You can use the default UDPSender
host: process.env.OPENTELEMETRY_HOST ?? '',
port: process.env.OPENTELEMETRY_PORT
? +process.env.OPENTELEMETRY_PORT
: 0,
port: process.env.OPENTELEMETRY_PORT ? +process.env.OPENTELEMETRY_PORT : 0,
};
// Configure span processor to send spans to the exporter
const exporter = new JaegerExporter(option);
provider.addSpanProcessor(new BatchSpanProcessor(exporter));
provider.addSpanProcessor(
new BatchSpanProcessor(new ConsoleSpanExporter()),
);
provider.addSpanProcessor(new BatchSpanProcessor(new ConsoleSpanExporter()));
provider.register();
}
}

0 comments on commit c314b6c

Please sign in to comment.