diff --git a/test/acceptance/auth/authorization.js b/test/acceptance/auth/authorization.js index 4e408c02..aebe9fb8 100644 --- a/test/acceptance/auth/authorization.js +++ b/test/acceptance/auth/authorization.js @@ -511,7 +511,6 @@ describe('authorization', function() { testClient.drain(done); }); }); - }); describe('Delete Named Map', function () { @@ -541,21 +540,27 @@ 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); - testClientCreate.createTemplate({ }, function (err, res, template) { + 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); + testClientDelete.deleteTemplate( + { + templateId: template.template_id , + response: { status: 204 } + }, + function (err, res) { + assert.ifError(err); - assert.equal(res.statusCode, 204); + assert.equal(res.statusCode, 204); - testClientDelete.drain(done); - }); + testClientDelete.drain(done); + } + ); }); }); @@ -563,24 +568,30 @@ 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) { - assert.ifError(err); + 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); + assert.equal(res.statusCode, 403); - assert.equal(response.errors.length, 1); - assert.ok(response.errors[0].match(/Forbidden/), response.errors[0]); + assert.equal(response.errors.length, 1); + assert.ok(response.errors[0].match(/Forbidden/), response.errors[0]); - testClientDelete.drain(done); - }); + 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,24 +607,30 @@ 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) { - assert.ifError(err); + testClientDelete.deleteTemplate( + { + templateId: template.template_id, + response: { status: 403 } + }, + function (err, res, response) { + assert.ifError(err); - assert.equal(res.statusCode, 403); + assert.equal(res.statusCode, 403); - assert.equal(response.errors.length, 1); - assert.ok(response.errors[0].match(/Forbidden/), response.errors[0]); + assert.equal(response.errors.length, 1); + assert.ok(response.errors[0].match(/Forbidden/), response.errors[0]); - testClientDelete.drain(done); - }); + 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,16 +638,171 @@ 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) { - assert.ifError(err); + testClientDelete.deleteTemplate( + { + templateId: template.template_id, + response: { status: 401 } + }, + function (err, res, response) { + assert.ifError(err); - assert.equal(res.statusCode, 401); + assert.equal(res.statusCode, 401); - assert.equal(response.errors.length, 1); - assert.ok(response.errors[0].match(/Unauthorized/), response.errors[0]); + assert.equal(response.errors.length, 1); + assert.ok(response.errors[0].match(/Unauthorized/), response.errors[0]); - testClientDelete.drain(done); - }); + 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); + } + ); }); }); diff --git a/test/support/test-client.js b/test/support/test-client.js index 04341e1b..3e35b740 100644 --- a/test/support/test-client.js +++ b/test/support/test-client.js @@ -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); + }); +}; \ No newline at end of file