Matteo Scandolo | bd13aab | 2017-08-08 13:05:24 -0700 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 19 | (function () { |
| 20 | 'use strict'; |
| 21 | |
| 22 | const chai = require('chai'); |
| 23 | const expect = chai.expect; |
| 24 | const sinonChai = require('sinon-chai'); |
| 25 | const supertest = require('supertest'); |
| 26 | const mockery = require('mockery'); |
| 27 | chai.use(sinonChai); |
| 28 | |
| 29 | const request = require('superagent'); |
| 30 | const stub = require('./spec_helper.js'); |
| 31 | |
| 32 | const configMock = { |
| 33 | xos: { |
| 34 | host: 'http://test-xos-url', |
| 35 | port: '80' |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | let app; |
| 40 | |
Matteo Scandolo | f64cdd5 | 2017-02-24 07:52:49 -0800 | [diff] [blame] | 41 | xdescribe('The core proxy routes [REST API moved to chameleon]', () => { |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 42 | |
| 43 | // stub for GET method |
| 44 | stub.makeStub('getSuccess', request, 'get', cb => { |
| 45 | cb(null, { |
| 46 | status: 200, |
| 47 | body: {msg: 'successfully proxied'} |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | // stub for POST method |
| 52 | stub.makeStub('postSuccess', request, 'post', cb => { |
| 53 | cb(null, { |
| 54 | status: 201, |
| 55 | body: {msg: 'successfully proxied'} |
| 56 | }); |
| 57 | }); |
| 58 | |
| 59 | // stub for PUT method |
| 60 | stub.makeStub('putSuccess', request, 'put', cb => { |
| 61 | cb(null, { |
| 62 | status: 200, |
| 63 | body: {msg: 'successfully proxied'} |
| 64 | }); |
| 65 | }); |
| 66 | |
| 67 | // stub for DELETE method |
| 68 | stub.makeStub('deleteSuccess', request, 'delete', cb => { |
| 69 | cb(null, { |
| 70 | status: 204 |
| 71 | }); |
| 72 | }); |
| 73 | |
| 74 | // mocking the config.rest module |
| 75 | before(() => { |
| 76 | mockery.enable({ |
| 77 | warnOnReplace: true, |
| 78 | warnOnUnregistered: false |
| 79 | }); |
| 80 | mockery.registerMock('../config/config.js', configMock); |
| 81 | |
| 82 | app = require('../src/server.js').app; |
| 83 | }); |
| 84 | |
| 85 | after(() => { |
| 86 | mockery.deregisterMock('../config/config.js'); |
| 87 | mockery.disable(); |
| 88 | }); |
| 89 | |
| 90 | it('should read XOS address from config.rest module', (done) => { |
| 91 | const myStub = stub.getStub('getSuccess'); |
| 92 | |
| 93 | supertest(app) |
| 94 | .get('/api/core/') |
| 95 | .end((err) => { |
| 96 | if (err) return done(err); |
| 97 | expect(myStub.get.called).to.be.true; |
| 98 | expect(myStub.get).to.have.been.calledWith('http://test-xos-url:80/api/core/'); |
| 99 | done(); |
| 100 | }); |
| 101 | }); |
| 102 | |
| 103 | it('should pass token and cookies along with the request', (done) => { |
| 104 | const myStub = stub.getStub('getSuccess'); |
| 105 | |
| 106 | supertest(app) |
| 107 | .get('/api/core/') |
| 108 | .set('Accept', 'application/json') |
| 109 | .set('x-csrftoken', 'testToken') |
Matteo Scandolo | f05d8a6 | 2016-12-06 13:36:49 -0800 | [diff] [blame] | 110 | .set('x-sessionid', 'testSession') |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 111 | .end(function(err) { |
| 112 | if (err) return done(err); |
| 113 | expect(myStub.set.getCall(0)).to.have.been.calledWith('x-csrftoken', 'testToken'); |
Matteo Scandolo | 2eca989 | 2016-12-23 09:10:27 -0800 | [diff] [blame] | 114 | expect(myStub.set.getCall(1)).to.have.been.calledWith('cookie', 'xoscsrftoken=testToken; xossessionid=testSession'); |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 115 | done(); |
| 116 | }); |
| 117 | }); |
| 118 | |
| 119 | it('should pass query paramenters along with the request', (done) => { |
| 120 | const myStub = stub.getStub('getSuccess'); |
| 121 | |
| 122 | supertest(app) |
| 123 | .get('/api/core/instances/?no_hyperlink=1&node=1') |
| 124 | .end((err) => { |
| 125 | if (err) return done(err); |
| 126 | expect(myStub.get.called).to.be.true; |
| 127 | expect(myStub.get).to.have.been.calledWith('http://test-xos-url:80/api/core/instances/?no_hyperlink=1&node=1'); |
| 128 | done(); |
| 129 | }); |
| 130 | }); |
| 131 | |
| 132 | it('should proxy GET request to XOS', (done) => { |
| 133 | supertest(app) |
| 134 | .get('/api/core/') |
| 135 | .set('Accept', 'application/json') |
| 136 | .set('x-csrftoken', 'testToken') |
| 137 | .set('cookie', 'testCookie') |
| 138 | .end(function(err, res) { |
| 139 | if (err) return done(err); |
| 140 | expect(res.status).to.equal(200); |
| 141 | expect(res.body).to.deep.equal({msg: 'successfully proxied'}); |
| 142 | done(); |
| 143 | }); |
| 144 | }); |
| 145 | |
| 146 | it('should proxy POST request to XOS', (done) => { |
| 147 | |
| 148 | const myStub = stub.getStub('postSuccess'); |
| 149 | |
| 150 | supertest(app) |
| 151 | .post('/api/core/') |
| 152 | .send({foo: 'bar'}) |
| 153 | .set('Accept', 'application/json') |
| 154 | .set('x-csrftoken', 'testToken') |
| 155 | .set('cookie', 'testCookie') |
| 156 | .end(function(err, res) { |
| 157 | if (err) return done(err); |
| 158 | expect(myStub.send.getCall(0)).to.have.been.calledWith({foo: 'bar'}); |
| 159 | expect(res.status).to.equal(201); |
| 160 | expect(res.body).to.deep.equal({msg: 'successfully proxied'}); |
| 161 | done(); |
| 162 | }); |
| 163 | }); |
| 164 | |
| 165 | it('should proxy PUT request to XOS', (done) => { |
| 166 | |
| 167 | const myStub = stub.getStub('putSuccess'); |
| 168 | |
| 169 | supertest(app) |
| 170 | .put('/api/core/') |
| 171 | .send({foo: 'bar'}) |
| 172 | .set('Accept', 'application/json') |
| 173 | .set('x-csrftoken', 'testToken') |
| 174 | .set('cookie', 'testCookie') |
| 175 | .end(function(err, res) { |
| 176 | if (err) return done(err); |
| 177 | expect(myStub.send.getCall(0)).to.have.been.calledWith({foo: 'bar'}); |
| 178 | expect(res.status).to.equal(200); |
| 179 | expect(res.body).to.deep.equal({msg: 'successfully proxied'}); |
| 180 | done(); |
| 181 | }); |
| 182 | }); |
| 183 | |
| 184 | it('should proxy DELETE request to XOS', (done) => { |
| 185 | |
| 186 | const myStub = stub.getStub('deleteSuccess'); |
| 187 | |
| 188 | supertest(app) |
| 189 | .delete('/api/core/') |
| 190 | .set('Accept', 'application/json') |
| 191 | .set('x-csrftoken', 'testToken') |
| 192 | .set('cookie', 'testCookie') |
| 193 | .end(function(err, res) { |
| 194 | if (err) return done(err); |
| 195 | expect(myStub.send).not.to.have.been.called; |
| 196 | expect(res.status).to.equal(204); |
| 197 | done(); |
| 198 | }); |
| 199 | }); |
| 200 | }); |
| 201 | })(); |