Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/complete tests for subscription #292

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/controllers/subscriber.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,11 @@ async function createTransaction(req, res) {
});
}
const transaction = subscriber.transaction;
const id = (transaction.platform === 'android-playstore' && transaction.nativePurchase.productId) || (transaction.platform === 'paypal' && transaction.products[0].id);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a logical operation, it will always return false or true, but never the id

return res.status(200).json({
ok: true,
data: {
id: transaction.nativePurchase.productId,
id,
latest_receipt: true,
transaction: {
data: { transaction, success: true },
Expand Down
54 changes: 53 additions & 1 deletion test/controllers/subscriber.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('Subscriber API calls', function() {
const {
subscriberData: mockSubscriberData,
transactionData,
payPalTransactionData,
createSubscriber,
deleteSubscriber,
} = helper.subscriber;
Expand Down Expand Up @@ -112,7 +113,7 @@ describe('Subscriber API calls', function() {
describe('POST /subscriber/${subscriber.id}/transaction', function() {
let subscriber;

const { mockPurchaseTokenVerification } = helper.subscriber;
const { mockPurchaseTokenVerification, mockPayPalVerification } = helper.subscriber;

before(async function() {
subscriber = await createSubscriber(user.userId);
Expand Down Expand Up @@ -284,6 +285,57 @@ describe('Subscriber API calls', function() {
firstCollection.isExpired.should.to.deep.equal(true);
firstCollection.isBillingRetryPeriod.should.to.deep.equal(true);
});

it('it should creates a transaction field in subscriber using paypal.', async function() {
const mockTransactionData = payPalTransactionData;
const verifiedPurchaseReply = mockPayPalVerification({ isValid: true })
const res = await request(server)
.post(`/subscriber/${subscriber._id}/transaction`)
.send(mockTransactionData)
.set('Authorization', `Bearer ${user.token}`)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200);

const transactionRes = res.body;
const collection = transactionRes.data.collection;
const firstCollection = collection[0];
transactionRes.ok.should.to.equal(true);
transactionRes.data.id.should.to.deep.equal(
mockTransactionData.products[0].id
);
transactionRes.data.latest_receipt.should.to.deep.equal(true);
transactionRes.data.transaction.type.should.to.deep.equal(
mockTransactionData.platform
);
transactionRes.data.transaction.data.transaction.nativePurchase.should.to.deep.equal(
{
...verifiedPurchaseReply
}
);
transactionRes.data.transaction.data.success.should.to.deep.equal(true);

firstCollection.expiryDate.should.to.deep.equal(verifiedPurchaseReply.billing_info.next_billing_time);
});

it('it should not creates a PayPal transaction field if subscriptionId is invalid', async function() {
mockPayPalVerification({ isValid: false });

const mockTransactionData = payPalTransactionData;
const res = await request(server)
.post(`/subscriber/${subscriber._id}/transaction`)
.send(mockTransactionData)
.set('Authorization', `Bearer ${user.token}`)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200);

const transactionRes = res.body;
transactionRes.ok.should.to.equal(false);
// transactionRes.error.message.should.to.equal(
// 'Validation failed: transaction: error verifying purchase. Check if the purchase token is valid'
// );
});
});

describe('get /subscriber/${subscriber.userId}', function() {
Expand Down
111 changes: 111 additions & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,32 @@ const subscriber = {
isAcknowledged: false,
renewalIntent: 'Renew',
},
payPalTransactionData: {
className: 'Transaction',
subscriptionId: 'subscriptionID',
transactionId: 'subscriptionID',
state: 'approved',
products: [{
"id": "test-monthly",
"subscriptionId": "test",
"billingPeriod": "P1M",
"price": {
"currencyCode": "USD",
"units": "6"
},
"title": "Test Subscription",
"tag": "test-monthly",
"paypalId": "P-7CR38290D5111153BMRDNDLA"
}],
platform: 'paypal',
nativePurchase: '',
purchaseId: 'orderID',
purchaseDate: '',
isPending: false,
subscriptionState: 'active',
expiryDate: '',
facilitatorAccessToken:'facilitatorAccessToken'
},
createSubscriber: async (userId) => {
const newSubscriber = subscriber.subscriberData;
newSubscriber.userId = userId;
Expand Down Expand Up @@ -307,6 +333,91 @@ const subscriber = {
},
});
},
mockPayPalVerification: ({ isValid }) => {
nock('https://api-m.sandbox.paypal.com')
.post(`/v1/oauth2/token`)
.reply(200,{data:{access_token:'ttttoken'}});

if (isValid) {
const subscriptionDetails = {
status: 'ACTIVE',
status_update_time: '2023-04-27T20:30:29Z',
id: 'I-9UN3TA1TXMC7',
plan_id: 'P-7CR38290D5111153BMRDNDLA',
start_time: '2023-04-27T20:29:47Z',
quantity: '1',
shipping_amount: { currency_code: 'USD', value: '0.0' },
subscriber: {
email_address: '[email protected]',
payer_id: '587FW7S4CZNEG',
name: { given_name: 'SANTIAGO', surname: 'EL QUESO' },
shipping_address: { address: {} }
},
billing_info: {
outstanding_balance: { currency_code: 'USD', value: '0.0' },
cycle_executions: [ {} ],
last_payment: { amount: {}, time: '2023-04-27T20:30:28Z' },
next_billing_time: '2023-05-27T10:00:00Z',
final_payment_time: '2027-03-27T10:00:00Z',
failed_payments_count: 0
},
create_time: '2023-04-27T20:30:28Z',
update_time: '2023-04-27T20:30:29Z',
plan_overridden: false,
links: [
{
href: 'https://api.sandbox.paypal.com/v1/billing/subscriptions/I-9UN3FA1TXHC7/cancel',
rel: 'cancel',
method: 'POST'
},
{
href: 'https://api.sandbox.paypal.com/v1/billing/subscriptions/I-9UN3FA1TXHC7',
rel: 'edit',
method: 'PATCH'
},
{
href: 'https://api.sandbox.paypal.com/v1/billing/subscriptions/I-9UN3FA1TXHC7',
rel: 'self',
method: 'GET'
},
{
href: 'https://api.sandbox.paypal.com/v1/billing/subscriptions/I-9UN3FA1TXHC7/suspend',
rel: 'suspend',
method: 'POST'
},
{
href: 'https://api.sandbox.paypal.com/v1/billing/subscriptions/I-9UN3FA1TXHC7/capture',
rel: 'capture',
method: 'POST'
}
]
}

const { subscriptionId } = subscriber.payPalTransactionData;
nock(
`https://api-m.sandbox.paypal.com/v1/billing/subscriptions/${subscriptionId}`
)
.get('')
.reply(200, subscriptionDetails);
return subscriptionDetails;
}

const invalidResponse = {
name: 'RESOURCE_NOT_FOUND',
message: 'The specified resource does not exist.',
debug_id: 'e662698658963',
details: [],
links: []
}

const { subscriptionId } = subscriber.payPalTransactionData;
nock(
`https://api-m.sandbox.paypal.com/v1/billing/subscriptions/${subscriptionId}`
)
.get('')
.reply(404, invalidResponse);
return invalidResponse;
}
};

const subscription = {
Expand Down
Loading