forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroleBindingsSpec.js
218 lines (204 loc) · 7.49 KB
/
roleBindingsSpec.js
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
'use strict';
describe('RoleBindingsService', function() {
var RoleBindingsService;
var $rootScope;
beforeEach(module('openshiftConsole', function($provide) {
// Mocking DataService to eliminate $http & the need to use $httpBackend to mock requests
var DataServiceMock = {
create: function(resource, name, object) {
return { then: function(fn) { fn(object);}};
},
update: function(resource, name, object) {
return { then: function(fn) { fn(object);}};
},
// delete may not need to return anything
delete: function(resource, name, object) {
return { then: function(fn) { fn(object);}};
}
};
$provide.service('DataService', function() {
return DataServiceMock;
});
}));
beforeEach(inject(function(_$rootScope_, _RoleBindingsService_) {
RoleBindingsService = _RoleBindingsService_;
$rootScope = _$rootScope_;
}));
describe('#addSubject', function() {
it('should add the subject to the rolebinding subject list', function() {
RoleBindingsService
.addSubject({subjects: []}, {name: 'jane', kind: 'user'})
.then(function(resp) {
expect(resp.subjects).toEqual([{ name: 'jane', kind: 'user'}]);
});
});
it('should provide defaults for a rolebinding object', function() {
RoleBindingsService
.addSubject({
metadata: { name: 'admin'},
subjects: []
}, {name: 'jane', kind: 'user'})
.then(function(resp) {
expect(resp.kind).toEqual('RoleBinding');
expect(resp.apiVersion).toEqual('v1');
expect(_.keys(resp.roleRef)).toEqual(['name', 'namespace']);
});
});
it('should explicitly null userNames and groupNames', function() {
RoleBindingsService
.addSubject({
metadata: { name: 'admin'},
subjects: []
}, {name: 'jane', kind: 'user'})
.then(function(resp) {
expect(resp.userNames).toEqual(null);
expect(resp.groupNames).toEqual(null);
});
});
it('should return the binding if no new subject provided', function() {
// TODO: the early rejection should still be a thenable object!
var binding = RoleBindingsService
.addSubject({
metadata: { name: 'admin'},
subjects: []
}, null);
expect(binding.subjects).toEqual([]);
expect(binding.kind).toEqual('RoleBinding');
expect(binding.apiVersion).toEqual('v1');
});
// TODO: this logic is currently in the controller,
// but it prob should be pushed up into the service
// it('should not add a subject if the subject is already present', function() {
// RoleBindingsService
// .addSubject({
// metadata: { name: 'admin'},
// subjects: [{name: 'jane', kind: 'user'}]
// }, {name: 'jane', kind: 'user'})
// .then(function(resp) {
// expect(resp.subjects).toEqual([{name: 'jane', kind: 'user'}]);
// });
// });
});
describe('#removeSubject', function() {
it('should update the rolebinding by removing the subject from the subject list', function() {
var bindings = [{
roleRef: { name: 'admin'},
subjects: [
{name: 'jane', kind: 'user'},
{name: 'jack', kind: 'user'}
]
}];
RoleBindingsService
.removeSubject('jane', 'admin', null, bindings)
.then(function(resp) {
expect(resp[0].subjects).toEqual([{
name: 'jack',
kind: 'user'
}]);
});
// resolve $q.all() via digest loop
$rootScope.$digest();
});
it('should honor the namespace of a subject if provided', function() {
var bindings = [{
roleRef: { name: 'admin'},
subjects: [
// multiple jim, only one should be removed in test 1
{name: 'jim', kind: 'user'},
{name: 'jim', kind: 'user', namespace: 'yourproject'},
{name: 'jim', kind: 'user', namespace: 'myproject'},
{name: 'jack', kind: 'user'}
]
}, {
roleRef: { name: 'view'},
subjects: [
// multiple jane, only one should be removed in test 2
{name: 'jane', kind: 'user', namespace: 'myproject'},
{name: 'jack', kind: 'user', namespace: 'yourproject'},
{name: 'jane', kind: 'user', namespace: 'herproject'},
{name: 'jack', kind: 'user', namespace: 'hisproject'}
]
}];
// test 1
RoleBindingsService
.removeSubject('jim', 'admin', 'myproject', bindings)
.then(function(resp) {
expect(resp[0].subjects).toEqual([
{name: 'jim', kind: 'user'},
{name: 'jim', kind: 'user', namespace: 'yourproject'},
{name: 'jack', kind: 'user'}
]);
});
// test 2
RoleBindingsService
.removeSubject('jane', 'view', 'herproject', bindings)
.then(function(resp) {
expect(resp[0].subjects).toEqual([
{name: 'jane', kind: 'user', namespace: 'myproject'},
{name: 'jack', kind: 'user', namespace: 'yourproject'},
{name: 'jack', kind: 'user', namespace: 'hisproject'}
]);
});
// resolve $q.all() via digest loop
$rootScope.$digest();
});
it('should delete the rolebinding if the removed subject was the only subject', function() {
var bindings = [{
roleRef: { name: 'admin'},
subjects: [{name: 'jane', kind: 'user'}]
}];
RoleBindingsService
.removeSubject('jane', 'admin', null, bindings)
.then(function(resp) {
expect(resp[0]).toBe(undefined);
});
$rootScope.$digest();
});
it('should remove the subject from all rolebindings for a particular roleRef', function() {
var bindings = [{
metadata: {name: 'admin01'},
roleRef: {name: 'admin'},
subjects: [{name: 'jane', kind: 'user'}, {name: 'jack', kind: 'user'}]
},{
metadata: {name: 'admin02'},
roleRef: {name: 'admin'},
subjects: [{name: 'jane', kind: 'user'}, {name: 'jack', kind: 'user'}]
},{
metadata: {name: 'view'},
roleRef: {name: 'view'},
subjects: [{name: 'jane', kind: 'user'}, {name: 'jack', kind: 'user'}]
}];
RoleBindingsService
.removeSubject('jane', 'admin', null, bindings)
.then(function(resp) {
_.each(resp, function(roleBinding) {
expect(roleBinding.subjects).toEqual([ {name: 'jack', kind: 'user'}]);
});
});
$rootScope.$digest();
});
it('should delete multiple rolebindings from a list of rolebindings if subjects for any rolebinding are empty', function() {
var bindings = [{
metadata: {name: 'admin01'},
roleRef: {name: 'admin'},
subjects: [{name: 'jane', kind: 'user'}]
},{
metadata: {name: 'admin02'},
roleRef: {name: 'admin'},
subjects: [{name: 'jane', kind: 'user'}, {name: 'jack', kind: 'user'}]
},{
metadata: {name: 'admin03'},
roleRef: {name: 'admin'},
subjects: [{name: 'jane', kind: 'user'}]
}];
RoleBindingsService
.removeSubject('jane', 'admin', null, bindings)
.then(function(resp) {
expect(resp[0]).toBe(undefined);
expect(resp[1].subjects).toEqual([ {name: 'jack', kind: 'user'}]);
expect(resp[2]).toBe(undefined);
});
$rootScope.$digest();
});
});
});