Browse Source

fix groups/replicators tests and update controllers to use injected api

Kieran Gibb 2 years ago
parent
commit
a6d302223a
No known key found for this signature in database

+ 8
- 10
app/controllers/replicators/index.js View File

@@ -57,18 +57,16 @@ const ReplicatorsController = module.exports = (api) => {
57 57
     body('name')
58 58
       .not().isEmpty().withMessage('\'name\' required')
59 59
       .custom((name) => !crypto.isKey(name)).withMessage('\'name\' must be below 32 characters')
60
-      .custom((name, { req }) => (
61
-        req.replicators.store.findBy({ name }).then((replicator) => {
62
-          if (replicator) return Promise.reject(`${name} already exists`)
63
-        })
64
-      )),
60
+      .custom(async (name, { req }) => {
61
+        var replicator = await Replicator.findBy({ name })
62
+        if (replicator) throw new Error(`${name} already exists`)
63
+      }),
65 64
     body('address')
66 65
       .custom((address) => !address || crypto.isKey(address)).withMessage('\'address\' must be a 32 byte address')
67
-      .custom((address, { req }) => (
68
-        req.replicators.store.findBy({ address }).then((replicator) => {
69
-          if (replicator) return Promise.reject(`${address} already exists`)
70
-        })
71
-      ))
66
+      .custom(async (address, { req }) => {
67
+        var replicator = await Replicator.findBy({ address })
68
+        if (replicator) throw new Error(`${name} already exists`)
69
+      })
72 70
   ]), async function create (req, res) {
73 71
     try {
74 72
       let replicator = await Replicator.create({

+ 0
- 1
test/controllers/groups/create.test.js View File

@@ -13,7 +13,6 @@ describe('POST /groups', (context) => {
13 13
   context('valid with name as param', (assert, next) => {
14 14
     var storage = tmp()
15 15
     var groups = { store: Store(path.join(storage, 'groups'), Group) }
16
-
17 16
     var name = 'magma'
18 17
 
19 18
     var request = httpMocks.createRequest({

+ 14
- 23
test/controllers/replicators/all.test.js View File

@@ -1,45 +1,36 @@
1 1
 const { describe } = require('tape-plus')
2 2
 const httpMocks = require('node-mocks-http')
3
-const Config = require('cobox-config')
4 3
 const BlindReplicator = require('cobox-blind-replicator/test/mock')
5 4
 const Store = require('cobox-group-store')
6 5
 const path = require('path')
7 6
 
8
-const routeHandler = require('../../../app/controllers')
7
+const ReplicatorsController = require('../../../app/controllers/replicators')
9 8
 const { tmp, cleanup } = require('../../util')
10 9
 const seedReplicators = require('../../fixtures/replicators')
11 10
 
12 11
 describe('GET /replicators', (context) => {
13
-  let storage, config, count
14
-  let request, response, seeds
15
-
16
-  context.beforeEach((c) => {
17
-    storage = tmp()
18
-    config = Config(storage)
19
-    count = 3
20
-    seeds = seedReplicators(config, count)
21
-
22
-    replicators = { store: Store(path.join(config.root, 'replicators'), BlindReplicator, { config }) }
23
-    replicators.store.seed(config.replicators.list())
12
+  context('valid with correct parameters', (assert, next) => {
13
+    var storage = tmp()
14
+    var seeds = seedReplicators()
15
+    var replicators = { store: Store(path.join(storage, 'replicators'), BlindReplicator) }
16
+    replicators.store.seed(seeds)
24 17
 
25
-    request = httpMocks.createRequest({
18
+    var request = httpMocks.createRequest({
26 19
       method: 'GET',
27
-      url: '/replicators',
28
-      replicators,
29
-      config
20
+      url: '/'
30 21
     })
31 22
 
32
-    response = httpMocks.createResponse({
23
+    var response = httpMocks.createResponse({
33 24
       eventEmitter: require('events').EventEmitter
34 25
     })
35
-  })
36 26
 
37
-  context('valid with correct parameters', (assert, next) => {
27
+    var routeHandler = ReplicatorsController({ replicators })
38 28
     routeHandler(request, response)
29
+
39 30
     response.on('end', () => {
40
-      var replicators = response._getJSONData()
41
-      assert.ok(Array.isArray(replicators))
42
-      assert.equal(replicators.length, count)
31
+      var data = response._getJSONData()
32
+      assert.ok(Array.isArray(data))
33
+      assert.equal(data.length, seeds.length)
43 34
       cleanup(storage, next)
44 35
     })
45 36
   })

+ 29
- 31
test/controllers/replicators/connections/create.test.js View File

@@ -1,44 +1,35 @@
1 1
 const { describe } = require('tape-plus')
2
-const https = require('node-mocks-http')
3
-const Config = require('cobox-config')
2
+const httpMocks = require('node-mocks-http')
4 3
 const crypto = require('cobox-crypto')
5 4
 const BlindReplicator = require('cobox-blind-replicator/test/mock')
6 5
 const Store = require('cobox-group-store')
7 6
 const path = require('path')
8 7
 
9
-const routeHandler = require('../../../../app/controllers')
8
+const ReplicatorsController = require('../../../../app/controllers/replicators')
10 9
 const { tmp, cleanup } = require('../../../util')
11 10
 const seedReplicators = require('../../../fixtures/replicators')
12 11
 
13 12
 describe('POST /replicators/:id/connections', (context) => {
14
-  let storage, config, count
15
-  let request, response
16
-
17
-  context.beforeEach((c) => {
18
-    storage = tmp()
19
-    count = 3
20
-    config = Config(storage)
21
-    seeds = seedReplicators(config, count)
22
-    seed = config.replicators.list()[0]
13
+  context('valid with correct parameters', (assert, next) => {
14
+    var storage = tmp()
15
+    var seeds = seedReplicators()
16
+    var replicators = { store: Store(path.join(storage, 'replicators'), BlindReplicator) }
17
+    replicators.store.seed(seeds)
23 18
 
24
-    replicators = { store: Store(path.join(config.root, 'replicators'), BlindReplicator, { config }) }
25
-    replicators.store.seed(config.replicators.list())
19
+    var seed = seeds[seeds.length-1]
20
+    var address = seed.address.toString('hex')
26 21
 
27
-    response = https.createResponse({
28
-      eventEmitter: require('events').EventEmitter
22
+    var request = httpMocks.createRequest({
23
+      method: 'POST',
24
+      url: `/${address}/connections`,
25
+      body: { address }
29 26
     })
30
-  })
31 27
 
32
-  context('valid with correct parameters', (assert, next) => {
33
-    var address = seed.address
34
-    request = https.createRequest({
35
-      method: 'POST',
36
-      url: `/replicators/${address}/connections`,
37
-      body: { address },
38
-      replicators,
39
-      config
28
+    var response = httpMocks.createResponse({
29
+      eventEmitter: require('events').EventEmitter
40 30
     })
41 31
 
32
+    var routeHandler = ReplicatorsController({ replicators })
42 33
     routeHandler(request, response)
43 34
 
44 35
     response.on('end', () => {
@@ -46,28 +37,35 @@ describe('POST /replicators/:id/connections', (context) => {
46 37
       assert.ok(replicator, 'success')
47 38
       assert.notOk(replicator.errors, 'returns no errors')
48 39
       assert.same(replicator.name, seed.name, 'returns replicator name')
49
-      assert.same(replicator.address, seed.address, 'returns replicator address')
40
+      assert.same(replicator.address, address, 'returns replicator address')
50 41
       cleanup(storage, next)
51 42
     })
52 43
   })
53 44
 
54 45
   context('invalid without name or address as param', (assert, next) => {
46
+    var storage = tmp()
47
+    var replicators = { store: Store(path.join(storage, 'replicators'), BlindReplicator) }
48
+
55 49
     var id = crypto.randomBytes(32).toString('hex')
56 50
 
57
-    request = https.createRequest({
51
+    var request = httpMocks.createRequest({
58 52
       method: 'POST',
59
-      url: `/replicators/${id}/connections`,
60
-      body: { id },
61
-      replicators,
62
-      config
53
+      url: `/${id}/connections`,
54
+      body: { id }
55
+    })
56
+
57
+    var response = httpMocks.createResponse({
58
+      eventEmitter: require('events').EventEmitter
63 59
     })
64 60
 
61
+    var routeHandler = ReplicatorsController({ replicators })
65 62
     routeHandler(request, response)
66 63
 
67 64
     response.on('end', () => {
68 65
       var data = response._getJSONData()
69 66
       let errors = data.errors
70 67
       assert.ok(Array.isArray(errors), 'returns an error')
68
+      assert.same(errors.length, 1, 'returns a single error message')
71 69
       assert.same(errors[0].msg, 'replicator does not exist, provide an address to replicate.', 'returns an error message')
72 70
       cleanup(storage, next)
73 71
     })

+ 28
- 31
test/controllers/replicators/connections/destroy.test.js View File

@@ -1,72 +1,69 @@
1 1
 const { describe } = require('tape-plus')
2
-const https = require('node-mocks-http')
3
-const Config = require('cobox-config')
2
+const httpMocks = require('node-mocks-http')
4 3
 const crypto = require('cobox-crypto')
5 4
 const BlindReplicator = require('cobox-blind-replicator/test/mock')
6 5
 const Store = require('cobox-group-store')
7 6
 const path = require('path')
8 7
 
9
-const routeHandler = require('../../../../app/controllers')
8
+const ReplicatorsController = require('../../../../app/controllers/replicators')
10 9
 const { tmp, cleanup } = require('../../../util')
11 10
 const seedReplicators = require('../../../fixtures/replicators')
12 11
 
13 12
 describe('DELETE /replicators/:id/connections', (context) => {
14
-  let storage, config, count
15
-  let request, response
16
-
17
-  context.beforeEach((c) => {
18
-    storage = tmp()
19
-    count = 3
20
-    config = Config(storage)
21
-    seeds = seedReplicators(config, count)
22
-    seed = config.replicators.list()[0]
13
+  context('valid with correct parameters', (assert, next) => {
14
+    var storage = tmp()
15
+    var seeds = seedReplicators()
16
+    var replicators = { store: Store(path.join(storage, 'replicators'), BlindReplicator) }
17
+    replicators.store.seed(seeds)
23 18
 
24
-    replicators = { store: Store(path.join(config.root, 'replicators'), BlindReplicator, { config }) }
25
-    replicators.store.seed(config.replicators.list())
19
+    var seed = seeds[seeds.length-1]
20
+    var address = seed.address.toString('hex')
26 21
 
27
-    response = https.createResponse({
28
-      eventEmitter: require('events').EventEmitter
22
+    var request = httpMocks.createRequest({
23
+      method: 'DELETE',
24
+      url: `/${address}/connections`,
25
+      body: { address }
29 26
     })
30
-  })
31 27
 
32
-  context('valid with correct parameters', (assert, next) => {
33
-    var address = seed.address.toString('hex')
34
-    request = https.createRequest({
35
-      method: 'DELETE',
36
-      url: `/replicators/${address}/connections`,
37
-      body: { address },
38
-      replicators,
39
-      config
28
+    var response = httpMocks.createResponse({
29
+      eventEmitter: require('events').EventEmitter
40 30
     })
41 31
 
32
+    var routeHandler = ReplicatorsController({ replicators })
42 33
     routeHandler(request, response)
43 34
 
44 35
     response.on('end', () => {
45 36
       var replicator = response._getJSONData()
46 37
       assert.ok(replicator, 'deletes a replicator')
47 38
       assert.same(replicator.name, seed.name, 'returns replicator name')
48
-      assert.same(replicator.address, seed.address, 'returns replicator address')
39
+      assert.same(replicator.address, address, 'returns replicator address')
49 40
       cleanup(storage, next)
50 41
     })
51 42
   })
52 43
 
53 44
   context('invalid without name or address as param', (assert, next) => {
45
+    var storage = tmp()
46
+    var replicators = { store: Store(path.join(storage, 'replicators'), BlindReplicator) }
47
+
54 48
     var id = crypto.randomBytes(32).toString('hex')
55 49
 
56
-    request = https.createRequest({
50
+    var request = httpMocks.createRequest({
57 51
       method: 'DELETE',
58
-      url: `/replicators/${id}/connections`,
59
-      body: { id },
60
-      replicators,
61
-      config
52
+      url: `/${id}/connections`
53
+    })
54
+
55
+    var response = httpMocks.createResponse({
56
+      eventEmitter: require('events').EventEmitter
62 57
     })
63 58
 
59
+    var routeHandler = ReplicatorsController({ replicators })
64 60
     routeHandler(request, response)
65 61
 
66 62
     response.on('end', () => {
67 63
       var data = response._getJSONData()
68 64
       let errors = data.errors
69 65
       assert.ok(Array.isArray(errors), 'returns an error')
66
+      assert.same(errors.length, 1, 'returns a single error message')
70 67
       assert.same(errors[0].msg, 'replicator does not exist, provide an address to replicate.', 'returns an error message')
71 68
       cleanup(storage, next)
72 69
     })

+ 51
- 43
test/controllers/replicators/create.test.js View File

@@ -1,46 +1,32 @@
1 1
 const { describe } = require('tape-plus')
2
-const https = require('node-mocks-http')
3
-const Config = require('cobox-config')
2
+const httpMocks = require('node-mocks-http')
4 3
 const crypto = require('cobox-crypto')
5 4
 const debug = require('debug')('cobox-server')
6 5
 const BlindReplicator = require('cobox-blind-replicator/test/mock')
7 6
 const Store = require('cobox-group-store')
8 7
 const path = require('path')
9 8
 
10
-const routeHandler = require('../../../app/controllers')
9
+const ReplicatorsController = require('../../../app/controllers/replicators')
11 10
 const { tmp, cleanup } = require('../../util')
12 11
 const seedReplicators = require('../../fixtures/replicators')
13 12
 
14 13
 describe('POST /replicators', (context) => {
15
-  let storage, config, count
16
-  let request, response
17
-
18
-  context.beforeEach((c) => {
19
-    storage = tmp()
20
-    count = 3
21
-    config = Config(storage)
22
-    seeds = seedReplicators(config, count)
23
-    seed = config.replicators.list()[0]
24
-
25
-    replicators = { store: Store(path.join(config.root, 'replicators'), BlindReplicator, { config }) }
26
-    replicators.store.seed(config.replicators.list())
27
-
28
-    response = https.createResponse({
29
-      eventEmitter: require('events').EventEmitter
30
-    })
31
-  })
32
-
33 14
   context('valid with name as param', (assert, next) => {
15
+    var storage = tmp()
16
+    var replicators = { store: Store(path.join(storage, 'replicators'), BlindReplicator) }
34 17
     var name = 'magma'
35 18
 
36
-    request = https.createRequest({
19
+    var request = httpMocks.createRequest({
37 20
       method: 'POST',
38
-      url: `/replicators`,
39
-      body: { name },
40
-      replicators,
41
-      config
21
+      url: `/`,
22
+      body: { name }
23
+    })
24
+
25
+    var response = httpMocks.createResponse({
26
+      eventEmitter: require('events').EventEmitter
42 27
     })
43 28
 
29
+    var routeHandler = ReplicatorsController({ replicators })
44 30
     routeHandler(request, response)
45 31
 
46 32
     response.on('end', () => {
@@ -53,66 +39,88 @@ describe('POST /replicators', (context) => {
53 39
   })
54 40
 
55 41
   context('invalid if name is a key', (assert, next) => {
56
-    var name = seed.address
42
+    var storage = tmp()
43
+    var replicators = { store: Store(path.join(storage, 'replicators'), BlindReplicator) }
44
+    var name = crypto.randomBytes(32).toString('hex')
57 45
 
58
-    request = https.createRequest({
46
+    var request = httpMocks.createRequest({
59 47
       method: 'POST',
60
-      url: `/replicators`,
61
-      body: { name },
62
-      replicators,
63
-      config
48
+      url: `/`,
49
+      body: { name }
64 50
     })
65 51
 
52
+    var response = httpMocks.createResponse({
53
+      eventEmitter: require('events').EventEmitter
54
+    })
55
+
56
+    var routeHandler = ReplicatorsController({ replicators })
66 57
     routeHandler(request, response)
67 58
 
68 59
     response.on('end', () => {
69 60
       var data = response._getJSONData()
70 61
       let errors = data.errors
71 62
       assert.ok(Array.isArray(errors), 'returns an error')
63
+      assert.same(errors.length, 1, 'returns a single error')
72 64
       assert.same(errors[0].msg, '\'name\' must be below 32 characters', 'error message states replicator already exists')
73 65
       cleanup(storage, next)
74 66
     })
75 67
   })
76 68
 
77
-  context('invalid if replicators exists', (assert, next) => {
69
+  context('invalid if replicator exists', (assert, next) => {
70
+    var storage = tmp()
71
+    var seeds = seedReplicators()
72
+    var replicators = { store: Store(path.join(storage, 'replicators'), BlindReplicator) }
73
+    replicators.store.seed(seeds)
74
+
75
+    var seed = seeds[seeds.length-1]
78 76
     var name = seed.name
79 77
 
80
-    request = https.createRequest({
78
+    var request = httpMocks.createRequest({
81 79
       method: 'POST',
82
-      url: `/replicators`,
83
-      body: { name },
84
-      replicators,
85
-      config
80
+      url: `/`,
81
+      body: { name }
82
+    })
83
+
84
+    var response = httpMocks.createResponse({
85
+      eventEmitter: require('events').EventEmitter
86 86
     })
87 87
 
88
+    var routeHandler = ReplicatorsController({ replicators })
88 89
     routeHandler(request, response)
89 90
 
90 91
     response.on('end', () => {
91 92
       var data = response._getJSONData()
92 93
       let errors = data.errors
93 94
       assert.ok(Array.isArray(errors), 'returns an error')
95
+      assert.same(errors.length, 1, 'returns a single error')
94 96
       assert.same(errors[0].msg, `${name} already exists`, 'error message states replicator already exists')
95 97
       cleanup(storage, next)
96 98
     })
97 99
   })
98 100
 
99 101
   context('invalid without name as param', (assert, next) => {
102
+    var storage = tmp()
103
+    var replicators = { store: Store(path.join(storage, 'replicators'), BlindReplicator) }
100 104
     var id = crypto.randomBytes(32).toString('hex')
101 105
 
102
-    request = https.createRequest({
106
+    var request = httpMocks.createRequest({
103 107
       method: 'POST',
104
-      url: `/replicators`,
105
-      body: { id },
106
-      replicators,
107
-      config
108
+      url: `/`,
109
+      body: { id }
110
+    })
111
+
112
+    var response = httpMocks.createResponse({
113
+      eventEmitter: require('events').EventEmitter
108 114
     })
109 115
 
116
+    var routeHandler = ReplicatorsController({ replicators })
110 117
     routeHandler(request, response)
111 118
 
112 119
     response.on('end', () => {
113 120
       var data = response._getJSONData()
114 121
       let errors = data.errors
115 122
       assert.ok(Array.isArray(errors), 'returns an error')
123
+      assert.same(errors.length, 1, 'returns a single error')
116 124
       assert.same(errors[0].msg, '\'name\' required', 'returns an error message')
117 125
       cleanup(storage, next)
118 126
     })

+ 48
- 25
test/controllers/replicators/find.test.js View File

@@ -1,50 +1,73 @@
1 1
 const { describe } = require('tape-plus')
2
-const https = require('node-mocks-http')
3
-const Config = require('cobox-config')
2
+const httpMocks = require('node-mocks-http')
4 3
 const BlindReplicator = require('cobox-blind-replicator/test/mock')
5 4
 const Store = require('cobox-group-store')
6 5
 const path = require('path')
6
+const crypto = require('cobox-crypto')
7 7
 
8
-const routeHandler = require('../../../app/controllers')
8
+const ReplicatorsController = require('../../../app/controllers/replicators')
9 9
 const { tmp, cleanup } = require('../../util')
10 10
 const seedReplicators = require('../../fixtures/replicators')
11 11
 
12 12
 describe('GET /replicators/:id', (context) => {
13
-  let storage, config, count
14
-  let request, response
13
+  context('valid with correct parameters', (assert, next) => {
14
+    var storage = tmp()
15
+    var seeds = seedReplicators()
16
+    var replicators = { store: Store(path.join(storage, 'replicators'), BlindReplicator) }
17
+    replicators.store.seed(seeds)
15 18
 
16
-  context.beforeEach((c) => {
17
-    storage = tmp()
18
-    count = 3
19
-    config = Config(storage)
20
-    seeds = seedReplicators(config, count)
21
-    seed = config.replicators.list()[0]
19
+    var seed = seeds[seeds.length-1]
20
+    var address = seed.address.toString('hex')
22 21
 
23
-    replicators = { store: Store(path.join(config.root, 'replicators'), BlindReplicator, { config }) }
24
-    replicators.store.seed(config.replicators.list())
22
+    var request = httpMocks.createRequest({
23
+      method: 'GET',
24
+      url: `/${address}`,
25
+      body: { address }
26
+    })
25 27
 
26
-    response = https.createResponse({
28
+    var response = httpMocks.createResponse({
27 29
       eventEmitter: require('events').EventEmitter
28 30
     })
31
+
32
+    var routeHandler = ReplicatorsController({ replicators })
33
+    routeHandler(request, response)
34
+
35
+    response.on('end', () => {
36
+      var replicator = response._getJSONData()
37
+      assert.ok(replicator, 'finds a replicator')
38
+      assert.same(replicator.name, seed.name, 'returns replicator name')
39
+      assert.same(replicator.address, address, 'returns replicator address')
40
+      cleanup(storage, next)
41
+    })
29 42
   })
30 43
 
31
-  context('valid with correct parameters', (assert, next) => {
32
-    var address = seed.address.toString('hex')
33
-    request = https.createRequest({
44
+  context('invalid if doesnt exist', (assert, next) => {
45
+    var storage = tmp()
46
+    var seeds = seedReplicators()
47
+    var replicators = { store: Store(path.join(storage, 'replicators'), BlindReplicator) }
48
+    replicators.store.seed(seeds)
49
+
50
+    var address = crypto.randomBytes(32).toString('hex')
51
+
52
+    var request = httpMocks.createRequest({
34 53
       method: 'GET',
35
-      url: `/replicators/${address}`,
36
-      body: { address },
37
-      replicators,
38
-      config
54
+      url: `/${address}`,
55
+      body: { address }
39 56
     })
40 57
 
58
+    var response = httpMocks.createResponse({
59
+      eventEmitter: require('events').EventEmitter
60
+    })
61
+
62
+    var routeHandler = ReplicatorsController({ replicators })
41 63
     routeHandler(request, response)
42 64
 
43 65
     response.on('end', () => {
44
-      var replicator = response._getJSONData()
45
-      assert.ok(replicator, 'finds a replicator')
46
-      assert.same(replicator.name, seed.name, 'returns replicator name')
47
-      assert.same(replicator.address, seed.address, 'returns replicator address')
66
+      var data = response._getJSONData()
67
+      let errors = data.errors
68
+      assert.ok(Array.isArray(errors), 'returns an error')
69
+      assert.same(errors.length, 1, 'returns a single error')
70
+      assert.same(errors[0].msg, 'replicator does not exist, provide an address to replicate.', 'returns an error message')
48 71
       cleanup(storage, next)
49 72
     })
50 73
   })

+ 6
- 6
test/fixtures/replicators.js View File

@@ -1,16 +1,16 @@
1 1
 const crypto = require('cobox-crypto')
2 2
 const randomWords = require('random-words')
3 3
 
4
-module.exports = function seedReplicators (config, num) {
4
+module.exports = function seedReplicators () {
5 5
   const replicators = Array
6
-    .from(new Array(num).keys())
7
-    .forEach((i) => {
8
-      var address = crypto.address(),
6
+    .from(new Array(Math.floor(1 + Math.random() * Math.floor(9))).keys())
7
+    .map((i) => {
8
+      var address = Buffer.from(crypto.address()),
9 9
         name = randomWords(1).pop()
10 10
 
11 11
       var entry = { name, address }
12
-      config.replicators.set(name, entry)
12
+      return entry
13 13
     })
14 14
 
15
-  return config.save()
15
+  return replicators
16 16
 }