Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -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 | 0968aa9 | 2015-10-23 16:31:17 +0200 | [diff] [blame] | 19 | 'use strict'; |
| 20 | |
| 21 | describe('The Content Provider SPA', () => { |
| 22 | |
Matteo Scandolo | 0314b34 | 2015-10-28 10:39:59 +0100 | [diff] [blame] | 23 | var scope, element, isolatedScope, httpBackend, mockLocation, httpProvider; |
| 24 | |
| 25 | var token = 'fakeToken'; |
Matteo Scandolo | 0968aa9 | 2015-10-23 16:31:17 +0200 | [diff] [blame] | 26 | |
| 27 | // injecting main module |
Matteo Scandolo | ad95de4 | 2015-11-04 16:55:21 +0100 | [diff] [blame] | 28 | beforeEach(module('xos.contentProvider')); |
| 29 | |
| 30 | beforeEach(module('templates')); |
Matteo Scandolo | 0968aa9 | 2015-10-23 16:31:17 +0200 | [diff] [blame] | 31 | |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 32 | beforeEach(function(){ |
Matteo Scandolo | 0314b34 | 2015-10-28 10:39:59 +0100 | [diff] [blame] | 33 | module(function($provide, $httpProvider){ |
| 34 | |
| 35 | httpProvider = $httpProvider; |
| 36 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 37 | // mocking stateParams to pass 1 as id |
| 38 | $provide.provider('$stateParams', function(){ |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 39 | /* eslint-disable no-invalid-this*/ |
| 40 | this.$get = function(){ |
Matteo Scandolo | 0fe4c3e | 2015-10-23 18:37:05 +0200 | [diff] [blame] | 41 | return {id: 1}; |
| 42 | }; |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 43 | /* eslint-enable no-invalid-this*/ |
Matteo Scandolo | 0fe4c3e | 2015-10-23 18:37:05 +0200 | [diff] [blame] | 44 | }); |
Matteo Scandolo | 0314b34 | 2015-10-28 10:39:59 +0100 | [diff] [blame] | 45 | |
| 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 Scandolo | 0fe4c3e | 2015-10-23 18:37:05 +0200 | [diff] [blame] | 54 | }); |
| 55 | }); |
| 56 | |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 57 | beforeEach(inject(function(_$location_, $httpBackend){ |
Matteo Scandolo | ba3b12a | 2015-10-23 17:45:45 +0200 | [diff] [blame] | 58 | spyOn(_$location_, 'url'); |
| 59 | mockLocation = _$location_; |
| 60 | httpBackend = $httpBackend; |
| 61 | // Setting up mock request |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 62 | $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 Scandolo | ba3b12a | 2015-10-23 17:45:45 +0200 | [diff] [blame] | 65 | })); |
| 66 | |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 67 | xit('should set the $http interceptor', () => { |
Matteo Scandolo | 0314b34 | 2015-10-28 10:39:59 +0100 | [diff] [blame] | 68 | expect(httpProvider.interceptors).toContain('SetCSRFToken'); |
| 69 | }); |
| 70 | |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 71 | xit('should add no_hyperlink param', inject(($http, $httpBackend) => { |
Matteo Scandolo | 0314b34 | 2015-10-28 10:39:59 +0100 | [diff] [blame] | 72 | $http.get('www.example.com'); |
| 73 | $httpBackend.expectGET('www.example.com?no_hyperlinks=1').respond(200); |
| 74 | $httpBackend.flush(); |
| 75 | })); |
| 76 | |
Matteo Scandolo | 54bc5f7 | 2016-05-18 14:06:45 -0700 | [diff] [blame] | 77 | xit('should set token in the headers', inject(($http, $httpBackend) => { |
Matteo Scandolo | 0314b34 | 2015-10-28 10:39:59 +0100 | [diff] [blame] | 78 | $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 Scandolo | ba3b12a | 2015-10-23 17:45:45 +0200 | [diff] [blame] | 86 | describe('the action directive', () => { |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 87 | beforeEach(inject(function($compile, $rootScope){ |
Matteo Scandolo | ba3b12a | 2015-10-23 17:45:45 +0200 | [diff] [blame] | 88 | 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 Scandolo | 0968aa9 | 2015-10-23 16:31:17 +0200 | [diff] [blame] | 103 | describe('the contentProvider list', () => { |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 104 | beforeEach(inject(function($compile, $rootScope){ |
Matteo Scandolo | 0968aa9 | 2015-10-23 16:31:17 +0200 | [diff] [blame] | 105 | scope = $rootScope.$new(); |
| 106 | |
| 107 | element = angular.element('<content-provider-list></content-provider-list>'); |
| 108 | $compile(element)(scope); |
| 109 | scope.$digest(); |
Matteo Scandolo | ba3b12a | 2015-10-23 17:45:45 +0200 | [diff] [blame] | 110 | httpBackend.flush(); |
| 111 | isolatedScope = element.isolateScope().vm; |
Matteo Scandolo | 0968aa9 | 2015-10-23 16:31:17 +0200 | [diff] [blame] | 112 | })); |
| 113 | |
| 114 | |
Matteo Scandolo | ba3b12a | 2015-10-23 17:45:45 +0200 | [diff] [blame] | 115 | 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 Scandolo | 0968aa9 | 2015-10-23 16:31:17 +0200 | [diff] [blame] | 123 | }); |
| 124 | }); |
Matteo Scandolo | 0fe4c3e | 2015-10-23 18:37:05 +0200 | [diff] [blame] | 125 | |
| 126 | describe('the contentProviderDetail directive', () => { |
Matteo Scandolo | bb3c013 | 2015-10-26 16:17:48 +0100 | [diff] [blame] | 127 | |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 128 | beforeEach(inject(function($compile, $rootScope){ |
Matteo Scandolo | bb3c013 | 2015-10-26 16:17:48 +0100 | [diff] [blame] | 129 | scope = $rootScope.$new(); |
| 130 | element = angular.element('<content-provider-detail></content-provider-detail>'); |
| 131 | $compile(element)(scope); |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 132 | httpBackend.expectGET('/hpcapi/contentproviders/1/?no_hyperlinks=1').respond(CPmock.CPlist[0]); |
Matteo Scandolo | bb3c013 | 2015-10-26 16:17:48 +0100 | [diff] [blame] | 133 | scope.$digest(); |
| 134 | httpBackend.flush(); |
| 135 | isolatedScope = element.isolateScope().vm; |
| 136 | })); |
| 137 | |
Matteo Scandolo | 0fe4c3e | 2015-10-23 18:37:05 +0200 | [diff] [blame] | 138 | describe('when an id is set in the route', () => { |
| 139 | |
Matteo Scandolo | bb3c013 | 2015-10-26 16:17:48 +0100 | [diff] [blame] | 140 | beforeEach(() => { |
| 141 | // spy the instance update method |
| 142 | spyOn(isolatedScope.cp, '$update').and.callThrough(); |
| 143 | }); |
Matteo Scandolo | 0fe4c3e | 2015-10-23 18:37:05 +0200 | [diff] [blame] | 144 | |
| 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 Scandolo | bb3c013 | 2015-10-26 16:17:48 +0100 | [diff] [blame] | 152 | expect(isolatedScope.cp.$update).toHaveBeenCalled(); |
Matteo Scandolo | 0fe4c3e | 2015-10-23 18:37:05 +0200 | [diff] [blame] | 153 | }); |
| 154 | }); |
| 155 | }); |
Matteo Scandolo | bb3c013 | 2015-10-26 16:17:48 +0100 | [diff] [blame] | 156 | |
| 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 Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 162 | 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 Scandolo | bb3c013 | 2015-10-26 16:17:48 +0100 | [diff] [blame] | 167 | 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 Scandolo | aa6ea28 | 2015-10-26 16:27:50 +0100 | [diff] [blame] | 189 | |
| 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 Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 195 | 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 Scandolo | aa6ea28 | 2015-10-26 16:27:50 +0100 | [diff] [blame] | 199 | 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 Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 220 | |
| 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 Scandolo | 0968aa9 | 2015-10-23 16:31:17 +0200 | [diff] [blame] | 265 | }); |