/ee/spec/finders/custom_project_templates_finder_spec.rb

https://gitlab.com/michold/git-lab-playground · Ruby · 108 lines · 80 code · 27 blank · 1 comment · 2 complexity · 26e06bc0a36bcdbd716913b65fde87ae MD5 · raw file

  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. RSpec.describe CustomProjectTemplatesFinder do
  4. let_it_be(:user) { create(:user) }
  5. let(:base_params) { { current_user: user } }
  6. let(:params) { {} }
  7. subject { described_class.new(**base_params.merge(params)) }
  8. it 'returns an empty relation if group is not set' do
  9. expect(subject.execute).to be_empty
  10. end
  11. context 'when group with custom project templates is set' do
  12. let_it_be(:group) { create(:group) }
  13. before do
  14. stub_ee_application_setting(custom_project_templates_group_id: group.id)
  15. end
  16. it 'returns an empty relation if group has no available project templates' do
  17. expect(group.projects).to be_empty
  18. expect(subject.execute).to be_empty
  19. end
  20. it 'ignores pages permissions as they are not exported' do
  21. project = create(:project, :internal, :metrics_dashboard_enabled, namespace: group, pages_access_level: ProjectFeature::PRIVATE)
  22. expect(subject.execute).to eq([project])
  23. end
  24. context 'when the group has projects' do
  25. using RSpec::Parameterized::TableSyntax
  26. let_it_be_with_reload(:private_project) { create :project, :metrics_dashboard_enabled, :private, namespace: group, name: 'private' }
  27. let_it_be_with_reload(:internal_project) { create :project, :metrics_dashboard_enabled, :internal, namespace: group, name: 'internal' }
  28. let_it_be_with_reload(:public_project) { create :project, :metrics_dashboard_enabled, :public, namespace: group, name: 'public' }
  29. where(:issues_access_level, :minimal_user_access, :available_templates) do
  30. :disabled | :no_access | %w[public internal]
  31. :disabled | :guest | %w[public internal private]
  32. :private | :guest | %w[public internal private]
  33. :private | :no_access | %w[]
  34. :enabled | :guest | %w[public internal private]
  35. :enabled | :no_access | %w[public internal]
  36. end
  37. with_them do
  38. context "when templates have the issues feature #{params[:issues_access_level]}" do
  39. before do
  40. private_project.project_feature.update!(issues_access_level: ProjectFeature::STRING_OPTIONS[issues_access_level])
  41. internal_project.project_feature.update!(issues_access_level: ProjectFeature::STRING_OPTIONS[issues_access_level])
  42. public_project.project_feature.update!(issues_access_level: ProjectFeature::STRING_OPTIONS[issues_access_level])
  43. end
  44. it "returns #{params[:available_templates].join(', ')} projects to users with #{params[:minimal_user_access]} to the project" do
  45. unless minimal_user_access == :no_access
  46. public_project.add_user(user, minimal_user_access)
  47. internal_project.add_user(user, minimal_user_access)
  48. private_project.add_user(user, minimal_user_access)
  49. end
  50. expect(subject.execute.pluck(:name)).to match_array(available_templates)
  51. end
  52. it "returns #{params[:available_templates].join(', ')} projects to users with #{params[:minimal_user_access]} to the group" do
  53. unless minimal_user_access == :no_access
  54. group.add_user(user, minimal_user_access)
  55. end
  56. expect(subject.execute.pluck(:name)).to match_array(available_templates)
  57. end
  58. end
  59. end
  60. context 'filtering the results' do
  61. let_it_be(:other_public_project) { create :project, :metrics_dashboard_enabled, :public, namespace: group, name: 'other public' }
  62. it 'allows to search available project templates by name' do
  63. params[:search] = 'publi'
  64. expect(subject.execute).to contain_exactly(public_project, other_public_project)
  65. end
  66. it 'filters by single project ID' do
  67. params[:project_id] = public_project.id
  68. expect(subject.execute).to contain_exactly(public_project)
  69. end
  70. it 'filters by list of project IDs' do
  71. params[:project_id] = [public_project.id, other_public_project.id]
  72. expect(subject.execute).to contain_exactly(public_project, other_public_project)
  73. end
  74. it 'does not return inaccessible projects' do
  75. params[:project_id] = private_project.id
  76. expect(subject.execute).to be_empty
  77. end
  78. end
  79. end
  80. end
  81. end