|
| 1 | +import { logger } from '@hubspot/local-dev-lib/logger'; |
| 2 | +import { initiateSync } from '@hubspot/local-dev-lib/api/sandboxSync'; |
| 3 | +import { getAccountId } from '@hubspot/local-dev-lib/config'; |
| 4 | +import { HUBSPOT_ACCOUNT_TYPES } from '@hubspot/local-dev-lib/constants/config'; |
| 5 | +import { Environment } from '@hubspot/local-dev-lib/types/Config'; |
| 6 | +import { makeHubSpotHttpError } from '../testUtils'; |
| 7 | +import { getAvailableSyncTypes } from '../sandboxes'; |
| 8 | +import { syncSandbox } from '../sandboxSync'; |
| 9 | +import SpinniesManager from '../ui/SpinniesManager'; |
| 10 | + |
| 11 | +jest.mock('@hubspot/local-dev-lib/logger'); |
| 12 | +jest.mock('@hubspot/local-dev-lib/api/sandboxSync'); |
| 13 | +jest.mock('@hubspot/local-dev-lib/config'); |
| 14 | +jest.mock('../sandboxes'); |
| 15 | +jest.mock('../ui/SpinniesManager'); |
| 16 | + |
| 17 | +const mockedLogger = logger as jest.Mocked<typeof logger>; |
| 18 | +const mockedInitiateSync = initiateSync as jest.Mock; |
| 19 | +const mockedGetAccountId = getAccountId as jest.Mock; |
| 20 | +const mockedGetAvailableSyncTypes = getAvailableSyncTypes as jest.Mock; |
| 21 | +const mockedSpinniesInit = SpinniesManager.init as jest.Mock; |
| 22 | +const mockedSpinniesAdd = SpinniesManager.add as jest.Mock; |
| 23 | +const mockedSpinniesSucceed = SpinniesManager.succeed as jest.Mock; |
| 24 | +const mockedSpinniesFail = SpinniesManager.fail as jest.Mock; |
| 25 | + |
| 26 | +describe('lib/sandboxSync', () => { |
| 27 | + const mockEnv = 'qa' as Environment; |
| 28 | + const mockParentAccount = { |
| 29 | + name: 'Parent Account', |
| 30 | + portalId: 123, |
| 31 | + accountType: HUBSPOT_ACCOUNT_TYPES.STANDARD_SANDBOX, |
| 32 | + env: mockEnv, |
| 33 | + }; |
| 34 | + const mockChildAccount = { |
| 35 | + name: 'Child Account', |
| 36 | + portalId: 456, |
| 37 | + accountType: HUBSPOT_ACCOUNT_TYPES.DEVELOPMENT_SANDBOX, |
| 38 | + env: mockEnv, |
| 39 | + }; |
| 40 | + const mockSyncTasks = [{ type: 'mock-sync-type' }]; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + mockedGetAccountId |
| 44 | + .mockReturnValueOnce(mockChildAccount.portalId) |
| 45 | + .mockReturnValueOnce(mockParentAccount.portalId); |
| 46 | + mockedGetAvailableSyncTypes.mockResolvedValue(mockSyncTasks); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('syncSandbox()', () => { |
| 50 | + it('successfully syncs a sandbox with provided sync tasks', async () => { |
| 51 | + mockedInitiateSync.mockResolvedValue({ status: 'SUCCESS' }); |
| 52 | + |
| 53 | + await syncSandbox( |
| 54 | + mockChildAccount, |
| 55 | + mockParentAccount, |
| 56 | + mockEnv, |
| 57 | + mockSyncTasks |
| 58 | + ); |
| 59 | + |
| 60 | + expect(mockedSpinniesInit).toHaveBeenCalled(); |
| 61 | + expect(mockedSpinniesAdd).toHaveBeenCalled(); |
| 62 | + expect(mockedInitiateSync).toHaveBeenCalledWith( |
| 63 | + mockParentAccount.portalId, |
| 64 | + mockChildAccount.portalId, |
| 65 | + mockSyncTasks, |
| 66 | + mockChildAccount.portalId |
| 67 | + ); |
| 68 | + expect(mockedSpinniesSucceed).toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('fetches sync types when no tasks are provided', async () => { |
| 72 | + mockedInitiateSync.mockResolvedValue({ status: 'SUCCESS' }); |
| 73 | + |
| 74 | + await syncSandbox(mockChildAccount, mockParentAccount, mockEnv, []); |
| 75 | + |
| 76 | + expect(mockedGetAvailableSyncTypes).toHaveBeenCalledWith( |
| 77 | + mockParentAccount, |
| 78 | + mockChildAccount |
| 79 | + ); |
| 80 | + |
| 81 | + expect(mockedGetAvailableSyncTypes).toHaveBeenCalledWith( |
| 82 | + mockParentAccount, |
| 83 | + mockChildAccount |
| 84 | + ); |
| 85 | + expect(mockedInitiateSync).toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('throws error when account IDs are missing', async () => { |
| 89 | + mockedGetAccountId.mockReset(); |
| 90 | + mockedGetAccountId.mockReturnValue(null); |
| 91 | + |
| 92 | + const errorRegex = new RegExp( |
| 93 | + `Couldn't sync ${mockChildAccount.portalId} because your account has been removed from` |
| 94 | + ); |
| 95 | + |
| 96 | + await expect( |
| 97 | + syncSandbox(mockChildAccount, mockParentAccount, mockEnv, mockSyncTasks) |
| 98 | + ).rejects.toThrow(errorRegex); |
| 99 | + }); |
| 100 | + |
| 101 | + it('handles sync in progress error', async () => { |
| 102 | + const error = makeHubSpotHttpError('', { |
| 103 | + status: 429, |
| 104 | + data: { |
| 105 | + category: 'RATE_LIMITS', |
| 106 | + subCategory: 'sandboxes-sync-api.SYNC_IN_PROGRESS', |
| 107 | + }, |
| 108 | + }); |
| 109 | + mockedInitiateSync.mockRejectedValue(error); |
| 110 | + |
| 111 | + await expect( |
| 112 | + syncSandbox(mockChildAccount, mockParentAccount, mockEnv, mockSyncTasks) |
| 113 | + ).rejects.toEqual(error); |
| 114 | + |
| 115 | + expect(mockedSpinniesFail).toHaveBeenCalled(); |
| 116 | + expect(mockedLogger.error).toHaveBeenCalledWith( |
| 117 | + expect.stringMatching( |
| 118 | + /Couldn't run the sync because there's another sync in progress/ |
| 119 | + ) |
| 120 | + ); |
| 121 | + }); |
| 122 | + |
| 123 | + it('handles invalid user error', async () => { |
| 124 | + const error = makeHubSpotHttpError('', { |
| 125 | + status: 403, |
| 126 | + data: { |
| 127 | + category: 'BANNED', |
| 128 | + subCategory: 'sandboxes-sync-api.SYNC_NOT_ALLOWED_INVALID_USER', |
| 129 | + }, |
| 130 | + }); |
| 131 | + mockedInitiateSync.mockRejectedValue(error); |
| 132 | + |
| 133 | + await expect( |
| 134 | + syncSandbox(mockChildAccount, mockParentAccount, mockEnv, mockSyncTasks) |
| 135 | + ).rejects.toEqual(error); |
| 136 | + |
| 137 | + expect(mockedSpinniesFail).toHaveBeenCalled(); |
| 138 | + expect(mockedLogger.error).toHaveBeenCalledWith( |
| 139 | + expect.stringMatching(/because your account has been removed from/) |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + it('handles not super admin error', async () => { |
| 144 | + const error = makeHubSpotHttpError('', { |
| 145 | + status: 403, |
| 146 | + data: { |
| 147 | + category: 'BANNED', |
| 148 | + subCategory: 'sandboxes-sync-api.SYNC_NOT_ALLOWED_INVALID_USERID', |
| 149 | + }, |
| 150 | + }); |
| 151 | + mockedInitiateSync.mockRejectedValue(error); |
| 152 | + |
| 153 | + await expect( |
| 154 | + syncSandbox(mockChildAccount, mockParentAccount, mockEnv, mockSyncTasks) |
| 155 | + ).rejects.toEqual(error); |
| 156 | + |
| 157 | + expect(mockedSpinniesFail).toHaveBeenCalled(); |
| 158 | + expect(mockedLogger.error).toHaveBeenCalledWith( |
| 159 | + expect.stringMatching( |
| 160 | + /Couldn't run the sync because you are not a super admin in/ |
| 161 | + ) |
| 162 | + ); |
| 163 | + }); |
| 164 | + |
| 165 | + it('handles sandbox not found error', async () => { |
| 166 | + const error = makeHubSpotHttpError('', { |
| 167 | + status: 404, |
| 168 | + data: { |
| 169 | + category: 'OBJECT_NOT_FOUND', |
| 170 | + subCategory: 'SandboxErrors.SANDBOX_NOT_FOUND', |
| 171 | + }, |
| 172 | + }); |
| 173 | + mockedInitiateSync.mockRejectedValue(error); |
| 174 | + |
| 175 | + await expect( |
| 176 | + syncSandbox(mockChildAccount, mockParentAccount, mockEnv, mockSyncTasks) |
| 177 | + ).rejects.toEqual(error); |
| 178 | + |
| 179 | + expect(mockedSpinniesFail).toHaveBeenCalled(); |
| 180 | + expect(mockedLogger.error).toHaveBeenCalledWith( |
| 181 | + expect.stringMatching(/may have been deleted through the UI/) |
| 182 | + ); |
| 183 | + }); |
| 184 | + |
| 185 | + it('displays slim info message when specified', async () => { |
| 186 | + mockedInitiateSync.mockResolvedValue({ status: 'SUCCESS' }); |
| 187 | + |
| 188 | + await syncSandbox( |
| 189 | + mockChildAccount, |
| 190 | + mockParentAccount, |
| 191 | + mockEnv, |
| 192 | + mockSyncTasks, |
| 193 | + true |
| 194 | + ); |
| 195 | + |
| 196 | + expect(mockedLogger.info).not.toHaveBeenCalled(); |
| 197 | + expect(mockedSpinniesSucceed).toHaveBeenCalledWith( |
| 198 | + 'sandboxSync', |
| 199 | + expect.objectContaining({ |
| 200 | + text: expect.stringMatching( |
| 201 | + /Initiated sync of object definitions from production to / |
| 202 | + ), |
| 203 | + }) |
| 204 | + ); |
| 205 | + }); |
| 206 | + }); |
| 207 | +}); |
0 commit comments