forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount_spec.rb
More file actions
1582 lines (1332 loc) · 61.2 KB
/
Copy pathaccount_spec.rb
File metadata and controls
1582 lines (1332 loc) · 61.2 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright (C) 2011 - present 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__) + '/../sharding_spec_helper.rb')
describe Account do
it "should provide a list of courses" do
expect{ Account.new.courses }.not_to raise_error
end
context "equella_settings" do
it "should respond to :equella_settings" do
expect(Account.new).to respond_to(:equella_settings)
expect(Account.new.equella_settings).to be_nil
end
it "should return the equella_settings data if defined" do
a = Account.new
a.equella_endpoint = "http://oer.equella.com/signon.do"
expect(a.equella_settings).not_to be_nil
expect(a.equella_settings.endpoint).to eql("http://oer.equella.com/signon.do")
expect(a.equella_settings.default_action).not_to be_nil
end
end
# it "should have an atom feed" do
# account_model
# @a.to_atom.should be_is_a(Atom::Entry)
# end
context "course lists" do
before :once do
@account = Account.create!
process_csv_data_cleanly([
"user_id,login_id,first_name,last_name,email,status",
"U001,user1,User,One,user1@example.com,active",
"U002,user2,User,Two,user2@example.com,active",
"U003,user3,User,Three,user3@example.com,active",
"U004,user4,User,Four,user4@example.com,active",
"U005,user5,User,Five,user5@example.com,active",
"U006,user6,User,Six,user6@example.com,active",
"U007,user7,User,Seven,user7@example.com,active",
"U008,user8,User,Eight,user8@example.com,active",
"U009,user9,User,Nine,user9@example.com,active",
"U010,user10,User,Ten,user10@example.com,active",
"U011,user11,User,Eleven,user11@example.com,deleted"
])
process_csv_data_cleanly([
"term_id,name,status,start_date,end_date",
"T001,Term 1,active,,",
"T002,Term 2,active,,",
"T003,Term 3,active,,"
])
process_csv_data_cleanly([
"course_id,short_name,long_name,account_id,term_id,status",
"C001,C001,Test Course 1,,T001,active",
"C002,C002,Test Course 2,,T001,deleted",
"C003,C003,Test Course 3,,T002,deleted",
"C004,C004,Test Course 4,,T002,deleted",
"C005,C005,Test Course 5,,T003,active",
"C006,C006,Test Course 6,,T003,active",
"C007,C007,Test Course 7,,T003,active",
"C008,C008,Test Course 8,,T003,active",
"C009,C009,Test Course 9,,T003,active",
"C001S,C001S,Test search Course 1,,T001,active",
"C002S,C002S,Test search Course 2,,T001,deleted",
"C003S,C003S,Test search Course 3,,T002,deleted",
"C004S,C004S,Test search Course 4,,T002,deleted",
"C005S,C005S,Test search Course 5,,T003,active",
"C006S,C006S,Test search Course 6,,T003,active",
"C007S,C007S,Test search Course 7,,T003,active",
"C008S,C008S,Test search Course 8,,T003,active",
"C009S,C009S,Test search Course 9,,T003,active"
])
process_csv_data_cleanly([
"section_id,course_id,name,start_date,end_date,status",
"S001,C001,Sec1,,,active",
"S002,C002,Sec2,,,active",
"S003,C003,Sec3,,,active",
"S004,C004,Sec4,,,active",
"S005,C005,Sec5,,,active",
"S006,C006,Sec6,,,active",
"S007,C007,Sec7,,,active",
"S008,C001,Sec8,,,active",
"S009,C008,Sec9,,,active",
"S001S,C001S,Sec1,,,active",
"S002S,C002S,Sec2,,,active",
"S003S,C003S,Sec3,,,active",
"S004S,C004S,Sec4,,,active",
"S005S,C005S,Sec5,,,active",
"S006S,C006S,Sec6,,,active",
"S007S,C007S,Sec7,,,active",
"S008S,C001S,Sec8,,,active",
"S009S,C008S,Sec9,,,active"
])
process_csv_data_cleanly([
"course_id,user_id,role,section_id,status,associated_user_id",
",U001,student,S001,active,",
",U005,student,S005,active,",
",U006,student,S006,deleted,",
",U007,student,S007,active,",
",U008,student,S008,active,",
",U009,student,S005,deleted,",
",U001,student,S001S,active,",
",U005,student,S005S,active,",
",U006,student,S006S,deleted,",
",U007,student,S007S,active,",
",U008,student,S008S,active,",
",U009,student,S005S,deleted,"
])
end
context "fast list" do
it "should list associated courses" do
expect(@account.fast_all_courses.map(&:sis_source_id).sort).to eq [
"C001", "C005", "C006", "C007", "C008", "C009",
"C001S", "C005S", "C006S", "C007S", "C008S", "C009S", ].sort
end
it "should list associated courses by term" do
expect(@account.fast_all_courses({:term => EnrollmentTerm.where(sis_source_id: "T001").first}).map(&:sis_source_id).sort).to eq ["C001", "C001S"]
expect(@account.fast_all_courses({:term => EnrollmentTerm.where(sis_source_id: "T002").first}).map(&:sis_source_id).sort).to eq []
expect(@account.fast_all_courses({:term => EnrollmentTerm.where(sis_source_id: "T003").first}).map(&:sis_source_id).sort).to eq ["C005", "C006", "C007", "C008", "C009", "C005S", "C006S", "C007S", "C008S", "C009S"].sort
end
it "should list associated nonenrollmentless courses" do
expect(@account.fast_all_courses({:hide_enrollmentless_courses => true}).map(&:sis_source_id).sort).to eq ["C001", "C005", "C007", "C001S", "C005S", "C007S"].sort #C007 probably shouldn't be here, cause the enrollment section is deleted, but we kinda want to minimize database traffic
end
it "should list associated nonenrollmentless courses by term" do
expect(@account.fast_all_courses({:term => EnrollmentTerm.where(sis_source_id: "T001").first, :hide_enrollmentless_courses => true}).map(&:sis_source_id).sort).to eq ["C001", "C001S"]
expect(@account.fast_all_courses({:term => EnrollmentTerm.where(sis_source_id: "T002").first, :hide_enrollmentless_courses => true}).map(&:sis_source_id).sort).to eq []
expect(@account.fast_all_courses({:term => EnrollmentTerm.where(sis_source_id: "T003").first, :hide_enrollmentless_courses => true}).map(&:sis_source_id).sort).to eq ["C005", "C007", "C005S", "C007S"].sort
end
it "should order list by specified parameter" do
order = "courses.created_at ASC"
@account.expects(:fast_course_base).with(order: order)
@account.fast_all_courses(order: order)
end
end
context "name searching" do
it "should list associated courses" do
expect(@account.courses_name_like("search").map(&:sis_source_id).sort).to eq [
"C001S", "C005S", "C006S", "C007S", "C008S", "C009S"]
end
it "should list associated courses by term" do
expect(@account.courses_name_like("search", {:term => EnrollmentTerm.where(sis_source_id: "T001").first}).map(&:sis_source_id).sort).to eq ["C001S"]
expect(@account.courses_name_like("search", {:term => EnrollmentTerm.where(sis_source_id: "T002").first}).map(&:sis_source_id).sort).to eq []
expect(@account.courses_name_like("search", {:term => EnrollmentTerm.where(sis_source_id: "T003").first}).map(&:sis_source_id).sort).to eq ["C005S", "C006S", "C007S", "C008S", "C009S"]
end
it "should list associated nonenrollmentless courses" do
expect(@account.courses_name_like("search", {:hide_enrollmentless_courses => true}).map(&:sis_source_id).sort).to eq ["C001S", "C005S", "C007S"] #C007 probably shouldn't be here, cause the enrollment section is deleted, but we kinda want to minimize database traffic
end
it "should list associated nonenrollmentless courses by term" do
expect(@account.courses_name_like("search", {:term => EnrollmentTerm.where(sis_source_id: "T001").first, :hide_enrollmentless_courses => true}).map(&:sis_source_id).sort).to eq ["C001S"]
expect(@account.courses_name_like("search", {:term => EnrollmentTerm.where(sis_source_id: "T002").first, :hide_enrollmentless_courses => true}).map(&:sis_source_id).sort).to eq []
expect(@account.courses_name_like("search", {:term => EnrollmentTerm.where(sis_source_id: "T003").first, :hide_enrollmentless_courses => true}).map(&:sis_source_id).sort).to eq ["C005S", "C007S"]
end
end
end
context "services" do
before do
@a = Account.new
end
it "should be able to specify a list of enabled services" do
@a.allowed_services = 'linked_in,twitter'
expect(@a.service_enabled?(:linked_in)).to be_truthy
expect(@a.service_enabled?(:twitter)).to be_truthy
expect(@a.service_enabled?(:diigo)).to be_falsey
expect(@a.service_enabled?(:avatars)).to be_falsey
end
it "should not enable services off by default" do
expect(@a.service_enabled?(:linked_in)).to be_truthy
expect(@a.service_enabled?(:avatars)).to be_falsey
end
it "should add and remove services from the defaults" do
@a.allowed_services = '+avatars,-linked_in'
expect(@a.service_enabled?(:avatars)).to be_truthy
expect(@a.service_enabled?(:twitter)).to be_truthy
expect(@a.service_enabled?(:linked_in)).to be_falsey
end
it "should allow settings services" do
expect {@a.enable_service(:completly_bogs)}.to raise_error("Invalid Service")
@a.disable_service(:twitter)
expect(@a.service_enabled?(:twitter)).to be_falsey
@a.enable_service(:twitter)
expect(@a.service_enabled?(:twitter)).to be_truthy
end
it "should use + and - by default when setting service availabilty" do
@a.enable_service(:twitter)
expect(@a.service_enabled?(:twitter)).to be_truthy
expect(@a.allowed_services).to be_nil
@a.disable_service(:twitter)
expect(@a.allowed_services).to match('\-twitter')
@a.disable_service(:avatars)
expect(@a.service_enabled?(:avatars)).to be_falsey
expect(@a.allowed_services).not_to match('avatars')
@a.enable_service(:avatars)
expect(@a.service_enabled?(:avatars)).to be_truthy
expect(@a.allowed_services).to match('\+avatars')
end
it "should be able to set service availibity for previously hard-coded values" do
@a.allowed_services = 'avatars,linked_in'
@a.enable_service(:twitter)
expect(@a.service_enabled?(:twitter)).to be_truthy
expect(@a.allowed_services).to match(/twitter/)
expect(@a.allowed_services).not_to match(/[+-]/)
@a.disable_service(:linked_in)
expect(@a.allowed_services).not_to match(/linked_in/)
expect(@a.allowed_services).not_to match(/[+-]/)
@a.disable_service(:avatars)
@a.disable_service(:twitter)
expect(@a.allowed_services).to be_nil
end
it "should not wipe out services that are substrings of each other" do
AccountServices.register_service(
:google_docs_prev,
{
:name => "My google docs prev", :description => "", :expose_to_ui => :service, :default => true
}
)
@a.disable_service('google_docs_previews')
@a.disable_service('google_docs_prev')
expect(@a.allowed_services).to eq '-google_docs_previews,-google_docs_prev'
end
describe "services_exposed_to_ui_hash" do
it "should return all ui services by default" do
expected_services = AccountServices.allowable_services.reject { |_, k| !k[:expose_to_ui] || (k[:expose_to_ui_proc] && !k[:expose_to_ui_proc].call(nil)) }.keys
expect(Account.services_exposed_to_ui_hash.keys).to eq expected_services
end
it "should return services of a type if specified" do
expected_services = AccountServices.allowable_services.reject { |_, k| k[:expose_to_ui] != :setting || (k[:expose_to_ui_proc] && !k[:expose_to_ui_proc].call(nil)) }.keys
expect(Account.services_exposed_to_ui_hash(:setting).keys).to eq expected_services
end
it "should filter based on user and account if a proc is specified" do
user1 = User.create!
user2 = User.create!
AccountServices.register_service(:myservice, {
name: "My Test Service",
description: "Nope",
expose_to_ui: :setting,
default: false,
expose_to_ui_proc: proc { |user, account| user == user2 && account == Account.default },
})
expect(Account.services_exposed_to_ui_hash(:setting).keys).not_to be_include(:myservice)
expect(Account.services_exposed_to_ui_hash(:setting, user1, Account.default).keys).not_to be_include(:myservice)
expect(Account.services_exposed_to_ui_hash(:setting, user2, Account.default).keys).to be_include(:myservice)
end
end
describe "plugin services" do
before do
AccountServices.register_service(:myplugin, { :name => "My Plugin", :description => "", :expose_to_ui => :setting, :default => false })
end
it "should return the service" do
expect(AccountServices.allowable_services.keys).to be_include(:myplugin)
end
it "should allow setting the service" do
expect(@a.service_enabled?(:myplugin)).to be_falsey
@a.enable_service(:myplugin)
expect(@a.service_enabled?(:myplugin)).to be_truthy
expect(@a.allowed_services).to match(/\+myplugin/)
@a.disable_service(:myplugin)
expect(@a.service_enabled?(:myplugin)).to be_falsey
expect(@a.allowed_services).to be_blank
end
describe "services_exposed_to_ui_hash" do
it "should return services defined in a plugin" do
expect(Account.services_exposed_to_ui_hash().keys).to be_include(:myplugin)
expect(Account.services_exposed_to_ui_hash(:setting).keys).to be_include(:myplugin)
end
end
end
end
context "settings=" do
it "should filter non-hash hash settings" do
a = Account.new
a.settings = {'sis_default_grade_export' => 'string'}.with_indifferent_access
expect(a.settings[:error_reporting]).to eql(nil)
a.settings = {'sis_default_grade_export' => {
'value' => true
}}.with_indifferent_access
expect(a.settings[:sis_default_grade_export]).to be_is_a(Hash)
expect(a.settings[:sis_default_grade_export][:value]).to eql true
end
end
context "allow_global_includes?" do
let(:root){ Account.default }
it "false unless they've checked the box to allow it" do
expect(root.allow_global_includes?).to be_falsey
end
it "true if they've checked the box to allow it" do
root.settings = {'global_includes' => true}
expect(root.allow_global_includes?).to be_truthy
end
describe "subaccount" do
let(:sub_account){ root.sub_accounts.create! }
it "false if root account hasn't checked global_includes AND subaccount branding" do
expect(sub_account.allow_global_includes?).to be_falsey
sub_account.root_account.settings = {'global_includes' => true, 'sub_account_includes' => false}
expect(sub_account.allow_global_includes?).to be_falsey
sub_account.root_account.settings = {'global_includes' => false, 'sub_account_includes' => true}
expect(sub_account.allow_global_includes?).to be_falsey
end
it "true if root account HAS checked global_includes and turned on subaccount branding" do
sub_account.root_account.settings = {'global_includes' => true, 'sub_account_includes' => true}
expect(sub_account.allow_global_includes?).to be_truthy
end
end
end
context "turnitin secret" do
it "should decrypt the turnitin secret to the original value" do
a = Account.new
a.turnitin_shared_secret = "asdf"
expect(a.turnitin_shared_secret).to eql("asdf")
a.turnitin_shared_secret = "2t87aot72gho8a37gh4g[awg'waegawe-,v-3o7fya23oya2o3"
expect(a.turnitin_shared_secret).to eql("2t87aot72gho8a37gh4g[awg'waegawe-,v-3o7fya23oya2o3")
end
end
context "closest_turnitin_originality" do
before :each do
@root_account = Account.create!(:turnitin_pledge => "root")
@root_account.turnitin_originality = 'after_grading'
@root_account.save!
end
it "should find closest_turnitin_originality from root account" do
expect(@root_account.closest_turnitin_originality).to eq('after_grading')
end
it "should find closest_turnitin_originality from sub account" do
sub_account = Account.create(:name => 'sub', :parent_account => @root_account)
sub_account.turnitin_originality = 'never'
expect(sub_account.closest_turnitin_originality).to eq('never')
end
it "should find closest_turnitin_originality from sub account when set on root account" do
sub_account = Account.create(:name => 'sub', :parent_account => @root_account)
expect(sub_account.closest_turnitin_originality).to eq('after_grading')
end
end
context "closest_turnitin_pledge" do
it "should work for custom sub, custom root" do
root_account = Account.create!(:turnitin_pledge => "root")
sub_account = Account.create!(:parent_account => root_account, :turnitin_pledge => "sub")
expect(root_account.closest_turnitin_pledge).to eq "root"
expect(sub_account.closest_turnitin_pledge).to eq "sub"
end
it "should work for nil sub, custom root" do
root_account = Account.create!(:turnitin_pledge => "root")
sub_account = Account.create!(:parent_account => root_account)
expect(root_account.closest_turnitin_pledge).to eq "root"
expect(sub_account.closest_turnitin_pledge).to eq "root"
end
it "should work for nil sub, nil root" do
root_account = Account.create!
sub_account = Account.create!(:parent_account => root_account)
expect(root_account.closest_turnitin_pledge).not_to be_empty
expect(sub_account.closest_turnitin_pledge).not_to be_empty
end
end
it "should make a default enrollment term if necessary" do
a = Account.create!(:name => "nada")
expect(a.enrollment_terms.size).to eq 1
expect(a.enrollment_terms.first.name).to eq EnrollmentTerm::DEFAULT_TERM_NAME
# don't create a new default term for sub-accounts
a2 = a.all_accounts.create!(:name => "sub")
expect(a2.enrollment_terms.size).to eq 0
end
def account_with_admin_and_restricted_user(account, restricted_role)
admin = User.create
user = User.create
account.account_users.create!(:user => admin, :role => admin_role)
account.account_users.create!(:user => user, :role => restricted_role)
[ admin, user ]
end
it "should set up access policy correctly" do
# stub out any "if" permission conditions
RoleOverride.permissions.each do |k, v|
next unless v[:if]
Account.any_instance.stubs(v[:if]).returns(true)
end
site_admin = Account.site_admin
# Set up a hierarchy of 4 accounts - a root account, a sub account,
# a sub sub account, and SiteAdmin account. Create a 'Restricted Admin'
# role available for each one, and create an admin user and a user in that restricted role
@sa_role = custom_account_role('Restricted SA Admin', account: site_admin)
site_admin.settings[:mfa_settings] = 'required'
site_admin.save!
root_account = Account.create
@root_role = custom_account_role('Restricted Root Admin', :account => root_account)
sub_account = Account.create(:parent_account => root_account)
sub_sub_account = Account.create(:parent_account => sub_account)
hash = {}
hash[:site_admin] = { :account => Account.site_admin}
hash[:root] = { :account => root_account}
hash[:sub] = { :account => sub_account}
hash[:sub_sub] = { :account => sub_sub_account}
hash.each do |k, v|
v[:account].update_attribute(:settings, {:no_enrollments_can_create_courses => false})
admin, user = account_with_admin_and_restricted_user(v[:account], (k == :site_admin ? @sa_role : @root_role))
hash[k][:admin] = admin
hash[k][:user] = user
end
limited_access = [ :read, :read_as_admin, :manage, :update, :delete, :read_outcomes ]
conditional_access = RoleOverride.permissions.select { |_, v| v[:account_allows] }.map(&:first)
full_access = RoleOverride.permissions.keys +
limited_access - conditional_access +
[:create_courses] +
[:create_tool_manually]
full_root_access = full_access - RoleOverride.permissions.select { |k, v| v[:account_only] == :site_admin }.map(&:first)
full_sub_access = full_root_access - RoleOverride.permissions.select { |k, v| v[:account_only] == :root }.map(&:first)
# site admin has access to everything everywhere
hash.each do |k, v|
account = v[:account]
expect(account.check_policy(hash[:site_admin][:admin]) - conditional_access).to match_array full_access + (k == :site_admin ? [:read_global_outcomes] : [])
expect(account.check_policy(hash[:site_admin][:user]) - conditional_access).to match_array limited_access + (k == :site_admin ? [:read_global_outcomes] : [])
end
# root admin has access to everything except site admin
account = hash[:site_admin][:account]
expect(account.check_policy(hash[:root][:admin])).to match_array [:read_global_outcomes]
expect(account.check_policy(hash[:root][:user])).to match_array [:read_global_outcomes]
hash.each do |k, v|
next if k == :site_admin
account = v[:account]
expect(account.check_policy(hash[:root][:admin]) - conditional_access).to match_array full_root_access
expect(account.check_policy(hash[:root][:user])).to match_array limited_access
end
# sub account has access to sub and sub_sub
hash.each do |k, v|
next unless k == :site_admin || k == :root
account = v[:account]
expect(account.check_policy(hash[:sub][:admin])).to match_array(k == :site_admin ? [:read_global_outcomes] : [:read_outcomes])
expect(account.check_policy(hash[:sub][:user])).to match_array(k == :site_admin ? [:read_global_outcomes] : [:read_outcomes])
end
hash.each do |k, v|
next if k == :site_admin || k == :root
account = v[:account]
expect(account.check_policy(hash[:sub][:admin]) - conditional_access).to match_array full_sub_access
expect(account.check_policy(hash[:sub][:user])).to match_array limited_access
end
# Grant 'Restricted Admin' a specific permission, and re-check everything
some_access = [:read_reports] + limited_access
hash.each do |k, v|
account = v[:account]
account.role_overrides.create!(:permission => 'read_reports', :role => (k == :site_admin ? @sa_role : @root_role), :enabled => true)
account.role_overrides.create!(:permission => 'reset_any_mfa', :role => @sa_role, :enabled => true)
# clear caches
account.tap{|a| a.settings[:mfa_settings] = :optional; a.save!}
v[:account] = Account.find(account.id)
end
RoleOverride.clear_cached_contexts
AdheresToPolicy::Cache.clear
hash.each do |k, v|
account = v[:account]
admin_array = full_access + (k == :site_admin ? [:read_global_outcomes] : [])
user_array = some_access + [:reset_any_mfa] +
(k == :site_admin ? [:read_global_outcomes] : [])
expect(account.check_policy(hash[:site_admin][:admin]) - conditional_access).to match_array admin_array
expect(account.check_policy(hash[:site_admin][:user])).to match_array user_array
end
account = hash[:site_admin][:account]
expect(account.check_policy(hash[:root][:admin])).to match_array [:read_global_outcomes]
expect(account.check_policy(hash[:root][:user])).to match_array [:read_global_outcomes]
hash.each do |k, v|
next if k == :site_admin
account = v[:account]
expect(account.check_policy(hash[:root][:admin]) - conditional_access).to match_array full_root_access
expect(account.check_policy(hash[:root][:user])).to match_array some_access
end
# sub account has access to sub and sub_sub
hash.each do |k, v|
next unless k == :site_admin || k == :root
account = v[:account]
expect(account.check_policy(hash[:sub][:admin])).to match_array(k == :site_admin ? [:read_global_outcomes] : [:read_outcomes])
expect(account.check_policy(hash[:sub][:user])).to match_array(k == :site_admin ? [:read_global_outcomes] : [:read_outcomes])
end
hash.each do |k, v|
next if k == :site_admin || k == :root
account = v[:account]
expect(account.check_policy(hash[:sub][:admin]) - conditional_access).to match_array full_sub_access
expect(account.check_policy(hash[:sub][:user])).to match_array some_access
end
end
context "sharding" do
specs_require_sharding
it "queries for enrollments correctly when another shard is active" do
teacher_in_course
@enrollment.accept!
@shard1.activate do
expect(@course.grants_right?(@user, :read_sis)).to eq true
end
end
end
it "should allow no_enrollments_can_create_courses correctly" do
a = Account.default
a.settings = { :no_enrollments_can_create_courses => true }
a.save!
user_factory
expect(a.grants_right?(@user, :create_courses)).to be_truthy
end
it "does not allow create_courses even to admins on site admin and children" do
a = Account.site_admin
a.settings = { :no_enrollments_can_create_courses => true }
manual = a.manually_created_courses_account
user_factory
expect(a.grants_right?(@user, :create_courses)).to eq false
expect(manual.grants_right?(@user, :create_courses)).to eq false
end
it "should correctly return sub-accounts as options" do
a = Account.default
sub = Account.create!(:name => 'sub', :parent_account => a)
sub2 = Account.create!(:name => 'sub2', :parent_account => a)
sub2_1 = Account.create!(:name => 'sub2-1', :parent_account => sub2)
options = a.sub_accounts_as_options
expect(options).to eq(
[
["Default Account", a.id],
[" sub", sub.id],
[" sub2", sub2.id],
[" sub2-1", sub2_1.id]
]
)
end
it "should correctly return sub-account_ids recursively" do
a = Account.default
subs = []
sub = Account.create!(name: 'sub', parent_account: a)
subs << grand_sub = Account.create!(name: 'grand_sub', parent_account: sub)
subs << great_grand_sub = Account.create!(name: 'great_grand_sub', parent_account: grand_sub)
subs << Account.create!(name: 'great_great_grand_sub', parent_account: great_grand_sub)
expect(Account.sub_account_ids_recursive(sub.id).sort).to eq(subs.map(&:id).sort)
end
it "should return the correct user count" do
a = Account.default
expect(a.all_users.count).to eq a.user_count
expect(a.user_count).to eq 0
u = User.create!
a.account_users.create!(user: u)
expect(a.all_users.count).to eq a.user_count
expect(a.user_count).to eq 1
course_with_teacher
@teacher.update_account_associations
expect(a.all_users.count).to eq a.user_count
expect(a.user_count).to eq 2
a2 = a.sub_accounts.create!
course_with_teacher(:account => a2)
@teacher.update_account_associations
expect(a.all_users.count).to eq a.user_count
expect(a.user_count).to eq 3
user_with_pseudonym
expect(a.all_users.count).to eq a.user_count
expect(a.user_count).to eq 4
end
it "group_categories should not include deleted categories" do
account = Account.default
expect(account.group_categories.count).to eq 0
category1 = account.group_categories.create(:name => 'category 1')
category2 = account.group_categories.create(:name => 'category 2')
expect(account.group_categories.count).to eq 2
category1.destroy
account.reload
expect(account.group_categories.count).to eq 1
expect(account.group_categories.to_a).to eq [category2]
end
it "all_group_categories should include deleted categories" do
account = Account.default
expect(account.all_group_categories.count).to eq 0
category1 = account.group_categories.create(:name => 'category 1')
category2 = account.group_categories.create(:name => 'category 2')
expect(account.all_group_categories.count).to eq 2
category1.destroy
account.reload
expect(account.all_group_categories.count).to eq 2
end
it "should return correct values for login_handle_name_with_inference" do
account = Account.default
expect(account.login_handle_name_with_inference).to eq "Email"
config = account.authentication_providers.create!(auth_type: 'cas')
account.authentication_providers.first.move_to_bottom
expect(account.login_handle_name_with_inference).to eq "Login"
config.destroy
config = account.authentication_providers.create!(auth_type: 'saml')
account.authentication_providers.active.first.move_to_bottom
expect(account.reload.login_handle_name_with_inference).to eq "Login"
config.destroy
account.authentication_providers.create!(auth_type: 'ldap')
account.authentication_providers.active.first.move_to_bottom
expect(account.reload.login_handle_name_with_inference).to eq "Email"
account.login_handle_name = "LDAP Login"
account.save!
expect(account.reload.login_handle_name_with_inference).to eq "LDAP Login"
end
context "users_not_in_groups" do
before :once do
@account = Account.default
@user1 = account_admin_user(:account => @account)
@user2 = account_admin_user(:account => @account)
@user3 = account_admin_user(:account => @account)
end
it "should not include deleted users" do
@user1.destroy
expect(@account.users_not_in_groups([]).size).to eq 2
end
it "should not include users in one of the groups" do
group = @account.groups.create
group.add_user(@user1)
users = @account.users_not_in_groups([group])
expect(users.size).to eq 2
expect(users).not_to be_include(@user1)
end
it "should include users otherwise" do
group = @account.groups.create
group.add_user(@user1)
users = @account.users_not_in_groups([group])
expect(users).to be_include(@user2)
expect(users).to be_include(@user3)
end
it "should allow ordering by user's sortable name" do
@user1.sortable_name = 'jonny'; @user1.save
@user2.sortable_name = 'bob'; @user2.save
@user3.sortable_name = 'richard'; @user3.save
users = @account.users_not_in_groups([], order: User.sortable_name_order_by_clause('users'))
expect(users.map{ |u| u.id }).to eq [@user2.id, @user1.id, @user3.id]
end
end
context "tabs_available" do
before :once do
@account = Account.default.sub_accounts.create!(:name => "sub-account")
end
it "should include 'Developer Keys' for the authorized users of the site_admin account" do
account_admin_user(:account => Account.site_admin)
tabs = Account.site_admin.tabs_available(@admin)
expect(tabs.map{|t| t[:id] }).to be_include(Account::TAB_DEVELOPER_KEYS)
tabs = Account.site_admin.tabs_available(nil)
expect(tabs.map{|t| t[:id] }).not_to be_include(Account::TAB_DEVELOPER_KEYS)
end
it "should include 'Developer Keys' for the admin users of an account" do
account = Account.create!
account_admin_user(:account => account)
tabs = account.tabs_available(@admin)
expect(tabs.map{|t| t[:id] }).to be_include(Account::TAB_DEVELOPER_KEYS)
tabs = account.tabs_available(nil)
expect(tabs.map{|t| t[:id] }).not_to be_include(Account::TAB_DEVELOPER_KEYS)
end
it "should not include 'Developer Keys' for non-site_admin accounts" do
tabs = @account.tabs_available(nil)
expect(tabs.map{|t| t[:id] }).not_to be_include(Account::TAB_DEVELOPER_KEYS)
tabs = @account.root_account.tabs_available(nil)
expect(tabs.map{|t| t[:id] }).not_to be_include(Account::TAB_DEVELOPER_KEYS)
end
it "should not include external tools if not configured for account navigation" do
tool = @account.context_external_tools.new(:name => "bob", :consumer_key => "bob", :shared_secret => "bob", :domain => "example.com")
tool.user_navigation = {:url => "http://www.example.com", :text => "Example URL"}
tool.save!
expect(tool.has_placement?(:account_navigation)).to eq false
tabs = @account.tabs_available(nil)
expect(tabs.map{|t| t[:id] }).not_to be_include(tool.asset_string)
end
it "should include active external tools if configured on the account" do
tools = []
2.times do |n|
t = @account.context_external_tools.new(
:name => "bob",
:consumer_key => "bob",
:shared_secret => "bob",
:domain => "example.com"
)
t.account_navigation = {
:text => "Example URL",
:url => "http://www.example.com",
}
t.save!
tools << t
end
tool1, tool2 = tools
tool2.destroy
tools.each { |t| expect(t.has_placement?(:account_navigation)).to eq true }
tabs = @account.tabs_available
tab_ids = tabs.map{|t| t[:id] }
expect(tab_ids).to be_include(tool1.asset_string)
expect(tab_ids).not_to be_include(tool2.asset_string)
tab = tabs.detect{|t| t[:id] == tool1.asset_string }
expect(tab[:label]).to eq tool1.settings[:account_navigation][:text]
expect(tab[:href]).to eq :account_external_tool_path
expect(tab[:args]).to eq [@account.id, tool1.id]
end
it "should include external tools if configured on the root account" do
tool = @account.context_external_tools.new(:name => "bob", :consumer_key => "bob", :shared_secret => "bob", :domain => "example.com")
tool.account_navigation = {:url => "http://www.example.com", :text => "Example URL"}
tool.save!
expect(tool.has_placement?(:account_navigation)).to eq true
tabs = @account.tabs_available(nil)
expect(tabs.map{|t| t[:id] }).to be_include(tool.asset_string)
tab = tabs.detect{|t| t[:id] == tool.asset_string }
expect(tab[:label]).to eq tool.settings[:account_navigation][:text]
expect(tab[:href]).to eq :account_external_tool_path
expect(tab[:args]).to eq [@account.id, tool.id]
end
it "should not include external tools for non-admins if visibility is set" do
course_with_teacher(:account => @account)
tool = @account.context_external_tools.new(:name => "bob", :consumer_key => "bob", :shared_secret => "bob", :domain => "example.com")
tool.account_navigation = {:url => "http://www.example.com", :text => "Example URL", :visibility => "admins"}
tool.save!
expect(tool.has_placement?(:account_navigation)).to eq true
tabs = @account.tabs_available(@teacher)
expect(tabs.map{|t| t[:id] }).to_not be_include(tool.asset_string)
admin = account_admin_user(:account => @account)
tabs = @account.tabs_available(admin)
expect(tabs.map{|t| t[:id] }).to be_include(tool.asset_string)
end
it "should use localized labels" do
tool = @account.context_external_tools.new(:name => "bob", :consumer_key => "test", :shared_secret => "secret",
:url => "http://example.com")
account_navigation = {
:text => 'this should not be the title',
:url => 'http://www.example.com',
:labels => {
'en' => 'English Label',
'sp' => 'Spanish Label'
}
}
tool.settings[:account_navigation] = account_navigation
tool.save!
tabs = @account.external_tool_tabs({})
expect(tabs.first[:label]).to eq "English Label"
end
it 'includes message handlers' do
mock_tab = {
:id => '1234',
:label => 'my_label',
:css_class => '1234',
:href => :launch_path_helper,
:visibility => nil,
:external => true,
:hidden => false,
:args => [1, 2]
}
Lti::MessageHandler.stubs(:lti_apps_tabs).returns([mock_tab])
expect(@account.tabs_available(nil)).to include(mock_tab)
end
it 'uses :manage_assignments to determine question bank tab visibility' do
account_admin_user_with_role_changes(acccount: @account, role_changes: { manage_assignments: true, manage_grades: false})
tabs = @account.tabs_available(@admin)
expect(tabs.map{|t| t[:id] }).to be_include(Account::TAB_QUESTION_BANKS)
end
end
describe "fast_all_users" do
it "should preserve sortable_name" do
user_with_pseudonym(:active_all => 1)
@user.update_attributes(:name => "John St. Clair", :sortable_name => "St. Clair, John")
@johnstclair = @user
user_with_pseudonym(:active_all => 1, :username => 'jt@instructure.com', :name => 'JT Olds')
@jtolds = @user
expect(Account.default.fast_all_users).to eq [@jtolds, @johnstclair]
end
end
it "should not allow setting an sis id for a root account" do
@account = Account.create!
@account.sis_source_id = 'abc'
expect(@account.save).to be_falsey
end
describe "user_list_search_mode_for" do
let_once(:account) { Account.default }
it "should be preferred for anyone if open registration is turned on" do
account.settings = { :open_registration => true }
expect(account.user_list_search_mode_for(nil)).to eq :preferred
expect(account.user_list_search_mode_for(user_factory)).to eq :preferred
end
it "should be preferred for account admins" do
expect(account.user_list_search_mode_for(nil)).to eq :closed
expect(account.user_list_search_mode_for(user_factory)).to eq :closed
user_factory
account.account_users.create!(user: @user)
expect(account.user_list_search_mode_for(@user)).to eq :preferred
end
end
context "sharding" do
specs_require_sharding
it "should properly return site admin permissions regardless of active shard" do
enable_cache do
user_factory
site_admin = Account.site_admin
site_admin.account_users.create!(user: @user)
@shard1.activate do
expect(site_admin.grants_right?(@user, :manage_site_settings)).to be_truthy
end
expect(site_admin.grants_right?(@user, :manage_site_settings)).to be_truthy
user_factory
@shard1.activate do
expect(site_admin.grants_right?(@user, :manage_site_settings)).to be_falsey
end
expect(site_admin.grants_right?(@user, :manage_site_settings)).to be_falsey
end
end
end
context "permissions" do
before(:once) { Account.default }
it "should grant :read_global_outcomes to any user iff site_admin" do
@site_admin = Account.site_admin
expect(@site_admin.grants_right?(User.new, :read_global_outcomes)).to be_truthy
@subaccount = @site_admin.sub_accounts.create!
expect(@subaccount.grants_right?(User.new, :read_global_outcomes)).to be_falsey
end
it "should not grant :read_outcomes to user's outside the account" do
expect(Account.default.grants_right?(User.new, :read_outcomes)).to be_falsey
end
it "should grant :read_outcomes to account admins" do
account_admin_user(:account => Account.default)
expect(Account.default.grants_right?(@admin, :read_outcomes)).to be_truthy
end
it "should grant :read_outcomes to subaccount admins" do
account_admin_user(:account => Account.default.sub_accounts.create!)
expect(Account.default.grants_right?(@admin, :read_outcomes)).to be_truthy
end
it "should grant :read_outcomes to enrollees in account courses" do
course_factory(:account => Account.default)
teacher_in_course
student_in_course
expect(Account.default.grants_right?(@teacher, :read_outcomes)).to be_truthy
expect(Account.default.grants_right?(@student, :read_outcomes)).to be_truthy
end
it "should grant :read_outcomes to enrollees in subaccount courses" do
course_factory(:account => Account.default.sub_accounts.create!)
teacher_in_course
student_in_course
expect(Account.default.grants_right?(@teacher, :read_outcomes)).to be_truthy
expect(Account.default.grants_right?(@student, :read_outcomes)).to be_truthy
end
end
describe "authentication_providers.active" do
let(:account){ Account.default }
let!(:aac){ account.authentication_providers.create!(auth_type: 'facebook') }
it "pulls active AACS" do
expect(account.authentication_providers.active).to include(aac)
end
it "ignores deleted AACs" do
aac.destroy
expect(account.authentication_providers.active).to_not include(aac)
end
end
describe "delegated_authentication?" do
let(:account){ Account.default }
before do
account.authentication_providers.scope.delete_all
end
it "is false for LDAP" do
account.authentication_providers.create!(auth_type: 'ldap')
expect(account.delegated_authentication?).to eq false
end
it "is true for CAS" do
account.authentication_providers.create!(auth_type: 'cas')