UNPKG

stellar-plus

Version:

beta version of stellar-plus, an all-in-one sdk for the Stellar blockchain

146 lines (145 loc) 8.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ const axios_1 = tslib_1.__importDefault(require("axios")); const base_1 = require("../../../stellar-plus/account/base"); const errors_1 = require("../../../stellar-plus/account/base/errors"); const axios_2 = require("../../../stellar-plus/error/helpers/axios"); const network_1 = require("../../../stellar-plus/network"); const default_1 = require("../account-handler/default"); const stellar_sdk_1 = require("@stellar/stellar-sdk"); jest.mock('axios', () => { const originalModule = jest.requireActual('axios'); // eslint-disable-next-line @typescript-eslint/no-unsafe-return return Object.assign(Object.assign({}, originalModule), { get: jest.fn() }); }); const MOCKED_AXIOS_GET = axios_1.default.get; const TESTNET_CONFIG = (0, network_1.TestNet)(); const MOCKED_PK = 'GAUFIAL2LV2OV7EA4NTXZDVPQASGI5Y3EXZV2HQS3UUWMZ7UWJDQURYS'; describe('Base Account Handler', () => { describe('Initialization', () => { it('should initialize the base account handler with a public key', () => { const account = new base_1.AccountBase({ publicKey: MOCKED_PK }); const spyPublicKey = jest.mocked(account.publicKey); expect(account).toBeDefined(); expect(account).toBeInstanceOf(base_1.AccountBase); expect(spyPublicKey).toBe(MOCKED_PK); }); it('should initialize with optional parameters', () => { const mockedHorizonHandler = jest.fn(); const account = new base_1.AccountBase({ publicKey: MOCKED_PK, networkConfig: TESTNET_CONFIG, horizonHandler: mockedHorizonHandler, }); const spyNetworkConfig = jest.mocked(account.networkConfig); const spyHorizonHandler = jest.mocked(account.horizonHandler); expect(account).toBeDefined(); expect(account).toBeInstanceOf(base_1.AccountBase); expect(spyNetworkConfig).toBe(TESTNET_CONFIG); expect(spyHorizonHandler).toBe(mockedHorizonHandler); }); }); describe('Core Functionalities', () => { let account; const mockedLoadAccount = jest.fn(); beforeEach(() => { jest.clearAllMocks(); account = new base_1.AccountBase({ publicKey: MOCKED_PK, networkConfig: TESTNET_CONFIG, horizonHandler: jest.mocked({ loadAccount: mockedLoadAccount, }), }); }); it('should return the public key of the account', () => { expect(account.getPublicKey()).toBe(MOCKED_PK); }); it('should initialize the account with the friendbot', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { MOCKED_AXIOS_GET.mockResolvedValue({ data: 'Success' }); yield account.initializeWithFriendbot(); expect(MOCKED_AXIOS_GET).toHaveBeenCalledExactlyOnceWith(`${TESTNET_CONFIG.friendbotUrl}?addr=${encodeURIComponent(MOCKED_PK)}`); })); it('should load the account balances', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const mockedBalances = [ { asset_type: 'native', balance: '10.0000000', }, ]; mockedLoadAccount.mockResolvedValue({ balances: mockedBalances }); const balances = yield account.getBalances(); expect(balances).toBe(mockedBalances); expect(mockedLoadAccount).toHaveBeenCalledExactlyOnceWith(MOCKED_PK); })); it('verify if a signature is valid', () => { const keypair = stellar_sdk_1.Keypair.random(); const dah = new default_1.DefaultAccountHandlerClient({ networkConfig: TESTNET_CONFIG, secretKey: keypair.secret() }); const data = Buffer.from('Mocked Data'); const signature = dah.signData(data); expect(dah.verifySignature(data, signature)).toBe(true); }); it('verify if a signature is not valid', () => { const keypair = stellar_sdk_1.Keypair.random(); const dah = new default_1.DefaultAccountHandlerClient({ networkConfig: TESTNET_CONFIG, secretKey: keypair.secret() }); const secondDah = new default_1.DefaultAccountHandlerClient({ networkConfig: TESTNET_CONFIG }); const data = Buffer.from('Mocked Data'); const signature = dah.signData(data); expect(secondDah.verifySignature(data, signature)).toBe(false); }); }); describe('Error Handling', () => { let account; const mockedLoadAccount = jest.fn(); beforeEach(() => { jest.clearAllMocks(); account = new base_1.AccountBase({ publicKey: MOCKED_PK, networkConfig: TESTNET_CONFIG, horizonHandler: jest.mocked({ loadAccount: mockedLoadAccount, }), }); }); it('should throw an error if friendbot fails to initialize the account', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const mockedError = new Error('Failed to initialize with friendbot'); MOCKED_AXIOS_GET.mockRejectedValue(mockedError); yield expect(account.initializeWithFriendbot()).rejects.toThrow(errors_1.ABError.failedToCreateAccountWithFriendbotError(mockedError)); })); it('should throw an error if the request to friendbot fails', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const mockedAxiosError = jest.mocked({ message: 'Failed request', type: axios_2.AxiosErrorTypes.AxiosRequestError, request: {}, }); MOCKED_AXIOS_GET.mockRejectedValue(mockedAxiosError); yield expect(account.initializeWithFriendbot()).rejects.toThrow(errors_1.ABError.failedToCreateAccountWithFriendbotError(mockedAxiosError)); })); it('should throw an error if the response from friendbot fails', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const mockedAxiosError = jest.mocked({ message: 'Failed response', type: axios_2.AxiosErrorTypes.AxiosResponseError, response: {}, }); MOCKED_AXIOS_GET.mockRejectedValue(mockedAxiosError); yield expect(account.initializeWithFriendbot()).rejects.toThrow(errors_1.ABError.failedToCreateAccountWithFriendbotError(mockedAxiosError)); })); it('should throw an error if the account balances cannot be loaded', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const mockedError = new Error('Failed to load account balances'); mockedLoadAccount.mockRejectedValue(mockedError); yield expect(account.getBalances()).rejects.toThrow(errors_1.ABError.failedToLoadBalances(mockedError)); expect(mockedLoadAccount).toHaveBeenCalledExactlyOnceWith(MOCKED_PK); })); it('should throw an error if the friendbot is not available', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { account = new base_1.AccountBase({ publicKey: MOCKED_PK }); yield expect(account.initializeWithFriendbot()).rejects.toThrow(errors_1.ABError.friendbotNotAvailableError()); })); it('should throw an error if the horizon handler is not available', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { account = new base_1.AccountBase({ publicKey: MOCKED_PK }); yield expect(account.getBalances()).rejects.toThrow(errors_1.ABError.horizonHandlerNotAvailableError()); })); }); });