|
| 1 | +import '../../src/env-test'; |
| 2 | +import { ObjectId } from 'mongodb'; |
| 3 | +import { PlanDBScheme, WorkspaceDBScheme } from '@hawk.so/types'; |
| 4 | +import billingNewResolver from '../../src/resolvers/billingNew'; |
| 5 | +import { ResolverContextWithUser } from '../../src/types/graphql'; |
| 6 | + |
| 7 | +// Set environment variables for test |
| 8 | +process.env.JWT_SECRET_BILLING_CHECKSUM = 'checksum_secret'; |
| 9 | +process.env.JWT_SECRET_ACCESS_TOKEN = 'belarus'; |
| 10 | +process.env.JWT_SECRET_REFRESH_TOKEN = 'abacaba'; |
| 11 | +process.env.JWT_SECRET_PROJECT_TOKEN = 'qwerty'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Creates test data and mocks for composePayment tests |
| 15 | + */ |
| 16 | +function createComposePaymentTestSetup(options: { |
| 17 | + isTariffPlanExpired?: boolean; |
| 18 | + isBlocked?: boolean; |
| 19 | + lastChargeDate?: Date; |
| 20 | + planMonthlyCharge?: number; |
| 21 | + planCurrency?: string; |
| 22 | +}) { |
| 23 | + const { |
| 24 | + isTariffPlanExpired = false, |
| 25 | + isBlocked = false, |
| 26 | + lastChargeDate = new Date(), |
| 27 | + planMonthlyCharge = 1000, |
| 28 | + planCurrency = 'RUB' |
| 29 | + } = options; |
| 30 | + |
| 31 | + const userId = new ObjectId().toString(); |
| 32 | + const workspaceId = new ObjectId().toString(); |
| 33 | + const planId = new ObjectId().toString(); |
| 34 | + |
| 35 | + const plan: PlanDBScheme = { |
| 36 | + _id: new ObjectId(planId), |
| 37 | + name: 'Test Plan', |
| 38 | + monthlyCharge: planMonthlyCharge, |
| 39 | + monthlyChargeCurrency: planCurrency, |
| 40 | + eventsLimit: 1000, |
| 41 | + isDefault: false, |
| 42 | + isHidden: false, |
| 43 | + }; |
| 44 | + |
| 45 | + const workspace: WorkspaceDBScheme = { |
| 46 | + _id: new ObjectId(workspaceId), |
| 47 | + name: 'Test Workspace', |
| 48 | + accountId: 'test-account-id', |
| 49 | + balance: 0, |
| 50 | + billingPeriodEventsCount: 0, |
| 51 | + isBlocked, |
| 52 | + lastChargeDate, |
| 53 | + tariffPlanId: new ObjectId(planId), |
| 54 | + inviteHash: 'test-invite-hash', |
| 55 | + subscriptionId: undefined, |
| 56 | + }; |
| 57 | + |
| 58 | + // Mock workspaces factory |
| 59 | + const mockWorkspacesFactory = { |
| 60 | + findById: jest.fn().mockResolvedValue({ |
| 61 | + ...workspace, |
| 62 | + getMemberInfo: jest.fn().mockResolvedValue({ isAdmin: true }), |
| 63 | + isTariffPlanExpired: jest.fn().mockReturnValue(isTariffPlanExpired), |
| 64 | + isBlocked, |
| 65 | + }), |
| 66 | + }; |
| 67 | + |
| 68 | + // Mock plans factory |
| 69 | + const mockPlansFactory = { |
| 70 | + findById: jest.fn().mockResolvedValue(plan), |
| 71 | + }; |
| 72 | + |
| 73 | + const mockContext: ResolverContextWithUser = { |
| 74 | + user: { |
| 75 | + id: userId, |
| 76 | + accessTokenExpired: false, |
| 77 | + }, |
| 78 | + factories: { |
| 79 | + workspacesFactory: mockWorkspacesFactory as any, |
| 80 | + plansFactory: mockPlansFactory as any, |
| 81 | + usersFactory: {} as any, |
| 82 | + projectsFactory: {} as any, |
| 83 | + businessOperationsFactory: {} as any, |
| 84 | + }, |
| 85 | + }; |
| 86 | + |
| 87 | + return { |
| 88 | + userId, |
| 89 | + workspaceId, |
| 90 | + planId, |
| 91 | + plan, |
| 92 | + workspace, |
| 93 | + mockContext, |
| 94 | + mockWorkspacesFactory, |
| 95 | + mockPlansFactory, |
| 96 | + }; |
| 97 | +} |
| 98 | + |
| 99 | +describe('GraphQLBillingNew', () => { |
| 100 | + describe('composePayment', () => { |
| 101 | + it('should return isCardLinkOperation = false in case of expired tariff plan', async () => { |
| 102 | + // Create 2 months ago date |
| 103 | + const expiredDate = new Date(); |
| 104 | + expiredDate.setMonth(expiredDate.getMonth() - 2); |
| 105 | + |
| 106 | + const { mockContext, planId, workspaceId } = createComposePaymentTestSetup({ |
| 107 | + isTariffPlanExpired: true, |
| 108 | + isBlocked: false, |
| 109 | + lastChargeDate: expiredDate, |
| 110 | + }); |
| 111 | + |
| 112 | + // Call composePayment resolver |
| 113 | + const result = await billingNewResolver.Query.composePayment( |
| 114 | + undefined, |
| 115 | + { |
| 116 | + input: { |
| 117 | + workspaceId, |
| 118 | + tariffPlanId: planId, |
| 119 | + shouldSaveCard: false, |
| 120 | + }, |
| 121 | + }, |
| 122 | + mockContext |
| 123 | + ); |
| 124 | + |
| 125 | + expect(result.isCardLinkOperation).toBe(false); |
| 126 | + |
| 127 | + // Check that nextPaymentDate is one month from now |
| 128 | + const oneMonthFromNow = new Date(); |
| 129 | + |
| 130 | + oneMonthFromNow.setMonth(oneMonthFromNow.getMonth() + 1); |
| 131 | + |
| 132 | + const oneMonthFromNowStr = oneMonthFromNow.toISOString().split('T')[0]; |
| 133 | + const nextPaymentDateStr = result.nextPaymentDate.toISOString().split('T')[0]; |
| 134 | + |
| 135 | + expect(nextPaymentDateStr).toBe(oneMonthFromNowStr); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should return isCardLinkOperation = true in case of active tariff plan', async () => { |
| 139 | + // Create 2 days ago date |
| 140 | + const lastChargeDate = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000); |
| 141 | + |
| 142 | + const { mockContext, planId, workspaceId, workspace } = createComposePaymentTestSetup({ |
| 143 | + isTariffPlanExpired: false, |
| 144 | + isBlocked: false, |
| 145 | + lastChargeDate, |
| 146 | + }); |
| 147 | + |
| 148 | + const result = await billingNewResolver.Query.composePayment( |
| 149 | + undefined, |
| 150 | + { |
| 151 | + input: { |
| 152 | + workspaceId, |
| 153 | + tariffPlanId: planId, |
| 154 | + shouldSaveCard: false, |
| 155 | + }, |
| 156 | + }, |
| 157 | + mockContext |
| 158 | + ); |
| 159 | + |
| 160 | + expect(result.isCardLinkOperation).toBe(true); |
| 161 | + |
| 162 | + const oneMonthFromLastChargeDate = new Date(workspace.lastChargeDate); |
| 163 | + oneMonthFromLastChargeDate.setMonth(oneMonthFromLastChargeDate.getMonth() + 1); |
| 164 | + |
| 165 | + const oneMonthFromLastChargeDateStr = oneMonthFromLastChargeDate.toISOString().split('T')[0]; |
| 166 | + const nextPaymentDateStr = result.nextPaymentDate.toISOString().split('T')[0]; |
| 167 | + expect(nextPaymentDateStr).toBe(oneMonthFromLastChargeDateStr); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should return isCardLinkOperation = false in case of blocked workspace', async () => { |
| 171 | + const { mockContext, planId, workspaceId } = createComposePaymentTestSetup({ |
| 172 | + isTariffPlanExpired: false, |
| 173 | + isBlocked: true, |
| 174 | + lastChargeDate: new Date(), |
| 175 | + }); |
| 176 | + |
| 177 | + const result = await billingNewResolver.Query.composePayment( |
| 178 | + undefined, |
| 179 | + { |
| 180 | + input: { |
| 181 | + workspaceId, |
| 182 | + tariffPlanId: planId, |
| 183 | + shouldSaveCard: false, |
| 184 | + }, |
| 185 | + }, |
| 186 | + mockContext |
| 187 | + ); |
| 188 | + |
| 189 | + expect(result.isCardLinkOperation).toBe(false); |
| 190 | + |
| 191 | + // Check that nextPaymentDate is one month from now |
| 192 | + const oneMonthFromNow = new Date(); |
| 193 | + |
| 194 | + oneMonthFromNow.setMonth(oneMonthFromNow.getMonth() + 1); |
| 195 | + |
| 196 | + const oneMonthFromNowStr = oneMonthFromNow.toISOString().split('T')[0]; |
| 197 | + const nextPaymentDateStr = result.nextPaymentDate.toISOString().split('T')[0]; |
| 198 | + |
| 199 | + expect(nextPaymentDateStr).toBe(oneMonthFromNowStr); |
| 200 | + }); |
| 201 | + }); |
| 202 | +}) |
0 commit comments