|
1 | 1 | /* eslint-env mocha */
|
2 | 2 |
|
3 |
| -// const expect = require('chai').expect |
4 |
| -// const APIctl = require('ipfs-api') |
| 3 | +const expect = require('chai').expect |
| 4 | +const fs = require('fs') |
| 5 | +const APIctl = require('ipfs-api') |
| 6 | + |
| 7 | +describe('config', () => { |
| 8 | + const configPath = process.cwd() + '/tests/repo-tests-run/config' |
| 9 | + const updatedConfig = () => JSON.parse(fs.readFileSync(configPath, 'utf8')) |
5 | 10 |
|
6 |
| -describe('version', () => { |
7 | 11 | describe('api', () => {
|
8 |
| - // TODO |
9 |
| - }) |
| 12 | + var api |
| 13 | + |
| 14 | + before('api', (done) => { |
| 15 | + api = require('../../src/http-api').server.select('API') |
| 16 | + done() |
| 17 | + }) |
| 18 | + |
| 19 | + describe('/config', () => { |
| 20 | + it('returns 400 for request without arguments', (done) => { |
| 21 | + api.inject({ |
| 22 | + method: 'POST', |
| 23 | + url: '/api/v0/config' |
| 24 | + }, res => { |
| 25 | + expect(res.statusCode).to.equal(400) |
| 26 | + done() |
| 27 | + }) |
| 28 | + }) |
| 29 | + |
| 30 | + it('returns 500 for request with invalid argument', (done) => { |
| 31 | + api.inject({ |
| 32 | + method: 'POST', |
| 33 | + url: '/api/v0/config?arg=kitten' |
| 34 | + }, res => { |
| 35 | + expect(res.statusCode).to.equal(500) |
| 36 | + expect(res.result.Code).to.equal(0) |
| 37 | + expect(res.result.Message).to.be.a('string') |
| 38 | + done() |
| 39 | + }) |
| 40 | + }) |
| 41 | + |
| 42 | + it('returns value for request with argument', (done) => { |
| 43 | + api.inject({ |
| 44 | + method: 'POST', |
| 45 | + url: '/api/v0/config?arg=API.HTTPHeaders' |
| 46 | + }, res => { |
| 47 | + expect(res.statusCode).to.equal(200) |
| 48 | + expect(res.result.Key).to.equal('API.HTTPHeaders') |
| 49 | + expect(res.result.Value).to.equal(null) |
| 50 | + done() |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + it('returns value for request as subcommand', (done) => { |
| 55 | + api.inject({ |
| 56 | + method: 'POST', |
| 57 | + url: '/api/v0/config/API.HTTPHeaders' |
| 58 | + }, res => { |
| 59 | + expect(res.statusCode).to.equal(200) |
| 60 | + expect(res.result.Key).to.equal('API.HTTPHeaders') |
| 61 | + expect(res.result.Value).to.equal(null) |
| 62 | + done() |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + it('updates value for request with both args', (done) => { |
| 67 | + api.inject({ |
| 68 | + method: 'POST', |
| 69 | + url: '/api/v0/config?arg=Datastore.Path&arg=kitten' |
| 70 | + }, res => { |
| 71 | + expect(res.statusCode).to.equal(200) |
| 72 | + expect(res.result.Key).to.equal('Datastore.Path') |
| 73 | + expect(res.result.Value).to.equal('kitten') |
| 74 | + expect(updatedConfig().Datastore.Path).to.equal('kitten') |
| 75 | + |
| 76 | + done() |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + it('returns 500 value for request with both args and JSON flag with invalid JSON argument', (done) => { |
| 81 | + api.inject({ |
| 82 | + method: 'POST', |
| 83 | + url: '/api/v0/config?arg=Datastore.Path&arg=kitten&json' |
| 84 | + }, res => { |
| 85 | + expect(res.statusCode).to.equal(500) |
| 86 | + expect(res.result.Code).to.equal(0) |
| 87 | + expect(res.result.Message).to.be.a('string') |
| 88 | + |
| 89 | + done() |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + it('updates value for request with both args and JSON flag with valid JSON argument', (done) => { |
| 94 | + api.inject({ |
| 95 | + method: 'POST', |
| 96 | + url: '/api/v0/config?arg=Datastore.Path&arg={\"kitten\": true}&json' |
| 97 | + }, res => { |
| 98 | + expect(res.statusCode).to.equal(200) |
| 99 | + expect(res.result.Key).to.equal('Datastore.Path') |
| 100 | + expect(res.result.Value).to.deep.equal({ kitten: true }) |
| 101 | + expect(updatedConfig().Datastore.Path).to.deep.equal({ kitten: true }) |
| 102 | + |
| 103 | + done() |
| 104 | + }) |
| 105 | + }) |
| 106 | + |
| 107 | + it('updates value for request with both args and bool flag and true argument', (done) => { |
| 108 | + api.inject({ |
| 109 | + method: 'POST', |
| 110 | + url: '/api/v0/config?arg=Datastore.Path&arg=true&bool' |
| 111 | + }, res => { |
| 112 | + expect(res.statusCode).to.equal(200) |
| 113 | + expect(res.result.Key).to.equal('Datastore.Path') |
| 114 | + expect(res.result.Value).to.deep.equal(true) |
| 115 | + expect(updatedConfig().Datastore.Path).to.deep.equal(true) |
| 116 | + |
| 117 | + done() |
| 118 | + }) |
| 119 | + }) |
| 120 | + |
| 121 | + it('updates value for request with both args and bool flag and false argument', (done) => { |
| 122 | + api.inject({ |
| 123 | + method: 'POST', |
| 124 | + url: '/api/v0/config?arg=Datastore.Path&arg=false&bool' |
| 125 | + }, res => { |
| 126 | + expect(res.statusCode).to.equal(200) |
| 127 | + expect(res.result.Key).to.equal('Datastore.Path') |
| 128 | + expect(res.result.Value).to.deep.equal(false) |
| 129 | + expect(updatedConfig().Datastore.Path).to.deep.equal(false) |
10 | 130 |
|
11 |
| - describe('gateway', () => { |
12 |
| - // TODO |
| 131 | + done() |
| 132 | + }) |
| 133 | + }) |
| 134 | + }) |
13 | 135 | })
|
14 | 136 |
|
15 | 137 | describe('using js-ipfs-api', () => {
|
16 |
| -// var ctl |
| 138 | + var ctl |
17 | 139 |
|
18 |
| - it('start IPFS API ctl', (done) => { |
19 |
| -// ctl = APIctl('/ip4/127.0.0.1/tcp/6001') |
| 140 | + before('start IPFS API ctl', (done) => { |
| 141 | + ctl = APIctl('/ip4/127.0.0.1/tcp/6001') |
20 | 142 | done()
|
21 | 143 | })
|
| 144 | + |
| 145 | + describe('ipfs.config', () => { |
| 146 | + it('returns error for request without arguments', (done) => { |
| 147 | + ctl.config.get(null, (err, res) => { |
| 148 | + expect(err).to.exist |
| 149 | + |
| 150 | + done() |
| 151 | + }) |
| 152 | + }) |
| 153 | + |
| 154 | + it('returns error for request with invalid argument', (done) => { |
| 155 | + ctl.config.get('kittens', (err, res) => { |
| 156 | + expect(err).to.exist |
| 157 | + |
| 158 | + done() |
| 159 | + }) |
| 160 | + }) |
| 161 | + |
| 162 | + it('returns value for request with argument', (done) => { |
| 163 | + ctl.config.get('API.HTTPHeaders', (err, res) => { |
| 164 | + expect(err).not.to.exist |
| 165 | + expect(res.Key).to.equal('API.HTTPHeaders') |
| 166 | + expect(res.Value).to.equal(null) |
| 167 | + |
| 168 | + done() |
| 169 | + }) |
| 170 | + }) |
| 171 | + |
| 172 | + it('updates value for request with both args', (done) => { |
| 173 | + ctl.config.set('Datastore.Path', 'kitten', (err, res) => { |
| 174 | + expect(err).not.to.exist |
| 175 | + expect(res.Key).to.equal('Datastore.Path') |
| 176 | + expect(res.Value).to.equal('kitten') |
| 177 | + expect(updatedConfig().Datastore.Path).to.equal('kitten') |
| 178 | + |
| 179 | + done() |
| 180 | + }) |
| 181 | + }) |
| 182 | + |
| 183 | + it('returns error for request with both args and JSON flag with invalid JSON argument', (done) => { |
| 184 | + ctl.config.set('Datastore.Path', 'kitten', { json: true }, (err, res) => { |
| 185 | + expect(err).to.exist |
| 186 | + |
| 187 | + done() |
| 188 | + }) |
| 189 | + }) |
| 190 | + |
| 191 | + it('updates value for request with both args and JSON flag with valid JSON argument', (done) => { |
| 192 | + ctl.config.set('Datastore.Path', JSON.stringify({ kitten: true }), { json: true }, (err, res) => { |
| 193 | + expect(err).not.to.exist |
| 194 | + expect(res.Key).to.equal('Datastore.Path') |
| 195 | + expect(res.Value).to.deep.equal({ kitten: true }) |
| 196 | + expect(updatedConfig().Datastore.Path).to.deep.equal({ kitten: true }) |
| 197 | + |
| 198 | + done() |
| 199 | + }) |
| 200 | + }) |
| 201 | + |
| 202 | + it('updates value for request with both args and bool flag and true argument', (done) => { |
| 203 | + ctl.config.set('Datastore.Path', true, { bool: true }, (err, res) => { |
| 204 | + expect(err).not.to.exist |
| 205 | + expect(res.Key).to.equal('Datastore.Path') |
| 206 | + expect(res.Value).to.deep.equal(true) |
| 207 | + expect(updatedConfig().Datastore.Path).to.deep.equal(true) |
| 208 | + |
| 209 | + done() |
| 210 | + }) |
| 211 | + }) |
| 212 | + |
| 213 | + it('updates value for request with both args and bool flag and false argument', (done) => { |
| 214 | + ctl.config.set('Datastore.Path', false, { bool: true }, (err, res) => { |
| 215 | + expect(err).not.to.exist |
| 216 | + expect(res.Key).to.equal('Datastore.Path') |
| 217 | + expect(res.Value).to.deep.equal(false) |
| 218 | + expect(updatedConfig().Datastore.Path).to.deep.equal(false) |
| 219 | + |
| 220 | + done() |
| 221 | + }) |
| 222 | + }) |
| 223 | + }) |
22 | 224 | })
|
23 | 225 | })
|
0 commit comments