index.test.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import nock from 'nock';
  2. import { BankClient } from '../src/index';
  3. import { StorageNode } from '../src/storage-node';
  4. import { IWebClient } from '../src/webclient';
  5. import { WebClientNode } from '../src/webclient-node';
  6. import CryptoNode from './../src/crypto-node';
  7. import { IWebClientOptions } from './../src/webclient-options';
  8. describe('BankClient', () => {
  9. const storage = new StorageNode('bankConfig');
  10. const cryptoNode = new CryptoNode();
  11. test('should successfully bootstrap', async () => {
  12. class FakeWebClient implements IWebClient {
  13. public requestJSON(options: object): Promise<object> {
  14. throw new Error("Method not implemented.");
  15. }
  16. public request(options: IWebClientOptions): Promise<string> {
  17. throw new Error("Method not implemented.");
  18. }
  19. }
  20. const bankClient = new BankClient('/urlBase', '/ipfsBase', storage, new FakeWebClient(), cryptoNode);
  21. const strapped = await bankClient.bootstrap();
  22. expect(strapped).toBeDefined();
  23. });
  24. test('should return hardcoded nonce', async () => {
  25. class FakeWebClient implements IWebClient {
  26. public requestJSON(options: object): Promise<object> {
  27. throw new Error("Method not implemented.");
  28. }
  29. public request(options: IWebClientOptions): Promise<string> {
  30. return Promise.resolve('42');
  31. }
  32. }
  33. const bankClient = new BankClient('/urlBase', '/ipfsBase', storage, new FakeWebClient(), cryptoNode);
  34. await bankClient.bootstrap();
  35. const nonce = await bankClient.getNonce();
  36. expect(nonce).toEqual(42);
  37. });
  38. describe('get nonce', () => {
  39. test('should work with real http client', async () => {
  40. const webClient = new WebClientNode();
  41. const bankClient = new BankClient('http://127.0.0.1:8000', '/ipfsBase', storage, webClient, cryptoNode);
  42. await bankClient.bootstrap();
  43. nock('http://127.0.0.1:8000')
  44. .get('/bank/nonce')
  45. .reply(200, "42");
  46. const nonce = await bankClient.getNonce();
  47. expect(nonce).toEqual(42);
  48. });
  49. });
  50. describe('get balance', () => {
  51. test('should work with real http client', async () => {
  52. const webClient = new WebClientNode();
  53. const bankClient = new BankClient('http://127.0.0.1:8000', '/ipfsBase', storage, webClient, cryptoNode);
  54. await bankClient.bootstrap();
  55. nock('http://127.0.0.1:8000')
  56. .post('/bank/getbalance')
  57. .reply(200, {
  58. balance: 42
  59. });
  60. nock('http://127.0.0.1:8000')
  61. .get('/bank/nonce')
  62. .reply(200, "42");
  63. const balance = await bankClient.getBalance();
  64. expect(balance).toEqual(42);
  65. });
  66. });
  67. });