Browse Source

fix tests and add devices

Kieran Gibb 2 years ago
parent
commit
2e9de849b2
No known key found for this signature in database
4 changed files with 16 additions and 6 deletions
  1. 7
    2
      index.js
  2. 1
    0
      package.json
  3. 3
    3
      test/index.test.js
  4. 5
    1
      test/key-handler.test.js

+ 7
- 2
index.js View File

@@ -20,7 +20,8 @@ const MapHandler = require('./lib/map-handler')
20 20
 const defaultConfig = (options = {}) => ({
21 21
   options,
22 22
   groups: { byKey: {}, byName: {} },
23
-  replicators: { byKey: {}, byName: {} }
23
+  replicators: { byKey: {}, byName: {} },
24
+  devices: { byKey: {}, byName: {} }
24 25
 })
25 26
 
26 27
 // filename-safe date (probably there is a better way to do this)
@@ -40,6 +41,7 @@ class CoBoxConfig {
40 41
       fs.writeFileSync(this.storage, yaml.safeDump(defaultConfig(), { sortKeys: true }))
41 42
       this._groups = config.groups
42 43
       this._replicators = config.replicators
44
+      this._devices = config.devices
43 45
       this._options = config.options
44 46
     } else {
45 47
       this.load()
@@ -50,11 +52,12 @@ class CoBoxConfig {
50 52
 
51 53
     this.deriveKeyPair = crypto.keyPair.bind(null, parentKey)
52 54
     this.identity = crypto.keyPair(parentKey, IDENTITY_SUBKEY_ID)
53
-
54 55
     this.logger = logger(path.join(this.root, 'logs', logfile))
55 56
     this.log = this.logger('cobox-config')
57
+
56 58
     this.groups = KeyHandler(this._groups)
57 59
     this.replicators = KeyHandler(this._replicators)
60
+    this.devices = KeyHandler(this._devices)
58 61
     this.options = MapHandler(this._options)
59 62
   }
60 63
 
@@ -63,6 +66,7 @@ class CoBoxConfig {
63 66
       var config = defaultConfig()
64 67
       config.groups = this._groups
65 68
       config.replicators = this._replicators
69
+      config.devices = this._devices
66 70
       config.options = this._options
67 71
 
68 72
       fs.writeFileSync(this.storage, yaml.safeDump(config, { sortKeys: true }))
@@ -78,6 +82,7 @@ class CoBoxConfig {
78 82
       const config = yaml.safeLoad(fs.readFileSync(this.storage, 'utf8'))
79 83
       this._groups = config.groups
80 84
       this._replicators = config.replicators
85
+      this._devices = config.devices
81 86
       this._options = config.options
82 87
       return true
83 88
     } catch (err) {

+ 1
- 0
package.json View File

@@ -21,6 +21,7 @@
21 21
   "devDependencies": {
22 22
     "hypercore-crypto": "^1.0.0",
23 23
     "nyc": "^14.1.1",
24
+    "random-words": "^1.1.0",
24 25
     "rimraf": "^2.6.3",
25 26
     "tap-spec": "^5.0.0",
26 27
     "tape": "^4.11.0",

+ 3
- 3
test/index.test.js View File

@@ -21,9 +21,9 @@ describe('load', (context) => {
21 21
   context('default', (assert, next) => {
22 22
     var storage = tmp()
23 23
     var config = Config(storage)
24
-    assert.ok(config.groups instanceof KeyHandler, 'groups uses KeyHandler')
25
-    assert.ok(config.replicators instanceof KeyHandler, 'replicators uses KeyHandler')
26
-    assert.ok(config.devices instanceof KeyHandler, 'devices uses KeyHandler')
24
+    assert.ok(config.groups.list() instanceof Array, 'groups uses KeyHandler')
25
+    assert.ok(config.replicators.list() instanceof Array, 'replicators uses KeyHandler')
26
+    assert.ok(config.devices.list() instanceof Array, 'devices uses KeyHandler')
27 27
     cleanup(storage, next)
28 28
   })
29 29
 })

+ 5
- 1
test/key-handler.test.js View File

@@ -4,6 +4,7 @@ const crypto = require('cobox-crypto')
4 4
 const fs = require('fs')
5 5
 const path = require('path')
6 6
 const yaml = require('js-yaml')
7
+const randomWords = require('random-words')
7 8
 
8 9
 const { tmp, cleanup } = require('./util')
9 10
 
@@ -20,7 +21,10 @@ describe('set', (context) => {
20 21
 
21 22
   context('byKey', (assert, next) => {
22 23
     var config = Config(storage)
23
-    var group = crypto.unpack(crypto.accessKey())
24
+    var group = {
25
+      address: crypto.address(),
26
+      name: randomWords(1).pop()
27
+    }
24 28
 
25 29
     config.groups.set(group.address, group)
26 30