Browse Source

trying out proxyquire and sinon

Kieran Gibb 2 years ago
parent
commit
b969b880b7
No known key found for this signature in database
9 changed files with 143 additions and 19 deletions
  1. 6
    0
      constants.js
  2. 2
    11
      index.js
  3. 4
    2
      lib/groups.js
  4. 4
    6
      lib/request.js
  5. 3
    0
      package.json
  6. 7
    0
      test/fixtures/groups.json
  7. 89
    0
      test/groups.test.js
  8. 0
    0
      test/index.test.js
  9. 28
    0
      test/request.test.js

+ 6
- 0
constants.js View File

@@ -0,0 +1,6 @@
1
+const BASE_URL = 'http://localhost:3000'
2
+
3
+module.exports = {
4
+  BASE_URL
5
+}
6
+

+ 2
- 11
index.js View File

@@ -5,8 +5,6 @@ const assert = require('assert')
5 5
 const crypto = require('crypto')
6 6
 const { isObject } = require('util')
7 7
 
8
-const BASE_URL = 'http://localhost:3000'
9
-
10 8
 const Groups = require('./lib/groups')
11 9
 const Replicators = require('./lib/replicators')
12 10
 
@@ -18,14 +16,7 @@ module.exports = class Client {
18 16
     this._id = opts._id || crypto.randombytes(2).toString('hex')
19 17
     debug(this._id, 'client aimed at', this.endpoint, opts)
20 18
 
21
-    this.groups = Object.assign(
22
-      new CRUDResource(endpoint, 'groups', opts),
23
-      new Groups(endpoint, opts)
24
-    )
25
-
26
-    this.groups = Object.assign(
27
-      new CRUDResource(endpoint, 'replicators', opts),
28
-      new Replicators(endpoint, opts)
29
-    )
19
+    this.groups = new Groups(endpoint, opts)
20
+    this.replicators = new Replicators(endpoint, opts)
30 21
   }
31 22
 }

+ 4
- 2
lib/groups.js View File

@@ -1,11 +1,13 @@
1 1
 const path = require('path')
2 2
 const request = require('./request')
3 3
 const debug = require('debug')('cobox-client')
4
+const constants = require('../constants')
5
+const Connections = require('./connections')
6
+const Mounts = require('./mounts')
4 7
 
5 8
 module.exports = class Groups {
6
-  constructor (endpoint, opts = {}) {
9
+  constructor (endpoint = constants.BASE_URL, opts = {}) {
7 10
     this.endpoint = path.join(endpoint, 'groups')
8
-
9 11
     this.connections = new Connections(this.endpoint, opts)
10 12
     this.mounts = new Mounts(this.endpoint, opts)
11 13
   }

+ 4
- 6
lib/request.js View File

@@ -2,19 +2,17 @@ const axios = require('axios')
2 2
 const debug = require('debug')('cobox-client')
3 3
 
4 4
 module.exports = async (opts) => {
5
-  const axiosOpts = {
6
-    method: opts.method || 'GET',
7
-    url: opts.url,
5
+  Object.assign(opts, {
8 6
     maxRedirects: 0,
9 7
     headers: { 'Content-Type': 'application/json' },
10 8
     data: opts.data || {},
11 9
     responseType: opts.responseType
12
-  }
10
+  })
13 11
 
14
-  debug('request', axiosOpts)
12
+  debug('request', opts)
15 13
 
16 14
   try {
17
-    const response = await axios.request(axiosOpts)
15
+    const response = await axios.request(opts)
18 16
     return response.data
19 17
   } catch (error) {
20 18
     return organiseErrors(error)

+ 3
- 0
package.json View File

@@ -3,6 +3,9 @@
3 3
   "version": "1.0.0",
4 4
   "description": "client for cobox",
5 5
   "main": "index.js",
6
+  "scripts": {
7
+    "test": "tape test/**/*.test.js | tap-spec"
8
+  },
6 9
   "repository": "https://ledger-git.dyne.org/cobox/cobox-client",
7 10
   "author": "magma collective",
8 11
   "license": "AGPL-3.0-or-later",

+ 7
- 0
test/fixtures/groups.json View File

@@ -0,0 +1,7 @@
1
+[
2
+  {
3
+    "name": "magma",
4
+    "address": "fb81f479cc3b8656fd6bc7e75edc099e5ca9df847d5cbc5c7919d9bd917e9c46",
5
+    "encryptionKey": "21fa68fc77be8b9060c585e234d9dc33d43c1032bdd695267296fd5fa7a40416"
6
+  }
7
+]

+ 89
- 0
test/groups.test.js View File

@@ -0,0 +1,89 @@
1
+const { describe } = require('tape-plus')
2
+const sinon = require('sinon')
3
+const Groups = require('../lib/groups')
4
+const constants = require('../constants')
5
+
6
+const data = require('./fixtures/groups.json')
7
+
8
+describe('groups', (context) => {
9
+  context('all()', async (assert, next) => {
10
+    var client = new Groups()
11
+
12
+    sinon.stub(client, 'all').resolves(data)
13
+
14
+    const groups = await client.all()
15
+
16
+    sinon.assert.calledOnce(client.all)
17
+    assert.same(groups, data, 'returns the data')
18
+
19
+    sinon.restore()
20
+    next()
21
+  })
22
+
23
+  context('find(id)', async (assert, next) => {
24
+    var client = new Groups()
25
+
26
+    sinon.stub(client, 'find').resolves(data[0])
27
+
28
+    const group = await client.find(data[0].name)
29
+
30
+    sinon.assert.calledOnce(client.find)
31
+    assert.same(group, data[0], 'returns the data')
32
+
33
+    sinon.restore()
34
+    next()
35
+  })
36
+
37
+  context('create(params)', async (assert, next) => {
38
+    var params = data[0]
39
+    var client = new Groups()
40
+
41
+    sinon.stub(client, 'create').resolves(params)
42
+
43
+    const group = await client.create(params)
44
+
45
+    sinon.assert.calledOnce(client.create)
46
+    assert.same(group, params, 'returns the data')
47
+
48
+    sinon.restore()
49
+    next()
50
+  })
51
+
52
+  context('join(params) with successful find', async (assert, next) => {
53
+    var params = data[0]
54
+    var client = new Groups()
55
+
56
+    sinon.stub(client, 'find').resolves(params)
57
+    sinon.stub(client.connections, 'create').resolves(params)
58
+
59
+    const group = await client.join(params)
60
+
61
+    sinon.assert.calledOnce(client.find)
62
+    sinon.assert.calledOnce(client.connections.create)
63
+    assert.same(group, params, 'returns the data')
64
+
65
+    sinon.restore()
66
+    next()
67
+  })
68
+
69
+  context('join(params) with failed find', async (assert, next) => {
70
+    var params = data[0]
71
+    var errors = [{ msg: 'group does not exist, please provide an address' }]
72
+    var client = new Groups()
73
+
74
+    sinon.stub(client, 'find').resolves({ errors })
75
+    sinon.stub(client, 'create').resolves(params)
76
+    sinon.stub(client.connections, 'create').resolves(params)
77
+
78
+    const group = await client.join(params)
79
+
80
+    sinon.assert.calledOnce(client.find)
81
+    sinon.assert.calledOnce(client.create)
82
+    sinon.assert.calledOnce(client.connections.create)
83
+    assert.same(group, params, 'returns the data')
84
+
85
+    sinon.restore()
86
+    next()
87
+  })
88
+})
89
+

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


+ 28
- 0
test/request.test.js View File

@@ -0,0 +1,28 @@
1
+const { describe } = require('tape-plus')
2
+const sinon = require('sinon')
3
+const proxyquire = require('proxyquire')
4
+const constants = require('../constants')
5
+const axios = require('axios')
6
+
7
+describe('request', (context) => {
8
+  context('success', async (assert, next) => {
9
+    var data = { success: true }
10
+    var stub = sinon
11
+      .stub(axios, 'request')
12
+      .resolves({ data })
13
+
14
+    var request = proxyquire('../lib/request', { axios: stub })
15
+
16
+    var opts = {
17
+      method: 'GET',
18
+      url: constants.BASE_URL
19
+    }
20
+
21
+    var response = await request(opts)
22
+
23
+    assert.same(response, data, 'returns a data object')
24
+    sinon.assert.calledWith(stub.request, opts)
25
+    sinon.restore()
26
+    next()
27
+  })
28
+})