Fixed bug where numbers where returned as string

This commit is contained in:
Lukas Metzger 2018-03-24 15:09:33 +01:00
parent 5928203a0a
commit 6ec9c81c32
4 changed files with 241 additions and 229 deletions

View file

@ -103,6 +103,8 @@ class Domains
if ($item['type'] != 'SLAVE') { if ($item['type'] != 'SLAVE') {
unset($item['master']); unset($item['master']);
} }
$item['id'] = intval($item['id']);
$item['records'] = intval($item['records']);
return $item; return $item;
}, $data); }, $data);
} }
@ -147,6 +149,7 @@ class Domains
$query->execute(); $query->execute();
$record = $query->fetch(); $record = $query->fetch();
$record['id'] = intval($record['id']);
if ($type !== 'SLAVE') { if ($type !== 'SLAVE') {
unset($record['master']); unset($record['master']);
} }

View file

@ -1,100 +1,105 @@
const cartesianProduct = require('cartesian-product'); const cartesianProduct = require('cartesian-product');
require('../testlib')('admin', async function (assert, req) { (async function () {
//Test missing fields await require('../testlib')('admin', async function (assert, req) {
var res = await req({ //Test missing fields
url: '/domains', var res = await req({
method: 'post', url: '/domains',
data: { method: 'post',
name: 'abc.de' data: {
} name: 'abc.de'
}); }
});
assert.equal(res.status, 422, 'Missing type filed should trigger error.'); assert.equal(res.status, 422, 'Missing type filed should trigger error.');
var res = await req({ var res = await req({
url: '/domains', url: '/domains',
method: 'post', method: 'post',
data: { data: {
name: 'abc.de', name: 'abc.de',
type: 'SLAVE' type: 'SLAVE'
} }
}); });
assert.equal(res.status, 422, 'Missing master field for SLAVE domain should trigger error.'); assert.equal(res.status, 422, 'Missing master field for SLAVE domain should trigger error.');
var res = await req({ var res = await req({
url: '/domains', url: '/domains',
method: 'post', method: 'post',
data: { data: {
name: 'foo.de', name: 'foo.de',
type: 'MASTER' type: 'MASTER'
} }
}); });
assert.equal(res.status, 409, 'Existing domain should trigger error.'); assert.equal(res.status, 409, 'Existing domain should trigger error.');
//Test creation of master zone //Test creation of master zone
var res = await req({ var res = await req({
url: '/domains', url: '/domains',
method: 'post', method: 'post',
data: { data: {
name: 'master.de',
type: 'MASTER'
}
});
assert.equal(res.status, 201, 'Creation should be successfull');
assert.equal(res.data, {
id: 6,
name: 'master.de', name: 'master.de',
type: 'MASTER' type: 'MASTER'
} }, 'Creation result fail.')
});
assert.equal(res.status, 201, 'Creation should be successfull'); //Test creation of native zone
assert.equal(res.data, { var res = await req({
id: '6', url: '/domains',
name: 'master.de', method: 'post',
type: 'MASTER' data: {
}, 'Creation result fail.') name: 'native.de',
type: 'NATIVE'
}
});
//Test creation of native zone assert.equal(res.status, 201, 'Creation should be successfull');
var res = await req({ assert.equal(res.data, {
url: '/domains', id: 7,
method: 'post',
data: {
name: 'native.de', name: 'native.de',
type: 'NATIVE' type: 'NATIVE'
} }, 'Creation result fail.')
});
assert.equal(res.status, 201, 'Creation should be successfull'); //Test creation of slave zone
assert.equal(res.data, { var res = await req({
id: '7', url: '/domains',
name: 'native.de', method: 'post',
type: 'NATIVE' data: {
}, 'Creation result fail.') name: 'slave.de',
type: 'SLAVE',
master: '1.2.3.4'
}
});
//Test creation of slave zone assert.equal(res.status, 201, 'Creation should be successfull');
var res = await req({ assert.equal(res.data, {
url: '/domains', id: 8,
method: 'post',
data: {
name: 'slave.de', name: 'slave.de',
type: 'SLAVE' type: 'SLAVE',
} master: '1.2.3.4'
}, 'Creation result fail.')
}); });
assert.equal(res.status, 201, 'Creation should be successfull'); await require('../testlib')('user', async function (assert, req) {
assert.equal(res.data, { //Test insufficient privileges
id: '8', var res = await req({
name: 'slave.de', url: '/domains',
type: 'SLAVE' method: 'post',
}, 'Creation result fail.') data: {
}); name: 'foo.de'
}
});
require('../testlib')('user', async function (assert, req) { assert.equal(res.status, 403, 'Domain creation should be forbidden for users.')
//Test insufficient privileges
var res = await req({
url: '/domains',
method: 'post',
data: {
name: 'foo.de'
}
}); });
assert.equal(res.status, 403, 'Domain creation should be forbidden for users.') })();
});

View file

@ -1,106 +1,108 @@
const cartesianProduct = require('cartesian-product'); const cartesianProduct = require('cartesian-product');
require('../testlib')('admin', async function (assert, req) { (async function () {
//GET /domains?page=5&pagesize=10&query=foo&sort=id-asc,name-desc,type-asc,records-asc&type=MASTER require('../testlib')('admin', async function (assert, req) {
//GET /domains?page=5&pagesize=10&query=foo&sort=id-asc,name-desc,type-asc,records-asc&type=MASTER
//Test sorting in all combinations //Test sorting in all combinations
const sortCombinations = cartesianProduct([ const sortCombinations = cartesianProduct([
['', 'id-asc', 'id-desc'], ['', 'id-asc', 'id-desc'],
['', 'name-asc', 'name-desc'], ['', 'name-asc', 'name-desc'],
['', 'type-asc', 'type-desc'], ['', 'type-asc', 'type-desc'],
['', 'records-asc', 'records-desc'] ['', 'records-asc', 'records-desc']
]); ]);
for (list of sortCombinations) { for (list of sortCombinations) {
list = list.filter((str) => str.length > 0); list = list.filter((str) => str.length > 0);
var sortQuery = list.join(','); var sortQuery = list.join(',');
var res = await req({
url: '/domains?sort=' + sortQuery,
method: 'get'
});
assert.equal(res.status, 200);
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;
}
}
return 0;
});
assert.equal(res.data.results, sortedData, 'Sort failed for ' + res.config.url);
}
//Test paging
var res = await req({ var res = await req({
url: '/domains?sort=' + sortQuery, url: '/domains?pagesize=3',
method: 'get' method: 'get'
}); });
assert.equal(res.status, 200); 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.");
var sortedData = res.data.results.slice(); var res = await req({
sortedData.sort(function (a, b) { url: '/domains?pagesize=3&page=2',
for (sort of list) { method: 'get'
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;
}
}
return 0;
}); });
assert.equal(res.data.results, sortedData, 'Sort failed for ' + res.config.url); 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.");
//Test paging //Test query
var res = await req({ var res = await req({
url: '/domains?pagesize=3', url: '/domains?query=.net&sort=id-asc',
method: 'get' method: 'get'
});
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);
//Type filter
var res = await req({
url: '/domains?type=NATIVE',
method: 'get'
});
assert.equal(res.status, 200, 'Status should be OK');
assert.equal(res.data.results, [
{
id: 3,
name: 'foo.de',
type: 'NATIVE',
records: 0
}
], 'Result fail for ' + res.config.url);
}); });
})();
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.");
var res = await req({
url: '/domains?pagesize=3&page=2',
method: 'get'
});
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.");
//Test query
var res = await req({
url: '/domains?query=.net&sort=id-asc',
method: 'get'
});
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);
//Type filter
var res = await req({
url: '/domains?type=NATIVE',
method: 'get'
});
assert.equal(res.status, 200, 'Status should be OK');
assert.equal(res.data.results, [
{
id: '3',
name: 'foo.de',
type: 'NATIVE',
records: '0'
}
], 'Result fail for ' + res.config.url);
});

View file

@ -1,61 +1,63 @@
require('../testlib')('admin', async function (assert, req) { (async function () {
//Try to login with invalid username and password require('../testlib')('admin', async function (assert, req) {
var res = await req({ //Try to login with invalid username and password
url: '/sessions', var res = await req({
method: 'post', url: '/sessions',
data: { method: 'post',
username: 'foo', data: {
password: 'bar' username: 'foo',
} password: 'bar'
}
});
assert.equal(res.status, 403, 'Status not valid');
//Try to login with invalid username
var res = await req({
url: '/sessions',
method: 'post',
data: {
username: 'foo',
password: 'admin'
}
});
assert.equal(res.status, 403, 'Status not valid');
//Try to login with invalid password
var res = await req({
url: '/sessions',
method: 'post',
data: {
username: 'admin',
password: 'foo'
}
});
assert.equal(res.status, 403, 'Status not valid');
//Try to login with missing field
var res = await req({
url: '/sessions',
method: 'post',
data: {
password: 'admin'
}
});
assert.equal(res.status, 422, 'Status not valid');
//Try to login with invalid username and password
var res = await req({
url: '/sessions',
method: 'post',
data: {
username: 'foo/admin',
password: 'admin'
}
});
assert.equal(res.status, 201, 'Status not valid');
}); });
})();
assert.equal(res.status, 403, 'Status not valid');
//Try to login with invalid username
var res = await req({
url: '/sessions',
method: 'post',
data: {
username: 'foo',
password: 'admin'
}
});
assert.equal(res.status, 403, 'Status not valid');
//Try to login with invalid password
var res = await req({
url: '/sessions',
method: 'post',
data: {
username: 'admin',
password: 'foo'
}
});
assert.equal(res.status, 403, 'Status not valid');
//Try to login with missing field
var res = await req({
url: '/sessions',
method: 'post',
data: {
password: 'admin'
}
});
assert.equal(res.status, 422, 'Status not valid');
//Try to login with invalid username and password
var res = await req({
url: '/sessions',
method: 'post',
data: {
username: 'foo/admin',
password: 'admin'
}
});
assert.equal(res.status, 201, 'Status not valid');
});