From 9a7a8a324395c8b6fa188485d9762ee30e285b9e Mon Sep 17 00:00:00 2001 From: Eneko Lakasta Date: Thu, 12 Apr 2018 12:43:41 +0200 Subject: [PATCH] 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)); + }); +};