Skip to content

Commit

Permalink
chore(auth): WIP - add tests for Apple IAP modules
Browse files Browse the repository at this point in the history
* add AppleIAP module test (currently has import error)
  • Loading branch information
biancadanforth committed Apr 14, 2022
1 parent bb52af3 commit 7bca437
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* 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/. */

export { AppleIAP } from './apple-iap';
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
decodeTransaction,
} from 'app-store-server-api';
import { TransactionType } from 'app-store-server-api/dist/types/Models';
import { Logger } from 'mozlog';
import Container from 'typedi';

import { AuthLogger } from '../../../types';
import {
APPLE_APP_STORE_FORM_OF_PAYMENT,
mergePurchaseWithFirestorePurchaseRecord,
Expand All @@ -25,7 +25,7 @@ import { PurchaseQueryError, PurchaseUpdateError } from './types';
* https://developer.apple.com/documentation/appstoreserverapi
*/
export class PurchaseManager {
private log: Logger;
private log: AuthLogger;

/*
* This class is intended to be initialized by the library.
Expand All @@ -37,7 +37,7 @@ export class PurchaseManager {
[key: string]: AppStoreServerAPI;
}
) {
this.log = Container.get(Logger);
this.log = Container.get(AuthLogger);
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/* 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 { mockLog } = require('../../../../mocks');
const {
AuthFirestore,
AuthLogger,
AppConfig,
} = require('../../../../../lib/types');
const { AppleIAP } = require('../../../../../lib/payments/iap/apple-app-store');

const mockConfig = {
authFirestore: {
prefix: 'mock-fxa-',
},
subscriptions: {
appStore: {
credentials: {
org_mozilla_ios_FirefoxVPN: {
issuerId: 'issuer_id',
serverApiKey: 'key',
serverApiKeyId: 'key_id',
},
},
},
},
};

describe('AppleIAP', () => {
let sandbox;
let firestore;
let log;
let appleIAP;
let planDbRefMock;
let purchasesDbRefMock;

beforeEach(() => {
sandbox = sinon.createSandbox();
planDbRefMock = {};
purchasesDbRefMock = {};
const collectionMock = sinon.stub();
collectionMock.onFirstCall().returns(planDbRefMock);
collectionMock.onSecondCall().returns(purchasesDbRefMock);
firestore = {
collection: collectionMock,
};
log = mockLog();
Container.set(AuthFirestore, firestore);
Container.set(AuthLogger, log);
Container.set(AppConfig, mockConfig);
Container.remove(AppleIAP);
});

afterEach(() => {
Container.reset();
sandbox.restore();
});

it('can be instantiated', () => {
const appleIAP = Container.get(AppleIAP);
assert.strictEqual(appleIAP.log, log);
assert.strictEqual(appleIAP.firestore, firestore);
assert.strictEqual(appleIAP.prefix, 'mock-fxa-iap-');
});

describe('plans', () => {
beforeEach(() => {
// Create and set a new one per test
appleIAP = new AppleIAP();
Container.set(AppleIAP, appleIAP);
});

it('returns successfully', async () => {
planDbRefMock.doc = sinon.fake.returns({
get: sinon.fake.resolves({
exists: true,
data: sinon.fake.returns({ plans: 'testObject' }),
}),
});
const result = await appleIAP.plans();
assert.strictEqual(result, 'testObject');
});

it('throws error with no document found', async () => {
planDbRefMock.doc = sinon.fake.returns({
get: sinon.fake.resolves({
exists: false,
}),
});
try {
await appleIAP.plans('testApp');
assert.fail('Expected exception thrown.');
} catch (err) {
assert.strictEqual(
err.message,
'IAP Plans document does not exist for testApp'
);
}
});
});

describe('getBundleId', () => {
beforeEach(() => {
// Create and set a new one per test
appleIAP = new AppleIAP();
Container.set(AppleIAP, appleIAP);
});

it('returns successfully', async () => {
planDbRefMock.doc = sinon.fake.returns({
get: sinon.fake.resolves({
exists: true,
data: sinon.fake.returns({
packageName: 'org.mozilla.testApp',
plans: 'testObject',
}),
}),
});
const result = await appleIAP.getBundleId('testApp');
assert.strictEqual(result, 'org.mozilla.testApp');
});

it('throws error with no document found', async () => {
planDbRefMock.doc = sinon.fake.returns({
get: sinon.fake.resolves({
exists: false,
}),
});
try {
await appleIAP.packageName('testApp');
assert.fail('Expected exception thrown.');
} catch (err) {
assert.strictEqual(
err.message,
'IAP Plans document does not exist for testApp'
);
}
});
});
});

0 comments on commit 7bca437

Please sign in to comment.