Skip to content

Commit

Permalink
feat(algoan): add a method to retrieve sa from sub id
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Coeurderoy committed Jun 22, 2020
1 parent f368311 commit 3256c06
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/Algoan.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RequestBuilder } from './RequestBuilder';
import { PostSubscriptionDTO, EventName } from './lib';
import { ServiceAccount } from './core/ServiceAccount';
import { Subscription } from './core';
/**
* Algoan API
*/
Expand Down Expand Up @@ -70,6 +71,16 @@ export class Algoan {
}
}

/**
* Get a service account with a given subscription id
* @param subscriptionId Unique subscription identifier
*/
public getServiceAccountBySubscriptionId(subscriptionId: string): ServiceAccount | undefined {
return this.serviceAccounts.find((sa: ServiceAccount) =>
sa.subscriptions.find((sub: Subscription) => sub.id === subscriptionId),
);
}

/**
* Transform a list of events to a Subscription request body
* @param target Base URL
Expand Down
2 changes: 2 additions & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './ServiceAccount';
export * from './Subscription';
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './Algoan';
export * from './lib';
export * from './core';
export * from './RequestBuilder';
28 changes: 26 additions & 2 deletions test/algoan.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as nock from 'nock';

import { Algoan, EventName } from '../src';
import { Algoan, EventName, ServiceAccount, Subscription, RequestBuilder } from '../src';
import { serviceAccounts } from './samples/service-accounts';
import { subscriptions as subscriptionsSample } from './samples/subscriptions';
import { getFakeAlgoanServer, getOAuthServer } from './utils/fake-server.utils';
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('Tests related to the Algoan class', () => {
expect(client.serviceAccounts).toHaveLength(2);
});

it('ALG001 - should correctly get subscriptions and service accounts - multiple bodies', async () => {
it('ALG004 - should correctly get subscriptions and service accounts - multiple bodies', async () => {
serviceAccountAPI = getFakeAlgoanServer({
baseUrl,
path: '/v1/service-accounts',
Expand All @@ -124,4 +124,28 @@ describe('Tests related to the Algoan class', () => {
expect(client.serviceAccounts[0].subscriptions).toHaveLength(2);
});
});

describe('getServiceAccountBySubscriptionId()', () => {
it('ALG010 - should get a service account stored in-memory', () => {
const client: Algoan = new Algoan({
baseUrl: 'random',
clientId: 'a',
clientSecret: 'b',
});

client.serviceAccounts.push(new ServiceAccount('https://', serviceAccounts[0]));
client.serviceAccounts[0].subscriptions.push(
new Subscription(
subscriptionsSample[0],
new RequestBuilder('http://', {
clientSecret: 'a',
clientId: 'a',
}),
),
);

expect(client.getServiceAccountBySubscriptionId(subscriptionsSample[0].id)).toEqual(client.serviceAccounts[0]);
expect(client.getServiceAccountBySubscriptionId('random')).toBeUndefined();
});
});
});

0 comments on commit 3256c06

Please sign in to comment.