|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe PlansController, type: :controller do |
| 6 | + describe 'templates availability helpers' do |
| 7 | + before do |
| 8 | + @org = create(:org, :organisation, name: 'The User Org') |
| 9 | + @org_id = @org.id |
| 10 | + |
| 11 | + @org_template = create(:template, :default, :organisationally_visible, :published, org: @org, |
| 12 | + title: 'Org Template') |
| 13 | + |
| 14 | + @funder1 = create(:org, :funder, name: 'Funder1 Org') |
| 15 | + @funder1_template = create(:template, :publicly_visible, :published, |
| 16 | + org: @funder1, title: 'Funder1 Template') |
| 17 | + |
| 18 | + @org_custom_funder1_template = create(:template, :organisationally_visible, :published, |
| 19 | + org: @org, customization_of: @funder1_template.family_id, |
| 20 | + title: 'Org Customised Funder1 Template') |
| 21 | + |
| 22 | + @funder2 = create(:org, :funder, name: 'Funder2 Org') |
| 23 | + @funder2_template = create(:template, :publicly_visible, :published, org: @funder2, title: 'Funder2 Template') |
| 24 | + |
| 25 | + @global_template = create(:template, :default, :publicly_visible, :published, title: 'Global Template') |
| 26 | + |
| 27 | + @other_org = create(:org, :organisation, name: 'The Other Org') |
| 28 | + @other_org_template = create(:template, :organisationally_visible, :published, |
| 29 | + org: @other_org, title: 'Other Org Template') |
| 30 | + end |
| 31 | + |
| 32 | + it 'returns a grouped hash containing the available templates' do |
| 33 | + grouped = controller.send(:templates_available_to_org_user, @org_id) |
| 34 | + expect(grouped).to be_a(Hash) |
| 35 | + puts "Grouped templates available to org id #{@org_id}: #{grouped.keys.inspect}" |
| 36 | + |
| 37 | + # flatten one level to inspect [title, id] entries across groups |
| 38 | + entries = grouped.values.flatten(1) |
| 39 | + puts entries.inspect |
| 40 | + # Available templates |
| 41 | + expect(entries).to include([@org_template.title, @org_template.id]) |
| 42 | + expect(entries).to include([@org_custom_funder1_template.title, @org_custom_funder1_template.id]) |
| 43 | + expect(entries).to include([@funder2_template.title, @funder2_template.id]) |
| 44 | + expect(entries).to include([@global_template.title, @global_template.id]) |
| 45 | + # Not available templates |
| 46 | + # As the org has already customised funder1_template, it should not appear in the available list |
| 47 | + expect(entries).not_to include([@funder1_template.title, @funder1_template.id]) |
| 48 | + # Templates from other orgs should not appear if only organisationally visible |
| 49 | + expect(entries).not_to include([@other_org_template.title, @other_org_template.id]) |
| 50 | + end |
| 51 | + |
| 52 | + it 'validates a template id that is available for the org' do |
| 53 | + expect(controller.send(:validate_template_available_to_org_user?, @org_template.id, @org_id)).to be true |
| 54 | + expect(controller.send(:validate_template_available_to_org_user?, @org_custom_funder1_template.id, |
| 55 | + @org_id)).to be true |
| 56 | + expect(controller.send(:validate_template_available_to_org_user?, @funder2_template.id, @org_id)).to be true |
| 57 | + expect(controller.send(:validate_template_available_to_org_user?, @global_template.id, @org_id)).to be true |
| 58 | + end |
| 59 | + |
| 60 | + it 'returns false for a template id that is not available for the org' do |
| 61 | + # As the org has already customised funder1_template, it should not be available |
| 62 | + expect(controller.send(:validate_template_available_to_org_user?, @funder1_template.id, @org_id)).to be false |
| 63 | + # Templates from other orgs should not be available if only organisationally visible |
| 64 | + expect(controller.send(:validate_template_available_to_org_user?, @other_org_template.id, @org_id)).to be false |
| 65 | + end |
| 66 | + end |
| 67 | +end |
0 commit comments