Browse Source

mock the group

Kieran Gibb 2 years ago
parent
commit
73b8a77bfa
No known key found for this signature in database
3 changed files with 179 additions and 132 deletions
  1. 34
    57
      index.js
  2. 124
    75
      test/index.test.js
  3. 21
    0
      test/mocks.js

+ 34
- 57
index.js View File

@@ -37,82 +37,59 @@ class GroupStore {
37 37
   }
38 38
 
39 39
   async all () {
40
-    try {
41
-      await this.ready()
42
-      return uniq(Object.values(this.groups))
43
-    } catch (err) {
44
-      debug(err)
45
-      return []
46
-    }
40
+    await this.ready()
41
+    return uniq(Object.values(this.groups))
47 42
   }
48 43
 
49 44
   async where (params = {}) {
50
-    try {
51
-      var response = await this.all()
52
-      var keys = Object.keys(params)
53
-
54
-      for (var i = 0; i < keys.length; ++i) {
55
-        var key = keys[i]
56
-        response = response.filter((entry) => (
57
-          entry[key].toString('hex') === params[key].toString('hex')
58
-        ))
59
-      }
45
+    var response = await this.all()
46
+    var keys = Object.keys(params)
60 47
 
61
-      return response
62
-    } catch (err) {
63
-      debug(err)
64
-      return []
48
+    for (var i = 0; i < keys.length; ++i) {
49
+      var key = keys[i]
50
+
51
+      response = response.filter((entry) => {
52
+        if (!entry[key] || !params[key]) return false
53
+        return entry[key].toString('hex') === params[key].toString('hex')
54
+      })
65 55
     }
56
+
57
+    return response
66 58
   }
67 59
 
68 60
   async findBy (params = {}) {
69
-    try {
70
-      let response = await this.where(params)
71
-      return response[0]
72
-    } catch (err) {
73
-      debug(err)
74
-      return false
75
-    }
61
+    let response = await this.where(params)
62
+    return response[0]
76 63
   }
77 64
 
78 65
   async create (params = {}) {
79
-    try {
80
-      var group = await this.findBy(params)
81
-      if (group) throw new Error('group already exists')
66
+    var group = await this.findBy(params)
67
+    if (group) throw new Error('group already exists')
82 68
 
83
-      var group = this._buildGroup(params)
69
+    var group = this._buildGroup(params)
84 70
 
85
-      await group.ready()
71
+    await group.ready()
86 72
 
87
-      this._cacheGroup(group)
88
-      this._saveGroup(group)
73
+    this._cacheGroup(group)
74
+    this._saveGroup(group)
89 75
 
90
-      return group
91
-    } catch (error) {
92
-      debug(error)
93
-      return false
94
-    }
76
+    return group
95 77
   }
96 78
 
97 79
   // TODO: this doesn't work currently, throws a level db error
98 80
   async destroy (params = {}) {
99
-    try {
100
-      var group = await this.findBy(params)
101
-      if (!group) throw new Error('no group exists')
102
-
103
-      this.config.groups.delete(group.address)
104
-      // TODO: delete group data
105
-
106
-      if (this.config.save()) {
107
-        delete this.groups[group.address.toString('hex')]
108
-        return true
109
-      } else {
110
-        this.config.groups.set(group.address, group)
111
-        throw new Error('failed to save config file')
112
-      }
113
-    } catch (error) {
114
-      debug(error)
115
-      return false
81
+    var group = await this.findBy(params)
82
+    if (!group) throw new Error('no group exists')
83
+
84
+    this.config.groups.delete(group.address)
85
+    // TODO: delete group data
86
+
87
+    if (this.config.save()) {
88
+      delete this.groups[group.address.toString('hex')]
89
+      return true
90
+    } else {
91
+      this.config.groups.set(group.address, group)
92
+      throw new Error('failed to save config file')
116 93
     }
117 94
   }
118 95
 

+ 124
- 75
test/index.test.js View File

@@ -1,10 +1,10 @@
1 1
 const { describe } = require('tape-plus')
2 2
 const Config = require('cobox-config')
3
-const { Decrypted, Encrypted } = require('cobox-group')
4 3
 const crypto = require('cobox-crypto')
5 4
 const Store = require('../')
6 5
 
7 6
 const { tmp, cleanup } = require('./util')
7
+const { createGroup } = require('./mocks')
8 8
 
9 9
 describe('group store: basic', (context) => {
10 10
   context('ready()', async function (assert, next) {
@@ -14,12 +14,15 @@ describe('group store: basic', (context) => {
14 14
 
15 15
     config.groups.set(params.address, params)
16 16
 
17
-    const store = Store(Decrypted, storage, { config })
17
+    const store = Store(createGroup, storage, { config })
18 18
 
19
-    await store.ready()
20
-
21
-    assert.same(Object.values(store.groups).length, 1, 'builds then caches groups from the config')
22
-    assert.ok(store.groups[params.address.toString('hex')], 'accessible using address hex string')
19
+    try {
20
+      await store.ready()
21
+      assert.same(Object.values(store.groups).length, 1, 'builds then caches groups from the config')
22
+      assert.ok(store.groups[params.address.toString('hex')], 'accessible using address hex string')
23
+    } catch (err) {
24
+      assert.error(err, 'no error')
25
+    }
23 26
 
24 27
     cleanup(storage, next)
25 28
   })
@@ -31,13 +34,17 @@ describe('group store: basic', (context) => {
31 34
 
32 35
     config.groups.set(params.address, params)
33 36
 
34
-    const store = Store(Decrypted, storage, { config })
37
+    const store = Store(createGroup, storage, { config })
35 38
 
36
-    await store.ready()
39
+    try {
40
+      await store.ready()
41
+      var response = await store.all(params)
42
+      assert.ok(Array.isArray(response), 'returns an array')
43
+      assert.same(response.length, 1, 'returns the correct number')
44
+    } catch (err) {
45
+      assert.error(err, 'no error')
46
+    }
37 47
 
38
-    var response = await store.all(params)
39
-    assert.ok(Array.isArray(response), 'returns an array')
40
-    assert.same(response.length, 1, 'returns the correct number')
41 48
     cleanup(storage, next)
42 49
   })
43 50
 
@@ -49,19 +56,23 @@ describe('group store: basic', (context) => {
49 56
     seedConfig()
50 57
     config.groups.set(params.address, params)
51 58
 
52
-    const store = Store(Decrypted, storage, { config })
53
-
54
-    await store.ready()
55
-
56
-    var response = await store.where({
57
-      name: 'magma',
58
-      address: params.address
59
-    })
59
+    const store = Store(createGroup, storage, { config })
60
+
61
+    try {
62
+      await store.ready()
63
+      var response = await store.where({
64
+        name: 'magma',
65
+        address: params.address
66
+      })
67
+
68
+      assert.ok(Array.isArray(response), 'returns an array')
69
+      var group = response[0]
70
+      assert.ok(group, 'finds a group')
71
+      assert.same(group && group.address, params.address, 'finds the correct group')
72
+    } catch (err) {
73
+      assert.error(err, 'no error')
74
+    }
60 75
 
61
-    assert.ok(Array.isArray(response), 'returns an array')
62
-    var group = response[0]
63
-    assert.ok(group, 'finds a group')
64
-    assert.same(group && group.address, params.address, 'finds the correct group')
65 76
     cleanup(storage, next)
66 77
 
67 78
     function seedConfig () {
@@ -82,16 +93,21 @@ describe('group store: basic', (context) => {
82 93
 
83 94
     config.groups.set(params.address, params)
84 95
 
85
-    const store = Store(Decrypted, storage, { config })
96
+    const store = Store(createGroup, storage, { config })
97
+
98
+    try {
99
+      await store.ready()
86 100
 
87
-    await store.ready()
101
+      var response = await store.where({
102
+        name: 'magma',
103
+        address: null
104
+      })
88 105
 
89
-    var response = await store.where({
90
-      name: 'magma',
91
-      address: null
92
-    })
106
+      assert.same(response.length, 0, 'fails to find a group')
107
+    } catch (err) {
108
+      assert.error(err, 'no error')
109
+    }
93 110
 
94
-    assert.same(response.length, 0, 'fails to find a group')
95 111
     cleanup(storage, next)
96 112
   })
97 113
 
@@ -102,16 +118,21 @@ describe('group store: basic', (context) => {
102 118
 
103 119
     config.groups.set(params.address, params)
104 120
 
105
-    const store = Store(Decrypted, storage, { config })
121
+    const store = Store(createGroup, storage, { config })
106 122
 
107
-    await store.ready()
123
+    try {
124
+      await store.ready()
108 125
 
109
-    var response = await store.where({
110
-      name: null,
111
-      address: params.address
112
-    })
126
+      var response = await store.where({
127
+        name: null,
128
+        address: params.address
129
+      })
130
+
131
+      assert.same(response.length, 0, 'fails to find a group')
132
+    } catch (err) {
133
+      assert.error(err, 'no error')
134
+    }
113 135
 
114
-    assert.same(response.length, 0, 'fails to find a group')
115 136
     cleanup(storage, next)
116 137
   })
117 138
 
@@ -122,16 +143,21 @@ describe('group store: basic', (context) => {
122 143
 
123 144
     config.groups.set(params.address, params)
124 145
 
125
-    const store = Store(Decrypted, storage, { config })
146
+    const store = Store(createGroup, storage, { config })
126 147
 
127
-    await store.ready()
148
+    try {
149
+      await store.ready()
128 150
 
129
-    var response = await store.where({
130
-      name: 'magma',
131
-      address: crypto.randomBytes(32).toString('hex')
132
-    })
151
+      var response = await store.where({
152
+        name: 'magma',
153
+        address: crypto.randomBytes(32).toString('hex')
154
+      })
155
+
156
+      assert.same(response.length, 0, 'fails to find a group')
157
+    } catch (err) {
158
+      assert.error(err, 'no error')
159
+    }
133 160
 
134
-    assert.same(response.length, 0, 'fails to find a group')
135 161
     cleanup(storage, next)
136 162
   })
137 163
 
@@ -142,16 +168,21 @@ describe('group store: basic', (context) => {
142 168
 
143 169
     config.groups.set(params.address, params)
144 170
 
145
-    const store = Store(Decrypted, storage, { config })
171
+    const store = Store(createGroup, storage, { config })
146 172
 
147
-    await store.ready()
173
+    try {
174
+      await store.ready()
148 175
 
149
-    var response = await store.where({
150
-      name: 'cobox',
151
-      address: params.address
152
-    })
176
+      var response = await store.where({
177
+        name: 'cobox',
178
+        address: params.address
179
+      })
180
+
181
+      assert.same(response.length, 0, 'fails to find a group')
182
+    } catch (err) {
183
+      assert.error(err, 'no error')
184
+    }
153 185
 
154
-    assert.same(response.length, 0, 'fails to find a group')
155 186
     cleanup(storage, next)
156 187
   })
157 188
 
@@ -162,15 +193,20 @@ describe('group store: basic', (context) => {
162 193
 
163 194
     config.groups.set(params.address, params)
164 195
 
165
-    const store = Store(Decrypted, storage, { config })
196
+    const store = Store(createGroup, storage, { config })
197
+
198
+    try {
199
+      await store.ready()
166 200
 
167
-    await store.ready()
201
+      var response = await store.where({ name: 'magma' })
202
+      assert.ok(Array.isArray(response), 'returns an array')
203
+      var group = response[0]
204
+      assert.ok(group, 'finds a group')
205
+      assert.same(group && group.address, params.address, 'finds the correct group')
206
+    } catch (err) {
207
+      assert.error(err, 'no error')
208
+    }
168 209
 
169
-    var response = await store.where({ name: 'magma' })
170
-    assert.ok(Array.isArray(response), 'returns an array')
171
-    var group = response[0]
172
-    assert.ok(group, 'finds a group')
173
-    assert.same(group && group.address, params.address, 'finds the correct group')
174 210
     cleanup(storage, next)
175 211
   })
176 212
 
@@ -181,15 +217,20 @@ describe('group store: basic', (context) => {
181 217
 
182 218
     config.groups.set(params.address, params)
183 219
 
184
-    const store = Store(Decrypted, storage, { config })
220
+    const store = Store(createGroup, storage, { config })
221
+
222
+    try {
223
+      await store.ready()
185 224
 
186
-    await store.ready()
225
+      var response = await store.where({ address: params.address })
226
+      assert.ok(Array.isArray(response), 'returns an array')
227
+      var group = response[0]
228
+      assert.ok(group, 'finds a group')
229
+      assert.same(group && group.address, params.address, 'finds the correct group')
230
+    } catch (err) {
231
+      assert.error(err, 'no error')
232
+    }
187 233
 
188
-    var response = await store.where({ address: params.address })
189
-    assert.ok(Array.isArray(response), 'returns an array')
190
-    var group = response[0]
191
-    assert.ok(group, 'finds a group')
192
-    assert.same(group && group.address, params.address, 'finds the correct group')
193 234
     cleanup(storage, next)
194 235
   })
195 236
 
@@ -200,14 +241,18 @@ describe('group store: basic', (context) => {
200 241
 
201 242
     config.groups.set(params.address, params)
202 243
 
203
-    const store = Store(Decrypted, storage, { config })
244
+    const store = Store(createGroup, storage, { config })
204 245
 
205
-    await store.ready()
246
+    try {
247
+      await store.ready()
206 248
 
207
-    delete params.encryptionKey
208
-    var response = await store.findBy(params)
209
-    assert.ok(response, 'finds a group')
210
-    assert.same(response && response.address, params.address, 'finds the correct group')
249
+      delete params.encryptionKey
250
+      var response = await store.findBy(params)
251
+      assert.ok(response, 'finds a group')
252
+      assert.same(response && response.address, params.address, 'finds the correct group')
253
+    } catch (err) {
254
+      assert.error(err, 'no error')
255
+    }
211 256
     cleanup(storage, next)
212 257
   })
213 258
 
@@ -216,10 +261,14 @@ describe('group store: basic', (context) => {
216 261
       config = Config(storage),
217 262
       params = Object.assign(crypto.keySet(), { name: 'magma' })
218 263
 
219
-    const store = Store(Decrypted, storage, { config })
220
-    var group = await store.create(params)
264
+    const store = Store(createGroup, storage, { config })
221 265
 
222
-    assert.ok(group && group.address, 'creates a new group')
266
+    try {
267
+      var group = await store.create(params)
268
+      assert.ok(group && group.address, 'creates a new group')
269
+    } catch (err) {
270
+      assert.error(err, 'no error')
271
+    }
223 272
     cleanup(storage, next)
224 273
   })
225 274
 
@@ -229,7 +278,7 @@ describe('group store: basic', (context) => {
229 278
   //     params = crypto.keySet()
230 279
 
231 280
   //   config.groups.set(params.address, params)
232
-  //   const store = Store(Decrypted, storage, { config })
281
+  //   const store = Store(createGroup, storage, { config })
233 282
 
234 283
   //   var groups = await store.all()
235 284
   //   console.log(groups.map((group) => group.address.toString('hex')))

+ 21
- 0
test/mocks.js View File

@@ -0,0 +1,21 @@
1
+const maybe = require('call-me-maybe')
2
+
3
+function createGroup (storage, address, opts = {}) {
4
+  this.storage = storage
5
+  this.address = Buffer.from(address, 'hex')
6
+  this.encryptionKey = Buffer.from(opts.encryptionKey, 'hex')
7
+  this.name = opts.name
8
+}
9
+
10
+createGroup.prototype.ready = (callback) => maybe(callback, new Promise((resolve, reject) => resolve(true)))
11
+createGroup.prototype.toConfig = function () {
12
+  return {
13
+    name: this.name,
14
+    address: this.address,
15
+    encryptionKey: this.encryptionKey
16
+  }
17
+}
18
+
19
+module.exports = {
20
+  createGroup: (storage, address, opts) => new createGroup(storage, address, opts)
21
+}