-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(auth): create AppStoreHelper class to manage client API inte…
…rnals
- Loading branch information
1 parent
6c38b06
commit c81b0d4
Showing
6 changed files
with
245 additions
and
63 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
packages/fxa-auth-server/lib/payments/iap/apple-app-store/app-store-helper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
import { | ||
AppStoreServerAPI, | ||
Environment, | ||
StatusResponse, | ||
} from 'app-store-server-api'; | ||
import { Container } from 'typedi'; | ||
|
||
import { AppConfig, AuthLogger } from '../../../types'; | ||
|
||
export class AppStoreHelper { | ||
private log: AuthLogger; | ||
private appStoreServerApiClients: { | ||
[key: string]: AppStoreServerAPI; | ||
}; | ||
private credentialsByBundleId: any; | ||
private environment: Environment; | ||
|
||
constructor() { | ||
this.log = Container.get(AuthLogger); | ||
const { | ||
subscriptions: { appStore }, | ||
} = Container.get(AppConfig); | ||
this.credentialsByBundleId = {}; | ||
// Initialize App Store Server API client per bundle ID | ||
this.environment = appStore.sandbox | ||
? Environment.Sandbox | ||
: Environment.Production; | ||
this.appStoreServerApiClients = {}; | ||
for (const [bundleIdWithUnderscores, credentials] of Object.entries( | ||
appStore.credentials | ||
)) { | ||
// Cannot use an actual bundleId (e.g. 'org.mozilla.ios.FirefoxVPN') as the key | ||
// due to https://github.com/mozilla/node-convict/issues/250 | ||
const bundleId = bundleIdWithUnderscores.replace('_', '.'); | ||
this.credentialsByBundleId[bundleId] = credentials; | ||
this.clientByBundleId(bundleId); | ||
} | ||
} | ||
|
||
/** | ||
* Returns an App Store Server API client by bundleId, initializing it first | ||
* if needed. | ||
*/ | ||
clientByBundleId(bundleId: string): AppStoreServerAPI { | ||
if (this.appStoreServerApiClients.hasOwnProperty(bundleId)) { | ||
return this.appStoreServerApiClients[bundleId]; | ||
} | ||
const { serverApiKey, serverApiKeyId, issuerId } = | ||
this.credentialsByBundleId[bundleId]; | ||
this.appStoreServerApiClients[bundleId] = new AppStoreServerAPI( | ||
serverApiKey, | ||
serverApiKeyId, | ||
issuerId, | ||
bundleId, | ||
this.environment | ||
); | ||
return this.appStoreServerApiClients[bundleId]; | ||
} | ||
|
||
async getSubscriptionStatuses( | ||
bundleId: string, | ||
originalTransactionId: string | ||
): Promise<StatusResponse> { | ||
const apiClient = this.clientByBundleId(bundleId); | ||
return apiClient.getSubscriptionStatuses(originalTransactionId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
packages/fxa-auth-server/test/local/payments/iap/apple-app-store/app-store-helper.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
'use strict'; | ||
|
||
const sinon = require('sinon'); | ||
const { assert } = require('chai'); | ||
const { default: Container } = require('typedi'); | ||
const proxyquire = require('proxyquire').noPreserveCache(); | ||
|
||
const { mockLog } = require('../../../../mocks'); | ||
const { AuthLogger, AppConfig } = require('../../../../../lib/types'); | ||
const { AppStoreServerAPI } = require('app-store-server-api'); | ||
|
||
const mockAppStoreServerAPI = sinon.createStubInstance(AppStoreServerAPI); | ||
const { AppStoreHelper } = proxyquire( | ||
'../../../../../lib/payments/iap/apple-app-store/app-store-helper', | ||
{ | ||
'app-store-server-api': { | ||
AppStoreServerAPI: function () { | ||
return mockAppStoreServerAPI; | ||
}, | ||
}, | ||
} | ||
); | ||
|
||
const mockBundleIdWithUnderscores = 'org_mozilla_ios_FirefoxVPN'; | ||
const mockBundleId = mockBundleIdWithUnderscores.replace('_', '.'); | ||
const mockConfig = { | ||
subscriptions: { | ||
appStore: { | ||
credentials: { | ||
[mockBundleIdWithUnderscores]: { | ||
issuerId: 'issuer_id', | ||
serverApiKey: 'key', | ||
serverApiKeyId: 'key_id', | ||
}, | ||
}, | ||
sandbox: true, | ||
}, | ||
}, | ||
}; | ||
|
||
describe('AppStoreHelper', () => { | ||
let appStoreHelper; | ||
let sandbox; | ||
let log; | ||
|
||
beforeEach(() => { | ||
sandbox = sinon.createSandbox(); | ||
log = mockLog(); | ||
Container.set(AuthLogger, log); | ||
Container.set(AppConfig, mockConfig); | ||
appStoreHelper = Container.get(AppStoreHelper); | ||
}); | ||
|
||
afterEach(() => { | ||
Container.reset(); | ||
sandbox.restore(); | ||
}); | ||
|
||
it('can be instantiated', () => { | ||
const appStoreServerApiClients = { | ||
[mockBundleId]: mockAppStoreServerAPI, | ||
}; | ||
const credentialsByBundleId = { | ||
[mockBundleId]: | ||
mockConfig.subscriptions.appStore.credentials[ | ||
mockBundleIdWithUnderscores | ||
], | ||
}; | ||
assert.strictEqual(appStoreHelper.log, log); | ||
assert.deepEqual( | ||
appStoreHelper.appStoreServerApiClients, | ||
appStoreServerApiClients | ||
); | ||
assert.deepEqual( | ||
appStoreHelper.credentialsByBundleId, | ||
credentialsByBundleId | ||
); | ||
assert.strictEqual(appStoreHelper.environment, 'Sandbox'); | ||
}); | ||
|
||
describe('clientByBundleId', () => { | ||
let mockApiClient; | ||
|
||
beforeEach(() => { | ||
mockApiClient = {}; | ||
}); | ||
|
||
it('returns the existing API client for the bundleId', () => { | ||
appStoreHelper.appStoreServerApiClients[mockBundleId] = mockApiClient; | ||
const actual = appStoreHelper.clientByBundleId(mockBundleId); | ||
const expected = mockApiClient; | ||
assert.deepEqual(actual, expected); | ||
}); | ||
it("initializes an API client for a given bundleId if it doesn't exist", () => { | ||
appStoreHelper.appStoreServerApiClients = {}; | ||
const actual = appStoreHelper.clientByBundleId(mockBundleId); | ||
const expected = mockAppStoreServerAPI; | ||
assert.deepEqual(actual, expected); | ||
}); | ||
}); | ||
|
||
describe('getSubscriptionStatuses', async () => { | ||
it('calls the corresponding method on the API client', async () => { | ||
const mockOriginalTransactionId = '100000000'; | ||
// Mock App Store Client API response | ||
const expected = { data: 'wow' }; | ||
mockAppStoreServerAPI.getSubscriptionStatuses = sinon | ||
.stub() | ||
.resolves(expected); | ||
appStoreHelper.clientByBundleId = sandbox | ||
.stub() | ||
.returns(mockAppStoreServerAPI); | ||
const actual = await appStoreHelper.getSubscriptionStatuses( | ||
mockBundleId, | ||
mockOriginalTransactionId | ||
); | ||
assert.deepEqual(actual, expected); | ||
|
||
sinon.assert.calledOnceWithExactly( | ||
appStoreHelper.clientByBundleId, | ||
mockBundleId | ||
); | ||
sinon.assert.calledOnceWithExactly( | ||
mockAppStoreServerAPI.getSubscriptionStatuses, | ||
mockOriginalTransactionId | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.