Skip to content

Commit

Permalink
patch getIndexerSettings to support Dicoogle <=2.3.1, add more tests …
Browse files Browse the repository at this point in the history
…to validate patche
  • Loading branch information
Enet4 committed Mar 3, 2016
1 parent fd74cf8 commit 7385ea9
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 56 deletions.
59 changes: 31 additions & 28 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,46 +455,49 @@ DicoogleAccess.prototype.IndexerSettings = IndexerSettings;
field = undefined;
}
const url = [url_, Endpoints.INDEXER_SETTINGS];
let all = true;
if (typeof field === 'string') {
all = false;
url.push(encodeURIComponent(field));
if (process.env.NODE_ENV !== 'production') {
const values = Object.keys(IndexerSettings).map(k => IndexerSettings[k]);
/* eslint-disable no-console */
const values = Object.keys(IndexerSettings).map(k => IndexerSettings[k]); // values()
if (values.indexOf(field) === -1) {
/* eslint-disable no-console */
console.error(`Warning: Attempting to get unrecognized indexer setting '${field}'.`);
/* eslint-enable no-console */
}
/* eslint-enable no-console */
}
// do not use the wrapper, or else we'll lose the output
const uri = url.join('/');
let req = request.get(uri);
if (token_) {
req = req.set('Authorization', token_);
}
// do not use the wrapper, or else we'll lose the output
let req = request.get(url.join('/'));
if (token_) {
req = req.set('Authorization', token_);
}
req.end(function (err, res) {
if (err) {
callback(err);
return;
}
req.end(function (err, res) {
if (err) {
callback(err);
return;
let out;
if (all) {
if (Object.getOwnPropertyNames(res.body).length === 0) {
out = JSON.parse(res.text);
} else {
out = res.body;
}
let out = res.text;
if ('effort' in out) out.effort = +out.effort;
if ('thumbnailSize' in out) out.thumbnailSize = +out.thumbnailSize;
} else {
out = res.text;
if (field === 'effort' || field === 'thumbnailSize') {
out = +out;
}
callback(null, out);
});
return;
}
serviceRequest('GET', url, {}, (error, data) => {
if (error) {
callback(error);
return;
}
if (typeof data === 'object') {
if ('effort' in data) data.effort = +data.effort;
if ('thumbnailSize' in data) data.thumbnailSize = +data.thumbnailSize;
}
callback(null, data);
}, token_);
if (field === 'watcher' || field === 'thumbnail' || field === 'zip') {
out = (out === 'true');
}
}
callback(null, out);
});
};

/** Set a particular Indexer setting. A valid field and value is required.
Expand Down
34 changes: 20 additions & 14 deletions test/mock/service-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ module.exports = function createDicoogleMock() {
}
];

var INDEXER_SETTINGS = {
path: '/opt/data',
zip: false,
effort: '100',
thumbnail: true,
thumbnailSize: '128',
watcher: false
};

nock(BASE_URL)
// mock /version
.get('/ext/version')
Expand Down Expand Up @@ -180,28 +189,25 @@ module.exports = function createDicoogleMock() {
})

// mock indexer settings
.get('/management/settings/index')
.reply(200, {
path: '/opt/data',
zip: false,
effort: '100',
thumbnail: true,
thumbnailSize: '128',
watcher: false
})

nock(BASE_URL).get('/management/settings/index')
.once().reply(200, JSON.stringify(INDEXER_SETTINGS)); // in Dicoogle 2.3.1
nock(BASE_URL).get('/management/settings/index')
.reply(200, INDEXER_SETTINGS); // with patched Dicoogle
// getters
nock(BASE_URL)
.get('/management/settings/index/path')
.reply(200, '/opt/data')
.reply(200, INDEXER_SETTINGS.path)
.get('/management/settings/index/zip')
.reply(200, false)
.reply(200, INDEXER_SETTINGS.zip)
.get('/management/settings/index/effort')
.reply(200, '100')
.reply(200, INDEXER_SETTINGS.effort)
.get('/management/settings/index/thumbnail')
.reply(200, true)
.reply(200, INDEXER_SETTINGS.thumbnail)
.get('/management/settings/index/thumbnailSize')
.reply(200, '128')
.get('/management/settings/index/watcher')
.reply(200, false)
.reply(200, INDEXER_SETTINGS.watcher)

// mock indexer settings setters
.post('/management/settings/index/path')
Expand Down
66 changes: 52 additions & 14 deletions test/test-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,28 +209,66 @@ describe('Dicoogle Client (under Node.js)', function() {
});

describe('#getIndexerSettings() all', function() {
it("should give indexer settings with no error", function(done) {
Dicoogle.getIndexerSettings(function (error, data) {
assert.equal(error, null);
assert.strictEqual(typeof data.path, 'string', 'path must be a string');
assert.strictEqual(typeof data.effort, 'number', 'effort must be a number');
assert.strictEqual(typeof data.watcher, 'boolean', 'watcher must be a boolean');
assert.strictEqual(typeof data.thumbnail, 'boolean', 'thumbnail must be a boolean');
assert.strictEqual(typeof data.zip, 'boolean', 'zip must be a boolean');
assert.strictEqual(typeof data.thumbnailSize, 'number', 'thumbnailSize must be a string');
done();
});
});
function testIndexerSettings(done) {
Dicoogle.getIndexerSettings(function (error, data) {
assert.equal(error, null);
assert.strictEqual(typeof data, 'object');
assert.strictEqual(typeof data.path, 'string', 'path must be a string');
assert.strictEqual(typeof data.effort, 'number', 'effort must be a number');
assert.strictEqual(typeof data.watcher, 'boolean', 'watcher must be a boolean');
assert.strictEqual(typeof data.thumbnail, 'boolean', 'thumbnail must be a boolean');
assert.strictEqual(typeof data.zip, 'boolean', 'zip must be a boolean');
assert.strictEqual(typeof data.thumbnailSize, 'number', 'thumbnailSize must be a string');
done();
});
}
it("give indexer settings with no error (2.3.1)", testIndexerSettings);
it("give indexer settings with no error (patched)", testIndexerSettings);
});

describe('#getIndexerSettings() path only', function() {
it("should give a path with no error", function(done) {
describe('#getIndexerSettings() one by one', function() {
it("should give path, no error", function(done) {
Dicoogle.getIndexerSettings(Dicoogle.IndexerSettings.PATH, function (error, data) {
assert.equal(error, null);
assert.strictEqual(data, '/opt/data', 'outcome must be path "/opt/data"');
done();
});
});
it("should give effort, no error", function(done) {
Dicoogle.getIndexerSettings(Dicoogle.IndexerSettings.EFFORT, function (error, data) {
assert.equal(error, null);
assert.strictEqual(typeof data, 'number');
done();
});
});
it("should give watcher = false, no error", function(done) {
Dicoogle.getIndexerSettings(Dicoogle.IndexerSettings.WATCHER, function (error, data) {
assert.equal(error, null);
assert.strictEqual(data, false);
done();
});
});
it("should give thumbnail = true, no error", function(done) {
Dicoogle.getIndexerSettings(Dicoogle.IndexerSettings.INDEX_THUMBNAIL, function (error, data) {
assert.equal(error, null);
assert.strictEqual(data, true);
done();
});
});
it("should give thumbnailSize, no error", function(done) {
Dicoogle.getIndexerSettings(Dicoogle.IndexerSettings.THUMBNAIL_SIZE, function (error, data) {
assert.equal(error, null);
assert.strictEqual(typeof data, 'number');
done();
});
});
it("should give zip, no error", function(done) {
Dicoogle.getIndexerSettings(Dicoogle.IndexerSettings.ZIP, function (error, data) {
assert.equal(error, null);
assert.strictEqual(typeof data, 'boolean');
done();
});
});
});

describe('#setIndexerSettings() path only', function() {
Expand Down

0 comments on commit 7385ea9

Please sign in to comment.