|
| 1 | +const request = require('request'); |
| 2 | +const { Node } = require('../../node/node'); |
| 3 | +const { createAPI } = require('../index'); |
| 4 | + |
| 5 | +jest.mock('../../node/iri'); |
| 6 | + |
| 7 | +const API_DATA = [ |
| 8 | + 'config', 'connectedPeers', 'heart', 'iriStats', 'isIRIHealthy', 'name', 'peerStats', 'ready', |
| 9 | + 'totalPeers','version' |
| 10 | +]; |
| 11 | + |
| 12 | +describe('API', () => { |
| 13 | + it('should get node info correctly', (done) => { |
| 14 | + const node = new Node({ silent: true, temporary: true, port: 16601 }); |
| 15 | + const server = createAPI({ |
| 16 | + node, |
| 17 | + apiPort: 12345, |
| 18 | + apiHostname: 'localhost' |
| 19 | + }); |
| 20 | + node.start().then(() => { |
| 21 | + request.get('http://localhost:12345/', (err, resp, body) => { |
| 22 | + const answer = JSON.parse(body); |
| 23 | + const keys = Object.keys(answer); |
| 24 | + keys.sort(); |
| 25 | + expect(keys).toEqual(API_DATA); |
| 26 | + server.close(); |
| 27 | + node.end().then(done); |
| 28 | + }); |
| 29 | + }) |
| 30 | + }); |
| 31 | + |
| 32 | + it('should deny public access to protected when no password set', (done) => { |
| 33 | + const node = new Node({ silent: true, temporary: true, port: 16602 }); |
| 34 | + const server = createAPI({ |
| 35 | + node, |
| 36 | + apiPort: 12345, |
| 37 | + apiHostname: 'localhost', |
| 38 | + username: 'pass', |
| 39 | + password: 'pass' |
| 40 | + }); |
| 41 | + node.start().then(() => { |
| 42 | + request.get('http://localhost:12345/', (err, resp, body) => { |
| 43 | + expect(resp.statusCode).toEqual(401); |
| 44 | + expect(body).toBeFalsy; |
| 45 | + server.close(); |
| 46 | + node.end().then(done); |
| 47 | + }); |
| 48 | + }) |
| 49 | + }); |
| 50 | + |
| 51 | + it('should deny public access to protected when wrong pass', (done) => { |
| 52 | + const node = new Node({ silent: true, temporary: true, port: 16603 }); |
| 53 | + const server = createAPI({ |
| 54 | + node, |
| 55 | + apiPort: 12345, |
| 56 | + apiHostname: 'localhost', |
| 57 | + username: 'pass', |
| 58 | + password: 'pass' |
| 59 | + }); |
| 60 | + node.start().then(() => { |
| 61 | + request.get({ |
| 62 | + url: 'http://localhost:12345/', |
| 63 | + auth: { |
| 64 | + user: 'pass', |
| 65 | + pass: 'nopass' |
| 66 | + } |
| 67 | + }, (err, resp, body) => { |
| 68 | + expect(resp.statusCode).toEqual(401); |
| 69 | + expect(body).toBeFalsy; |
| 70 | + server.close(); |
| 71 | + node.end().then(done); |
| 72 | + }); |
| 73 | + }) |
| 74 | + }); |
| 75 | + |
| 76 | + it('should allow access to protected when auth ok', (done) => { |
| 77 | + const node = new Node({ silent: true, temporary: true, port: 16604 }); |
| 78 | + const server = createAPI({ |
| 79 | + node, |
| 80 | + apiPort: 12345, |
| 81 | + apiHostname: 'localhost', |
| 82 | + username: 'pass', |
| 83 | + password: 'pass' |
| 84 | + }); |
| 85 | + node.start().then(() => { |
| 86 | + request.get({ |
| 87 | + url: 'http://localhost:12345/', |
| 88 | + auth: { |
| 89 | + user: 'pass', |
| 90 | + pass: 'pass' |
| 91 | + } |
| 92 | + }, (err, resp, body) => { |
| 93 | + const answer = JSON.parse(body); |
| 94 | + const keys = Object.keys(answer); |
| 95 | + keys.sort(); |
| 96 | + expect(keys).toEqual(API_DATA); |
| 97 | + server.close(); |
| 98 | + node.end().then(done); |
| 99 | + }); |
| 100 | + }) |
| 101 | + }); |
| 102 | + |
| 103 | + it('should get peer stats info correctly', (done) => { |
| 104 | + const node = new Node({ silent: true, temporary: true, port: 16605 }); |
| 105 | + const server = createAPI({ |
| 106 | + node, |
| 107 | + apiPort: 12346, |
| 108 | + apiHostname: 'localhost' |
| 109 | + }); |
| 110 | + node.start().then(() => { |
| 111 | + request.get('http://localhost:12346/peer-stats', (err, resp, body) => { |
| 112 | + const summary = JSON.parse(body); |
| 113 | + expect(summary.newNodes).toBeTruthy; |
| 114 | + expect(summary.activeNodes).toBeTruthy; |
| 115 | + server.close(); |
| 116 | + node.end().then(done); |
| 117 | + }); |
| 118 | + }) |
| 119 | + }); |
| 120 | + |
| 121 | + it('should get peers info correctly', (done) => { |
| 122 | + const node = new Node({ silent: true, temporary: true, port: 16606 }); |
| 123 | + const server = createAPI({ |
| 124 | + node, |
| 125 | + apiPort: 12347, |
| 126 | + apiHostname: 'localhost' |
| 127 | + }); |
| 128 | + node.start().then(() => { |
| 129 | + request.get('http://localhost:12347/peers', (err, resp, body) => { |
| 130 | + const peers = JSON.parse(body); |
| 131 | + expect(Array.isArray(peers)).toBeTruthy; |
| 132 | + server.close(); |
| 133 | + node.end().then(done); |
| 134 | + }); |
| 135 | + }) |
| 136 | + }); |
| 137 | +}); |
0 commit comments