summaryrefslogtreecommitdiff
path: root/openstack_dashboard/static/app/core/openstack-service-api/swift.service.spec.js
blob: 06a64908d8411a90e29953beafc91abf3a732fb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 *    (c) Copyright 2015 Copyright 2015, Rackspace, US, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

(function() {
  'use strict';

  describe('Swift API', function() {
    var testCall, service;
    var apiService = {};
    var toastService = {};

    beforeEach(
      module('horizon.mock.openstack-service-api',
        function($provide, initServices) {
          testCall = initServices($provide, apiService, toastService);
        })
    );

    beforeEach(module('horizon.app.core.openstack-service-api'));

    beforeEach(inject(['horizon.app.core.openstack-service-api.swift', function(swiftAPI) {
      service = swiftAPI;
    }]));

    it('defines the service', function() {
      expect(service).toBeDefined();
    });

    var tests = [
      {
        func: "getInfo",
        method: "get",
        path: "/api/swift/info/",
        error: "Unable to get the Swift service info."
      },
      {
        func: 'getContainers',
        method: 'get',
        path: '/api/swift/containers/',
        error: 'Unable to get the Swift container listing.',
        data: {}
      },
      {
        func: 'getContainer',
        method: 'get',
        path: '/api/swift/containers/spam/metadata/',
        error: 'Unable to get the container details.',
        testInput: [ 'spam' ]
      },
      {
        func: 'getPolicyDetails',
        method: 'get',
        path: '/api/swift/policies/',
        error: 'Unable to fetch the policy details.'
      },
      {
        func: 'createContainer',
        method: 'post',
        path: '/api/swift/containers/new-spam/metadata/',
        data: {is_public: false, storage_policy: 'nz--o1--mr-r3'},
        error: 'Unable to create the container.',
        testInput: [ 'new-spam', false, 'nz--o1--mr-r3' ]
      },
      {
        func: 'createContainer',
        method: 'post',
        path: '/api/swift/containers/new-spam/metadata/',
        data: {is_public: false, storage_policy: 'nz--o1--mr-r3'},
        error: 'Unable to create the container.',
        testInput: [ 'new-spam', false, 'nz--o1--mr-r3' ]
      },
      {
        func: 'createContainer',
        method: 'post',
        path: '/api/swift/containers/new-spam/metadata/',
        data: {is_public: true, storage_policy: 'nz--o1--mr-r3'},
        error: 'Unable to create the container.',
        testInput: [ 'new-spam', true, 'nz--o1--mr-r3' ]
      },
      {
        func: 'deleteContainer',
        method: 'delete',
        path: '/api/swift/containers/spam/metadata/',
        error: 'Unable to delete the container.',
        testInput: [ 'spam' ]
      },
      {
        func: 'setContainerAccess',
        method: 'put',
        data: {is_public: false},
        path: '/api/swift/containers/spam/metadata/',
        error: 'Unable to change the container access.',
        testInput: [ 'spam', false ]
      },
      {
        func: 'getObjects',
        method: 'get',
        data: {},
        path: '/api/swift/containers/spam/objects/',
        error: 'Unable to get the objects in container.',
        testInput: [ 'spam' ]
      },
      {
        func: 'getObjects',
        method: 'get',
        data: {params: {path: '/foo/bar'}},
        path: '/api/swift/containers/spam/objects/',
        error: 'Unable to get the objects in container.',
        testInput: [ 'spam', {path: '/foo/bar'} ]
      },
      {
        func: 'uploadObject',
        method: 'post',
        call_args: [
          '/api/swift/containers/spam/object/ham',
          {file: 'some junk'}
        ],
        error: 'Unable to upload the object.',
        testInput: [ 'spam', 'ham', 'some junk' ]
      },
      {
        func: 'deleteObject',
        method: 'delete',
        path: '/api/swift/containers/spam/object/ham',
        error: 'Unable to delete the object.',
        testInput: [ 'spam', 'ham' ]
      },
      {
        func: 'getObjectDetails',
        method: 'get',
        path: '/api/swift/containers/spam/metadata/ham',
        error: 'Unable to get details of the object.',
        testInput: [ 'spam', 'ham' ]
      },
      {
        func: 'createFolder',
        method: 'post',
        call_args: ['/api/swift/containers/spam/object/ham/', {}],
        error: 'Unable to create the folder.',
        testInput: [ 'spam', 'ham' ]
      },
      {
        func: 'copyObject',
        method: 'post',
        call_args: [
          '/api/swift/containers/spam/copy/ham',
          {dest_container: 'eggs', dest_name: 'bacon'}
        ],
        error: 'Unable to copy the object.',
        testInput: [ 'spam', 'ham', 'eggs', 'bacon' ]
      },
      {
        func: 'copyObject',
        method: 'post',
        call_args: [
          '/api/swift/containers/spam/copy/ham',
          {dest_container: 'eggs', dest_name: 'bacon'}
        ],
        error: 'Unable to copy the object.',
        testInput: [ 'spam', 'ham', 'eggs', 'bacon' ]
      }
    ];

    // Iterate through the defined tests and apply as Jasmine specs.
    angular.forEach(tests, function(params) {
      it('defines the ' + params.func + ' call properly', function test() {
        var callParams = [apiService, service, toastService, params];
        testCall.apply(this, callParams);
      });
    });

    it('returns a relevant error message when createFolder returns a 409 error', function test() {
      var promise = {catch: angular.noop};
      spyOn(apiService, 'post').and.returnValue(promise);
      spyOn(promise, 'catch');
      service.createFolder('spam', 'ham');
      spyOn(toastService, 'add');
      var innerFunc = promise.catch.calls.argsFor(0)[0];
      // In the case of 409
      var message = 'A pseudo-folder with the name "ham" already exists.';
      innerFunc({data: message, status: 409});
      expect(toastService.add).toHaveBeenCalledWith('error', message);
    });

    it('returns a relevant error message when deleteContainer returns a 409 error',
      function test() {
        var promise = {catch: angular.noop};
        spyOn(apiService, 'delete').and.returnValue(promise);
        spyOn(promise, 'catch');
        service.deleteContainer('spam', 'ham');
        spyOn(toastService, 'add');
        var innerFunc = promise.catch.calls.argsFor(0)[0];
        // In the case of 409
        var message = 'Unable to delete the container because it is not empty.';
        innerFunc({data: message, status: 409});
        expect(toastService.add).toHaveBeenCalledWith('error', message);
      }
    );

    it('returns a relevant error message when deleteObject returns a 409 error', function test() {
      var promise = {catch: angular.noop};
      spyOn(apiService, 'delete').and.returnValue(promise);
      spyOn(promise, 'catch');
      service.deleteObject('spam', 'ham');

      expect(apiService.delete).toHaveBeenCalledWith('/api/swift/containers/spam/object/ham');

      var innerFunc = promise.catch.calls.argsFor(0)[0];
      expect(innerFunc).toBeDefined();
      spyOn(toastService, 'add');
      innerFunc({status: 409});
      expect(toastService.add).toHaveBeenCalledWith(
        'error',
        'Unable to delete the folder because it is not empty.'
      );
    });

    it('returns a relevant error message when copyObject returns a 409 error', function test() {
      var promise = {catch: angular.noop};
      spyOn(apiService, 'post').and.returnValue(promise);
      spyOn(promise, 'catch');
      service.copyObject('spam', 'ham', 'eggs', 'bacon');
      spyOn(toastService, 'add');
      var innerFunc = promise.catch.calls.argsFor(0)[0];
      // In the case of 409
      var message = 'Some error message';
      innerFunc({data: message, status: 409});
      expect(toastService.add).toHaveBeenCalledWith('error', message);
    });

    it('getContainer suppresses errors when asked', function test() {
      var promise = {catch: angular.noop};
      spyOn(apiService, 'get').and.returnValue(promise);
      spyOn(promise, 'catch');
      spyOn(toastService, 'add');
      service.getContainer('spam', true);
      expect(toastService.add).not.toHaveBeenCalled();
    });

    it('getObjectDetails suppresses errors when asked', function test() {
      var promise = {catch: angular.noop};
      spyOn(apiService, 'get').and.returnValue(promise);
      spyOn(promise, 'catch');
      spyOn(toastService, 'add');
      service.getObjectDetails('spam', 'ham', true);
      expect(toastService.add).not.toHaveBeenCalled();
    });

    it('constructs container URLs', function test() {
      expect(service.getContainerURL('spam')).toEqual('/api/swift/containers/spam');
    });

    it('constructs container URLs with reserved characters', function test() {
      expect(service.getContainerURL('sp#m')).toEqual(
        '/api/swift/containers/sp%23m'
      );
    });

    it('constructs object URLs', function test() {
      expect(service.getObjectURL('spam', 'ham')).toEqual(
        '/api/swift/containers/spam/object/ham'
      );
    });

    it('constructs object URLs without munging any slashes', function test() {
      expect(service.getObjectURL('spam', 'ham/sam/i/am')).toEqual(
        '/api/swift/containers/spam/object/ham/sam/i/am'
      );
    });

    it('constructs object URLs for different functions', function test() {
      expect(service.getObjectURL('spam', 'ham', 'blah')).toEqual(
        '/api/swift/containers/spam/blah/ham'
      );
    });

    it('constructs object URLs with reserved characters', function test() {
      expect(service.getObjectURL('sp#m', 'ham/f#o')).toEqual(
        '/api/swift/containers/sp%23m/object/ham/f%23o'
      );
    });
  });

})();