blob: 5cd4a706b5178a37120f13b96a88262549e6e870 [file] [log] [blame]
Matteo Scandoloe3ed0162016-12-01 10:09:12 -08001(function () {
2 'use strict';
3
4 const chai = require('chai');
5 const expect = chai.expect;
6 const sinonChai = require('sinon-chai');
7 const supertest = require('supertest');
8 const mockery = require('mockery');
9 chai.use(sinonChai);
10
11 const request = require('superagent');
12 const stub = require('./spec_helper.js');
13
14 const configMock = {
15 xos: {
16 host: 'http://test-xos-url',
17 port: '80'
18 }
19 };
20
21 let app;
22
23 describe('The core proxy routes', () => {
24
25 // stub for GET method
26 stub.makeStub('getSuccess', request, 'get', cb => {
27 cb(null, {
28 status: 200,
29 body: {msg: 'successfully proxied'}
30 });
31 });
32
33 // stub for POST method
34 stub.makeStub('postSuccess', request, 'post', cb => {
35 cb(null, {
36 status: 201,
37 body: {msg: 'successfully proxied'}
38 });
39 });
40
41 // stub for PUT method
42 stub.makeStub('putSuccess', request, 'put', cb => {
43 cb(null, {
44 status: 200,
45 body: {msg: 'successfully proxied'}
46 });
47 });
48
49 // stub for DELETE method
50 stub.makeStub('deleteSuccess', request, 'delete', cb => {
51 cb(null, {
52 status: 204
53 });
54 });
55
56 // mocking the config.rest module
57 before(() => {
58 mockery.enable({
59 warnOnReplace: true,
60 warnOnUnregistered: false
61 });
62 mockery.registerMock('../config/config.js', configMock);
63
64 app = require('../src/server.js').app;
65 });
66
67 after(() => {
68 mockery.deregisterMock('../config/config.js');
69 mockery.disable();
70 });
71
72 it('should read XOS address from config.rest module', (done) => {
73 const myStub = stub.getStub('getSuccess');
74
75 supertest(app)
76 .get('/api/core/')
77 .end((err) => {
78 if (err) return done(err);
79 expect(myStub.get.called).to.be.true;
80 expect(myStub.get).to.have.been.calledWith('http://test-xos-url:80/api/core/');
81 done();
82 });
83 });
84
85 it('should pass token and cookies along with the request', (done) => {
86 const myStub = stub.getStub('getSuccess');
87
88 supertest(app)
89 .get('/api/core/')
90 .set('Accept', 'application/json')
91 .set('x-csrftoken', 'testToken')
Matteo Scandolof05d8a62016-12-06 13:36:49 -080092 .set('x-sessionid', 'testSession')
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080093 .end(function(err) {
94 if (err) return done(err);
95 expect(myStub.set.getCall(0)).to.have.been.calledWith('x-csrftoken', 'testToken');
Matteo Scandolo2eca9892016-12-23 09:10:27 -080096 expect(myStub.set.getCall(1)).to.have.been.calledWith('cookie', 'xoscsrftoken=testToken; xossessionid=testSession');
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080097 done();
98 });
99 });
100
101 it('should pass query paramenters along with the request', (done) => {
102 const myStub = stub.getStub('getSuccess');
103
104 supertest(app)
105 .get('/api/core/instances/?no_hyperlink=1&node=1')
106 .end((err) => {
107 if (err) return done(err);
108 expect(myStub.get.called).to.be.true;
109 expect(myStub.get).to.have.been.calledWith('http://test-xos-url:80/api/core/instances/?no_hyperlink=1&node=1');
110 done();
111 });
112 });
113
114 it('should proxy GET request to XOS', (done) => {
115 supertest(app)
116 .get('/api/core/')
117 .set('Accept', 'application/json')
118 .set('x-csrftoken', 'testToken')
119 .set('cookie', 'testCookie')
120 .end(function(err, res) {
121 if (err) return done(err);
122 expect(res.status).to.equal(200);
123 expect(res.body).to.deep.equal({msg: 'successfully proxied'});
124 done();
125 });
126 });
127
128 it('should proxy POST request to XOS', (done) => {
129
130 const myStub = stub.getStub('postSuccess');
131
132 supertest(app)
133 .post('/api/core/')
134 .send({foo: 'bar'})
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(myStub.send.getCall(0)).to.have.been.calledWith({foo: 'bar'});
141 expect(res.status).to.equal(201);
142 expect(res.body).to.deep.equal({msg: 'successfully proxied'});
143 done();
144 });
145 });
146
147 it('should proxy PUT request to XOS', (done) => {
148
149 const myStub = stub.getStub('putSuccess');
150
151 supertest(app)
152 .put('/api/core/')
153 .send({foo: 'bar'})
154 .set('Accept', 'application/json')
155 .set('x-csrftoken', 'testToken')
156 .set('cookie', 'testCookie')
157 .end(function(err, res) {
158 if (err) return done(err);
159 expect(myStub.send.getCall(0)).to.have.been.calledWith({foo: 'bar'});
160 expect(res.status).to.equal(200);
161 expect(res.body).to.deep.equal({msg: 'successfully proxied'});
162 done();
163 });
164 });
165
166 it('should proxy DELETE request to XOS', (done) => {
167
168 const myStub = stub.getStub('deleteSuccess');
169
170 supertest(app)
171 .delete('/api/core/')
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).not.to.have.been.called;
178 expect(res.status).to.equal(204);
179 done();
180 });
181 });
182 });
183})();