|
1 | 1 | /* eslint-env mocha */ |
| 2 | +'use strict' |
2 | 3 |
|
3 | 4 | const expect = require('chai').expect |
4 | 5 | const fs = require('fs') |
5 | 6 | const APIctl = require('ipfs-api') |
| 7 | +const FormData = require('form-data') |
| 8 | +const streamToPromise = require('stream-to-promise') |
6 | 9 |
|
7 | 10 | describe('config', () => { |
8 | 11 | const configPath = process.cwd() + '/tests/repo-tests-run/config' |
| 12 | + const originalConfigPath = process.cwd() + '/tests/repo-example/config' |
9 | 13 | const updatedConfig = () => JSON.parse(fs.readFileSync(configPath, 'utf8')) |
| 14 | + const restoreConfig = () => fs.writeFileSync(configPath, fs.readFileSync(originalConfigPath, 'utf8'), 'utf8') |
10 | 15 |
|
11 | 16 | describe('api', () => { |
12 | 17 | var api |
@@ -143,6 +148,69 @@ describe('config', () => { |
143 | 148 | done() |
144 | 149 | }) |
145 | 150 | }) |
| 151 | + |
| 152 | + describe('/config/replace', () => { |
| 153 | + it('returns 400 if no config is provided', (done) => { |
| 154 | + const form = new FormData() |
| 155 | + const headers = form.getHeaders() |
| 156 | + |
| 157 | + streamToPromise(form).then(payload => { |
| 158 | + api.inject({ |
| 159 | + method: 'POST', |
| 160 | + url: '/api/v0/config/replace', |
| 161 | + headers: headers, |
| 162 | + payload: payload |
| 163 | + }, res => { |
| 164 | + expect(res.statusCode).to.equal(400) |
| 165 | + done() |
| 166 | + }) |
| 167 | + }) |
| 168 | + }) |
| 169 | + |
| 170 | + it('returns 500 if the config is invalid', (done) => { |
| 171 | + const form = new FormData() |
| 172 | + const filePath = 'tests/badconfig' |
| 173 | + form.append('file', fs.createReadStream(filePath)) |
| 174 | + const headers = form.getHeaders() |
| 175 | + |
| 176 | + streamToPromise(form).then(payload => { |
| 177 | + api.inject({ |
| 178 | + method: 'POST', |
| 179 | + url: '/api/v0/config/replace', |
| 180 | + headers: headers, |
| 181 | + payload: payload |
| 182 | + }, res => { |
| 183 | + expect(res.statusCode).to.equal(500) |
| 184 | + done() |
| 185 | + }) |
| 186 | + }) |
| 187 | + }) |
| 188 | + |
| 189 | + it('updates value', (done) => { |
| 190 | + const form = new FormData() |
| 191 | + const filePath = 'tests/otherconfig' |
| 192 | + form.append('file', fs.createReadStream(filePath)) |
| 193 | + const headers = form.getHeaders() |
| 194 | + const expectedConfig = JSON.parse(fs.readFileSync(filePath, 'utf8')) |
| 195 | + |
| 196 | + streamToPromise(form).then(payload => { |
| 197 | + api.inject({ |
| 198 | + method: 'POST', |
| 199 | + url: '/api/v0/config/replace', |
| 200 | + headers: headers, |
| 201 | + payload: payload |
| 202 | + }, res => { |
| 203 | + expect(res.statusCode).to.equal(200) |
| 204 | + expect(updatedConfig()).to.deep.equal(expectedConfig) |
| 205 | + done() |
| 206 | + }) |
| 207 | + }) |
| 208 | + }) |
| 209 | + |
| 210 | + after(() => { |
| 211 | + restoreConfig() |
| 212 | + }) |
| 213 | + }) |
146 | 214 | }) |
147 | 215 |
|
148 | 216 | describe('using js-ipfs-api', () => { |
@@ -240,5 +308,27 @@ describe('config', () => { |
240 | 308 | done() |
241 | 309 | }) |
242 | 310 | }) |
| 311 | + |
| 312 | + describe('ipfs.config.replace', () => { |
| 313 | + it('returns error if the config is invalid', (done) => { |
| 314 | + const filePath = 'tests/badconfig' |
| 315 | + |
| 316 | + ctl.config.replace(filePath, (err) => { |
| 317 | + expect(err).to.exist |
| 318 | + done() |
| 319 | + }) |
| 320 | + }) |
| 321 | + |
| 322 | + it('updates value', (done) => { |
| 323 | + const filePath = 'tests/otherconfig' |
| 324 | + const expectedConfig = JSON.parse(fs.readFileSync(filePath, 'utf8')) |
| 325 | + |
| 326 | + ctl.config.replace(filePath, (err) => { |
| 327 | + expect(err).not.to.exist |
| 328 | + expect(expectedConfig).to.deep.equal(updatedConfig()) |
| 329 | + done() |
| 330 | + }) |
| 331 | + }) |
| 332 | + }) |
243 | 333 | }) |
244 | 334 | }) |
0 commit comments