forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature_spec.rb
More file actions
230 lines (199 loc) · 9.43 KB
/
Copy pathfeature_spec.rb
File metadata and controls
230 lines (199 loc) · 9.43 KB
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
#
# Copyright (C) 2013 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
describe Feature do
let(:t_site_admin) { Account.site_admin }
let(:t_root_account) { account_model }
let(:t_sub_account) { account_model parent_account: t_root_account }
let(:t_course) { course_factory account: t_sub_account, active_all: true }
let(:t_user) { user_with_pseudonym account: t_root_account }
before do
User.any_instance.stubs(:set_default_feature_flags)
Feature.stubs(:definitions).returns({
'RA' => Feature.new(feature: 'RA', applies_to: 'RootAccount', state: 'hidden'),
'A' => Feature.new(feature: 'A', applies_to: 'Account', state: 'on'),
'C' => Feature.new(feature: 'C', applies_to: 'Course', state: 'off'),
'U' => Feature.new(feature: 'U', applies_to: 'User', state: 'allowed'),
})
end
describe "applies_to_object" do
it "should work for RootAccount features" do
feature = Feature.definitions['RA']
expect(feature.applies_to_object(t_root_account)).to be_truthy
expect(feature.applies_to_object(t_sub_account)).to be_falsey
expect(feature.applies_to_object(t_course)).to be_falsey
expect(feature.applies_to_object(t_user)).to be_falsey
end
it "should work for Account features" do
feature = Feature.definitions['A']
expect(feature.applies_to_object(t_root_account)).to be_truthy
expect(feature.applies_to_object(t_sub_account)).to be_truthy
expect(feature.applies_to_object(t_course)).to be_falsey
expect(feature.applies_to_object(t_user)).to be_falsey
end
it "should work for Course features" do
feature = Feature.definitions['C']
expect(feature.applies_to_object(t_root_account)).to be_truthy
expect(feature.applies_to_object(t_sub_account)).to be_truthy
expect(feature.applies_to_object(t_course)).to be_truthy
expect(feature.applies_to_object(t_user)).to be_falsey
end
it "should work for User features" do
feature = Feature.definitions['U']
expect(feature.applies_to_object(t_site_admin)).to be_truthy
expect(feature.applies_to_object(t_root_account)).to be_falsey
expect(feature.applies_to_object(t_sub_account)).to be_falsey
expect(feature.applies_to_object(t_course)).to be_falsey
expect(feature.applies_to_object(t_user)).to be_truthy
end
end
describe "applicable_features" do
it "should work for Site Admin" do
expect(Feature.applicable_features(t_site_admin).map(&:feature).sort).to eql %w(A C RA U)
end
it "should work for RootAccounts" do
expect(Feature.applicable_features(t_root_account).map(&:feature).sort).to eql %w(A C RA)
end
it "should work for Accounts" do
expect(Feature.applicable_features(t_sub_account).map(&:feature).sort).to eql %w(A C)
end
it "should work for Courses" do
expect(Feature.applicable_features(t_course).map(&:feature)).to eql %w(C)
end
it "should work for Users" do
expect(Feature.applicable_features(t_user).map(&:feature)).to eql %w(U)
end
end
describe "locked?" do
it "should return true if context is nil" do
expect(Feature.definitions['RA'].locked?(nil)).to be_truthy
expect(Feature.definitions['A'].locked?(nil)).to be_truthy
expect(Feature.definitions['C'].locked?(nil)).to be_truthy
expect(Feature.definitions['U'].locked?(nil)).to be_truthy
end
it "should return true in a lower context if the definition disallows override" do
expect(Feature.definitions['RA'].locked?(t_site_admin)).to be_falsey
expect(Feature.definitions['A'].locked?(t_site_admin)).to be_truthy
expect(Feature.definitions['C'].locked?(t_site_admin)).to be_truthy
expect(Feature.definitions['U'].locked?(t_site_admin)).to be_falsey
end
end
describe "RootAccount feature" do
it "should imply root_opt_in" do
expect(Feature.definitions['RA'].root_opt_in).to be_truthy
end
end
describe "default_transitions" do
it "should enumerate RootAccount transitions" do
fd = Feature.definitions['RA']
expect(fd.default_transitions(t_site_admin, 'allowed')).to eql({'off'=>{'locked'=>false},'on'=>{'locked'=>false}})
expect(fd.default_transitions(t_site_admin, 'on')).to eql({'allowed'=>{'locked'=>false},'off'=>{'locked'=>false}})
expect(fd.default_transitions(t_site_admin, 'off')).to eql({'allowed'=>{'locked'=>false},'on'=>{'locked'=>false}})
expect(fd.default_transitions(t_root_account, 'allowed')).to eql({'off'=>{'locked'=>false},'on'=>{'locked'=>false}})
expect(fd.default_transitions(t_root_account, 'on')).to eql({'allowed'=>{'locked'=>true},'off'=>{'locked'=>false}})
expect(fd.default_transitions(t_root_account, 'off')).to eql({'allowed'=>{'locked'=>true},'on'=>{'locked'=>false}})
end
it "should enumerate Account transitions" do
fd = Feature.definitions['A']
expect(fd.default_transitions(t_root_account, 'allowed')).to eql({'off'=>{'locked'=>false},'on'=>{'locked'=>false}})
expect(fd.default_transitions(t_root_account, 'on')).to eql({'allowed'=>{'locked'=>false},'off'=>{'locked'=>false}})
expect(fd.default_transitions(t_root_account, 'off')).to eql({'allowed'=>{'locked'=>false},'on'=>{'locked'=>false}})
expect(fd.default_transitions(t_sub_account, 'allowed')).to eql({'off'=>{'locked'=>false},'on'=>{'locked'=>false}})
expect(fd.default_transitions(t_sub_account, 'on')).to eql({'allowed'=>{'locked'=>false},'off'=>{'locked'=>false}})
expect(fd.default_transitions(t_sub_account, 'off')).to eql({'allowed'=>{'locked'=>false},'on'=>{'locked'=>false}})
end
it "should enumerate Course transitions" do
fd = Feature.definitions['C']
expect(fd.default_transitions(t_course, 'allowed')).to eql({'off'=>{'locked'=>false},'on'=>{'locked'=>false}})
expect(fd.default_transitions(t_course, 'on')).to eql({'off'=>{'locked'=>false}})
expect(fd.default_transitions(t_course, 'off')).to eql({'on'=>{'locked'=>false}})
end
it "should enumerate User transitions" do
fd = Feature.definitions['U']
expect(fd.default_transitions(t_user, 'allowed')).to eql({'off'=>{'locked'=>false},'on'=>{'locked'=>false}})
expect(fd.default_transitions(t_user, 'on')).to eql({'off'=>{'locked'=>false}})
expect(fd.default_transitions(t_user, 'off')).to eql({'on'=>{'locked'=>false}})
end
end
end
describe "Feature.register" do
before do
# unregister the default features
@old_features = Feature.instance_variable_get(:@features)
Feature.instance_variable_set(:@features, nil)
end
after do
Feature.instance_variable_set(:@features, @old_features)
end
let(:t_feature_hash) do
{
display_name: -> { "some feature or other" },
description: -> { "this does something" },
applies_to: 'RootAccount',
state: 'allowed'
}
end
let(:t_dev_feature_hash) do
t_feature_hash.merge(development: true)
end
it "should register a feature" do
Feature.register({some_feature: t_feature_hash})
expect(Feature.definitions).to be_frozen
expect(Feature.definitions['some_feature'].display_name.call).to eql('some feature or other')
end
describe "development" do
it "should register in a test environment" do
Feature.register({dev_feature: t_dev_feature_hash})
expect(Feature.definitions['dev_feature']).not_to be_nil
end
it "should register in a dev environment" do
Rails.env.stubs(:test?).returns(false)
Rails.env.stubs(:development?).returns(true)
Feature.register({dev_feature: t_dev_feature_hash})
expect(Feature.definitions['dev_feature']).not_to be_nil
end
it "should register in a production test cluster" do
Rails.env.stubs(:test?).returns(false)
Rails.env.stubs(:production?).returns(true)
ApplicationController.stubs(:test_cluster?).returns(true)
Feature.register({dev_feature: t_dev_feature_hash})
expect(Feature.definitions['dev_feature']).not_to be_nil
end
it "should not register in production" do
Rails.env.stubs(:test?).returns(false)
Rails.env.stubs(:production?).returns(true)
Feature.register({dev_feature: t_dev_feature_hash})
expect(Feature.definitions['dev_feature']).to be_nil
end
end
let(:t_hidden_in_prod_feature_hash) do
t_feature_hash.merge(state: 'hidden_in_prod')
end
describe 'hidden_in_prod' do
it "should register as 'allowed' in a test environment" do
Feature.register({dev_feature: t_hidden_in_prod_feature_hash})
expect(Feature.definitions['dev_feature']).to be_allowed
end
it "should register as 'hidden' in production" do
Rails.env.stubs(:test?).returns(false)
Rails.env.stubs(:production?).returns(true)
Feature.register({dev_feature: t_hidden_in_prod_feature_hash})
expect(Feature.definitions['dev_feature']).to be_hidden
end
end
end