diff --git a/app/controllers/projects/services_controller.rb b/app/controllers/projects/services_controller.rb index 5ed9baf04f6507395b22e60017a5d5f888e891ac..aa99b7cb8d3b13949db9f25b42faa901a2c5377a 100644 --- a/app/controllers/projects/services_controller.rb +++ b/app/controllers/projects/services_controller.rb @@ -57,11 +57,10 @@ def service_test_response return { error: true, message: _('Validations failed.'), service_response: @service.errors.full_messages.join(','), test_failed: false } end - data = @service.test_data(project, current_user) - outcome = @service.test(data) + result = Integrations::Test::ProjectService.new(@service, current_user, params[:event]).execute - unless outcome[:success] - return { error: true, message: _('Test failed.'), service_response: outcome[:result].to_s, test_failed: true } + unless result[:success] + return { error: true, message: _('Test failed.'), service_response: result[:message].to_s, test_failed: true } end {} diff --git a/app/models/project_services/jira_service.rb b/app/models/project_services/jira_service.rb index eee594ab4cab7045ad369e6c5824406de05899d2..b1b2e60ba78157ef7ca8654e695388887ed3260a 100644 --- a/app/models/project_services/jira_service.rb +++ b/app/models/project_services/jira_service.rb @@ -211,11 +211,6 @@ def test(_) { success: success, result: result } end - # Jira does not need test data. - def test_data(_, _) - nil - end - override :support_close_issue? def support_close_issue? true diff --git a/app/models/project_services/pipelines_email_service.rb b/app/models/project_services/pipelines_email_service.rb index a58a264de5e59849714cbb6176ec8059084e3033..c11b2f7cc6541caab1b2d1818b01d301f56507e6 100644 --- a/app/models/project_services/pipelines_email_service.rb +++ b/app/models/project_services/pipelines_email_service.rb @@ -56,12 +56,6 @@ def can_test? project&.ci_pipelines&.any? end - def test_data(project, user) - data = Gitlab::DataBuilder::Pipeline.build(project.ci_pipelines.last) - data[:user] = user.hook_attrs - data - end - def fields [ { type: 'textarea', diff --git a/app/models/service.rb b/app/models/service.rb index a2c23947932d7532926613d8ca4e81fb4ee3f81d..42b0ae5c9db0f691c4ba9db57a24ea92c98614c3 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -142,10 +142,6 @@ def to_data_fields_hash data_fields.as_json(only: data_fields.class.column_names).except('id', 'service_id') end - def test_data(project, user) - Gitlab::DataBuilder::Push.build_sample(project, user) - end - def event_channel_names [] end diff --git a/app/services/concerns/integrations/project_test_data.rb b/app/services/concerns/integrations/project_test_data.rb new file mode 100644 index 0000000000000000000000000000000000000000..4d5514303153e25eb1f2f8102a084631f25573e4 --- /dev/null +++ b/app/services/concerns/integrations/project_test_data.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +module Integrations + module ProjectTestData + private + + def push_events_data + Gitlab::DataBuilder::Push.build_sample(project, current_user) + end + + def note_events_data + note = project.notes.first + return { error: s_('TestHooks|Ensure the project has notes.') } unless note.present? + + Gitlab::DataBuilder::Note.build(note, current_user) + end + + def issues_events_data + issue = project.issues.first + return { error: s_('TestHooks|Ensure the project has issues.') } unless issue.present? + + issue.to_hook_data(current_user) + end + + def merge_requests_events_data + merge_request = project.merge_requests.first + return { error: s_('TestHooks|Ensure the project has merge requests.') } unless merge_request.present? + + merge_request.to_hook_data(current_user) + end + + def job_events_data + build = project.builds.first + return { error: s_('TestHooks|Ensure the project has CI jobs.') } unless build.present? + + Gitlab::DataBuilder::Build.build(build) + end + + def pipeline_events_data + pipeline = project.ci_pipelines.newest_first.first + return { error: s_('TestHooks|Ensure the project has CI pipelines.') } unless pipeline.present? + + Gitlab::DataBuilder::Pipeline.build(pipeline) + end + + def wiki_page_events_data + page = project.wiki.list_pages(limit: 1).first + if !project.wiki_enabled? || page.blank? + return { error: s_('TestHooks|Ensure the wiki is enabled and has pages.') } + end + + Gitlab::DataBuilder::WikiPage.build(page, current_user, 'create') + end + + def deployment_events_data + deployment = project.deployments.first + return { error: s_('TestHooks|Ensure the project has deployments.') } unless deployment.present? + + Gitlab::DataBuilder::Deployment.build(deployment) + end + end +end diff --git a/app/services/integrations/test/base_service.rb b/app/services/integrations/test/base_service.rb new file mode 100644 index 0000000000000000000000000000000000000000..a8a027092d5184a077640bd53d7948faf07e36f2 --- /dev/null +++ b/app/services/integrations/test/base_service.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module Integrations + module Test + class BaseService + include BaseServiceUtility + + attr_accessor :integration, :current_user, :event + + # @param integration [Service] The external service that will be called + # @param current_user [User] The user calling the service + # @param event [String/nil] The event that triggered this + def initialize(integration, current_user, event = nil) + @integration = integration + @current_user = current_user + @event = event + end + + def execute + if event && (integration.supported_events.exclude?(event) || data.blank?) + return error('Testing not available for this event') + end + + return error(data[:error]) if data[:error].present? + + integration.test(data) + end + + private + + def data + raise NotImplementedError + end + end + end +end diff --git a/app/services/integrations/test/project_service.rb b/app/services/integrations/test/project_service.rb new file mode 100644 index 0000000000000000000000000000000000000000..941d70c2cc448738258af3a862be80aa902789e8 --- /dev/null +++ b/app/services/integrations/test/project_service.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +module Integrations + module Test + class ProjectService < Integrations::Test::BaseService + include Integrations::ProjectTestData + include Gitlab::Utils::StrongMemoize + + def project + strong_memoize(:project) do + integration.project + end + end + + private + + def data + strong_memoize(:data) do + next pipeline_events_data if integration.is_a?(::PipelinesEmailService) + + case event + when 'push', 'tag_push' + push_events_data + when 'note', 'confidential_note' + note_events_data + when 'issue', 'confidential_issue' + issues_events_data + when 'merge_request' + merge_requests_events_data + when 'job' + job_events_data + when 'pipeline' + pipeline_events_data + when 'wiki_page' + wiki_page_events_data + when 'deployment' + deployment_events_data + else + push_events_data + end + end + end + end + end +end + +Integrations::Test::ProjectService.prepend_if_ee('::EE::Integrations::Test::ProjectService') diff --git a/app/services/test_hooks/base_service.rb b/app/services/test_hooks/base_service.rb index ebebf29c28b450dc3e86bc8b51f31385a93d6643..0fda6fb1ed074567c74d8aca808f7edb9d2d54da 100644 --- a/app/services/test_hooks/base_service.rb +++ b/app/services/test_hooks/base_service.rb @@ -2,6 +2,8 @@ module TestHooks class BaseService + include BaseServiceUtility + attr_accessor :hook, :current_user, :trigger def initialize(hook, current_user, trigger) @@ -12,31 +14,11 @@ def initialize(hook, current_user, trigger) def execute trigger_key = hook.class.triggers.key(trigger.to_sym) - trigger_data_method = "#{trigger}_data" - - if trigger_key.nil? || !self.respond_to?(trigger_data_method, true) - return error('Testing not available for this hook') - end - - error_message = catch(:validation_error) do # rubocop:disable Cop/BanCatchThrow - sample_data = self.__send__(trigger_data_method) # rubocop:disable GitlabSecurity/PublicSend - - return hook.execute(sample_data, trigger_key) # rubocop:disable Cop/AvoidReturnFromBlocks - end - - error(error_message) - end - - private - def error(message, http_status = nil) - result = { - message: message, - status: :error - } + return error('Testing not available for this hook') if trigger_key.nil? || data.blank? + return error(data[:error]) if data[:error].present? - result[:http_status] = http_status if http_status - result + hook.execute(data, trigger_key) end end end diff --git a/app/services/test_hooks/project_service.rb b/app/services/test_hooks/project_service.rb index aa80cc928b94a02b480a94cd959647419a7bc100..4e554dce357af51bb3a8ef1034ed74e9ef227ee5 100644 --- a/app/services/test_hooks/project_service.rb +++ b/app/services/test_hooks/project_service.rb @@ -2,6 +2,9 @@ module TestHooks class ProjectService < TestHooks::BaseService + include Integrations::ProjectTestData + include Gitlab::Utils::StrongMemoize + attr_writer :project def project @@ -10,58 +13,25 @@ def project private - def push_events_data - throw(:validation_error, s_('TestHooks|Ensure the project has at least one commit.')) if project.empty_repo? # rubocop:disable Cop/BanCatchThrow - - Gitlab::DataBuilder::Push.build_sample(project, current_user) - end - - alias_method :tag_push_events_data, :push_events_data - - def note_events_data - note = project.notes.first - throw(:validation_error, s_('TestHooks|Ensure the project has notes.')) unless note.present? # rubocop:disable Cop/BanCatchThrow - - Gitlab::DataBuilder::Note.build(note, current_user) - end - - def issues_events_data - issue = project.issues.first - throw(:validation_error, s_('TestHooks|Ensure the project has issues.')) unless issue.present? # rubocop:disable Cop/BanCatchThrow - - issue.to_hook_data(current_user) - end - - alias_method :confidential_issues_events_data, :issues_events_data - - def merge_requests_events_data - merge_request = project.merge_requests.first - throw(:validation_error, s_('TestHooks|Ensure the project has merge requests.')) unless merge_request.present? # rubocop:disable Cop/BanCatchThrow - - merge_request.to_hook_data(current_user) - end - - def job_events_data - build = project.builds.first - throw(:validation_error, s_('TestHooks|Ensure the project has CI jobs.')) unless build.present? # rubocop:disable Cop/BanCatchThrow - - Gitlab::DataBuilder::Build.build(build) - end - - def pipeline_events_data - pipeline = project.ci_pipelines.first - throw(:validation_error, s_('TestHooks|Ensure the project has CI pipelines.')) unless pipeline.present? # rubocop:disable Cop/BanCatchThrow - - Gitlab::DataBuilder::Pipeline.build(pipeline) - end - - def wiki_page_events_data - page = project.wiki.list_pages(limit: 1).first - if !project.wiki_enabled? || page.blank? - throw(:validation_error, s_('TestHooks|Ensure the wiki is enabled and has pages.')) # rubocop:disable Cop/BanCatchThrow + def data + strong_memoize(:data) do + case trigger + when 'push_events', 'tag_push_events' + push_events_data + when 'note_events' + note_events_data + when 'issues_events', 'confidential_issues_events' + issues_events_data + when 'merge_requests_events' + merge_requests_events_data + when 'job_events' + job_events_data + when 'pipeline_events' + pipeline_events_data + when 'wiki_page_events' + wiki_page_events_data + end end - - Gitlab::DataBuilder::WikiPage.build(page, current_user, 'create') end end end diff --git a/app/services/test_hooks/system_service.rb b/app/services/test_hooks/system_service.rb index 5c7961f417de02312a2dba223694e304cbe548c1..66d78bfc5787c4d57692b4884dbf6e43ed843c4c 100644 --- a/app/services/test_hooks/system_service.rb +++ b/app/services/test_hooks/system_service.rb @@ -2,23 +2,26 @@ module TestHooks class SystemService < TestHooks::BaseService - private - - def push_events_data - Gitlab::DataBuilder::Push.sample_data - end + include Gitlab::Utils::StrongMemoize - def tag_push_events_data - Gitlab::DataBuilder::Push.sample_data - end + private - def repository_update_events_data - Gitlab::DataBuilder::Repository.sample_data + def data + strong_memoize(:data) do + case trigger + when 'push_events', 'tag_push_events' + Gitlab::DataBuilder::Push.sample_data + when 'repository_update_events' + Gitlab::DataBuilder::Repository.sample_data + when 'merge_requests_events' + merge_requests_events_data + end + end end def merge_requests_events_data merge_request = MergeRequest.of_projects(current_user.projects.select(:id)).first - throw(:validation_error, s_('TestHooks|Ensure one of your projects has merge requests.')) unless merge_request.present? # rubocop:disable Cop/BanCatchThrow + return { error: s_('TestHooks|Ensure one of your projects has merge requests.') } unless merge_request.present? merge_request.to_hook_data(current_user) end diff --git a/ee/app/models/project_services/github_service.rb b/ee/app/models/project_services/github_service.rb index e17ba336ee7b7622b78e6a13b2c1d18655ba66b7..c5ae970723c8d4c62559e27d9a0a8889d1b78b32 100644 --- a/ee/app/models/project_services/github_service.rb +++ b/ee/app/models/project_services/github_service.rb @@ -63,10 +63,6 @@ def can_test? project&.ci_pipelines&.any? end - def disabled_title - 'Please set up a pipeline on your repository.' - end - def execute(data) return if disabled? || invalid? || irrelevant_result?(data) @@ -75,14 +71,6 @@ def execute(data) update_status(status_message) end - def test_data(project, user) - pipeline = project.ci_pipelines.newest_first.first - - raise disabled_title unless pipeline - - Gitlab::DataBuilder::Pipeline.build(pipeline) - end - def test(data) begin result = execute(data) diff --git a/ee/app/services/ee/integrations/test/project_service.rb b/ee/app/services/ee/integrations/test/project_service.rb new file mode 100644 index 0000000000000000000000000000000000000000..e3c03de73823c9c7be76f1da37cb336c040e3aba --- /dev/null +++ b/ee/app/services/ee/integrations/test/project_service.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module EE + module Integrations + module Test + module ProjectService + extend ::Gitlab::Utils::Override + include ::Gitlab::Utils::StrongMemoize + + private + + override :data + def data + strong_memoize(:data) do + next pipeline_events_data if integration.is_a?(::GithubService) + + super + end + end + end + end + end +end diff --git a/ee/spec/factories/services.rb b/ee/spec/factories/services.rb index 89144c17b3dde0c6e620fb71a04fad595c026b8e..4552e561892db882859518746975241881caedcb 100644 --- a/ee/spec/factories/services.rb +++ b/ee/spec/factories/services.rb @@ -7,13 +7,6 @@ type { 'GitlabSlackApplicationService' } end - factory :github_service do - project - active { true } - token { 'github-token' } - type { 'GithubService' } - end - factory :slack_slash_commands_service do project active { true } diff --git a/ee/spec/features/projects/services/user_activates_github_spec.rb b/ee/spec/features/projects/services/user_activates_github_spec.rb index 9532622ca763e7b8b3e3be49958137565ffbf0d1..edabec926213db2b8bc6435e8284c13add7d6dd4 100644 --- a/ee/spec/features/projects/services/user_activates_github_spec.rb +++ b/ee/spec/features/projects/services/user_activates_github_spec.rb @@ -54,7 +54,6 @@ def fill_in_details ) click_button 'Test settings and save changes' - wait_for_requests expect(page).to have_content('GitHub activated.') end diff --git a/ee/spec/models/project_services/github_service_spec.rb b/ee/spec/models/project_services/github_service_spec.rb index acf4cb488530d990df092ce9bf71d5d65b6efa5b..0a659416c0b3c772071396fa2aa15935494455c2 100644 --- a/ee/spec/models/project_services/github_service_spec.rb +++ b/ee/spec/models/project_services/github_service_spec.rb @@ -299,23 +299,6 @@ end end - describe '#test_data' do - let(:user) { project.owner } - let(:test_data) { subject.test_data(project, user) } - - it 'raises error if no pipeline found' do - project.ci_pipelines.delete_all - - expect { test_data }.to raise_error 'Please set up a pipeline on your repository.' - end - - it 'generates data for latest pipeline' do - pipeline - - expect(test_data[:object_kind]).to eq 'pipeline' - end - end - describe '#test' do it 'mentions creator in success message' do dummy_response = { context: "default", creator: { login: "YourUser" } } diff --git a/ee/spec/services/ee/integrations/test/project_service_spec.rb b/ee/spec/services/ee/integrations/test/project_service_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..270cf55122cde9f864aebaeedb9176c45bb73625 --- /dev/null +++ b/ee/spec/services/ee/integrations/test/project_service_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe ::Integrations::Test::ProjectService do + let(:user) { double('user') } + + describe '#execute' do + let(:project) { create(:project) } + let(:event) { nil } + let(:sample_data) { { data: 'sample' } } + let(:success_result) { { success: true, result: {} } } + + subject { described_class.new(integration, user, event).execute } + + context 'without event specified' do + context 'GitHubService' do + let(:integration) { create(:github_service, project: project) } + + it_behaves_like 'tests for integration with pipeline data' + end + end + end +end diff --git a/locale/gitlab.pot b/locale/gitlab.pot index d47a78c8c290ecf97d63c19ff3c813b13540b632..912953d06d7fb13fc31d083a75fba9b987fd11e6 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -21656,7 +21656,7 @@ msgstr "" msgid "TestHooks|Ensure the project has CI pipelines." msgstr "" -msgid "TestHooks|Ensure the project has at least one commit." +msgid "TestHooks|Ensure the project has deployments." msgstr "" msgid "TestHooks|Ensure the project has issues." diff --git a/spec/factories/services.rb b/spec/factories/services.rb index b6696769da91fad559ecaa6872d0d3482e3ac331..5364f62b2c56488e7040bc813394c5d7c243e2fc 100644 --- a/spec/factories/services.rb +++ b/spec/factories/services.rb @@ -165,6 +165,20 @@ type { 'SlackService' } end + factory :github_service do + project + type { 'GithubService' } + active { true } + token { 'github-token' } + end + + factory :pipelines_email_service do + project + active { true } + type { 'PipelinesEmailService' } + recipients { 'test@example.com' } + end + # this is for testing storing values inside properties, which is deprecated and will be removed in # https://gitlab.com/gitlab-org/gitlab/issues/29404 trait :without_properties_callback do diff --git a/spec/models/project_services/pipelines_email_service_spec.rb b/spec/models/project_services/pipelines_email_service_spec.rb index f29414c80c9efd44fa11a9559594d72f26dde71c..de1edf2099a1590f3d9ef39706d43783ab236fb5 100644 --- a/spec/models/project_services/pipelines_email_service_spec.rb +++ b/spec/models/project_services/pipelines_email_service_spec.rb @@ -37,22 +37,6 @@ end end - describe '#test_data' do - let(:build) { create(:ci_build) } - let(:project) { build.project } - let(:user) { create(:user) } - - before do - project.add_developer(user) - end - - it 'builds test data' do - data = subject.test_data(project, user) - - expect(data[:object_kind]).to eq('pipeline') - end - end - shared_examples 'sending email' do |branches_to_be_notified: nil| before do subject.recipients = recipients diff --git a/spec/services/integrations/test/project_service_spec.rb b/spec/services/integrations/test/project_service_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..fdb43ca345a7ba29e658ae965f1b09a4b9a2424a --- /dev/null +++ b/spec/services/integrations/test/project_service_spec.rb @@ -0,0 +1,195 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Integrations::Test::ProjectService do + let(:user) { double('user') } + + describe '#execute' do + let(:project) { create(:project) } + let(:integration) { create(:slack_service, project: project) } + let(:event) { nil } + let(:sample_data) { { data: 'sample' } } + let(:success_result) { { success: true, result: {} } } + + subject { described_class.new(integration, user, event).execute } + + context 'without event specified' do + it 'tests the integration with default data' do + allow(Gitlab::DataBuilder::Push).to receive(:build_sample).and_return(sample_data) + + expect(integration).to receive(:test).with(sample_data).and_return(success_result) + expect(subject).to eq(success_result) + end + + context 'PipelinesEmailService' do + let(:integration) { create(:pipelines_email_service, project: project) } + + it_behaves_like 'tests for integration with pipeline data' + end + end + + context 'with event specified' do + context 'event not supported by integration' do + let(:integration) { create(:jira_service, project: project) } + let(:event) { 'push' } + + it 'returns error message' do + expect(subject).to include({ status: :error, message: 'Testing not available for this event' }) + end + end + + context 'push' do + let(:event) { 'push' } + + it 'executes integration' do + allow(Gitlab::DataBuilder::Push).to receive(:build_sample).and_return(sample_data) + + expect(integration).to receive(:test).with(sample_data).and_return(success_result) + expect(subject).to eq(success_result) + end + end + + context 'tag_push' do + let(:event) { 'tag_push' } + + it 'executes integration' do + allow(Gitlab::DataBuilder::Push).to receive(:build_sample).and_return(sample_data) + + expect(integration).to receive(:test).with(sample_data).and_return(success_result) + expect(subject).to eq(success_result) + end + end + + context 'note' do + let(:event) { 'note' } + + it 'returns error message if not enough data' do + expect(integration).not_to receive(:test) + expect(subject).to include({ status: :error, message: 'Ensure the project has notes.' }) + end + + it 'executes integration' do + allow(project).to receive(:notes).and_return([Note.new]) + allow(Gitlab::DataBuilder::Note).to receive(:build).and_return(sample_data) + + expect(integration).to receive(:test).with(sample_data).and_return(success_result) + expect(subject).to eq(success_result) + end + end + + context 'issue' do + let(:event) { 'issue' } + let(:issue) { build(:issue) } + + it 'returns error message if not enough data' do + expect(integration).not_to receive(:test) + expect(subject).to include({ status: :error, message: 'Ensure the project has issues.' }) + end + + it 'executes integration' do + allow(project).to receive(:issues).and_return([issue]) + allow(issue).to receive(:to_hook_data).and_return(sample_data) + + expect(integration).to receive(:test).with(sample_data).and_return(success_result) + expect(subject).to eq(success_result) + end + end + + context 'confidential_issue' do + let(:event) { 'confidential_issue' } + let(:issue) { build(:issue) } + + it 'returns error message if not enough data' do + expect(integration).not_to receive(:test) + expect(subject).to include({ status: :error, message: 'Ensure the project has issues.' }) + end + + it 'executes integration' do + allow(project).to receive(:issues).and_return([issue]) + allow(issue).to receive(:to_hook_data).and_return(sample_data) + + expect(integration).to receive(:test).with(sample_data).and_return(success_result) + expect(subject).to eq(success_result) + end + end + + context 'merge_request' do + let(:event) { 'merge_request' } + + it 'returns error message if not enough data' do + expect(integration).not_to receive(:test) + expect(subject).to include({ status: :error, message: 'Ensure the project has merge requests.' }) + end + + it 'executes integration' do + create(:merge_request, source_project: project) + allow_any_instance_of(MergeRequest).to receive(:to_hook_data).and_return(sample_data) + + expect(integration).to receive(:test).with(sample_data).and_return(success_result) + expect(subject).to eq(success_result) + end + end + + context 'deployment' do + let(:project) { create(:project, :test_repo) } + let(:event) { 'deployment' } + + it 'returns error message if not enough data' do + expect(integration).not_to receive(:test) + expect(subject).to include({ status: :error, message: 'Ensure the project has deployments.' }) + end + + it 'executes integration' do + create(:deployment, project: project) + allow(Gitlab::DataBuilder::Deployment).to receive(:build).and_return(sample_data) + + expect(integration).to receive(:test).with(sample_data).and_return(success_result) + expect(subject).to eq(success_result) + end + end + + context 'pipeline' do + let(:event) { 'pipeline' } + + it 'returns error message if not enough data' do + expect(integration).not_to receive(:test) + expect(subject).to include({ status: :error, message: 'Ensure the project has CI pipelines.' }) + end + + it 'executes integration' do + create(:ci_empty_pipeline, project: project) + allow(Gitlab::DataBuilder::Pipeline).to receive(:build).and_return(sample_data) + + expect(integration).to receive(:test).with(sample_data).and_return(success_result) + expect(subject).to eq(success_result) + end + end + + context 'wiki_page' do + let(:project) { create(:project, :wiki_repo) } + let(:event) { 'wiki_page' } + + it 'returns error message if wiki disabled' do + allow(project).to receive(:wiki_enabled?).and_return(false) + + expect(integration).not_to receive(:test) + expect(subject).to include({ status: :error, message: 'Ensure the wiki is enabled and has pages.' }) + end + + it 'returns error message if not enough data' do + expect(integration).not_to receive(:test) + expect(subject).to include({ status: :error, message: 'Ensure the wiki is enabled and has pages.' }) + end + + it 'executes integration' do + create(:wiki_page, wiki: project.wiki) + allow(Gitlab::DataBuilder::WikiPage).to receive(:build).and_return(sample_data) + + expect(integration).to receive(:test).with(sample_data).and_return(success_result) + expect(subject).to eq(success_result) + end + end + end + end +end diff --git a/spec/services/test_hooks/project_service_spec.rb b/spec/services/test_hooks/project_service_spec.rb index 8d30f5018dd36add41de5e179f7ce19600e9d144..3c5bc0d85f2d51d1fa36a3a47a45c88cdb6dbe30 100644 --- a/spec/services/test_hooks/project_service_spec.rb +++ b/spec/services/test_hooks/project_service_spec.rb @@ -31,15 +31,7 @@ let(:trigger) { 'push_events' } let(:trigger_key) { :push_hooks } - it 'returns error message if not enough data' do - allow(project).to receive(:empty_repo?).and_return(true) - - expect(hook).not_to receive(:execute) - expect(service.execute).to include({ status: :error, message: 'Ensure the project has at least one commit.' }) - end - it 'executes hook' do - allow(project).to receive(:empty_repo?).and_return(false) allow(Gitlab::DataBuilder::Push).to receive(:build_sample).and_return(sample_data) expect(hook).to receive(:execute).with(sample_data, trigger_key).and_return(success_result) @@ -51,15 +43,7 @@ let(:trigger) { 'tag_push_events' } let(:trigger_key) { :tag_push_hooks } - it 'returns error message if not enough data' do - allow(project).to receive(:empty_repo?).and_return(true) - - expect(hook).not_to receive(:execute) - expect(service.execute).to include({ status: :error, message: 'Ensure the project has at least one commit.' }) - end - it 'executes hook' do - allow(project).to receive(:empty_repo?).and_return(false) allow(Gitlab::DataBuilder::Push).to receive(:build_sample).and_return(sample_data) expect(hook).to receive(:execute).with(sample_data, trigger_key).and_return(success_result) diff --git a/spec/services/test_hooks/system_service_spec.rb b/spec/services/test_hooks/system_service_spec.rb index 799b57eb04ee4dae7a84bf6746414e32991674f7..8a86b14a2a12874d1ff228160653b0eee4c3a175 100644 --- a/spec/services/test_hooks/system_service_spec.rb +++ b/spec/services/test_hooks/system_service_spec.rb @@ -29,7 +29,6 @@ let(:trigger_key) { :push_hooks } it 'executes hook' do - allow(project).to receive(:empty_repo?).and_return(false) expect(Gitlab::DataBuilder::Push).to receive(:sample_data).and_call_original expect(hook).to receive(:execute).with(Gitlab::DataBuilder::Push::SAMPLE_DATA, trigger_key).and_return(success_result) @@ -55,7 +54,6 @@ let(:trigger_key) { :repository_update_hooks } it 'executes hook' do - allow(project).to receive(:empty_repo?).and_return(false) expect(Gitlab::DataBuilder::Repository).to receive(:sample_data).and_call_original expect(hook).to receive(:execute).with(Gitlab::DataBuilder::Repository::SAMPLE_DATA, trigger_key).and_return(success_result) diff --git a/spec/support/shared_examples/integrations/test_examples.rb b/spec/support/shared_examples/integrations/test_examples.rb new file mode 100644 index 0000000000000000000000000000000000000000..eb2e83ce5d1d124b5fcdeac4af5e8bf8977e3c9b --- /dev/null +++ b/spec/support/shared_examples/integrations/test_examples.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +RSpec.shared_examples 'tests for integration with pipeline data' do + it 'tests the integration with pipeline data' do + create(:ci_empty_pipeline, project: project) + allow(Gitlab::DataBuilder::Pipeline).to receive(:build).and_return(sample_data) + + expect(integration).to receive(:test).with(sample_data).and_return(success_result) + expect(subject).to eq(success_result) + end +end