Skip to content

Commit b5fd9c4

Browse files
committed
feat: error case tests
1 parent c57a4b3 commit b5fd9c4

File tree

4 files changed

+151
-5
lines changed

4 files changed

+151
-5
lines changed

__tests__/common/njord.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,39 @@ describe('purchaseCores', () => {
756756
(clientSpy.mock.calls[0][1]!.headers as Headers).get('authorization'),
757757
).toStartWith('Bearer ');
758758
});
759+
760+
it('should throw on njord error', async () => {
761+
jest.spyOn(njordCommon, 'getNjordClient').mockImplementation(() =>
762+
createClient(
763+
Credits,
764+
createMockNjordErrorTransport({
765+
errorStatus: TransferStatus.INTERNAL_ERROR,
766+
}),
767+
),
768+
);
769+
770+
const transaction = await con.getRepository(UserTransaction).save({
771+
processor: UserTransactionProcessor.Njord,
772+
receiverId: 't-pc-2',
773+
status: UserTransactionStatus.Success,
774+
productId: null,
775+
senderId: null,
776+
value: 42,
777+
valueIncFees: 42,
778+
fee: 0,
779+
request: {},
780+
flags: {
781+
note: 'Test test!',
782+
},
783+
});
784+
785+
await expect(
786+
async () =>
787+
await njordCommon.purchaseCores({
788+
transaction,
789+
}),
790+
).rejects.toBeInstanceOf(TransferError);
791+
});
759792
});
760793

761794
describe('createNjordAuth', () => {

__tests__/routes/webhooks/apple.ts

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import {
1111
User,
1212
UserSubscriptionStatus,
1313
} from '../../../src/entity';
14-
import { createMockNjordTransport, saveFixtures } from '../../helpers';
14+
import {
15+
createMockNjordErrorTransport,
16+
createMockNjordTransport,
17+
saveFixtures,
18+
} from '../../helpers';
1519
import {
1620
NotificationTypeV2,
1721
Subtype,
@@ -24,7 +28,7 @@ import { env } from 'process';
2428
import { deleteRedisKey, getRedisHash } from '../../../src/redis';
2529
import { StorageKey } from '../../../src/config';
2630
import { createClient } from '@connectrpc/connect';
27-
import { Credits } from '@dailydotdev/schema';
31+
import { Credits, TransferStatus } from '@dailydotdev/schema';
2832
import * as njordCommon from '../../..//src/common/njord';
2933
import { getTransactionForProviderId } from '../../../src/common/paddle';
3034
import { UserTransactionProcessor } from '../../../src/entity/user/UserTransaction';
@@ -555,5 +559,63 @@ describe('POST /webhooks/apple/notifications', () => {
555559
});
556560
expect(userTransaction).toBeNull();
557561
});
562+
563+
it('should error cores purchase on njord error', async () => {
564+
jest.spyOn(njordCommon, 'getNjordClient').mockImplementation(() =>
565+
createClient(
566+
Credits,
567+
createMockNjordErrorTransport({
568+
errorStatus: TransferStatus.INSUFFICIENT_FUNDS,
569+
errorMessage: 'Insufficient funds',
570+
}),
571+
),
572+
);
573+
574+
const purchaseCoresSpy = jest.spyOn(njordCommon, 'purchaseCores');
575+
576+
await request(app.server)
577+
.post('/webhooks/apple/notifications')
578+
.send({
579+
signedPayload: signedPayload({
580+
notificationType: NotificationTypeV2.ONE_TIME_CHARGE,
581+
data: {
582+
signedTransactionInfo: {
583+
productId: 'cores_100',
584+
quantity: 1,
585+
type: 'Consumable',
586+
appAccountToken: '18138f83-b4d3-456a-831f-1f3f7bcbb0bd',
587+
transactionId: '220698',
588+
},
589+
},
590+
}),
591+
})
592+
.expect(200);
593+
594+
expect(purchaseCoresSpy).toHaveBeenCalledTimes(1);
595+
596+
const userTransaction = await getTransactionForProviderId({
597+
con,
598+
providerId: '220698',
599+
});
600+
601+
expect(userTransaction).toEqual({
602+
id: expect.any(String),
603+
createdAt: expect.any(Date),
604+
fee: 0,
605+
flags: {
606+
providerId: '220698',
607+
error: 'Insufficient Cores balance.',
608+
},
609+
processor: UserTransactionProcessor.AppleStoreKit,
610+
productId: null,
611+
receiverId: 'storekit-user-c-1',
612+
request: {},
613+
senderId: null,
614+
status: 1,
615+
updatedAt: expect.any(Date),
616+
value: 100,
617+
valueIncFees: 100,
618+
});
619+
});
558620
});
559621
});

__tests__/routes/webhooks/paddle.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { DataSource } from 'typeorm';
22
import createOrGetConnection from '../../../src/db';
3-
import { createMockNjordTransport, saveFixtures } from '../../helpers';
3+
import {
4+
createMockNjordErrorTransport,
5+
createMockNjordTransport,
6+
saveFixtures,
7+
} from '../../helpers';
48
import { SubscriptionProvider, User } from '../../../src/entity';
59
import { usersFixture } from '../../fixture';
610

@@ -26,7 +30,7 @@ import { logger } from '../../../src/logger';
2630
import { CoresRole } from '../../../src/types';
2731
import * as njordCommon from '../../../src/common/njord';
2832
import { createClient } from '@connectrpc/connect';
29-
import { Credits } from '@dailydotdev/schema';
33+
import { Credits, TransferStatus } from '@dailydotdev/schema';
3034

3135
let con: DataSource;
3236

@@ -586,4 +590,48 @@ describe('cores product', () => {
586590
expect(userTransaction!.status).toBe(202);
587591
expect(userTransaction!.flags.error).toBeNull();
588592
});
593+
594+
it('transaction njord error on completed', async () => {
595+
jest.spyOn(njordCommon, 'getNjordClient').mockImplementation(() =>
596+
createClient(
597+
Credits,
598+
createMockNjordErrorTransport({
599+
errorStatus: TransferStatus.INSUFFICIENT_FUNDS,
600+
errorMessage: 'Insufficient funds',
601+
}),
602+
),
603+
);
604+
605+
const purchaseCoresSpy = jest.spyOn(njordCommon, 'purchaseCores');
606+
607+
await processTransactionCompleted({ event: coresTransactionCompleted });
608+
609+
expect(purchaseCoresSpy).toHaveBeenCalledTimes(1);
610+
611+
const userTransaction = await getTransactionForProviderId({
612+
con,
613+
providerId: coresTransactionCompleted.data.id,
614+
});
615+
616+
expect(userTransaction).not.toBeNull();
617+
618+
expect(userTransaction).toEqual({
619+
id: expect.any(String),
620+
createdAt: expect.any(Date),
621+
fee: 0,
622+
flags: {
623+
providerId: 'txn_01jrwyswhztmre55nbd7d09qvp',
624+
error: 'Insufficient Cores balance.',
625+
},
626+
processor: 'paddle',
627+
productId: null,
628+
receiverId: 'whcp-1',
629+
request: {},
630+
senderId: null,
631+
status: 1,
632+
updatedAt: expect.any(Date),
633+
value: 600,
634+
valueIncFees: 600,
635+
});
636+
});
589637
});

src/routes/webhooks/paddle.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { logger } from '../../logger';
2929
import {
3030
AnalyticsEventName,
3131
sendAnalyticsEvent,
32+
TargetType,
3233
} from '../../integrations/analytics';
3334
import { JsonContains, type DataSource, type EntityManager } from 'typeorm';
3435
import {
@@ -278,7 +279,9 @@ const logPaddleAnalyticsEvent = async (
278279
app_platform: 'api',
279280
user_id: userId,
280281
extra: JSON.stringify(getAnalyticsExtra(data)),
281-
target_type: isCoreTransaction({ event }) ? 'credits' : 'plus',
282+
target_type: isCoreTransaction({ event })
283+
? TargetType.Credits
284+
: TargetType.Plus,
282285
},
283286
]);
284287
};

0 commit comments

Comments
 (0)