2018-03-24 15:37:35 +00:00
|
|
|
const test = require('../testlib');
|
2018-03-24 12:32:34 +00:00
|
|
|
const cartesianProduct = require('cartesian-product');
|
|
|
|
|
2018-03-24 15:37:35 +00:00
|
|
|
test.run(async function () {
|
2018-03-26 17:32:32 +00:00
|
|
|
await test('admin', async function (assert, req) {
|
2018-03-24 14:09:33 +00:00
|
|
|
//GET /domains?page=5&pagesize=10&query=foo&sort=id-asc,name-desc,type-asc,records-asc&type=MASTER
|
2018-03-24 12:32:34 +00:00
|
|
|
|
2018-03-24 14:09:33 +00:00
|
|
|
//Test sorting in all combinations
|
|
|
|
const sortCombinations = cartesianProduct([
|
|
|
|
['', 'id-asc', 'id-desc'],
|
|
|
|
['', 'name-asc', 'name-desc'],
|
|
|
|
['', 'type-asc', 'type-desc'],
|
|
|
|
['', 'records-asc', 'records-desc']
|
|
|
|
]);
|
2018-03-24 12:32:34 +00:00
|
|
|
|
2018-03-24 14:09:33 +00:00
|
|
|
for (list of sortCombinations) {
|
|
|
|
list = list.filter((str) => str.length > 0);
|
|
|
|
var sortQuery = list.join(',');
|
2018-03-24 12:32:34 +00:00
|
|
|
|
2018-03-24 14:09:33 +00:00
|
|
|
var res = await req({
|
|
|
|
url: '/domains?sort=' + sortQuery,
|
|
|
|
method: 'get'
|
|
|
|
});
|
2018-03-24 12:32:34 +00:00
|
|
|
|
2018-03-24 14:09:33 +00:00
|
|
|
assert.equal(res.status, 200);
|
2018-03-24 12:32:34 +00:00
|
|
|
|
2018-03-24 14:09:33 +00:00
|
|
|
var sortedData = res.data.results.slice();
|
|
|
|
sortedData.sort(function (a, b) {
|
|
|
|
for (sort of list) {
|
|
|
|
var spec = sort.split('-');
|
|
|
|
if (a[spec[0]] < b[spec[0]]) {
|
|
|
|
return spec[1] == 'asc' ? -1 : 1;
|
|
|
|
} else if (a[spec[0]] > b[spec[0]]) {
|
|
|
|
return spec[1] == 'asc' ? 1 : -1;
|
|
|
|
}
|
2018-03-24 12:32:34 +00:00
|
|
|
}
|
2018-03-24 14:09:33 +00:00
|
|
|
return 0;
|
|
|
|
});
|
2018-03-24 12:32:34 +00:00
|
|
|
|
2018-03-24 14:09:33 +00:00
|
|
|
assert.equal(res.data.results, sortedData, 'Sort failed for ' + res.config.url);
|
|
|
|
}
|
2018-03-24 12:32:34 +00:00
|
|
|
|
2018-03-24 14:09:33 +00:00
|
|
|
//Test paging
|
|
|
|
var res = await req({
|
|
|
|
url: '/domains?pagesize=3',
|
|
|
|
method: 'get'
|
|
|
|
});
|
2018-03-24 12:32:34 +00:00
|
|
|
|
2018-03-24 14:09:33 +00:00
|
|
|
assert.equal(res.status, 200, 'Status should be OK');
|
|
|
|
assert.equal(res.data.paging, {
|
|
|
|
page: 1,
|
|
|
|
total: 2,
|
|
|
|
pagesize: 3
|
|
|
|
}, 'Paging data fail for ' + res.config.url);
|
|
|
|
assert.equal(res.data.results.length, 3, "Should be 3 results.");
|
2018-03-24 12:32:34 +00:00
|
|
|
|
2018-03-24 14:09:33 +00:00
|
|
|
var res = await req({
|
|
|
|
url: '/domains?pagesize=3&page=2',
|
|
|
|
method: 'get'
|
|
|
|
});
|
2018-03-24 12:32:34 +00:00
|
|
|
|
2018-03-24 14:09:33 +00:00
|
|
|
assert.equal(res.status, 200, 'Status should be OK');
|
|
|
|
assert.equal(res.data.paging, {
|
|
|
|
page: 2,
|
|
|
|
total: 2,
|
|
|
|
pagesize: 3
|
|
|
|
}, 'Paging data fail for ' + res.config.url);
|
|
|
|
assert.equal(res.data.results.length, 2, "Should be 2 results.");
|
2018-03-24 12:32:34 +00:00
|
|
|
|
2018-03-24 14:09:33 +00:00
|
|
|
//Test query
|
|
|
|
var res = await req({
|
|
|
|
url: '/domains?query=.net&sort=id-asc',
|
|
|
|
method: 'get'
|
|
|
|
});
|
2018-03-24 12:32:34 +00:00
|
|
|
|
2018-03-24 14:09:33 +00:00
|
|
|
assert.equal(res.status, 200, 'Status should be OK');
|
|
|
|
assert.equal(res.data.results, [
|
|
|
|
{
|
|
|
|
id: 2,
|
|
|
|
name: 'slave.example.net',
|
|
|
|
type: 'SLAVE',
|
|
|
|
master: '12.34.56.78',
|
|
|
|
records: 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 4,
|
|
|
|
name: 'bar.net',
|
|
|
|
type: 'MASTER',
|
|
|
|
records: 0
|
|
|
|
}
|
|
|
|
], 'Result fail for ' + res.config.url);
|
2018-03-24 12:32:34 +00:00
|
|
|
|
2018-03-24 14:09:33 +00:00
|
|
|
//Type filter
|
|
|
|
var res = await req({
|
|
|
|
url: '/domains?type=NATIVE',
|
|
|
|
method: 'get'
|
|
|
|
});
|
2018-03-24 12:32:34 +00:00
|
|
|
|
2018-03-24 14:09:33 +00:00
|
|
|
assert.equal(res.status, 200, 'Status should be OK');
|
|
|
|
assert.equal(res.data.results, [
|
|
|
|
{
|
|
|
|
id: 3,
|
|
|
|
name: 'foo.de',
|
|
|
|
type: 'NATIVE',
|
2018-03-30 08:02:25 +00:00
|
|
|
records: 1
|
2018-03-24 14:09:33 +00:00
|
|
|
}
|
|
|
|
], 'Result fail for ' + res.config.url);
|
|
|
|
});
|
2018-03-24 15:37:35 +00:00
|
|
|
});
|