Skip to content

Commit

Permalink
tests and misc
Browse files Browse the repository at this point in the history
  • Loading branch information
xShteff committed Apr 15, 2024
1 parent cef4379 commit f2b3f87
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 20 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# pulumi-link-mobility-provider

```bash
npm link ..
```
1 change: 1 addition & 0 deletions example/Pulumi.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
encryptionsalt: v1:XbnIsPJjAKk=:v1:JP1+ntD/KahbnJtH:nqEMZk68CMXs21lq0nMDfil9dtcMUQ==
12 changes: 6 additions & 6 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import {
import 'dotenv/config';

const provider = new LinkMobilityPartnerGateDestinationProvider({
username: process.env.USERNAME!,
password: process.env.PASSWORD!,
url: 'https://XX.linkmobility.io',
partner: '0000',
platform: 'XXXX',
username: process.env.LINK_MOBILITY_USERNAME!,
password: process.env.LINK_MOBILITY_PASSWORD!,
url: process.env.LINK_MOBILITY_URL!,
partner: process.env.LINK_MOBILITY_PARTNER!,
platform: process.env.LINK_MOBILITY_PLATFORM!,
});

new LinkMobilityPartnerGateDestination('link-mobility-foo-bar-destination', {
provider: provider,
partnerGateId: 'xxxxx',
partnerGateId: process.env.FOOBAR_PARTNER_GATE_ID!,
destination: {
url: 'https://foo.bar',
contentType: 'application/json',
Expand Down
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "pulumi-link-mobility-provider",
"name": "@lego/pulumi-link-mobility-provider",
"version": "1.0.0",
"description": "A custom provider used to create resources within the Link Mobility Platform",
"main": "src/index.ts",
"scripts": {
"build": "tsc -p .",
"lint": "eslint . --ext .ts",
"test": "jest --coverage",
"test:watch": "yarn test --watch",
"format": "prettier --write \"**/**/*.ts\"",
"release": "semantic-release",
"prepare": "husky"
Expand All @@ -30,5 +31,13 @@
"semantic-release": "^20.1.1",
"ts-jest": "^28.0.3",
"typescript": "^4.7.2"
},
"jest": {
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testMatch": [
"**/*.test.ts"
]
}
}
38 changes: 26 additions & 12 deletions src/link-mobility.provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as pulumi from '@pulumi/pulumi';
import { LinkMobilityPartnerGateService } from './link-mobility.service';
import { LinkMobilityGateDestination } from './models';
import { isLinkMobilityPartnerGateDestinationInputs } from './utils';

export type LinkMobilityPartnerGateDestinationProviderInputs = {
username: string;
Expand Down Expand Up @@ -29,6 +30,7 @@ export class LinkMobilityPartnerGateDestinationProvider implements pulumi.dynami
this.partner = inputs.partner;
this.platform = inputs.platform;
this.url = inputs.url;

this.linkMobilityService = new LinkMobilityPartnerGateService({
username: this.username,
password: this.password,
Expand All @@ -41,26 +43,38 @@ export class LinkMobilityPartnerGateDestinationProvider implements pulumi.dynami
async create(
input: LinkMobilityPartnerGateDestinationInputs
): Promise<pulumi.dynamic.CreateResult<LinkMobilityPartnerGateDestinationInputs>> {
await this.linkMobilityService.createOrUpdateDestination(
input.partnerGateId,
input.destination
);

return {
id: input.destination.url,
outs: {
partnerGateId: input.partnerGateId,
destination: input.destination,
},
let outputs: pulumi.dynamic.CreateResult<LinkMobilityPartnerGateDestinationInputs> = {
id: '',
};
if (isLinkMobilityPartnerGateDestinationInputs(input)) {
await this.linkMobilityService.createOrUpdateDestination(
input.partnerGateId,
input.destination,
false
);

outputs = {
id: input.destination.url,
outs: {
partnerGateId: input.partnerGateId,
destination: input.destination,
},
};
}

return outputs;
}

async update(
_id: string,
_olds: LinkMobilityPartnerGateDestinationInputs,
news: LinkMobilityPartnerGateDestinationInputs
): Promise<pulumi.dynamic.UpdateResult<LinkMobilityPartnerGateDestinationInputs>> {
await this.linkMobilityService.createOrUpdateDestination(news.partnerGateId, news.destination);
await this.linkMobilityService.createOrUpdateDestination(
news.partnerGateId,
news.destination,
true
);

return {
outs: {
Expand Down
15 changes: 14 additions & 1 deletion src/link-mobility.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export class LinkMobilityPartnerGateService {
constructor(inputs: LinkMobilityPartnerGateServiceInputs) {
this.username = inputs.username;
this.password = inputs.password;
const urlRegex = new RegExp('^(https?|http)://[a-zA-Z0-9-.]+.[a-zA-Z]{2,}(:[0-9]{2,4})?');
if (!urlRegex.test(inputs.url)) {
throw new Error(
'Invalid Link Mobility URL. URL must follow the following pattern: ^(https?|http)://[a-zA-Z0-9-.]+.[a-zA-Z]{2,}(:[0-9]{2,4})?'
);
}
this.url = `${inputs.url}/gate/partnergate/platform/${inputs.platform}/partner/${inputs.partner}`;
}

Expand Down Expand Up @@ -45,14 +51,21 @@ export class LinkMobilityPartnerGateService {
return await response.json();
}

public async createOrUpdateDestination(gateId: string, destination: LinkMobilityGateDestination) {
public async createOrUpdateDestination(
gateId: string,
destination: LinkMobilityGateDestination,
overwrite: boolean
) {
const gate: LinkMobilityGate = await this.getById(gateId);

const destinationIndex = gate.destinations.findIndex((d) => d.url === destination.url);

if (destinationIndex === -1) {
gate.destinations.push(destination);
} else {
if (!overwrite) {
throw new Error('Can not create destination as it already exists.');
}
gate.destinations[destinationIndex] = destination;
}

Expand Down
58 changes: 58 additions & 0 deletions src/tests/link-mobility.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {
LinkMobilityPartnerGateService,
LinkMobilityPartnerGateServiceInputs,
} from '../link-mobility.service';

describe('Test suite for link mobility partner gate service', () => {
let linkMobilityService: LinkMobilityPartnerGateService;
const mockFetch = jest.fn();

beforeEach(async () => {
jest.resetModules();
global.fetch = mockFetch;
linkMobilityService = new LinkMobilityPartnerGateService({
username: 'username',
password: 'password',
url: 'http://some-url.com',
platform: 'platform',
partner: 'partner',
});
});

afterEach(() => {
jest.clearAllMocks();
});

it('should fail to initialise if url is invalid', async () => {
// Arrange
const ctorInput: LinkMobilityPartnerGateServiceInputs = {
username: 'username',
password: 'password',
url: 'invalid-url',
platform: 'platform',
partner: 'partner',
};

// Act & Assert
expect(() => {
new LinkMobilityPartnerGateService(ctorInput);
}).toThrowError();
});
});

it('should initialise if url is valid', async () => {
// Arrange
const ctorInput: LinkMobilityPartnerGateServiceInputs = {
username: 'username',
password: 'password',
url: 'http://some-url.com',
platform: 'platform',
partner: 'partner',
};

// Act
const service = new LinkMobilityPartnerGateService(ctorInput);

// Assert
expect(service).toBeDefined();
});
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './is-partner-gate-destination-inputs';
9 changes: 9 additions & 0 deletions src/utils/is-partner-gate-destination-inputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { LinkMobilityPartnerGateDestinationInputs } from '../link-mobility.provider';

export function isLinkMobilityPartnerGateDestinationInputs(
inputs: any
): inputs is LinkMobilityPartnerGateDestinationInputs {
return (
inputs && typeof inputs.partnerGateId === 'string' && typeof inputs.destination === 'object'
);
}

0 comments on commit f2b3f87

Please sign in to comment.