diff --git a/test/acceptance/auth/authorization.js b/test/acceptance/auth/authorization.js index 813f4e13..4e408c02 100644 --- a/test/acceptance/auth/authorization.js +++ b/test/acceptance/auth/authorization.js @@ -353,6 +353,7 @@ describe('authorization', function() { testClient.drain(done); }); }); + describe('Listing named maps', function() { it('should fail while listing named maps with a regular apikey token', function (done) { @@ -422,7 +423,7 @@ describe('authorization', function() { }); }); - describe.only('Create Named Map', function () { + describe('Create Named Map', function () { const template = { version: '0.0.1', name: 'auth-api-template', @@ -513,6 +514,128 @@ describe('authorization', function() { }); + describe('Delete 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 delete a named map using the master apikey token', function (done) { + const apikeyTokenCreate = 1234; + const apikeyTokenDelete = 1234; + + const template = Object.assign({}, templateBase, { name: templateBase.name + '-master'}); + + 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: 204 } }, function (err, res, response) { + assert.ifError(err); + + assert.equal(res.statusCode, 204); + + testClientDelete.drain(done); + }); + }); + }); + + it('should fail deleting a named map using a regular apikey token', function (done) { + const apikeyTokenCreate = 1234; + const apikeyTokenDelete = 'regular1'; + + const template = Object.assign({}, templateBase, { name: templateBase.name + '-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) { + 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 deleting a named map using the default apikey token', function (done) { + const apikeyTokenCreate = 1234; + const apikeyTokenDelete = 'default_public'; + + const template = Object.assign({}, templateBase, { name: templateBase.name + '-default' }); + + 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) { + 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 creating 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 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: 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) { const apikeyToken = 'regular1'; diff --git a/test/support/test-client.js b/test/support/test-client.js index b89009ba..04341e1b 100644 --- a/test/support/test-client.js +++ b/test/support/test-client.js @@ -1395,6 +1395,54 @@ TestClient.prototype.createTemplate = function (params, callback) { } assert.response(this.server, createTemplateRequest, createTemplateResponse, (res, err) => { - return callback(err, res, JSON.parse(res.body)); + 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); + }); +}; + + +TestClient.prototype.deleteTemplate = function (params, callback) { + if (!this.apiKey) { + return callback(new Error('apiKey param is mandatory to create a new template')); + } + + const deleteTemplateRequest = { + url: `/api/v1/map/named/${params.templateId}?${qs.stringify({ api_key: this.apiKey })}`, + method: 'DELETE', + headers: { + host: 'localhost', + } + }; + + let deleteTemplateResponse = { + status: 204, + headers: {} + }; + + if (params.response) { + deleteTemplateResponse = Object.assign(deleteTemplateResponse, params.response); + } + + assert.response(this.server, deleteTemplateRequest, deleteTemplateResponse, (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); }); };