blob: b19ce0c5de3943dc907f78e24ccee994b6af5b50 [file] [log] [blame]
Matteo Scandolo920df1a2015-10-23 16:31:17 +02001'use strict';
2
3describe('The Content Provider SPA', () => {
4
Matteo Scandolob64a1262015-10-28 10:39:59 +01005 var scope, element, isolatedScope, httpBackend, mockLocation, httpProvider;
6
7 var token = 'fakeToken';
Matteo Scandolo920df1a2015-10-23 16:31:17 +02008
9 // injecting main module
Matteo Scandolo947d4082015-11-04 16:55:21 +010010 beforeEach(module('xos.contentProvider'));
11
12 beforeEach(module('templates'));
Matteo Scandolo920df1a2015-10-23 16:31:17 +020013
Matteo Scandoloc11a8a42015-10-27 16:41:47 +010014 beforeEach(function(){
Matteo Scandolob64a1262015-10-28 10:39:59 +010015 module(function($provide, $httpProvider){
16
17 httpProvider = $httpProvider;
18
Matteo Scandolo6f7f9742015-11-06 18:49:33 +010019 // mocking stateParams to pass 1 as id
20 $provide.provider('$stateParams', function(){
Matteo Scandoloc11a8a42015-10-27 16:41:47 +010021 /* eslint-disable no-invalid-this*/
22 this.$get = function(){
Matteo Scandoloc1b52722015-10-23 18:37:05 +020023 return {id: 1};
24 };
Matteo Scandoloc11a8a42015-10-27 16:41:47 +010025 /* eslint-enable no-invalid-this*/
Matteo Scandoloc1b52722015-10-23 18:37:05 +020026 });
Matteo Scandolob64a1262015-10-28 10:39:59 +010027
28 //mock $cookie to return a fake xoscsrftoken
29 $provide.service('$cookies', function(){
30 /* eslint-disable no-invalid-this*/
31 this.get = () => {
32 return token;
33 };
34 /* eslint-enable no-invalid-this*/
35 });
Matteo Scandoloc1b52722015-10-23 18:37:05 +020036 });
37 });
38
Matteo Scandoloc11a8a42015-10-27 16:41:47 +010039 beforeEach(inject(function(_$location_, $httpBackend){
Matteo Scandolo6b478dc2015-10-23 17:45:45 +020040 spyOn(_$location_, 'url');
41 mockLocation = _$location_;
42 httpBackend = $httpBackend;
43 // Setting up mock request
Matteo Scandoloc11a8a42015-10-27 16:41:47 +010044 $httpBackend.whenGET('/hpcapi/contentproviders/?no_hyperlinks=1').respond(CPmock.CPlist);
45 $httpBackend.whenGET('/hpcapi/serviceproviders/?no_hyperlinks=1').respond(CPmock.SPlist);
46 $httpBackend.whenDELETE('/hpcapi/contentproviders/1/?no_hyperlinks=1').respond();
Matteo Scandolo6b478dc2015-10-23 17:45:45 +020047 }));
48
Matteo Scandolob64a1262015-10-28 10:39:59 +010049 it('should set the $http interceptor', () => {
50 expect(httpProvider.interceptors).toContain('SetCSRFToken');
51 });
52
53 it('should add no_hyperlink param', inject(($http, $httpBackend) => {
54 $http.get('www.example.com');
55 $httpBackend.expectGET('www.example.com?no_hyperlinks=1').respond(200);
56 $httpBackend.flush();
57 }));
58
59 it('should set token in the headers', inject(($http, $httpBackend) => {
60 $http.post('http://example.com');
61 $httpBackend.expectPOST('http://example.com?no_hyperlinks=1', undefined, function(headers){
62 // if this condition is false the httpBackend expectation fail
63 return headers['X-CSRFToken'] === token;
64 }).respond(200, {name: 'example'});
65 httpBackend.flush();
66 }));
67
Matteo Scandolo6b478dc2015-10-23 17:45:45 +020068 describe('the action directive', () => {
Matteo Scandoloc11a8a42015-10-27 16:41:47 +010069 beforeEach(inject(function($compile, $rootScope){
Matteo Scandolo6b478dc2015-10-23 17:45:45 +020070 scope = $rootScope.$new();
71
72 element = angular.element('<cp-actions id="\'1\'"></cp-actions>');
73 $compile(element)(scope);
74 scope.$digest();
75 isolatedScope = element.isolateScope().vm;
76 }));
77
78 it('should delete an element and redirect to list', () => {
79 isolatedScope.deleteCp(1);
80 httpBackend.flush();
81 expect(mockLocation.url).toHaveBeenCalled();
82 });
83 });
84
Matteo Scandolo920df1a2015-10-23 16:31:17 +020085 describe('the contentProvider list', () => {
Matteo Scandoloc11a8a42015-10-27 16:41:47 +010086 beforeEach(inject(function($compile, $rootScope){
Matteo Scandolo920df1a2015-10-23 16:31:17 +020087 scope = $rootScope.$new();
88
89 element = angular.element('<content-provider-list></content-provider-list>');
90 $compile(element)(scope);
91 scope.$digest();
Matteo Scandolo6b478dc2015-10-23 17:45:45 +020092 httpBackend.flush();
93 isolatedScope = element.isolateScope().vm;
Matteo Scandolo920df1a2015-10-23 16:31:17 +020094 }));
95
96
Matteo Scandolo6b478dc2015-10-23 17:45:45 +020097 it('should load 2 contentProvider', () => {
98 expect(isolatedScope.contentProviderList.length).toBe(2);
99 });
100
101 it('should delete a contentProvider', () => {
102 isolatedScope.deleteCp(1);
103 httpBackend.flush();
104 expect(isolatedScope.contentProviderList.length).toBe(1);
Matteo Scandolo920df1a2015-10-23 16:31:17 +0200105 });
106 });
Matteo Scandoloc1b52722015-10-23 18:37:05 +0200107
108 describe('the contentProviderDetail directive', () => {
Matteo Scandoloaf176d32015-10-26 16:17:48 +0100109
Matteo Scandoloc11a8a42015-10-27 16:41:47 +0100110 beforeEach(inject(function($compile, $rootScope){
Matteo Scandoloaf176d32015-10-26 16:17:48 +0100111 scope = $rootScope.$new();
112 element = angular.element('<content-provider-detail></content-provider-detail>');
113 $compile(element)(scope);
Matteo Scandoloc11a8a42015-10-27 16:41:47 +0100114 httpBackend.expectGET('/hpcapi/contentproviders/1/?no_hyperlinks=1').respond(CPmock.CPlist[0]);
Matteo Scandoloaf176d32015-10-26 16:17:48 +0100115 scope.$digest();
116 httpBackend.flush();
117 isolatedScope = element.isolateScope().vm;
118 }));
119
Matteo Scandoloc1b52722015-10-23 18:37:05 +0200120 describe('when an id is set in the route', () => {
121
Matteo Scandoloaf176d32015-10-26 16:17:48 +0100122 beforeEach(() => {
123 // spy the instance update method
124 spyOn(isolatedScope.cp, '$update').and.callThrough();
125 });
Matteo Scandoloc1b52722015-10-23 18:37:05 +0200126
127 it('should request the correct contentProvider', () => {
128 expect(isolatedScope.cp.name).toEqual(CPmock.CPlist[0].name);
129 });
130
131 it('should update a contentProvider', () => {
132 isolatedScope.cp.name = 'new name';
133 isolatedScope.saveContentProvider(isolatedScope.cp);
Matteo Scandoloaf176d32015-10-26 16:17:48 +0100134 expect(isolatedScope.cp.$update).toHaveBeenCalled();
Matteo Scandoloc1b52722015-10-23 18:37:05 +0200135 });
136 });
137 });
Matteo Scandoloaf176d32015-10-26 16:17:48 +0100138
139 describe('the contentProviderCdn directive', () => {
140 beforeEach(inject(($compile, $rootScope) => {
141 scope = $rootScope.$new();
142 element = angular.element('<content-provider-cdn></content-provider-cdn>');
143 $compile(element)(scope);
Matteo Scandoloc11a8a42015-10-27 16:41:47 +0100144 httpBackend.expectGET('/hpcapi/contentproviders/1/?no_hyperlinks=1').respond(CPmock.CPlist[0]);
145 // httpBackend.expectGET('/hpcapi/cdnprefixs/?no_hyperlinks=1&contentProvider=1').respond([CPmock.CDNlist[0]]);
146 httpBackend.expectGET('/hpcapi/cdnprefixs/?no_hyperlinks=1').respond(CPmock.CDNlist);
147 httpBackend.whenPOST('/hpcapi/cdnprefixs/?no_hyperlinks=1').respond(CPmock.CDNlist[0]);
148 httpBackend.whenDELETE('/hpcapi/cdnprefixs/5/?no_hyperlinks=1').respond();
Matteo Scandoloaf176d32015-10-26 16:17:48 +0100149 scope.$digest();
150 httpBackend.flush();
151 isolatedScope = element.isolateScope().vm;
152 }));
153
154 it('should load associated CDN prefix', () => {
155 expect(isolatedScope.cp_prf.length).toBe(1);
156 expect(isolatedScope.prf.length).toBe(2);
157 });
158
159 it('should add a CDN Prefix', () => {
160 isolatedScope.addPrefix({prefix: 'test.io', defaultOriginServer: '/hpcapi/originservers/2/'});
161 httpBackend.flush();
162 expect(isolatedScope.cp_prf.length).toBe(2);
163 });
164
165 it('should remove a CDN Prefix', () => {
166 isolatedScope.removePrefix(isolatedScope.cp_prf[0]);
167 httpBackend.flush();
168 expect(isolatedScope.cp_prf.length).toBe(0);
169 });
170 });
Matteo Scandolocd7240a2015-10-26 16:27:50 +0100171
172 describe('the contentProviderServer directive', () => {
173 beforeEach(inject(($compile, $rootScope) => {
174 scope = $rootScope.$new();
175 element = angular.element('<content-provider-server></content-provider-server>');
176 $compile(element)(scope);
Matteo Scandoloc11a8a42015-10-27 16:41:47 +0100177 httpBackend.expectGET('/hpcapi/contentproviders/1/?no_hyperlinks=1').respond(CPmock.CPlist[0]);
178 httpBackend.expectGET('/hpcapi/originservers/?no_hyperlinks=1&contentProvider=1').respond(CPmock.OSlist);
179 httpBackend.whenPOST('/hpcapi/originservers/?no_hyperlinks=1').respond(CPmock.OSlist[0]);
180 httpBackend.whenDELETE('/hpcapi/originservers/8/?no_hyperlinks=1').respond();
Matteo Scandolocd7240a2015-10-26 16:27:50 +0100181 scope.$digest();
182 httpBackend.flush();
183 isolatedScope = element.isolateScope().vm;
184 }));
185
186 it('should load associated OriginServer', () => {
187 expect(isolatedScope.cp_os.length).toBe(4);
188 });
189
190 it('should add a OriginServer', () => {
191 isolatedScope.addOrigin({protocol: 'http', url: 'test.io'});
192 httpBackend.flush();
193 expect(isolatedScope.cp_os.length).toBe(5);
194 });
195
196 it('should remove a OriginServer', () => {
197 isolatedScope.removeOrigin(isolatedScope.cp_os[0]);
198 httpBackend.flush();
199 expect(isolatedScope.cp_os.length).toBe(3);
200 });
201 });
Matteo Scandoloc11a8a42015-10-27 16:41:47 +0100202
203 describe('the contentProviderUsers directive', () => {
204 beforeEach(inject(($compile, $rootScope) => {
205 scope = $rootScope.$new();
206 element = angular.element('<content-provider-users></content-provider-users>');
207 $compile(element)(scope);
208 httpBackend.expectGET('/xos/users/?no_hyperlinks=1').respond(CPmock.UserList);
209 httpBackend.expectGET('/hpcapi/contentproviders/1/?no_hyperlinks=1').respond(CPmock.CPlist[0]);
210 httpBackend.whenPUT('/hpcapi/contentproviders/1/?no_hyperlinks=1').respond(CPmock.CPlist[0]);
211 scope.$digest();
212 httpBackend.flush();
213 isolatedScope = element.isolateScope().vm;
214 }));
215
216 it('should render one user', () => {
217 expect(isolatedScope.cp.users.length).toBe(1);
218 expect(typeof isolatedScope.cp.users[0]).toEqual('object');
219 });
220
221 it('should add a user', () => {
222 isolatedScope.addUserToCp({name: 'teo'});
223 expect(isolatedScope.cp.users.length).toBe(2);
224 });
225
226 it('should remove a user', () => {
227 isolatedScope.addUserToCp({name: 'teo'});
228 expect(isolatedScope.cp.users.length).toBe(2);
229 isolatedScope.removeUserFromCp({name: 'teo'});
230 expect(isolatedScope.cp.users.length).toBe(1);
231 });
232
233 it('should save and reformat users', () => {
234 // add a user
235 isolatedScope.cp.users.push(1);
236
237 //trigger save
238 isolatedScope.saveContentProvider(isolatedScope.cp);
239
240 httpBackend.flush();
241
242 // I'll get one as the BE is mocked, the important is to check the conversion
243 expect(isolatedScope.cp.users.length).toBe(1);
244 expect(typeof isolatedScope.cp.users[0]).toEqual('object');
245 });
246 });
Matteo Scandolo920df1a2015-10-23 16:31:17 +0200247});