|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
2 | 2 |
|
| 3 | +import MockAdapter from 'axios-mock-adapter'; |
3 | 4 | import chai, { expect } from 'chai'; |
4 | 5 | import chaiAsPromised from 'chai-as-promised'; |
5 | 6 | import fs from 'fs'; |
6 | 7 | import pino from 'pino'; |
7 | 8 | import { Registry } from 'prom-client'; |
8 | 9 | import sinon from 'sinon'; |
9 | | -import { CacheService } from '../../src/lib/services/cacheService/cacheService'; |
10 | 10 |
|
11 | 11 | import { Relay } from '../../src'; |
| 12 | +import { CacheService } from '../../src/lib/services/cacheService/cacheService'; |
12 | 13 | import { overrideEnvsInMochaDescribe, withOverriddenEnvsInMochaTest } from '../helpers'; |
13 | 14 |
|
14 | 15 | chai.use(chaiAsPromised); |
@@ -103,4 +104,60 @@ describe('Relay', () => { |
103 | 104 | }); |
104 | 105 | }); |
105 | 106 | }); |
| 107 | + |
| 108 | + describe('ensureOperatorHasBalance', function () { |
| 109 | + withOverriddenEnvsInMochaTest({ READ_ONLY: true }, () => { |
| 110 | + it('should never throw', async function () { |
| 111 | + await expect(relay.ensureOperatorHasBalance()).to.not.be.rejectedWith(); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + withOverriddenEnvsInMochaTest({ READ_ONLY: false }, () => { |
| 116 | + let restMock: MockAdapter; |
| 117 | + let operatorId: string; |
| 118 | + |
| 119 | + beforeEach(() => { |
| 120 | + const mirrorNodeInstance = relay.mirrorClient().getMirrorNodeRestInstance(); |
| 121 | + restMock = new MockAdapter(mirrorNodeInstance, { onNoMatch: 'throwException' }); |
| 122 | + // @ts-expect-error: Property 'clientMain' is private and only accessible within class 'Relay' |
| 123 | + const clientMain = relay.clientMain; |
| 124 | + operatorId = clientMain.operatorAccountId!.toString(); |
| 125 | + }); |
| 126 | + |
| 127 | + afterEach(() => { |
| 128 | + restMock.restore(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should not throw when operator has balance', async function () { |
| 132 | + const balance = { |
| 133 | + account: operatorId, |
| 134 | + balance: { |
| 135 | + balance: 99960581137, |
| 136 | + }, |
| 137 | + }; |
| 138 | + restMock.onGet(`accounts/${operatorId}?limit=100`).reply(200, JSON.stringify(balance)); |
| 139 | + await expect(relay.ensureOperatorHasBalance()).to.not.be.rejectedWith(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should throw when operator has no balance', async function () { |
| 143 | + const balance = { |
| 144 | + account: operatorId, |
| 145 | + balance: { |
| 146 | + balance: 0, |
| 147 | + }, |
| 148 | + }; |
| 149 | + restMock.onGet(`accounts/${operatorId}?limit=100`).reply(200, JSON.stringify(balance)); |
| 150 | + |
| 151 | + const message = `Operator account '${operatorId}' has no balance`; |
| 152 | + await expect(relay.ensureOperatorHasBalance()).to.be.rejectedWith(message); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should throw when operator has not been found', async function () { |
| 156 | + restMock.onGet(`accounts/${operatorId}?limit=100`).reply(404, JSON.stringify({})); |
| 157 | + |
| 158 | + const message = `Operator account '${operatorId}' has no balance`; |
| 159 | + await expect(relay.ensureOperatorHasBalance()).to.be.rejectedWith(message); |
| 160 | + }); |
| 161 | + }); |
| 162 | + }); |
106 | 163 | }); |
0 commit comments