Matteo Scandolo | 0968aa9 | 2015-10-23 16:31:17 +0200 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | describe('The Content Provider SPA', () => { |
| 4 | |
Matteo Scandolo | 0314b34 | 2015-10-28 10:39:59 +0100 | [diff] [blame] | 5 | var scope, element, isolatedScope, httpBackend, mockLocation, httpProvider; |
| 6 | |
| 7 | var token = 'fakeToken'; |
Matteo Scandolo | 0968aa9 | 2015-10-23 16:31:17 +0200 | [diff] [blame] | 8 | |
| 9 | // injecting main module |
Matteo Scandolo | ad95de4 | 2015-11-04 16:55:21 +0100 | [diff] [blame] | 10 | beforeEach(module('xos.contentProvider')); |
| 11 | |
| 12 | beforeEach(module('templates')); |
Matteo Scandolo | 0968aa9 | 2015-10-23 16:31:17 +0200 | [diff] [blame] | 13 | |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 14 | beforeEach(function(){ |
Matteo Scandolo | 0314b34 | 2015-10-28 10:39:59 +0100 | [diff] [blame] | 15 | module(function($provide, $httpProvider){ |
| 16 | |
| 17 | httpProvider = $httpProvider; |
| 18 | |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 19 | // mocking stateParams to pass 1 as id |
| 20 | $provide.provider('$stateParams', function(){ |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 21 | /* eslint-disable no-invalid-this*/ |
| 22 | this.$get = function(){ |
Matteo Scandolo | 0fe4c3e | 2015-10-23 18:37:05 +0200 | [diff] [blame] | 23 | return {id: 1}; |
| 24 | }; |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 25 | /* eslint-enable no-invalid-this*/ |
Matteo Scandolo | 0fe4c3e | 2015-10-23 18:37:05 +0200 | [diff] [blame] | 26 | }); |
Matteo Scandolo | 0314b34 | 2015-10-28 10:39:59 +0100 | [diff] [blame] | 27 | |
| 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 Scandolo | 0fe4c3e | 2015-10-23 18:37:05 +0200 | [diff] [blame] | 36 | }); |
| 37 | }); |
| 38 | |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 39 | beforeEach(inject(function(_$location_, $httpBackend){ |
Matteo Scandolo | ba3b12a | 2015-10-23 17:45:45 +0200 | [diff] [blame] | 40 | spyOn(_$location_, 'url'); |
| 41 | mockLocation = _$location_; |
| 42 | httpBackend = $httpBackend; |
| 43 | // Setting up mock request |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 44 | $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 Scandolo | ba3b12a | 2015-10-23 17:45:45 +0200 | [diff] [blame] | 47 | })); |
| 48 | |
Matteo Scandolo | 0314b34 | 2015-10-28 10:39:59 +0100 | [diff] [blame] | 49 | 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 Scandolo | ba3b12a | 2015-10-23 17:45:45 +0200 | [diff] [blame] | 68 | describe('the action directive', () => { |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 69 | beforeEach(inject(function($compile, $rootScope){ |
Matteo Scandolo | ba3b12a | 2015-10-23 17:45:45 +0200 | [diff] [blame] | 70 | 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 Scandolo | 0968aa9 | 2015-10-23 16:31:17 +0200 | [diff] [blame] | 85 | describe('the contentProvider list', () => { |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 86 | beforeEach(inject(function($compile, $rootScope){ |
Matteo Scandolo | 0968aa9 | 2015-10-23 16:31:17 +0200 | [diff] [blame] | 87 | scope = $rootScope.$new(); |
| 88 | |
| 89 | element = angular.element('<content-provider-list></content-provider-list>'); |
| 90 | $compile(element)(scope); |
| 91 | scope.$digest(); |
Matteo Scandolo | ba3b12a | 2015-10-23 17:45:45 +0200 | [diff] [blame] | 92 | httpBackend.flush(); |
| 93 | isolatedScope = element.isolateScope().vm; |
Matteo Scandolo | 0968aa9 | 2015-10-23 16:31:17 +0200 | [diff] [blame] | 94 | })); |
| 95 | |
| 96 | |
Matteo Scandolo | ba3b12a | 2015-10-23 17:45:45 +0200 | [diff] [blame] | 97 | 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 Scandolo | 0968aa9 | 2015-10-23 16:31:17 +0200 | [diff] [blame] | 105 | }); |
| 106 | }); |
Matteo Scandolo | 0fe4c3e | 2015-10-23 18:37:05 +0200 | [diff] [blame] | 107 | |
| 108 | describe('the contentProviderDetail directive', () => { |
Matteo Scandolo | bb3c013 | 2015-10-26 16:17:48 +0100 | [diff] [blame] | 109 | |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 110 | beforeEach(inject(function($compile, $rootScope){ |
Matteo Scandolo | bb3c013 | 2015-10-26 16:17:48 +0100 | [diff] [blame] | 111 | scope = $rootScope.$new(); |
| 112 | element = angular.element('<content-provider-detail></content-provider-detail>'); |
| 113 | $compile(element)(scope); |
Matteo Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 114 | httpBackend.expectGET('/hpcapi/contentproviders/1/?no_hyperlinks=1').respond(CPmock.CPlist[0]); |
Matteo Scandolo | bb3c013 | 2015-10-26 16:17:48 +0100 | [diff] [blame] | 115 | scope.$digest(); |
| 116 | httpBackend.flush(); |
| 117 | isolatedScope = element.isolateScope().vm; |
| 118 | })); |
| 119 | |
Matteo Scandolo | 0fe4c3e | 2015-10-23 18:37:05 +0200 | [diff] [blame] | 120 | describe('when an id is set in the route', () => { |
| 121 | |
Matteo Scandolo | bb3c013 | 2015-10-26 16:17:48 +0100 | [diff] [blame] | 122 | beforeEach(() => { |
| 123 | // spy the instance update method |
| 124 | spyOn(isolatedScope.cp, '$update').and.callThrough(); |
| 125 | }); |
Matteo Scandolo | 0fe4c3e | 2015-10-23 18:37:05 +0200 | [diff] [blame] | 126 | |
| 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 Scandolo | bb3c013 | 2015-10-26 16:17:48 +0100 | [diff] [blame] | 134 | expect(isolatedScope.cp.$update).toHaveBeenCalled(); |
Matteo Scandolo | 0fe4c3e | 2015-10-23 18:37:05 +0200 | [diff] [blame] | 135 | }); |
| 136 | }); |
| 137 | }); |
Matteo Scandolo | bb3c013 | 2015-10-26 16:17:48 +0100 | [diff] [blame] | 138 | |
| 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 Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 144 | 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 Scandolo | bb3c013 | 2015-10-26 16:17:48 +0100 | [diff] [blame] | 149 | 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 Scandolo | aa6ea28 | 2015-10-26 16:27:50 +0100 | [diff] [blame] | 171 | |
| 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 Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 177 | 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 Scandolo | aa6ea28 | 2015-10-26 16:27:50 +0100 | [diff] [blame] | 181 | 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 Scandolo | 8a31ee2 | 2015-10-27 16:41:47 +0100 | [diff] [blame] | 202 | |
| 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 Scandolo | 0968aa9 | 2015-10-23 16:31:17 +0200 | [diff] [blame] | 247 | }); |