Browse Source

remove knowledge of mounts from config

Kieran Gibb 2 years ago
parent
commit
eb9db35f2f
No known key found for this signature in database
4 changed files with 146 additions and 171 deletions
  1. 2
    2
      index.js
  2. 1
    0
      package.json
  3. 0
    169
      test/index.test.js
  4. 143
    0
      test/key-handler.test.js

+ 2
- 2
index.js View File

@@ -5,6 +5,7 @@ const os = require('os')
5 5
 const mkdirp = require('mkdirp')
6 6
 const logger = require('./logger')
7 7
 const crypto = require('cobox-crypto')
8
+const constants = require('cobox-constants')
8 9
 
9 10
 const CONFIG_FILE = 'config.yml'
10 11
 const IDENTITY_SUBKEY_ID = 0
@@ -27,9 +28,8 @@ const logfile = [new Date().toISOString().replace(/:/g, '-').replace(/\./, '-'),
27 28
 
28 29
 class CoBoxConfig {
29 30
   constructor (storage, opts = {}) {
30
-    this.root = storage || path.join(os.homedir(), '.cobox')
31
+    this.root = storage || constants.storage
31 32
     this.storage = path.join(this.root, CONFIG_FILE)
32
-    opts.mount = opts.mount || '/cobox'
33 33
     this._opts = opts
34 34
 
35 35
     mkdirp.sync(path.join(this.root, 'logs'))

+ 1
- 0
package.json View File

@@ -8,6 +8,7 @@
8 8
   },
9 9
   "dependencies": {
10 10
     "cobox-crypto": "git+https://ledger-git.dyne.org/cobox/cobox-crypto",
11
+    "cobox-constants": "git+https://ledger-git.dyne.org/cobox/cobox-constants",
11 12
     "debug": "^4.1.1",
12 13
     "js-yaml": "^3.13.1",
13 14
     "mkdirp": "^0.5.1",

+ 0
- 169
test/index.test.js View File

@@ -56,172 +56,3 @@ describe('master_key', (context) => {
56 56
     cleanup(storage, next)
57 57
   })
58 58
 })
59
-
60
-describe('options', (context) => {
61
-  var storage
62
-
63
-  context.beforeEach((c) => {
64
-    storage = tmp()
65
-  })
66
-
67
-  context.afterEach((c) => {
68
-    cleanup(storage.root)
69
-  })
70
-
71
-  context('get', (assert, next) => {
72
-    var config = Config(storage)
73
-    assert.same(config.options.get('mount'), '/cobox', 'mount defaults to /cobox')
74
-    next()
75
-  })
76
-
77
-  context('set', (assert, next) => {
78
-    var config = Config(storage)
79
-    config.options.set('mount', './mnt')
80
-    assert.same(config._options['mount'], './mnt', 'sets mount option to ./mnt')
81
-    config.save()
82
-    config.load()
83
-    assert.same(config.options.get('mount'), './mnt', 'saved options')
84
-    next()
85
-  })
86
-
87
-  context('custom', (assert, next) => {
88
-    var config = Config(storage, { mount: '/tmp' })
89
-    assert.same(config.options.get('mount'), '/tmp', 'mount defaults to /tmp')
90
-    next()
91
-  })
92
-})
93
-
94
-describe('set', (context) => {
95
-  var storage
96
-
97
-  context.beforeEach((c) => {
98
-    storage = tmp()
99
-  })
100
-
101
-  context.afterEach((c) => {
102
-    cleanup(storage)
103
-  })
104
-
105
-  context('byKey', (assert, next) => {
106
-    var config = Config(storage)
107
-    var group = crypto.unpack(crypto.accessKey())
108
-
109
-    config.groups.set(group.address, group)
110
-
111
-    config.save()
112
-
113
-    var reload = yaml.safeLoad(fs.readFileSync(path.join(storage, 'config.yml')))
114
-    var rawGroup = reload.groups.byKey[group.address.toString('hex')]
115
-
116
-    assert.same(config.groups.get(group.address), rawGroup, 'saves groups to storage')
117
-    next()
118
-  })
119
-
120
-  context('byName', (assert, next) => {
121
-    var config = Config(storage)
122
-    var group = Object.assign({ name: 'group-a' }, crypto.unpack(crypto.accessKey()))
123
-
124
-    config.groups.set(group.address, group)
125
-
126
-    config.save()
127
-
128
-    var reload = yaml.safeLoad(fs.readFileSync(path.join(storage, 'config.yml')))
129
-    var rawGroup = reload.groups.byKey[group.address.toString('hex')]
130
-
131
-    assert.ok(rawGroup.name, 'saves group with a name')
132
-    assert.same(config.groups.get(group.name), rawGroup, 'gets the group using a name')
133
-
134
-    var reload = Config(storage)
135
-
136
-    assert.same(reload._groups, config._groups, 'load groups from storage')
137
-    next()
138
-  })
139
-})
140
-
141
-describe('get', (context) => {
142
-  var storage
143
-
144
-  context.beforeEach((c) => {
145
-    storage = tmp()
146
-  })
147
-
148
-  context.afterEach((c) => {
149
-    cleanup(storage)
150
-  })
151
-
152
-  context('works with buffers or strings', (assert, next) => {
153
-    var config = Config(storage)
154
-    var group = crypto.unpack(crypto.accessKey())
155
-
156
-    config.groups.set(group.address, group)
157
-
158
-    config.save()
159
-
160
-    assert.ok(config.groups.get(group.address), 'get group works with a buffer')
161
-    assert.ok(config.groups.get(group.address.toString('hex')), 'get group works with a hex')
162
-
163
-    next()
164
-  })
165
-})
166
-
167
-describe('del', (context) => {
168
-  var storage
169
-
170
-  context.beforeEach((c) => {
171
-    storage = tmp()
172
-  })
173
-
174
-  context.afterEach((c) => {
175
-    cleanup(storage)
176
-  })
177
-
178
-  context('deletes relevant entry using buffers', (assert, next) => {
179
-    var config = Config(storage)
180
-    var group = crypto.unpack(crypto.accessKey())
181
-
182
-    config.groups.set(group.address, group)
183
-
184
-    config.save()
185
-    config.load()
186
-
187
-    config.groups.delete(group.address)
188
-
189
-    assert.same(config.groups.list(), [], 'deleted the group')
190
-    next()
191
-  })
192
-
193
-  context('deletes relevant entry using buffers', (assert, next) => {
194
-    var config = Config(storage)
195
-    var group = crypto.unpack(crypto.accessKey())
196
-
197
-    config.groups.set(group.address, group)
198
-
199
-    config.save()
200
-    config.load()
201
-
202
-    config.groups.delete(group.address.toString('hex'))
203
-
204
-    assert.same(config.groups.list(), [], 'deleted the group')
205
-    next()
206
-  })
207
-
208
-  context('deletes relevant entry using names', (assert, next) => {
209
-    var config = Config(storage)
210
-    var group = Object.assign({ name: 'group-a' }, crypto.unpack(crypto.accessKey()))
211
-
212
-    config.groups.set(group.address, group)
213
-
214
-    config.save()
215
-    config.load()
216
-
217
-    assert.same(config.groups.list().length, 1, 'saved the group')
218
-
219
-    config.groups.delete(group.name)
220
-
221
-    config.save()
222
-    config.load()
223
-
224
-    assert.same(config.groups.list(), [], 'deleted the group')
225
-    next()
226
-  })
227
-})

+ 143
- 0
test/key-handler.test.js View File

@@ -0,0 +1,143 @@
1
+const { describe } = require('tape-plus')
2
+const Config = require('../')
3
+const crypto = require('cobox-crypto')
4
+const fs = require('fs')
5
+const path = require('path')
6
+const yaml = require('js-yaml')
7
+
8
+const { tmp, cleanup } = require('./util')
9
+
10
+describe('set', (context) => {
11
+  var storage
12
+
13
+  context.beforeEach((c) => {
14
+    storage = tmp()
15
+  })
16
+
17
+  context.afterEach((c) => {
18
+    cleanup(storage)
19
+  })
20
+
21
+  context('byKey', (assert, next) => {
22
+    var config = Config(storage)
23
+    var group = crypto.unpack(crypto.accessKey())
24
+
25
+    config.groups.set(group.address, group)
26
+
27
+    config.save()
28
+
29
+    var reload = yaml.safeLoad(fs.readFileSync(path.join(storage, 'config.yml')))
30
+    var rawGroup = reload.groups.byKey[group.address.toString('hex')]
31
+
32
+    assert.same(config.groups.get(group.address), rawGroup, 'saves groups to storage')
33
+    next()
34
+  })
35
+
36
+  context('byName', (assert, next) => {
37
+    var config = Config(storage)
38
+    var group = Object.assign({ name: 'group-a' }, crypto.unpack(crypto.accessKey()))
39
+
40
+    config.groups.set(group.address, group)
41
+
42
+    config.save()
43
+
44
+    var reload = yaml.safeLoad(fs.readFileSync(path.join(storage, 'config.yml')))
45
+    var rawGroup = reload.groups.byKey[group.address.toString('hex')]
46
+
47
+    assert.ok(rawGroup.name, 'saves group with a name')
48
+    assert.same(config.groups.get(group.name), rawGroup, 'gets the group using a name')
49
+
50
+    var reload = Config(storage)
51
+
52
+    assert.same(reload._groups, config._groups, 'load groups from storage')
53
+    next()
54
+  })
55
+})
56
+
57
+describe('get', (context) => {
58
+  var storage
59
+
60
+  context.beforeEach((c) => {
61
+    storage = tmp()
62
+  })
63
+
64
+  context.afterEach((c) => {
65
+    cleanup(storage)
66
+  })
67
+
68
+  context('works with buffers or strings', (assert, next) => {
69
+    var config = Config(storage)
70
+    var group = crypto.unpack(crypto.accessKey())
71
+
72
+    config.groups.set(group.address, group)
73
+
74
+    config.save()
75
+
76
+    assert.ok(config.groups.get(group.address), 'get group works with a buffer')
77
+    assert.ok(config.groups.get(group.address.toString('hex')), 'get group works with a hex')
78
+
79
+    next()
80
+  })
81
+})
82
+
83
+describe('del', (context) => {
84
+  var storage
85
+
86
+  context.beforeEach((c) => {
87
+    storage = tmp()
88
+  })
89
+
90
+  context.afterEach((c) => {
91
+    cleanup(storage)
92
+  })
93
+
94
+  context('deletes relevant entry using buffers', (assert, next) => {
95
+    var config = Config(storage)
96
+    var group = crypto.unpack(crypto.accessKey())
97
+
98
+    config.groups.set(group.address, group)
99
+
100
+    config.save()
101
+    config.load()
102
+
103
+    config.groups.delete(group.address)
104
+
105
+    assert.same(config.groups.list(), [], 'deleted the group')
106
+    next()
107
+  })
108
+
109
+  context('deletes relevant entry using buffers', (assert, next) => {
110
+    var config = Config(storage)
111
+    var group = crypto.unpack(crypto.accessKey())
112
+
113
+    config.groups.set(group.address, group)
114
+
115
+    config.save()
116
+    config.load()
117
+
118
+    config.groups.delete(group.address.toString('hex'))
119
+
120
+    assert.same(config.groups.list(), [], 'deleted the group')
121
+    next()
122
+  })
123
+
124
+  context('deletes relevant entry using names', (assert, next) => {
125
+    var config = Config(storage)
126
+    var group = Object.assign({ name: 'group-a' }, crypto.unpack(crypto.accessKey()))
127
+
128
+    config.groups.set(group.address, group)
129
+
130
+    config.save()
131
+    config.load()
132
+
133
+    assert.same(config.groups.list().length, 1, 'saved the group')
134
+
135
+    config.groups.delete(group.name)
136
+
137
+    config.save()
138
+    config.load()
139
+
140
+    assert.same(config.groups.list(), [], 'deleted the group')
141
+    next()
142
+  })
143
+})