Skip to content

Commit 264f3ce

Browse files
authored
Merge pull request #1206 from ElixirTeSS/copilot/add-tests-for-disabled-features
Add tests for Space model disabled_features field
2 parents 206be22 + 4c4a8c7 commit 264f3ce

File tree

4 files changed

+271
-0
lines changed

4 files changed

+271
-0
lines changed

test/controllers/static_controller_test.rb

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,4 +520,178 @@ class StaticControllerTest < ActionController::TestCase
520520
end
521521
end
522522
end
523+
524+
test 'should respect space disabled_features when displaying tabs' do
525+
space = spaces(:plants)
526+
space.disabled_features = ['events', 'materials']
527+
space.save!
528+
529+
features = {
530+
'events': true,
531+
'materials': true,
532+
'elearning_materials': true,
533+
'workflows': true,
534+
'collections': true,
535+
'content_providers': true,
536+
'trainers': true,
537+
'nodes': true,
538+
'spaces': true
539+
}
540+
541+
with_settings(feature: features) do
542+
with_host('plants.mytess.training') do
543+
get :home
544+
545+
# These should NOT appear because they're disabled for this space
546+
assert_select 'ul.nav.navbar-nav' do
547+
assert_select 'li a[href=?]', events_path, count: 0
548+
assert_select 'li a[href=?]', materials_path, count: 0
549+
end
550+
551+
# These should still appear because they're not disabled
552+
assert_select 'ul.nav.navbar-nav' do
553+
assert_select 'li a[href=?]', about_path
554+
assert_select 'li a[href=?]', elearning_materials_path
555+
assert_select 'li a[href=?]', workflows_path
556+
assert_select 'li a[href=?]', collections_path
557+
end
558+
end
559+
end
560+
end
561+
562+
test 'space disabled_features do not affect default space' do
563+
space = spaces(:plants)
564+
space.disabled_features = ['events', 'materials']
565+
space.save!
566+
567+
features = {
568+
'events': true,
569+
'materials': true,
570+
'elearning_materials': true,
571+
'workflows': true,
572+
'collections': true,
573+
'content_providers': true,
574+
'trainers': true,
575+
'nodes': true,
576+
'spaces': true
577+
}
578+
579+
with_settings(feature: features) do
580+
# Access the default space (not plants)
581+
get :home
582+
583+
# All features should appear in the default space
584+
assert_select 'ul.nav.navbar-nav' do
585+
assert_select 'li a[href=?]', about_path
586+
assert_select 'li a[href=?]', events_path
587+
assert_select 'li a[href=?]', materials_path
588+
assert_select 'li a[href=?]', elearning_materials_path
589+
assert_select 'li a[href=?]', workflows_path
590+
assert_select 'li a[href=?]', collections_path
591+
end
592+
end
593+
end
594+
595+
test 'different spaces can have different disabled features' do
596+
plants_space = spaces(:plants)
597+
plants_space.disabled_features = ['events']
598+
plants_space.save!
599+
600+
astro_space = spaces(:astro)
601+
astro_space.disabled_features = ['materials']
602+
astro_space.save!
603+
604+
features = {
605+
'events': true,
606+
'materials': true,
607+
'workflows': true,
608+
'spaces': true
609+
}
610+
611+
with_settings(feature: features) do
612+
# Check plants space - events disabled
613+
with_host('plants.mytess.training') do
614+
get :home
615+
616+
assert_select 'ul.nav.navbar-nav' do
617+
assert_select 'li a[href=?]', events_path, count: 0
618+
assert_select 'li a[href=?]', materials_path
619+
assert_select 'li a[href=?]', workflows_path
620+
end
621+
end
622+
623+
# Check astro space - materials disabled
624+
with_host('space.mytess.training') do
625+
get :home
626+
627+
assert_select 'ul.nav.navbar-nav' do
628+
assert_select 'li a[href=?]', events_path
629+
assert_select 'li a[href=?]', materials_path, count: 0
630+
assert_select 'li a[href=?]', workflows_path
631+
end
632+
end
633+
end
634+
end
635+
636+
test 'space with no disabled features shows all enabled global features' do
637+
space = spaces(:plants)
638+
space.disabled_features = []
639+
space.save!
640+
641+
features = {
642+
'events': true,
643+
'materials': true,
644+
'elearning_materials': true,
645+
'workflows': true,
646+
'collections': true,
647+
'content_providers': true,
648+
'trainers': true,
649+
'nodes': true,
650+
'spaces': true
651+
}
652+
653+
with_settings(feature: features) do
654+
with_host('plants.mytess.training') do
655+
get :home
656+
657+
# All globally enabled features should appear
658+
assert_select 'ul.nav.navbar-nav' do
659+
assert_select 'li a[href=?]', about_path
660+
assert_select 'li a[href=?]', events_path
661+
assert_select 'li a[href=?]', materials_path
662+
assert_select 'li a[href=?]', elearning_materials_path
663+
assert_select 'li a[href=?]', workflows_path
664+
assert_select 'li a[href=?]', collections_path
665+
end
666+
end
667+
end
668+
end
669+
670+
test 'space disabled features work with directory tabs' do
671+
space = spaces(:plants)
672+
space.disabled_features = ['content_providers']
673+
space.save!
674+
675+
features = {
676+
'events': true,
677+
'materials': true,
678+
'content_providers': true,
679+
'trainers': true,
680+
'nodes': true,
681+
'spaces': true
682+
}
683+
684+
with_settings(feature: features, site: { tab_order: [], directory_tabs: ['content_providers', 'trainers', 'nodes'] }) do
685+
with_host('plants.mytess.training') do
686+
get :home
687+
688+
# content_providers should not appear even in directory menu
689+
assert_select 'li.dropdown.directory-menu' do
690+
assert_select 'li a[href=?]', content_providers_path, count: 0
691+
assert_select 'li a[href=?]', trainers_path
692+
assert_select 'li a[href=?]', nodes_path
693+
end
694+
end
695+
end
696+
end
523697
end

test/fixtures/spaces.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ plants:
66
astro:
77
title: TeSS Space Community
88
host: space.mytess.training
9+
user: astro_owner
910

1011
other:
1112
title: Other TeSS Community
1213
host: other.mytess.training
14+
user: other_owner

test/fixtures/users.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,15 @@ space_admin:
172172
email: 'plantboss@example.com'
173173
encrypted_password: <%= Devise::Encryptor.digest(User, 'plantsrule') %>
174174
confirmed_at: <%= Time.zone.now %>
175+
176+
astro_owner:
177+
username: 'astro_boss'
178+
email: 'astroboss@example.com'
179+
encrypted_password: <%= Devise::Encryptor.digest(User, 'spacerules') %>
180+
confirmed_at: <%= Time.zone.now %>
181+
182+
other_owner:
183+
username: 'other_boss'
184+
email: 'otherboss@example.com'
185+
encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
186+
confirmed_at: <%= Time.zone.now %>

test/models/space_test.rb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,87 @@ class SpaceTest < ActiveSupport::TestCase
3131

3232
assert_empty @space.administrators
3333
end
34+
35+
test 'disabled_features defaults to empty array' do
36+
new_space = Space.new(title: 'Test Space', host: 'test.example.com', user: users(:regular_user))
37+
assert_equal [], new_space.disabled_features
38+
end
39+
40+
test 'disabled_features can be set to valid features' do
41+
@space.disabled_features = ['events', 'materials']
42+
assert @space.valid?
43+
assert_equal ['events', 'materials'], @space.disabled_features
44+
end
45+
46+
test 'disabled_features rejects invalid features' do
47+
@space.disabled_features = ['invalid_feature', 'events']
48+
assert_not @space.valid?
49+
assert @space.errors.added?(:disabled_features, :inclusion)
50+
end
51+
52+
test 'disabled_features allows empty strings' do
53+
@space.disabled_features = ['events', '', 'materials']
54+
assert @space.valid?
55+
end
56+
57+
test 'feature_enabled? returns true for enabled features' do
58+
@space.disabled_features = ['materials']
59+
assert @space.feature_enabled?('events')
60+
assert @space.feature_enabled?('workflows')
61+
end
62+
63+
test 'feature_enabled? returns false for disabled features' do
64+
@space.disabled_features = ['events', 'materials']
65+
assert_not @space.feature_enabled?('events')
66+
assert_not @space.feature_enabled?('materials')
67+
end
68+
69+
test 'feature_enabled? returns true when no features are disabled' do
70+
@space.disabled_features = []
71+
Space::FEATURES.each do |feature|
72+
assert @space.feature_enabled?(feature), "Feature #{feature} should be enabled"
73+
end
74+
end
75+
76+
test 'feature_enabled? falls back to TeSS::Config for non-space features' do
77+
@space.disabled_features = []
78+
# Test with a feature that's not in Space::FEATURES
79+
with_settings(feature: { 'custom_feature' => true }) do
80+
assert @space.feature_enabled?('custom_feature')
81+
end
82+
83+
with_settings(feature: { 'custom_feature' => false }) do
84+
assert_not @space.feature_enabled?('custom_feature')
85+
end
86+
end
87+
88+
test 'enabled_features returns all features except disabled ones' do
89+
@space.disabled_features = ['events', 'materials']
90+
enabled = @space.enabled_features
91+
92+
assert_not_includes enabled, 'events'
93+
assert_not_includes enabled, 'materials'
94+
assert_includes enabled, 'workflows'
95+
assert_includes enabled, 'collections'
96+
end
97+
98+
test 'enabled_features= sets disabled_features to complement' do
99+
@space.enabled_features = ['events', 'materials']
100+
101+
assert_includes @space.disabled_features, 'workflows'
102+
assert_includes @space.disabled_features, 'collections'
103+
assert_includes @space.disabled_features, 'elearning_materials'
104+
assert_not_includes @space.disabled_features, 'events'
105+
assert_not_includes @space.disabled_features, 'materials'
106+
end
107+
108+
test 'enabled_features= with empty array disables all features' do
109+
@space.enabled_features = []
110+
assert_equal Space::FEATURES.sort, @space.disabled_features.sort
111+
end
112+
113+
test 'enabled_features= with all features enables all features' do
114+
@space.enabled_features = Space::FEATURES
115+
assert_equal [], @space.disabled_features
116+
end
34117
end

0 commit comments

Comments
 (0)