blob: 3de80d1a825444ab53aef0456f6fdda01f7412b2 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
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 Scandolo0968aa92015-10-23 16:31:17 +020019'use strict';
20
21describe('The Content Provider SPA', () => {
22
Matteo Scandolo0314b342015-10-28 10:39:59 +010023 var scope, element, isolatedScope, httpBackend, mockLocation, httpProvider;
24
25 var token = 'fakeToken';
Matteo Scandolo0968aa92015-10-23 16:31:17 +020026
27 // injecting main module
Matteo Scandoload95de42015-11-04 16:55:21 +010028 beforeEach(module('xos.contentProvider'));
29
30 beforeEach(module('templates'));
Matteo Scandolo0968aa92015-10-23 16:31:17 +020031
Matteo Scandolo8a31ee22015-10-27 16:41:47 +010032 beforeEach(function(){
Matteo Scandolo0314b342015-10-28 10:39:59 +010033 module(function($provide, $httpProvider){
34
35 httpProvider = $httpProvider;
36
Matteo Scandolo7db08432015-11-06 18:49:33 +010037 // mocking stateParams to pass 1 as id
38 $provide.provider('$stateParams', function(){
Matteo Scandolo8a31ee22015-10-27 16:41:47 +010039 /* eslint-disable no-invalid-this*/
40 this.$get = function(){
Matteo Scandolo0fe4c3e2015-10-23 18:37:05 +020041 return {id: 1};
42 };
Matteo Scandolo8a31ee22015-10-27 16:41:47 +010043 /* eslint-enable no-invalid-this*/
Matteo Scandolo0fe4c3e2015-10-23 18:37:05 +020044 });
Matteo Scandolo0314b342015-10-28 10:39:59 +010045
46 //mock $cookie to return a fake xoscsrftoken
47 $provide.service('$cookies', function(){
48 /* eslint-disable no-invalid-this*/
49 this.get = () => {
50 return token;
51 };
52 /* eslint-enable no-invalid-this*/
53 });
Matteo Scandolo0fe4c3e2015-10-23 18:37:05 +020054 });
55 });
56
Matteo Scandolo8a31ee22015-10-27 16:41:47 +010057 beforeEach(inject(function(_$location_, $httpBackend){
Matteo Scandoloba3b12a2015-10-23 17:45:45 +020058 spyOn(_$location_, 'url');
59 mockLocation = _$location_;
60 httpBackend = $httpBackend;
61 // Setting up mock request
Matteo Scandolo8a31ee22015-10-27 16:41:47 +010062 $httpBackend.whenGET('/hpcapi/contentproviders/?no_hyperlinks=1').respond(CPmock.CPlist);
63 $httpBackend.whenGET('/hpcapi/serviceproviders/?no_hyperlinks=1').respond(CPmock.SPlist);
64 $httpBackend.whenDELETE('/hpcapi/contentproviders/1/?no_hyperlinks=1').respond();
Matteo Scandoloba3b12a2015-10-23 17:45:45 +020065 }));
66
Matteo Scandolo54bc5f72016-05-18 14:06:45 -070067 xit('should set the $http interceptor', () => {
Matteo Scandolo0314b342015-10-28 10:39:59 +010068 expect(httpProvider.interceptors).toContain('SetCSRFToken');
69 });
70
Matteo Scandolo54bc5f72016-05-18 14:06:45 -070071 xit('should add no_hyperlink param', inject(($http, $httpBackend) => {
Matteo Scandolo0314b342015-10-28 10:39:59 +010072 $http.get('www.example.com');
73 $httpBackend.expectGET('www.example.com?no_hyperlinks=1').respond(200);
74 $httpBackend.flush();
75 }));
76
Matteo Scandolo54bc5f72016-05-18 14:06:45 -070077 xit('should set token in the headers', inject(($http, $httpBackend) => {
Matteo Scandolo0314b342015-10-28 10:39:59 +010078 $http.post('http://example.com');
79 $httpBackend.expectPOST('http://example.com?no_hyperlinks=1', undefined, function(headers){
80 // if this condition is false the httpBackend expectation fail
81 return headers['X-CSRFToken'] === token;
82 }).respond(200, {name: 'example'});
83 httpBackend.flush();
84 }));
85
Matteo Scandoloba3b12a2015-10-23 17:45:45 +020086 describe('the action directive', () => {
Matteo Scandolo8a31ee22015-10-27 16:41:47 +010087 beforeEach(inject(function($compile, $rootScope){
Matteo Scandoloba3b12a2015-10-23 17:45:45 +020088 scope = $rootScope.$new();
89
90 element = angular.element('<cp-actions id="\'1\'"></cp-actions>');
91 $compile(element)(scope);
92 scope.$digest();
93 isolatedScope = element.isolateScope().vm;
94 }));
95
96 it('should delete an element and redirect to list', () => {
97 isolatedScope.deleteCp(1);
98 httpBackend.flush();
99 expect(mockLocation.url).toHaveBeenCalled();
100 });
101 });
102
Matteo Scandolo0968aa92015-10-23 16:31:17 +0200103 describe('the contentProvider list', () => {
Matteo Scandolo8a31ee22015-10-27 16:41:47 +0100104 beforeEach(inject(function($compile, $rootScope){
Matteo Scandolo0968aa92015-10-23 16:31:17 +0200105 scope = $rootScope.$new();
106
107 element = angular.element('<content-provider-list></content-provider-list>');
108 $compile(element)(scope);
109 scope.$digest();
Matteo Scandoloba3b12a2015-10-23 17:45:45 +0200110 httpBackend.flush();
111 isolatedScope = element.isolateScope().vm;
Matteo Scandolo0968aa92015-10-23 16:31:17 +0200112 }));
113
114
Matteo Scandoloba3b12a2015-10-23 17:45:45 +0200115 it('should load 2 contentProvider', () => {
116 expect(isolatedScope.contentProviderList.length).toBe(2);
117 });
118
119 it('should delete a contentProvider', () => {
120 isolatedScope.deleteCp(1);
121 httpBackend.flush();
122 expect(isolatedScope.contentProviderList.length).toBe(1);
Matteo Scandolo0968aa92015-10-23 16:31:17 +0200123 });
124 });
Matteo Scandolo0fe4c3e2015-10-23 18:37:05 +0200125
126 describe('the contentProviderDetail directive', () => {
Matteo Scandolobb3c0132015-10-26 16:17:48 +0100127
Matteo Scandolo8a31ee22015-10-27 16:41:47 +0100128 beforeEach(inject(function($compile, $rootScope){
Matteo Scandolobb3c0132015-10-26 16:17:48 +0100129 scope = $rootScope.$new();
130 element = angular.element('<content-provider-detail></content-provider-detail>');
131 $compile(element)(scope);
Matteo Scandolo8a31ee22015-10-27 16:41:47 +0100132 httpBackend.expectGET('/hpcapi/contentproviders/1/?no_hyperlinks=1').respond(CPmock.CPlist[0]);
Matteo Scandolobb3c0132015-10-26 16:17:48 +0100133 scope.$digest();
134 httpBackend.flush();
135 isolatedScope = element.isolateScope().vm;
136 }));
137
Matteo Scandolo0fe4c3e2015-10-23 18:37:05 +0200138 describe('when an id is set in the route', () => {
139
Matteo Scandolobb3c0132015-10-26 16:17:48 +0100140 beforeEach(() => {
141 // spy the instance update method
142 spyOn(isolatedScope.cp, '$update').and.callThrough();
143 });
Matteo Scandolo0fe4c3e2015-10-23 18:37:05 +0200144
145 it('should request the correct contentProvider', () => {
146 expect(isolatedScope.cp.name).toEqual(CPmock.CPlist[0].name);
147 });
148
149 it('should update a contentProvider', () => {
150 isolatedScope.cp.name = 'new name';
151 isolatedScope.saveContentProvider(isolatedScope.cp);
Matteo Scandolobb3c0132015-10-26 16:17:48 +0100152 expect(isolatedScope.cp.$update).toHaveBeenCalled();
Matteo Scandolo0fe4c3e2015-10-23 18:37:05 +0200153 });
154 });
155 });
Matteo Scandolobb3c0132015-10-26 16:17:48 +0100156
157 describe('the contentProviderCdn directive', () => {
158 beforeEach(inject(($compile, $rootScope) => {
159 scope = $rootScope.$new();
160 element = angular.element('<content-provider-cdn></content-provider-cdn>');
161 $compile(element)(scope);
Matteo Scandolo8a31ee22015-10-27 16:41:47 +0100162 httpBackend.expectGET('/hpcapi/contentproviders/1/?no_hyperlinks=1').respond(CPmock.CPlist[0]);
163 // httpBackend.expectGET('/hpcapi/cdnprefixs/?no_hyperlinks=1&contentProvider=1').respond([CPmock.CDNlist[0]]);
164 httpBackend.expectGET('/hpcapi/cdnprefixs/?no_hyperlinks=1').respond(CPmock.CDNlist);
165 httpBackend.whenPOST('/hpcapi/cdnprefixs/?no_hyperlinks=1').respond(CPmock.CDNlist[0]);
166 httpBackend.whenDELETE('/hpcapi/cdnprefixs/5/?no_hyperlinks=1').respond();
Matteo Scandolobb3c0132015-10-26 16:17:48 +0100167 scope.$digest();
168 httpBackend.flush();
169 isolatedScope = element.isolateScope().vm;
170 }));
171
172 it('should load associated CDN prefix', () => {
173 expect(isolatedScope.cp_prf.length).toBe(1);
174 expect(isolatedScope.prf.length).toBe(2);
175 });
176
177 it('should add a CDN Prefix', () => {
178 isolatedScope.addPrefix({prefix: 'test.io', defaultOriginServer: '/hpcapi/originservers/2/'});
179 httpBackend.flush();
180 expect(isolatedScope.cp_prf.length).toBe(2);
181 });
182
183 it('should remove a CDN Prefix', () => {
184 isolatedScope.removePrefix(isolatedScope.cp_prf[0]);
185 httpBackend.flush();
186 expect(isolatedScope.cp_prf.length).toBe(0);
187 });
188 });
Matteo Scandoloaa6ea282015-10-26 16:27:50 +0100189
190 describe('the contentProviderServer directive', () => {
191 beforeEach(inject(($compile, $rootScope) => {
192 scope = $rootScope.$new();
193 element = angular.element('<content-provider-server></content-provider-server>');
194 $compile(element)(scope);
Matteo Scandolo8a31ee22015-10-27 16:41:47 +0100195 httpBackend.expectGET('/hpcapi/contentproviders/1/?no_hyperlinks=1').respond(CPmock.CPlist[0]);
196 httpBackend.expectGET('/hpcapi/originservers/?no_hyperlinks=1&contentProvider=1').respond(CPmock.OSlist);
197 httpBackend.whenPOST('/hpcapi/originservers/?no_hyperlinks=1').respond(CPmock.OSlist[0]);
198 httpBackend.whenDELETE('/hpcapi/originservers/8/?no_hyperlinks=1').respond();
Matteo Scandoloaa6ea282015-10-26 16:27:50 +0100199 scope.$digest();
200 httpBackend.flush();
201 isolatedScope = element.isolateScope().vm;
202 }));
203
204 it('should load associated OriginServer', () => {
205 expect(isolatedScope.cp_os.length).toBe(4);
206 });
207
208 it('should add a OriginServer', () => {
209 isolatedScope.addOrigin({protocol: 'http', url: 'test.io'});
210 httpBackend.flush();
211 expect(isolatedScope.cp_os.length).toBe(5);
212 });
213
214 it('should remove a OriginServer', () => {
215 isolatedScope.removeOrigin(isolatedScope.cp_os[0]);
216 httpBackend.flush();
217 expect(isolatedScope.cp_os.length).toBe(3);
218 });
219 });
Matteo Scandolo8a31ee22015-10-27 16:41:47 +0100220
221 describe('the contentProviderUsers directive', () => {
222 beforeEach(inject(($compile, $rootScope) => {
223 scope = $rootScope.$new();
224 element = angular.element('<content-provider-users></content-provider-users>');
225 $compile(element)(scope);
226 httpBackend.expectGET('/xos/users/?no_hyperlinks=1').respond(CPmock.UserList);
227 httpBackend.expectGET('/hpcapi/contentproviders/1/?no_hyperlinks=1').respond(CPmock.CPlist[0]);
228 httpBackend.whenPUT('/hpcapi/contentproviders/1/?no_hyperlinks=1').respond(CPmock.CPlist[0]);
229 scope.$digest();
230 httpBackend.flush();
231 isolatedScope = element.isolateScope().vm;
232 }));
233
234 it('should render one user', () => {
235 expect(isolatedScope.cp.users.length).toBe(1);
236 expect(typeof isolatedScope.cp.users[0]).toEqual('object');
237 });
238
239 it('should add a user', () => {
240 isolatedScope.addUserToCp({name: 'teo'});
241 expect(isolatedScope.cp.users.length).toBe(2);
242 });
243
244 it('should remove a user', () => {
245 isolatedScope.addUserToCp({name: 'teo'});
246 expect(isolatedScope.cp.users.length).toBe(2);
247 isolatedScope.removeUserFromCp({name: 'teo'});
248 expect(isolatedScope.cp.users.length).toBe(1);
249 });
250
251 it('should save and reformat users', () => {
252 // add a user
253 isolatedScope.cp.users.push(1);
254
255 //trigger save
256 isolatedScope.saveContentProvider(isolatedScope.cp);
257
258 httpBackend.flush();
259
260 // I'll get one as the BE is mocked, the important is to check the conversion
261 expect(isolatedScope.cp.users.length).toBe(1);
262 expect(typeof isolatedScope.cp.users[0]).toEqual('object');
263 });
264 });
Matteo Scandolo0968aa92015-10-23 16:31:17 +0200265});