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