add named maps Update auth tests

This commit is contained in:
Eneko Lakasta 2018-04-12 16:03:02 +02:00
parent fbcf312071
commit f5bdb8b15b
2 changed files with 247 additions and 35 deletions

View File

@ -511,7 +511,6 @@ describe('authorization', function() {
testClient.drain(done);
});
});
});
describe('Delete Named Map', function () {
@ -541,7 +540,7 @@ describe('authorization', function() {
const apikeyTokenCreate = 1234;
const apikeyTokenDelete = 1234;
const template = Object.assign({}, templateBase, { name: templateBase.name + '-master'});
const template = Object.assign({}, templateBase, { name: templateBase.name + '-delete-master'});
const testClientCreate = new TestClient(template, apikeyTokenCreate);
@ -549,13 +548,19 @@ describe('authorization', function() {
assert.ifError(err);
const testClientDelete = new TestClient(template, apikeyTokenDelete);
testClientDelete.deleteTemplate({ templateId: template.template_id , response: { status: 204 } }, function (err, res, response) {
testClientDelete.deleteTemplate(
{
templateId: template.template_id ,
response: { status: 204 }
},
function (err, res) {
assert.ifError(err);
assert.equal(res.statusCode, 204);
testClientDelete.drain(done);
});
}
);
});
});
@ -563,15 +568,20 @@ describe('authorization', function() {
const apikeyTokenCreate = 1234;
const apikeyTokenDelete = 'regular1';
const template = Object.assign({}, templateBase, { name: templateBase.name + '-regular' });
const template = Object.assign({}, templateBase, { name: templateBase.name + '-delete-regular' });
const testClientCreate = new TestClient(template, apikeyTokenCreate);
testClientCreate.createTemplate({}, function (err, res, template) {
assert.ifError(err);
const testClientDelete = new TestClient(template, apikeyTokenDelete);
testClientDelete.deleteTemplate({ templateId: template.template_id, response: { status: 403 } }, function (err, res, response) {
const testClientDelete = new TestClient({}, apikeyTokenDelete);
testClientDelete.deleteTemplate(
{
templateId: template.template_id,
response: { status: 403 }
},
function (err, res, response) {
assert.ifError(err);
assert.equal(res.statusCode, 403);
@ -580,7 +590,8 @@ describe('authorization', function() {
assert.ok(response.errors[0].match(/Forbidden/), response.errors[0]);
testClientDelete.drain(done);
});
}
);
});
});
@ -588,7 +599,7 @@ describe('authorization', function() {
const apikeyTokenCreate = 1234;
const apikeyTokenDelete = 'default_public';
const template = Object.assign({}, templateBase, { name: templateBase.name + '-default' });
const template = Object.assign({}, templateBase, { name: templateBase.name + '-delete-default' });
const testClientCreate = new TestClient(template, apikeyTokenCreate);
@ -596,7 +607,12 @@ describe('authorization', function() {
assert.ifError(err);
const testClientDelete = new TestClient(template, apikeyTokenDelete);
testClientDelete.deleteTemplate({ templateId: template.template_id, response: { status: 403 } }, function (err, res, response) {
testClientDelete.deleteTemplate(
{
templateId: template.template_id,
response: { status: 403 }
},
function (err, res, response) {
assert.ifError(err);
assert.equal(res.statusCode, 403);
@ -605,15 +621,16 @@ describe('authorization', function() {
assert.ok(response.errors[0].match(/Forbidden/), response.errors[0]);
testClientDelete.drain(done);
});
}
);
});
});
it('should fail creating a named map using a non-existent apikey token', function (done) {
it('should fail deleting a named map using a non-existent apikey token', function (done) {
const apikeyTokenCreate = 1234;
const apikeyTokenDelete = 'wadus';
const template = Object.assign({}, templateBase, { name: templateBase.name + '-wadus' });
const template = Object.assign({}, templateBase, { name: templateBase.name + '-delete-wadus' });
const testClientCreate = new TestClient(template, apikeyTokenCreate);
@ -621,7 +638,12 @@ describe('authorization', function() {
assert.ifError(err);
const testClientDelete = new TestClient(template, apikeyTokenDelete);
testClientDelete.deleteTemplate({ templateId: template.template_id, response: { status: 401 } }, function (err, res, response) {
testClientDelete.deleteTemplate(
{
templateId: template.template_id,
response: { status: 401 }
},
function (err, res, response) {
assert.ifError(err);
assert.equal(res.statusCode, 401);
@ -630,10 +652,160 @@ describe('authorization', function() {
assert.ok(response.errors[0].match(/Unauthorized/), response.errors[0]);
testClientDelete.drain(done);
}
);
});
});
});
describe.only('Update Named Map', function () {
const templateBase = {
version: '0.0.1',
name: 'auth-api-template',
placeholders: {
buffersize: {
type: 'number',
default: 0
}
},
layergroup: {
version: '1.7.0',
layers: [{
type: 'cartodb',
options: {
sql: 'select * from test_table_localhost_regular1',
cartocss: TestClient.CARTOCSS.POINTS,
cartocss_version: '2.3.0',
}
}]
}
};
it('should update a named map using the master apikey token', function (done) {
const apikeyTokenCreate = 1234;
const apikeyTokenUpdate = 1234;
const template = Object.assign({}, templateBase, { name: templateBase.name + '-update-master' });
const templateUpdate = Object.assign({}, template, { zoom: 3 });
const testClientCreate = new TestClient(template, apikeyTokenCreate);
testClientCreate.createTemplate({}, function (err, res, template) {
assert.ifError(err);
const testClientDelete = new TestClient({}, apikeyTokenUpdate);
testClientDelete.updateTemplate(
{
templateId: template.template_id,
templateData: templateUpdate,
response: { status: 200 }
},
function (err, res) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
testClientDelete.drain(done);
}
);
});
});
it('should fail updating a named map using a regular apikey token', function (done) {
const apikeyTokenCreate = 1234;
const apikeyTokenUpdate = 'regular1';
const template = Object.assign({}, templateBase, { name: templateBase.name + '-update-regular' });
const templateUpdate = Object.assign({}, template, { zoom: 3 });
const testClientCreate = new TestClient(template, apikeyTokenCreate);
testClientCreate.createTemplate({}, function (err, res, template) {
assert.ifError(err);
const testClientDelete = new TestClient({}, apikeyTokenUpdate);
testClientDelete.updateTemplate(
{
templateId: template.template_id,
templateData: templateUpdate,
response: { status: 403 }
},
function (err, res, response) {
assert.ifError(err);
assert.equal(res.statusCode, 403);
assert.equal(response.errors.length, 1);
assert.ok(response.errors[0].match(/Forbidden/), response.errors[0]);
testClientDelete.drain(done);
}
);
});
});
it('should fail updating a named map using the default apikey token', function (done) {
const apikeyTokenCreate = 1234;
const apikeyTokenUpdate = 'default_public';
const template = Object.assign({}, templateBase, { name: templateBase.name + '-update-default' });
const templateUpdate = Object.assign({}, template, { zoom: 3 });
const testClientCreate = new TestClient(template, apikeyTokenCreate);
testClientCreate.createTemplate({}, function (err, res, template) {
assert.ifError(err);
const testClientDelete = new TestClient({}, apikeyTokenUpdate);
testClientDelete.updateTemplate(
{
templateId: template.template_id,
templateData: templateUpdate,
response: { status: 403 }
},
function (err, res, response) {
assert.ifError(err);
assert.equal(res.statusCode, 403);
assert.equal(response.errors.length, 1);
assert.ok(response.errors[0].match(/Forbidden/), response.errors[0]);
testClientDelete.drain(done);
}
);
});
});
it('should fail updating a named map using a non-existent apikey token', function (done) {
const apikeyTokenCreate = 1234;
const apikeyTokenUpdate = 'wadus';
const template = Object.assign({}, templateBase, { name: templateBase.name + '-update-wadus' });
const templateUpdate = Object.assign({}, template, { zoom: 3 });
const testClientCreate = new TestClient(template, apikeyTokenCreate);
testClientCreate.createTemplate({}, function (err, res, template) {
assert.ifError(err);
const testClientDelete = new TestClient({}, apikeyTokenUpdate);
testClientDelete.updateTemplate(
{
templateId: template.template_id,
templateData: templateUpdate,
response: { status: 401 }
},
function (err, res, response) {
assert.ifError(err);
assert.equal(res.statusCode, 401);
assert.equal(response.errors.length, 1);
assert.ok(response.errors[0].match(/Unauthorized/), response.errors[0]);
testClientDelete.drain(done);
}
);
});
});
});
it.skip('should fail creating a named map using a regular apikey token and a private table', function (done) {

View File

@ -1409,7 +1409,6 @@ TestClient.prototype.createTemplate = function (params, callback) {
});
};
TestClient.prototype.deleteTemplate = function (params, callback) {
if (!this.apiKey) {
return callback(new Error('apiKey param is mandatory to create a new template'));
@ -1446,3 +1445,44 @@ TestClient.prototype.deleteTemplate = function (params, callback) {
return callback(err, res, body);
});
};
TestClient.prototype.updateTemplate = function (params, callback) {
if (!this.apiKey) {
return callback(new Error('apiKey param is mandatory to create a new template'));
}
const updateTemplateRequest = {
url: `/api/v1/map/named/${params.templateId}?${qs.stringify({ api_key: this.apiKey })}`,
method: 'PUT',
headers: {
host: 'localhost',
'Content-Type': 'application/json; charset=utf-8'
},
data: JSON.stringify(params.templateData)
};
let updateTemplateResponse = {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
};
if (params.response) {
updateTemplateResponse = Object.assign(updateTemplateResponse, params.response);
}
assert.response(this.server, updateTemplateRequest, updateTemplateResponse, (res, err) => {
let body;
switch (res.headers['content-type']) {
case 'application/json; charset=utf-8':
body = JSON.parse(res.body);
break;
default:
body = res.body;
break;
}
return callback(err, res, body);
});
};