From 9a3eb3e0fdf70f263c9e8f9b4134080d979224a9 Mon Sep 17 00:00:00 2001 From: Eneko Lakasta Date: Wed, 11 Apr 2018 12:26:09 +0200 Subject: [PATCH 1/7] add named maps Listing auth tests --- test/acceptance/auth/authorization.js | 72 ++++++++++++++++++++------- 1 file changed, 54 insertions(+), 18 deletions(-) diff --git a/test/acceptance/auth/authorization.js b/test/acceptance/auth/authorization.js index 02668251..3de51b85 100644 --- a/test/acceptance/auth/authorization.js +++ b/test/acceptance/auth/authorization.js @@ -353,36 +353,72 @@ 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) { - const apikeyToken = 'regular1'; + it('should fail while listing named maps with a regular apikey token', function (done) { + const apikeyToken = 'regular1'; - const testClient = new TestClient({}, apikeyToken); + const testClient = new TestClient({}, apikeyToken); - testClient.getNamedMapList({ response: {status: 403 }}, function (err, res, body) { - assert.ifError(err); + testClient.getNamedMapList({ response: { status: 403 } }, function (err, res, body) { + assert.ifError(err); - assert.equal(res.statusCode, 403); + assert.equal(res.statusCode, 403); - assert.equal(body.errors.length, 1); - assert.ok(body.errors[0].match(/Forbidden/), body.errors[0]); + assert.equal(body.errors.length, 1); + assert.ok(body.errors[0].match(/Forbidden/), body.errors[0]); - testClient.drain(done); + testClient.drain(done); + }); }); - }); - it('should list named maps with master apikey token', function (done) { - const apikeyToken = 1234; + it('should fail while listing named maps with the default apikey token', function (done) { + const apikeyToken = 'default_public'; - const testClient = new TestClient({}, apikeyToken); + const testClient = new TestClient({}, apikeyToken); - testClient.getNamedMapList({}, function (err, res, body) { - assert.ifError(err); + testClient.getNamedMapList({ response: { status: 403 } }, function (err, res, body) { + assert.ifError(err); - assert.equal(res.statusCode, 200); - assert.ok(Array.isArray(body.template_ids)); + assert.equal(res.statusCode, 403); - testClient.drain(done); + assert.equal(body.errors.length, 1); + assert.ok(body.errors[0].match(/Forbidden/), body.errors[0]); + + testClient.drain(done); + }); + }); + + it('should fail while listing named maps with non-existent apikey token', function (done) { + const apikeyToken = 'wadus-wadus'; + + const testClient = new TestClient({}, apikeyToken); + + testClient.getNamedMapList({ response: { status: 401 } }, function (err, res, body) { + assert.ifError(err); + + assert.equal(res.statusCode, 401); + + assert.equal(body.errors.length, 1); + assert.ok(body.errors[0].match(/Unauthorized/), body.errors[0]); + + testClient.drain(done); + }); + }); + + it('should list named maps with master apikey token', function (done) { + const apikeyToken = 1234; + + const testClient = new TestClient({}, apikeyToken); + + testClient.getNamedMapList({}, function (err, res, body) { + assert.ifError(err); + + assert.equal(res.statusCode, 200); + assert.ok(Array.isArray(body.template_ids)); + + testClient.drain(done); + }); }); }); From 9a7a8a324395c8b6fa188485d9762ee30e285b9e Mon Sep 17 00:00:00 2001 From: Eneko Lakasta Date: Thu, 12 Apr 2018 12:43:41 +0200 Subject: [PATCH 2/7] add named maps Create auth tests --- test/acceptance/auth/authorization.js | 72 +++++++++++++++++++++++---- test/support/test-client.js | 31 ++++++++++++ 2 files changed, 94 insertions(+), 9 deletions(-) diff --git a/test/acceptance/auth/authorization.js b/test/acceptance/auth/authorization.js index 3de51b85..813f4e13 100644 --- a/test/acceptance/auth/authorization.js +++ b/test/acceptance/auth/authorization.js @@ -422,9 +422,7 @@ describe('authorization', function() { }); }); - it.skip('should create and get a named map tile using a regular apikey token', function (done) { - const apikeyToken = 'regular1'; - + describe.only('Create Named Map', function () { const template = { version: '0.0.1', name: 'auth-api-template', @@ -447,16 +445,72 @@ describe('authorization', function() { } }; - const testClient = new TestClient(template, apikeyToken); + it('should create and get a named map tile using the master apikey token', function (done) { + const apikeyToken = 1234; - testClient.getTile(0, 0, 0, function (err, res, tile) { - assert.ifError(err); + const testClient = new TestClient(template, apikeyToken); - assert.equal(res.statusCode, 200); - assert.ok(tile instanceof mapnik.Image); + testClient.getTile(0, 0, 0, function (err, res, tile) { + assert.ifError(err); - testClient.drain(done); + assert.equal(res.statusCode, 200); + assert.ok(tile instanceof mapnik.Image); + + testClient.drain(done); + }); }); + + it('should fail creating a named map using a regular apikey token', function (done) { + const apikeyToken = 'regular1'; + + const testClient = new TestClient(template, apikeyToken); + + testClient.createTemplate({ 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]); + + testClient.drain(done); + }); + }); + + it('should fail creating a named map using the default apikey token', function (done) { + const apikeyToken = 'default_public'; + + const testClient = new TestClient(template, apikeyToken); + + testClient.createTemplate({ 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]); + + testClient.drain(done); + }); + }); + + it('should fail creating a named map using a non-existent apikey token', function (done) { + const apikeyToken = 'wadus-wadus'; + + const testClient = new TestClient(template, apikeyToken); + + testClient.createTemplate({ 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]); + + testClient.drain(done); + }); + }); + }); it.skip('should fail creating a named map using a regular apikey token and a private table', function (done) { diff --git a/test/support/test-client.js b/test/support/test-client.js index 7b068901..b89009ba 100644 --- a/test/support/test-client.js +++ b/test/support/test-client.js @@ -1367,3 +1367,34 @@ TestClient.prototype.getNamedTile = function (name, z, x, y, format, options, c }); }); }; + +TestClient.prototype.createTemplate = function (params, callback) { + if (!this.apiKey) { + return callback(new Error('apiKey param is mandatory to create a new template')); + } + + const createTemplateRequest = { + url: `/api/v1/map/named?${qs.stringify({ api_key: this.apiKey })}`, + method: 'POST', + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, + data: JSON.stringify(this.template) + }; + + let createTemplateResponse = { + status: 200, + headers: { + 'Content-Type': 'application/json; charset=utf-8' + } + }; + + if (params.response) { + createTemplateResponse = Object.assign(createTemplateResponse, params.response); + } + + assert.response(this.server, createTemplateRequest, createTemplateResponse, (res, err) => { + return callback(err, res, JSON.parse(res.body)); + }); +}; From fbcf312071cd93eea4c554ce18685b1548b30a66 Mon Sep 17 00:00:00 2001 From: Eneko Lakasta Date: Thu, 12 Apr 2018 14:46:08 +0200 Subject: [PATCH 3/7] add named maps Delete auth tests --- test/acceptance/auth/authorization.js | 125 +++++++++++++++++++++++++- test/support/test-client.js | 50 ++++++++++- 2 files changed, 173 insertions(+), 2 deletions(-) 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); }); }; From f5bdb8b15bdd3f0e471dd4456111716ed6506548 Mon Sep 17 00:00:00 2001 From: Eneko Lakasta Date: Thu, 12 Apr 2018 16:03:02 +0200 Subject: [PATCH 4/7] add named maps Update auth tests --- test/acceptance/auth/authorization.js | 240 ++++++++++++++++++++++---- test/support/test-client.js | 42 ++++- 2 files changed, 247 insertions(+), 35 deletions(-) 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 From 31e3b9953fe82652273044ca8936554fd1ba48ed Mon Sep 17 00:00:00 2001 From: Eneko Lakasta Date: Thu, 12 Apr 2018 16:22:56 +0200 Subject: [PATCH 5/7] add named maps GET auth tests --- test/acceptance/auth/authorization.js | 1010 ++++++++++++++----------- test/support/test-client.js | 40 + 2 files changed, 599 insertions(+), 451 deletions(-) diff --git a/test/acceptance/auth/authorization.js b/test/acceptance/auth/authorization.js index aebe9fb8..37407531 100644 --- a/test/acceptance/auth/authorization.js +++ b/test/acceptance/auth/authorization.js @@ -353,496 +353,604 @@ describe('authorization', function() { testClient.drain(done); }); }); + describe.only('Named maps', function () { + describe('LIST Named maps', function () { - describe('Listing named maps', function() { + it('should fail while listing named maps with a regular apikey token', function (done) { + const apikeyToken = 'regular1'; - it('should fail while listing named maps with a regular apikey token', function (done) { - const apikeyToken = 'regular1'; + const testClient = new TestClient({}, apikeyToken); - const testClient = new TestClient({}, apikeyToken); + testClient.getNamedMapList({ response: { status: 403 } }, function (err, res, body) { + assert.ifError(err); - testClient.getNamedMapList({ response: { status: 403 } }, function (err, res, body) { - assert.ifError(err); + assert.equal(res.statusCode, 403); - assert.equal(res.statusCode, 403); + assert.equal(body.errors.length, 1); + assert.ok(body.errors[0].match(/Forbidden/), body.errors[0]); - assert.equal(body.errors.length, 1); - assert.ok(body.errors[0].match(/Forbidden/), body.errors[0]); + testClient.drain(done); + }); + }); - testClient.drain(done); + it('should fail while listing named maps with the default apikey token', function (done) { + const apikeyToken = 'default_public'; + + const testClient = new TestClient({}, apikeyToken); + + testClient.getNamedMapList({ response: { status: 403 } }, function (err, res, body) { + assert.ifError(err); + + assert.equal(res.statusCode, 403); + + assert.equal(body.errors.length, 1); + assert.ok(body.errors[0].match(/Forbidden/), body.errors[0]); + + testClient.drain(done); + }); + }); + + it('should fail while listing named maps with non-existent apikey token', function (done) { + const apikeyToken = 'wadus-wadus'; + + const testClient = new TestClient({}, apikeyToken); + + testClient.getNamedMapList({ response: { status: 401 } }, function (err, res, body) { + assert.ifError(err); + + assert.equal(res.statusCode, 401); + + assert.equal(body.errors.length, 1); + assert.ok(body.errors[0].match(/Unauthorized/), body.errors[0]); + + testClient.drain(done); + }); + }); + + it('should list named maps with master apikey token', function (done) { + const apikeyToken = 1234; + + const testClient = new TestClient({}, apikeyToken); + + testClient.getNamedMapList({}, function (err, res, body) { + assert.ifError(err); + + assert.equal(res.statusCode, 200); + assert.ok(Array.isArray(body.template_ids)); + + testClient.drain(done); + }); }); }); - it('should fail while listing named maps with the default apikey token', function (done) { - const apikeyToken = 'default_public'; - - const testClient = new TestClient({}, apikeyToken); - - testClient.getNamedMapList({ response: { status: 403 } }, function (err, res, body) { - assert.ifError(err); - - assert.equal(res.statusCode, 403); - - assert.equal(body.errors.length, 1); - assert.ok(body.errors[0].match(/Forbidden/), body.errors[0]); - - testClient.drain(done); - }); - }); - - it('should fail while listing named maps with non-existent apikey token', function (done) { - const apikeyToken = 'wadus-wadus'; - - const testClient = new TestClient({}, apikeyToken); - - testClient.getNamedMapList({ response: { status: 401 } }, function (err, res, body) { - assert.ifError(err); - - assert.equal(res.statusCode, 401); - - assert.equal(body.errors.length, 1); - assert.ok(body.errors[0].match(/Unauthorized/), body.errors[0]); - - testClient.drain(done); - }); - }); - - it('should list named maps with master apikey token', function (done) { - const apikeyToken = 1234; - - const testClient = new TestClient({}, apikeyToken); - - testClient.getNamedMapList({}, function (err, res, body) { - assert.ifError(err); - - assert.equal(res.statusCode, 200); - assert.ok(Array.isArray(body.template_ids)); - - testClient.drain(done); - }); - }); - }); - - describe('Create Named Map', function () { - const template = { - version: '0.0.1', - name: 'auth-api-template', - placeholders: { - buffersize: { - type: 'number', - default: 0 + describe('CREATE Named Map', function () { + const template = { + 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', + } + }] } - }, - 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 create and get a named map tile using the master apikey token', function (done) { + const apikeyToken = 1234; + + const testClient = new TestClient(template, apikeyToken); + + testClient.getTile(0, 0, 0, function (err, res, tile) { + assert.ifError(err); + + assert.equal(res.statusCode, 200); + assert.ok(tile instanceof mapnik.Image); + + testClient.drain(done); + }); + }); + + it('should fail creating a named map using a regular apikey token', function (done) { + const apikeyToken = 'regular1'; + + const testClient = new TestClient(template, apikeyToken); + + testClient.createTemplate({ 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]); + + testClient.drain(done); + }); + }); + + it('should fail creating a named map using the default apikey token', function (done) { + const apikeyToken = 'default_public'; + + const testClient = new TestClient(template, apikeyToken); + + testClient.createTemplate({ 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]); + + testClient.drain(done); + }); + }); + + it('should fail creating a named map using a non-existent apikey token', function (done) { + const apikeyToken = 'wadus-wadus'; + + const testClient = new TestClient(template, apikeyToken); + + testClient.createTemplate({ 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]); + + testClient.drain(done); + }); + }); + }); + + describe('DELETE Named Map', function () { + const templateBase = { + version: '0.0.1', + name: 'auth-api-template', + placeholders: { + buffersize: { + type: 'number', + default: 0 } - }] - } - }; - - it('should create and get a named map tile using the master apikey token', function (done) { - const apikeyToken = 1234; - - const testClient = new TestClient(template, apikeyToken); - - testClient.getTile(0, 0, 0, function (err, res, tile) { - assert.ifError(err); - - assert.equal(res.statusCode, 200); - assert.ok(tile instanceof mapnik.Image); - - testClient.drain(done); - }); - }); - - it('should fail creating a named map using a regular apikey token', function (done) { - const apikeyToken = 'regular1'; - - const testClient = new TestClient(template, apikeyToken); - - testClient.createTemplate({ 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]); - - testClient.drain(done); - }); - }); - - it('should fail creating a named map using the default apikey token', function (done) { - const apikeyToken = 'default_public'; - - const testClient = new TestClient(template, apikeyToken); - - testClient.createTemplate({ 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]); - - testClient.drain(done); - }); - }); - - it('should fail creating a named map using a non-existent apikey token', function (done) { - const apikeyToken = 'wadus-wadus'; - - const testClient = new TestClient(template, apikeyToken); - - testClient.createTemplate({ 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]); - - testClient.drain(done); - }); - }); - }); - - 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', + } + }] } - }, - 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; + 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 + '-delete-master'}); + const template = Object.assign({}, templateBase, { name: templateBase.name + '-delete-master' }); - const testClientCreate = new TestClient(template, apikeyTokenCreate); + const testClientCreate = new TestClient(template, apikeyTokenCreate); - testClientCreate.createTemplate({}, function (err, res, template) { - assert.ifError(err); + 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) { - assert.ifError(err); + const testClientDelete = new TestClient(template, apikeyTokenDelete); + 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); + } + ); + }); + }); + + 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 + '-delete-regular' }); + + const testClientCreate = new TestClient(template, apikeyTokenCreate); + + testClientCreate.createTemplate({}, function (err, res, template) { + 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(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 + '-delete-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 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 + '-delete-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('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 + '-delete-regular' }); - - const testClientCreate = new TestClient(template, apikeyTokenCreate); - - testClientCreate.createTemplate({}, function (err, res, template) { - 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(response.errors.length, 1); - assert.ok(response.errors[0].match(/Forbidden/), response.errors[0]); - - testClientDelete.drain(done); + describe('GET Named Map', function () { + const templateBase = { + version: '0.0.1', + name: 'auth-api-template', + placeholders: { + buffersize: { + type: 'number', + default: 0 } - ); - }); - }); - - 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 + '-delete-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 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 + '-delete-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); - } - ); - }); - }); - }); - - 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', + } + }] } - }, - 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; + it('should get a named map using the master apikey token', function (done) { + const apikeyTokenCreate = 1234; + const apikeyTokenGet = 1234; - const template = Object.assign({}, templateBase, { name: templateBase.name + '-update-master' }); - const templateUpdate = Object.assign({}, template, { zoom: 3 }); - const testClientCreate = new TestClient(template, apikeyTokenCreate); + const template = Object.assign({}, templateBase, { name: templateBase.name + '-get-master' }); - testClientCreate.createTemplate({}, function (err, res, template) { - assert.ifError(err); + const testClientCreate = new TestClient(template, apikeyTokenCreate); - const testClientDelete = new TestClient({}, apikeyTokenUpdate); - testClientDelete.updateTemplate( - { - templateId: template.template_id, - templateData: templateUpdate, - response: { status: 200 } - }, - function (err, res) { - assert.ifError(err); + testClientCreate.createTemplate({}, function (err, res, template) { + assert.ifError(err); - assert.equal(res.statusCode, 200); + const testClientDelete = new TestClient({}, apikeyTokenGet); + testClientDelete.getTemplate( + { + templateId: template.template_id, + response: { status: 200 } + }, + function (err, res) { + assert.ifError(err); - testClientDelete.drain(done); - } - ); + assert.equal(res.statusCode, 200); + + testClientDelete.drain(done); + } + ); + }); + }); + + it('should fail getting a named map using a regular apikey token', function (done) { + const apikeyTokenCreate = 1234; + const apikeyTokenGet = 'regular1'; + + const template = Object.assign({}, templateBase, { name: templateBase.name + '-get-regular' }); + + const testClientCreate = new TestClient(template, apikeyTokenCreate); + + testClientCreate.createTemplate({}, function (err, res, template) { + assert.ifError(err); + + const testClientGet = new TestClient({}, apikeyTokenGet); + testClientGet.getTemplate( + { + 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]); + + testClientGet.drain(done); + } + ); + }); + }); + + it('should fail getting a named map using the default apikey token', function (done) { + const apikeyTokenCreate = 1234; + const apikeyTokenGet = 'default_public'; + + const template = Object.assign({}, templateBase, { name: templateBase.name + '-get-default' }); + + const testClientCreate = new TestClient(template, apikeyTokenCreate); + + testClientCreate.createTemplate({}, function (err, res, template) { + assert.ifError(err); + + const testClientGet = new TestClient(template, apikeyTokenGet); + testClientGet.getTemplate( + { + 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]); + + testClientGet.drain(done); + } + ); + }); + }); + + it('should fail getting a named map using a non-existent apikey token', function (done) { + const apikeyTokenCreate = 1234; + const apikeyTokenGet = 'wadus'; + + const template = Object.assign({}, templateBase, { name: templateBase.name + '-get-wadus' }); + + const testClientCreate = new TestClient(template, apikeyTokenCreate); + + testClientCreate.createTemplate({}, function (err, res, template) { + assert.ifError(err); + + const testClientGet = new TestClient(template, apikeyTokenGet); + testClientGet.getTemplate( + { + 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]); + + testClientGet.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); + describe('UPDATE Named Map', function () { + const templateBase = { + version: '0.0.1', + name: 'auth-api-template', + placeholders: { + buffersize: { + type: 'number', + default: 0 } - ); - }); - }); - - 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) { - const apikeyToken = 'regular1'; - - const template = { - version: '0.0.1', - name: 'auth-api-template-private', - 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', + } + }] } - }, - layergroup: { - version: '1.7.0', - layers: [{ - type: 'cartodb', - options: { - sql: 'select * from populated_places_simple_reduced_private', - cartocss: TestClient.CARTOCSS.POINTS, - cartocss_version: '2.3.0', - } - }] - } - }; + }; - const testClient = new TestClient(template, apikeyToken); + it('should update a named map using the master apikey token', function (done) { + const apikeyTokenCreate = 1234; + const apikeyTokenUpdate = 1234; - testClient.getTile(0, 0, 0, { response: PERMISSION_DENIED_RESPONSE }, function (err, res, body) { - assert.ifError(err); + const template = Object.assign({}, templateBase, { name: templateBase.name + '-update-master' }); + const templateUpdate = Object.assign({}, template, { zoom: 3 }); + const testClientCreate = new TestClient(template, apikeyTokenCreate); - assert.ok(body.hasOwnProperty('errors')); - assert.equal(body.errors.length, 1); - assert.ok(body.errors[0].match(/permission denied/), body.errors[0]); + testClientCreate.createTemplate({}, function (err, res, template) { + assert.ifError(err); - testClient.drain(done); - }); + 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 3e35b740..f7911cc1 100644 --- a/test/support/test-client.js +++ b/test/support/test-client.js @@ -1483,6 +1483,46 @@ TestClient.prototype.updateTemplate = function (params, callback) { break; } + return callback(err, res, body); + }); +}; + + +TestClient.prototype.getTemplate = function (params, callback) { + if (!this.apiKey) { + return callback(new Error('apiKey param is mandatory to create a new template')); + } + + const getTemplateRequest = { + url: `/api/v1/map/named/${params.templateId}?${qs.stringify({ api_key: this.apiKey })}`, + method: 'GET', + headers: { + host: 'localhost' + } + }; + + let getTemplateResponse = { + status: 200, + headers: { + 'Content-Type': 'application/json; charset=utf-8' + } + }; + + if (params.response) { + getTemplateResponse = Object.assign(getTemplateResponse, params.response); + } + + assert.response(this.server, getTemplateRequest, getTemplateResponse, (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 From d1ba4a1759d06397dbd7fe87972e46fa1e3a351b Mon Sep 17 00:00:00 2001 From: Eneko Lakasta Date: Thu, 12 Apr 2018 17:41:07 +0200 Subject: [PATCH 6/7] remove unnecessary test --- test/acceptance/auth/authorization.js | 28 ++------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/test/acceptance/auth/authorization.js b/test/acceptance/auth/authorization.js index 37407531..2974d36d 100644 --- a/test/acceptance/auth/authorization.js +++ b/test/acceptance/auth/authorization.js @@ -37,31 +37,6 @@ describe('authorization', function() { }); }); - it.skip('should create and get a named map tile using a regular apikey token', function (done) { - const apikeyToken = 'regular1'; - const mapConfig = { - version: '1.7.0', - layers: [ - { - options: { - sql: 'select * FROM test_table_localhost_regular1', - cartocss: TestClient.CARTOCSS.POINTS, - cartocss_version: '2.3.0' - } - } - ] - }; - const testClient = new TestClient(mapConfig, apikeyToken); - - testClient.getTile(0, 0, 0, function (err, res, tile) { - assert.ifError(err); - - assert.equal(res.statusCode, 200); - assert.ok(tile instanceof mapnik.Image); - - testClient.drain(done); - }); - }); it('should fail getting a named map tile with default apikey token', function (done) { const apikeyTokenCreate = 'regular1'; @@ -353,7 +328,8 @@ describe('authorization', function() { testClient.drain(done); }); }); - describe.only('Named maps', function () { + + describe('Named maps', function () { describe('LIST Named maps', function () { it('should fail while listing named maps with a regular apikey token', function (done) { From 940cafacac487058b0e3b87658eec31662ee8dbf Mon Sep 17 00:00:00 2001 From: Eneko Lakasta Date: Thu, 12 Apr 2018 17:51:33 +0200 Subject: [PATCH 7/7] add newline add EOF --- test/support/test-client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/support/test-client.js b/test/support/test-client.js index f7911cc1..4bf2d91a 100644 --- a/test/support/test-client.js +++ b/test/support/test-client.js @@ -1525,4 +1525,4 @@ TestClient.prototype.getTemplate = function (params, callback) { return callback(err, res, body); }); -}; \ No newline at end of file +};