@@ -11,7 +11,7 @@ import {
1111 User ,
1212 UserSubscriptionStatus ,
1313} from '../../../src/entity' ;
14- import { saveFixtures } from '../../helpers' ;
14+ import { createMockNjordTransport , saveFixtures } from '../../helpers' ;
1515import {
1616 NotificationTypeV2 ,
1717 Subtype ,
@@ -23,6 +23,11 @@ import nock from 'nock';
2323import { env } from 'process' ;
2424import { deleteRedisKey , getRedisHash } from '../../../src/redis' ;
2525import { StorageKey } from '../../../src/config' ;
26+ import { createClient } from '@connectrpc/connect' ;
27+ import { Credits } from '@dailydotdev/schema' ;
28+ import * as njordCommon from '../../..//src/common/njord' ;
29+ import { getTransactionForProviderId } from '../../../src/common/paddle' ;
30+ import { UserTransactionProcessor } from '../../../src/entity/user/UserTransaction' ;
2631
2732function createSignedData ( payload ) : string {
2833 const keyPairOptions : ECKeyPairOptions < 'pem' , 'pem' > = {
@@ -407,4 +412,124 @@ describe('POST /webhooks/apple/notifications', () => {
407412 GBP : '0.8' ,
408413 } ) ;
409414 } ) ;
415+
416+ describe ( 'cores' , ( ) => {
417+ const mockTransport = createMockNjordTransport ( ) ;
418+
419+ beforeEach ( async ( ) => {
420+ jest
421+ . spyOn ( njordCommon , 'getNjordClient' )
422+ . mockImplementation ( ( ) => createClient ( Credits , mockTransport ) ) ;
423+
424+ await saveFixtures ( con , User , [
425+ {
426+ id : 'storekit-user-c-1' ,
427+ username : 'storekit-user-c-1' ,
428+ subscriptionFlags : {
429+ appAccountToken : '18138f83-b4d3-456a-831f-1f3f7bcbb0bd' ,
430+ } ,
431+ coresRole : 3 ,
432+ } ,
433+ {
434+ id : 'storekit-user-c-2' ,
435+ username : 'storekit-user-c-2' ,
436+ subscriptionFlags : {
437+ appAccountToken : 'd9db8906-9b8b-44bc-bc35-3ac0515bad0c' ,
438+ } ,
439+ coresRole : 0 ,
440+ } ,
441+ {
442+ id : 'storekit-user-c-3' ,
443+ username : 'storekit-user-c-3' ,
444+ subscriptionFlags : {
445+ appAccountToken : 'edd16b7c-9717-4a2b-8b51-13ed104d7296' ,
446+ } ,
447+ coresRole : 1 ,
448+ } ,
449+ ] ) ;
450+ } ) ;
451+
452+ it ( 'should purchase cores' , async ( ) => {
453+ await request ( app . server )
454+ . post ( '/webhooks/apple/notifications' )
455+ . send ( {
456+ signedPayload : signedPayload ( {
457+ notificationType : NotificationTypeV2 . ONE_TIME_CHARGE ,
458+ data : {
459+ signedTransactionInfo : {
460+ productId : 'cores_100' ,
461+ quantity : 1 ,
462+ type : 'Consumable' ,
463+ appAccountToken : '18138f83-b4d3-456a-831f-1f3f7bcbb0bd' ,
464+ transactionId : '220698' ,
465+ } ,
466+ } ,
467+ } ) ,
468+ } )
469+ . expect ( 200 ) ;
470+
471+ const userTransaction = await getTransactionForProviderId ( {
472+ con,
473+ providerId : '220698' ,
474+ } ) ;
475+
476+ expect ( userTransaction ) . toEqual ( {
477+ id : expect . any ( String ) ,
478+ createdAt : expect . any ( Date ) ,
479+ fee : 0 ,
480+ flags : {
481+ providerId : '220698' ,
482+ } ,
483+ processor : UserTransactionProcessor . AppleStoreKit ,
484+ productId : null ,
485+ receiverId : 'storekit-user-c-1' ,
486+ request : { } ,
487+ senderId : null ,
488+ status : 0 ,
489+ updatedAt : expect . any ( Date ) ,
490+ value : 100 ,
491+ valueIncFees : 100 ,
492+ } ) ;
493+ } ) ;
494+
495+ it ( 'transaction completed throw if user coresRole is none' , async ( ) => {
496+ await request ( app . server )
497+ . post ( '/webhooks/apple/notifications' )
498+ . send ( {
499+ signedPayload : signedPayload ( {
500+ notificationType : NotificationTypeV2 . ONE_TIME_CHARGE ,
501+ data : {
502+ signedTransactionInfo : {
503+ productId : 'cores_100' ,
504+ quantity : 1 ,
505+ type : 'Consumable' ,
506+ appAccountToken : 'd9db8906-9b8b-44bc-bc35-3ac0515bad0c' ,
507+ transactionId : '220698' ,
508+ } ,
509+ } ,
510+ } ) ,
511+ } )
512+ . expect ( 500 ) ;
513+ } ) ;
514+
515+ it ( 'transaction completed throw if user coresRole is readonly' , async ( ) => {
516+ await request ( app . server )
517+ . post ( '/webhooks/apple/notifications' )
518+ . send ( {
519+ signedPayload : signedPayload ( {
520+ notificationType : NotificationTypeV2 . ONE_TIME_CHARGE ,
521+ data : {
522+ signedTransactionInfo : {
523+ productId : 'cores_100' ,
524+ quantity : 1 ,
525+ type : 'Consumable' ,
526+ appAccountToken : 'edd16b7c-9717-4a2b-8b51-13ed104d7296' ,
527+ transactionId : '220698' ,
528+ } ,
529+ } ,
530+ } ) ,
531+ } )
532+ . expect ( 500 ) ;
533+ } ) ;
534+ } ) ;
410535} ) ;
0 commit comments