|
1 |
| -const { expectEvent } = require('@openzeppelin/test-helpers') |
| 1 | +const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers') |
2 | 2 |
|
3 | 3 | // contracts
|
4 |
| -const ServiceRegisty = artifacts.require('./ServiceRegistry.sol') |
| 4 | +const deployment = require('./lib/deployment') |
5 | 5 |
|
6 |
| -contract('Service Registry', accounts => { |
7 |
| - let deployedServiceRegistry |
8 |
| - const governor = accounts[0] |
9 |
| - const indexer = accounts[1] |
| 6 | +contract('Service Registry', ([governor, indexer]) => { |
| 7 | + beforeEach(async function() { |
| 8 | + this.serviceRegisty = await deployment.deployServiceRegistry(governor) |
| 9 | + this.geohash = '69y7hdrhm6mp' |
10 | 10 |
|
11 |
| - before(async () => { |
12 |
| - // deploy ServiceRegistry contract |
13 |
| - deployedServiceRegistry = await ServiceRegisty.new( |
14 |
| - governor, // governor |
15 |
| - { from: governor }, |
16 |
| - ) |
17 |
| - }) |
| 11 | + this.shouldRegister = async function(url, geohash) { |
| 12 | + // Register the indexer service |
| 13 | + const { logs } = await this.serviceRegisty.register(url, geohash, { |
| 14 | + from: indexer, |
| 15 | + }) |
18 | 16 |
|
19 |
| - it('...should allow setting URL with arbitrary length string', async () => { |
20 |
| - const url = 'https://192.168.2.1/' |
| 17 | + // Updated state |
| 18 | + const indexerService = await this.serviceRegisty.services(indexer) |
| 19 | + expect(indexerService.url).to.be.eq(url) |
| 20 | + expect(indexerService.geohash).to.be.eq(geohash) |
21 | 21 |
|
22 |
| - // Set the url |
23 |
| - const { logs } = await deployedServiceRegistry.setUrl(url, { |
24 |
| - from: indexer, |
25 |
| - }) |
| 22 | + // Event emitted |
| 23 | + expectEvent.inLogs(logs, 'ServiceRegistered', { |
| 24 | + indexer: indexer, |
| 25 | + url: url, |
| 26 | + geohash: geohash, |
| 27 | + }) |
| 28 | + } |
| 29 | + }) |
26 | 30 |
|
27 |
| - expectEvent.inLogs(logs, 'ServiceUrlSet', { |
28 |
| - serviceProvider: indexer, |
29 |
| - urlString: url, |
| 31 | + describe('register()', function() { |
| 32 | + it('should allow registering', async function() { |
| 33 | + const url = 'https://192.168.2.1/' |
| 34 | + await this.shouldRegister(url, this.geohash) |
30 | 35 | })
|
31 |
| - }) |
32 | 36 |
|
33 |
| - it('...should allow setting URL with a very long string', async () => { |
34 |
| - const url = 'https://' + 'a'.repeat(125) + '.com' |
| 37 | + it('should allow registering with a very long string', async function() { |
| 38 | + const url = 'https://' + 'a'.repeat(125) + '.com' |
| 39 | + await this.shouldRegister(url, this.geohash) |
| 40 | + }) |
35 | 41 |
|
36 |
| - // Set the url |
37 |
| - const { logs } = await deployedServiceRegistry.setUrl(url, { |
38 |
| - from: indexer, |
| 42 | + it('should allow updating a registration', async function() { |
| 43 | + const [url1, geohash1] = ['https://thegraph.com', '69y7hdrhm6mp'] |
| 44 | + const [url2, geohash2] = ['https://192.168.0.1', 'dr5regw2z6y'] |
| 45 | + await this.shouldRegister(url1, geohash1) |
| 46 | + await this.shouldRegister(url2, geohash2) |
39 | 47 | })
|
40 | 48 |
|
41 |
| - expectEvent.inLogs(logs, 'ServiceUrlSet', { |
42 |
| - serviceProvider: indexer, |
43 |
| - urlString: url, |
| 49 | + it('reject registering empty URL', async function() { |
| 50 | + await expectRevert( |
| 51 | + this.serviceRegisty.register('', '', { |
| 52 | + from: indexer, |
| 53 | + }), |
| 54 | + 'Service must specify a URL', |
| 55 | + ) |
44 | 56 | })
|
45 | 57 | })
|
46 | 58 |
|
47 |
| - it('...should allow setting multiple graph bootstrap indexer URLs', async () => { |
48 |
| - const url = 'https://192.168.2.1/' |
49 |
| - const urlBytes = web3.utils.utf8ToHex(url) |
50 |
| - |
51 |
| - const indexers = accounts.slice(5, 8) |
| 59 | + describe('unregister()', function() { |
| 60 | + it('should unregister existing registration', async function() { |
| 61 | + const url = 'https://thegraph.com' |
52 | 62 |
|
53 |
| - for (let i = 0; i < 3; i++) { |
54 |
| - // Set the url, only governor can |
55 |
| - const { logs } = await deployedServiceRegistry.setBootstrapIndexerURL(indexers[i], url, { |
56 |
| - from: governor, |
| 63 | + // Register the indexer service |
| 64 | + await this.serviceRegisty.register(url, this.geohash, { |
| 65 | + from: indexer, |
57 | 66 | })
|
58 | 67 |
|
59 |
| - // Verify that the the ServiceUrlSet event is emitted |
60 |
| - expectEvent.inLogs(logs, 'ServiceUrlSet', { |
61 |
| - serviceProvider: indexers[i], |
62 |
| - urlString: url, |
| 68 | + // Unregister the indexer service |
| 69 | + const { logs } = await this.serviceRegisty.unregister({ from: indexer }) |
| 70 | + |
| 71 | + // Event emitted |
| 72 | + expectEvent.inLogs(logs, 'ServiceUnregistered', { |
| 73 | + indexer: indexer, |
63 | 74 | })
|
| 75 | + }) |
64 | 76 |
|
65 |
| - // Verify that the indexer URL has been updated |
66 |
| - const indexerUrlBytes = await deployedServiceRegistry.bootstrapIndexerURLs(indexers[i]) |
67 |
| - assert(indexerUrlBytes === urlBytes, `Indexer ${i} URL was not set to ${url}`) |
68 |
| - } |
| 77 | + it('reject unregister non-existing registration', async function() { |
| 78 | + await expectRevert( |
| 79 | + this.serviceRegisty.unregister({ from: indexer }), |
| 80 | + 'Service already unregistered', |
| 81 | + ) |
| 82 | + }) |
69 | 83 | })
|
70 | 84 | })
|
0 commit comments