From 572585665f838584b9547831528f26fb60df8d0f Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Mon, 19 Sep 2016 13:54:16 +0800 Subject: Use EmailHelpers where possible, feedback: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/6342#note_15434860 --- spec/support/email_helpers.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/email_helpers.rb b/spec/support/email_helpers.rb index 0bfc4685532..dfb252544e7 100644 --- a/spec/support/email_helpers.rb +++ b/spec/support/email_helpers.rb @@ -1,6 +1,6 @@ module EmailHelpers def sent_to_user?(user) - ActionMailer::Base.deliveries.map(&:to).flatten.count(user.email) == 1 + ActionMailer::Base.deliveries.flat_map(&:to).count(user.email) == 1 end def reset_delivered_emails! @@ -20,4 +20,8 @@ module EmailHelpers def should_not_email(user) expect(sent_to_user?(user)).to be_falsey end + + def should_email_no_one + expect(ActionMailer::Base.deliveries).to be_empty + end end -- cgit v1.2.1 From beb47c257a6673addb44fc6573c176f6be6cbd1b Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Mon, 19 Sep 2016 14:01:58 +0800 Subject: Try to slightly optimize EmailHeleprs --- spec/support/email_helpers.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'spec/support') diff --git a/spec/support/email_helpers.rb b/spec/support/email_helpers.rb index dfb252544e7..2931967de35 100644 --- a/spec/support/email_helpers.rb +++ b/spec/support/email_helpers.rb @@ -1,6 +1,8 @@ module EmailHelpers - def sent_to_user?(user) - ActionMailer::Base.deliveries.flat_map(&:to).count(user.email) == 1 + def sent_to_user?(user, recipients = nil) + recipients ||= ActionMailer::Base.deliveries.flat_map(&:to) + + recipients.count(user.email) == 1 end def reset_delivered_emails! @@ -8,17 +10,19 @@ module EmailHelpers end def should_only_email(*users) - users.each {|user| should_email(user) } recipients = ActionMailer::Base.deliveries.flat_map(&:to) + + users.each { |user| should_email(user, recipients) } + expect(recipients.count).to eq(users.count) end - def should_email(user) - expect(sent_to_user?(user)).to be_truthy + def should_email(user, recipients = nil) + expect(sent_to_user?(user, recipients)).to be_truthy end - def should_not_email(user) - expect(sent_to_user?(user)).to be_falsey + def should_not_email(user, recipients = nil) + expect(sent_to_user?(user, recipients)).to be_falsey end def should_email_no_one -- cgit v1.2.1 From 9622ef64e41058b8dec97c98f568d8fdea95fd61 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Wed, 28 Sep 2016 15:39:54 +0800 Subject: Introduce email_recipients and use include? Feedback: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/6342#note_16075554 https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/6342#note_16075622 --- spec/support/email_helpers.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'spec/support') diff --git a/spec/support/email_helpers.rb b/spec/support/email_helpers.rb index 2931967de35..caa6ab1345a 100644 --- a/spec/support/email_helpers.rb +++ b/spec/support/email_helpers.rb @@ -1,8 +1,6 @@ module EmailHelpers - def sent_to_user?(user, recipients = nil) - recipients ||= ActionMailer::Base.deliveries.flat_map(&:to) - - recipients.count(user.email) == 1 + def sent_to_user?(user, recipients = email_recipients) + recipients.include?(user.email) end def reset_delivered_emails! @@ -10,22 +8,26 @@ module EmailHelpers end def should_only_email(*users) - recipients = ActionMailer::Base.deliveries.flat_map(&:to) + recipients = email_recipients users.each { |user| should_email(user, recipients) } expect(recipients.count).to eq(users.count) end - def should_email(user, recipients = nil) + def should_email(user, recipients = email_recipients) expect(sent_to_user?(user, recipients)).to be_truthy end - def should_not_email(user, recipients = nil) + def should_not_email(user, recipients = email_recipients) expect(sent_to_user?(user, recipients)).to be_falsey end def should_email_no_one expect(ActionMailer::Base.deliveries).to be_empty end + + def email_recipients + ActionMailer::Base.deliveries.flat_map(&:to) + end end -- cgit v1.2.1 From 98217bc0674253f30dea3892a8e833436b89d5b9 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Mon, 17 Oct 2016 18:03:53 +0800 Subject: should_email_no_one -> should_not_email_anyone, feedback: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/6342/diffs#note_17039876 --- spec/support/email_helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/email_helpers.rb b/spec/support/email_helpers.rb index caa6ab1345a..36f5a2d5975 100644 --- a/spec/support/email_helpers.rb +++ b/spec/support/email_helpers.rb @@ -23,7 +23,7 @@ module EmailHelpers expect(sent_to_user?(user, recipients)).to be_falsey end - def should_email_no_one + def should_not_email_anyone expect(ActionMailer::Base.deliveries).to be_empty end -- cgit v1.2.1 From 045c6715330d25adff95304a3de908037c63a163 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Tue, 18 Oct 2016 20:02:35 +0800 Subject: Use bcc for pipeline emails because: We use bcc here because we don't want to generate this emails for a thousand times. This could be potentially expensive in a loop, and recipients would contain all project watchers so it could be a lot. --- spec/support/email_helpers.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'spec/support') diff --git a/spec/support/email_helpers.rb b/spec/support/email_helpers.rb index 36f5a2d5975..a946f10f71a 100644 --- a/spec/support/email_helpers.rb +++ b/spec/support/email_helpers.rb @@ -7,8 +7,8 @@ module EmailHelpers ActionMailer::Base.deliveries.clear end - def should_only_email(*users) - recipients = email_recipients + def should_only_email(*users, kind: :to) + recipients = email_recipients(kind: kind) users.each { |user| should_email(user, recipients) } @@ -27,7 +27,7 @@ module EmailHelpers expect(ActionMailer::Base.deliveries).to be_empty end - def email_recipients - ActionMailer::Base.deliveries.flat_map(&:to) + def email_recipients(kind: :to) + ActionMailer::Base.deliveries.flat_map(&kind) end end -- cgit v1.2.1 From 2c09d2a13d83494f195fda8620e322dff7738dc5 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Fri, 21 Oct 2016 18:22:09 +0800 Subject: Test against notification email --- spec/support/email_helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/email_helpers.rb b/spec/support/email_helpers.rb index a946f10f71a..3e979f2f470 100644 --- a/spec/support/email_helpers.rb +++ b/spec/support/email_helpers.rb @@ -1,6 +1,6 @@ module EmailHelpers def sent_to_user?(user, recipients = email_recipients) - recipients.include?(user.email) + recipients.include?(user.notification_email) end def reset_delivered_emails! -- cgit v1.2.1 From ebc44befdc773b39a171d38dc13c38cd6630828a Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Mon, 7 Nov 2016 20:36:58 +0200 Subject: Fix broken commits search --- spec/support/search_helpers.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 spec/support/search_helpers.rb (limited to 'spec/support') diff --git a/spec/support/search_helpers.rb b/spec/support/search_helpers.rb new file mode 100644 index 00000000000..abbbb636d66 --- /dev/null +++ b/spec/support/search_helpers.rb @@ -0,0 +1,5 @@ +module SearchHelpers + def select_filter(name) + find(:xpath, "//ul[contains(@class, 'search-filter')]//a[contains(.,'#{name}')]").click + end +end -- cgit v1.2.1 From 79d94b167999544086db235602a9213a2d37831e Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Wed, 26 Oct 2016 17:34:06 +0000 Subject: Merge branch '22481-honour-issue-visibility-for-groups' into 'security' Honour issue and merge request visibility in their respective finders This MR fixes a security issue with the IssuesFinder and MergeRequestFinder where they would return items the user did not have permission to see. This was most visible on the issue and merge requests page for a group containing projects that had set their issues or merge requests to "private". Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/22481 See merge request !2000 --- ..._features_apply_to_issuables_shared_examples.rb | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 spec/support/project_features_apply_to_issuables_shared_examples.rb (limited to 'spec/support') diff --git a/spec/support/project_features_apply_to_issuables_shared_examples.rb b/spec/support/project_features_apply_to_issuables_shared_examples.rb new file mode 100644 index 00000000000..4621d17549b --- /dev/null +++ b/spec/support/project_features_apply_to_issuables_shared_examples.rb @@ -0,0 +1,56 @@ +shared_examples 'project features apply to issuables' do |klass| + let(:described_class) { klass } + + let(:group) { create(:group) } + let(:user_in_group) { create(:group_member, :developer, user: create(:user), group: group ).user } + let(:user_outside_group) { create(:user) } + + let(:project) { create(:empty_project, :public, project_args) } + + def project_args + feature = "#{described_class.model_name.plural}_access_level".to_sym + + args = { group: group } + args[feature] = access_level + + args + end + + before do + _ = issuable + login_as(user) + visit path + end + + context 'public access level' do + let(:access_level) { ProjectFeature::ENABLED } + + context 'group member' do + let(:user) { user_in_group } + + it { expect(page).to have_content(issuable.title) } + end + + context 'non-member' do + let(:user) { user_outside_group } + + it { expect(page).to have_content(issuable.title) } + end + end + + context 'private access level' do + let(:access_level) { ProjectFeature::PRIVATE } + + context 'group member' do + let(:user) { user_in_group } + + it { expect(page).to have_content(issuable.title) } + end + + context 'non-member' do + let(:user) { user_outside_group } + + it { expect(page).not_to have_content(issuable.title) } + end + end +end -- cgit v1.2.1 From bf061d0aff091a73611037b811cea2d3380962f4 Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Tue, 1 Nov 2016 20:18:51 +0000 Subject: Merge branch 'issue_23548_dev' into 'master' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit disable markdown in comments when referencing disabled features fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/23548 This MR prevents the following references when tool is disabled: - issues - snippets - commits - when repo is disabled - commit range - when repo is disabled - milestones This MR does not prevent references to repository files, since they are just markdown links and don't leak information. See merge request !2011 Signed-off-by: Rémy Coutable --- spec/support/cycle_analytics_helpers.rb | 3 +- spec/support/reference_parser_shared_examples.rb | 43 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 spec/support/reference_parser_shared_examples.rb (limited to 'spec/support') diff --git a/spec/support/cycle_analytics_helpers.rb b/spec/support/cycle_analytics_helpers.rb index 62a5b46d47b..75c95d70951 100644 --- a/spec/support/cycle_analytics_helpers.rb +++ b/spec/support/cycle_analytics_helpers.rb @@ -49,7 +49,8 @@ module CycleAnalyticsHelpers end def merge_merge_requests_closing_issue(issue) - merge_requests = issue.closed_by_merge_requests + merge_requests = issue.closed_by_merge_requests(user) + merge_requests.each { |merge_request| MergeRequests::MergeService.new(project, user).execute(merge_request) } end diff --git a/spec/support/reference_parser_shared_examples.rb b/spec/support/reference_parser_shared_examples.rb new file mode 100644 index 00000000000..8eb74635a60 --- /dev/null +++ b/spec/support/reference_parser_shared_examples.rb @@ -0,0 +1,43 @@ +RSpec.shared_examples "referenced feature visibility" do |*related_features| + let(:feature_fields) do + related_features.map { |feature| (feature + "_access_level").to_sym } + end + + before { link['data-project'] = project.id.to_s } + + context "when feature is disabled" do + it "does not create reference" do + set_features_fields_to(ProjectFeature::DISABLED) + expect(subject.nodes_visible_to_user(user, [link])).to eq([]) + end + end + + context "when feature is enabled only for team members" do + before { set_features_fields_to(ProjectFeature::PRIVATE) } + + it "does not create reference for non member" do + non_member = create(:user) + + expect(subject.nodes_visible_to_user(non_member, [link])).to eq([]) + end + + it "creates reference for member" do + project.team << [user, :developer] + + expect(subject.nodes_visible_to_user(user, [link])).to eq([link]) + end + end + + context "when feature is enabled" do + # The project is public + it "creates reference" do + set_features_fields_to(ProjectFeature::ENABLED) + + expect(subject.nodes_visible_to_user(user, [link])).to eq([link]) + end + end + + def set_features_fields_to(visibility_level) + feature_fields.each { |field| project.project_feature.update_attribute(field, visibility_level) } + end +end -- cgit v1.2.1 From 3fa265d19547669c60788e38e389fa12bb119235 Mon Sep 17 00:00:00 2001 From: Adam Niedzielski Date: Thu, 10 Nov 2016 18:24:12 +0100 Subject: Fix expanding a collapsed diff when converting a symlink to a regular file In this case comparing old_path and new_path is not enough because there are two entires that match. --- spec/support/test_env.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'spec/support') diff --git a/spec/support/test_env.rb b/spec/support/test_env.rb index 778e665500d..103f7542286 100644 --- a/spec/support/test_env.rb +++ b/spec/support/test_env.rb @@ -23,6 +23,7 @@ module TestEnv 'binary-encoding' => '7b1cf43', 'gitattributes' => '5a62481', 'expand-collapse-diffs' => '4842455', + 'symlink-expand-diff' => '81e6355', 'expand-collapse-files' => '025db92', 'expand-collapse-lines' => '238e82d', 'video' => '8879059', -- cgit v1.2.1 From 0f61bb2dc5298a7e06f8e211adcf46dd282b9c86 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Sat, 27 Aug 2016 20:59:48 -0700 Subject: Fix Error 500 when creating a merge request that contains an image that was deleted and added MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Steps to reproduce: 1. Start with a repo with an image 2. Add a commit to delete the image 3. Add another commit to replace the image with another image In a diff comparison, we really just compare about what the image was before the diff, not the direct parent of the last commit. This MR fixes that. Closes #3893, gitlab-org/gitlab-ee#678 Signed-off-by: Rémy Coutable --- spec/support/test_env.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'spec/support') diff --git a/spec/support/test_env.rb b/spec/support/test_env.rb index 103f7542286..4cf81be3adc 100644 --- a/spec/support/test_env.rb +++ b/spec/support/test_env.rb @@ -35,6 +35,7 @@ module TestEnv 'conflict-missing-side' => 'eb227b3', 'conflict-non-utf8' => 'd0a293c', 'conflict-too-large' => '39fa04f', + 'deleted-image-test' => '6c17798' } # gitlab-test-fork is a fork of gitlab-fork, but we don't necessarily -- cgit v1.2.1 From 6f714dfb4a9b823ab75508f252d06e19e286d5f2 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Wed, 16 Nov 2016 23:10:27 +0100 Subject: Improve code design after code review --- spec/support/matchers/be_url.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 spec/support/matchers/be_url.rb (limited to 'spec/support') diff --git a/spec/support/matchers/be_url.rb b/spec/support/matchers/be_url.rb new file mode 100644 index 00000000000..f8096af1b22 --- /dev/null +++ b/spec/support/matchers/be_url.rb @@ -0,0 +1,5 @@ +RSpec::Matchers.define :be_url do |_| + match do |actual| + URI.parse(actual) rescue false + end +end -- cgit v1.2.1 From 0c052f116c9e093936847280e833ca8985d2d94c Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Fri, 4 Nov 2016 16:19:08 -0200 Subject: Remove default value for `project` argument on subscribable concern --- .../features/issuable_slash_commands_shared_examples.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'spec/support') diff --git a/spec/support/features/issuable_slash_commands_shared_examples.rb b/spec/support/features/issuable_slash_commands_shared_examples.rb index 5e3b8f2b23e..194620d0a68 100644 --- a/spec/support/features/issuable_slash_commands_shared_examples.rb +++ b/spec/support/features/issuable_slash_commands_shared_examples.rb @@ -230,31 +230,31 @@ shared_examples 'issuable record that supports slash commands in its description context "with a note subscribing to the #{issuable_type}" do it "creates a new todo for the #{issuable_type}" do - expect(issuable.subscribed?(master)).to be_falsy + expect(issuable.subscribed?(master, project)).to be_falsy write_note("/subscribe") expect(page).not_to have_content '/subscribe' expect(page).to have_content 'Your commands have been executed!' - expect(issuable.subscribed?(master)).to be_truthy + expect(issuable.subscribed?(master, project)).to be_truthy end end context "with a note unsubscribing to the #{issuable_type} as done" do before do - issuable.subscribe(master) + issuable.subscribe(master, project) end it "creates a new todo for the #{issuable_type}" do - expect(issuable.subscribed?(master)).to be_truthy + expect(issuable.subscribed?(master, project)).to be_truthy write_note("/unsubscribe") expect(page).not_to have_content '/unsubscribe' expect(page).to have_content 'Your commands have been executed!' - expect(issuable.subscribed?(master)).to be_falsy + expect(issuable.subscribed?(master, project)).to be_falsy end end end -- cgit v1.2.1 From f95fa7145b41dbeb0cbaea1e9677fb7dd65a2f04 Mon Sep 17 00:00:00 2001 From: Timothy Andrew Date: Thu, 17 Nov 2016 19:23:42 +0530 Subject: Write a spec covering the race condition during group deletion. - Use multiple threads / database connections to: 1. Escape the transaction the spec seems to be running in (`config.use_transactional_fixtures` is off, but `ActiveRecord::Base.connection.open_transactions` is not empty at the beginning of the spec. 2. Simulate a Sidekiq worker performing the hard delete outside of the soft-delete transaction. - The spec is a little clunky, but it was the smallest thing I could get working - and even this took a couple of hours. Let me know if you have any suggestions to improve it! --- spec/support/database_connection_helpers.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 spec/support/database_connection_helpers.rb (limited to 'spec/support') diff --git a/spec/support/database_connection_helpers.rb b/spec/support/database_connection_helpers.rb new file mode 100644 index 00000000000..763329499f0 --- /dev/null +++ b/spec/support/database_connection_helpers.rb @@ -0,0 +1,9 @@ +module DatabaseConnectionHelpers + def run_with_new_database_connection + pool = ActiveRecord::Base.connection_pool + conn = pool.checkout + yield conn + ensure + pool.checkin(conn) + end +end -- cgit v1.2.1 From 85dd05b5b3afb27743998b4f3f9f042b5b5bfa81 Mon Sep 17 00:00:00 2001 From: Felipe Artur Date: Wed, 9 Nov 2016 19:55:21 -0200 Subject: Add JIRA remotelinks and prevent duplicated closing messages --- spec/support/jira_service_helper.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'spec/support') diff --git a/spec/support/jira_service_helper.rb b/spec/support/jira_service_helper.rb index 96e0dad6b55..7437ba2688d 100644 --- a/spec/support/jira_service_helper.rb +++ b/spec/support/jira_service_helper.rb @@ -57,6 +57,10 @@ module JiraServiceHelper JIRA_API + "/issue/#{issue_id}/comment" end + def jira_api_remote_link_url(issue_id) + JIRA_API + "/issue/#{issue_id}/remotelink" + end + def jira_api_transition_url(issue_id) JIRA_API + "/issue/#{issue_id}/transitions" end @@ -75,6 +79,7 @@ module JiraServiceHelper WebMock.stub_request(:get, jira_issue_url(issue_id)) WebMock.stub_request(:get, jira_api_test_url) WebMock.stub_request(:post, jira_api_comment_url(issue_id)) + WebMock.stub_request(:post, jira_api_remote_link_url(issue_id)) WebMock.stub_request(:post, jira_api_transition_url(issue_id)) end end -- cgit v1.2.1 From d399e1ae2b92bc399962a286fddc59eaa09670a7 Mon Sep 17 00:00:00 2001 From: Felipe Artur Date: Mon, 14 Nov 2016 19:30:01 -0200 Subject: Allow enabling and disabling commit and MR events for JIRA --- spec/support/jira_service_helper.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/jira_service_helper.rb b/spec/support/jira_service_helper.rb index 7437ba2688d..929fc0c5182 100644 --- a/spec/support/jira_service_helper.rb +++ b/spec/support/jira_service_helper.rb @@ -6,7 +6,8 @@ module JiraServiceHelper properties = { title: "JIRA tracker", url: JIRA_URL, - project_key: "JIRA" + project_key: "JIRA", + jira_issue_transition_id: '1' } jira_tracker.update_attributes(properties: properties, active: true) -- cgit v1.2.1 From fd05e26618dd0c123ca476b6f5a3d85f1cfe397a Mon Sep 17 00:00:00 2001 From: Ahmad Sherif Date: Tue, 11 Oct 2016 14:25:17 +0200 Subject: Precalculate user's authorized projects in database Closes #23150 --- spec/support/db_cleaner.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'spec/support') diff --git a/spec/support/db_cleaner.rb b/spec/support/db_cleaner.rb index ac38e31b77e..247f0954221 100644 --- a/spec/support/db_cleaner.rb +++ b/spec/support/db_cleaner.rb @@ -11,6 +11,10 @@ RSpec.configure do |config| DatabaseCleaner.strategy = :truncation end + config.before(:each, truncate: true) do + DatabaseCleaner.strategy = :truncation + end + config.before(:each) do DatabaseCleaner.start end -- cgit v1.2.1 From 78529bbd19252beba7e747f047894033bdc84100 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 23 Nov 2016 09:36:45 +0100 Subject: Clean build storage after each RSpec test example This prevents subsequent test examples to be affected by previous tests that were executed. --- spec/support/setup_builds_storage.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'spec/support') diff --git a/spec/support/setup_builds_storage.rb b/spec/support/setup_builds_storage.rb index a4f21e95338..a0a7120ceb5 100644 --- a/spec/support/setup_builds_storage.rb +++ b/spec/support/setup_builds_storage.rb @@ -1,6 +1,6 @@ RSpec.configure do |config| def builds_path - Rails.root.join('tmp/builds') + Rails.root.join('tmp/tests/builds') end config.before(:each) do @@ -9,7 +9,7 @@ RSpec.configure do |config| Settings.gitlab_ci['builds_path'] = builds_path end - config.after(:suite) do + config.after(:each) do Dir[File.join(builds_path, '*')].each do |path| next if File.basename(path) == '.gitkeep' -- cgit v1.2.1 From d917ac0f864a25265407947f334c4f8ee2f5a768 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 23 Nov 2016 09:51:50 +0100 Subject: Clear Carrierwave upload after each test example --- spec/support/carrierwave.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/carrierwave.rb b/spec/support/carrierwave.rb index aa89afd8fb3..72af2c70324 100644 --- a/spec/support/carrierwave.rb +++ b/spec/support/carrierwave.rb @@ -1,7 +1,7 @@ CarrierWave.root = 'tmp/tests/uploads' RSpec.configure do |config| - config.after(:suite) do + config.after(:each) do FileUtils.rm_rf('tmp/tests/uploads') end end -- cgit v1.2.1 From 01f588edbcf1f0f0453cae1677c9372c76e25775 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 23 Nov 2016 11:52:38 +0100 Subject: Remove entire test files directory for test builds --- spec/support/setup_builds_storage.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'spec/support') diff --git a/spec/support/setup_builds_storage.rb b/spec/support/setup_builds_storage.rb index a0a7120ceb5..fd729434898 100644 --- a/spec/support/setup_builds_storage.rb +++ b/spec/support/setup_builds_storage.rb @@ -3,17 +3,15 @@ RSpec.configure do |config| Rails.root.join('tmp/tests/builds') end + config.before(:suite) do + Settings.gitlab_ci['builds_path'] = builds_path + end + config.before(:each) do FileUtils.mkdir_p(builds_path) - FileUtils.touch(File.join(builds_path, ".gitkeep")) - Settings.gitlab_ci['builds_path'] = builds_path end config.after(:each) do - Dir[File.join(builds_path, '*')].each do |path| - next if File.basename(path) == '.gitkeep' - - FileUtils.rm_rf(path) - end + FileUtils.rm_rf(builds_path) end end -- cgit v1.2.1 From 71ad3d294ec6ddfb36346e19e6b50ec5eb8d4ef2 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 23 Nov 2016 13:27:45 +0100 Subject: Clear test build storage directory before each example --- spec/support/setup_builds_storage.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'spec/support') diff --git a/spec/support/setup_builds_storage.rb b/spec/support/setup_builds_storage.rb index fd729434898..2e7c88bfc09 100644 --- a/spec/support/setup_builds_storage.rb +++ b/spec/support/setup_builds_storage.rb @@ -7,11 +7,12 @@ RSpec.configure do |config| Settings.gitlab_ci['builds_path'] = builds_path end - config.before(:each) do + config.before(:all) do FileUtils.mkdir_p(builds_path) end - config.after(:each) do + config.before(:each) do FileUtils.rm_rf(builds_path) + FileUtils.mkdir_p(builds_path) end end -- cgit v1.2.1 From 918bc207c680bed46e82bef996aea027ac72b38d Mon Sep 17 00:00:00 2001 From: winniehell Date: Sat, 19 Nov 2016 22:59:32 +0100 Subject: Use Rails test host name for frontend fixtures --- spec/support/javascript_fixtures_helpers.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/javascript_fixtures_helpers.rb b/spec/support/javascript_fixtures_helpers.rb index adc3f48b434..7e066aa115d 100644 --- a/spec/support/javascript_fixtures_helpers.rb +++ b/spec/support/javascript_fixtures_helpers.rb @@ -1,3 +1,4 @@ +require 'action_dispatch/testing/test_request' require 'fileutils' require 'gitlab/popen' @@ -36,7 +37,8 @@ module JavaScriptFixturesHelpers fixture = doc.to_html # replace relative links - fixture.gsub!(%r{="/}, '="https://fixture.invalid/') + test_host = ActionDispatch::TestRequest::DEFAULT_ENV['HTTP_HOST'] + fixture.gsub!(%r{="/}, "=\"http://#{test_host}/") end FileUtils.mkdir_p(File.dirname(fixture_file_name)) -- cgit v1.2.1 From d9fe5c259d8880fcb9f8c1cc3322836d4d35ef93 Mon Sep 17 00:00:00 2001 From: winniehell Date: Wed, 23 Nov 2016 21:36:34 +0100 Subject: Strip tags from fixtures to ignore CSS --- spec/support/javascript_fixtures_helpers.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'spec/support') diff --git a/spec/support/javascript_fixtures_helpers.rb b/spec/support/javascript_fixtures_helpers.rb index 7e066aa115d..99e98eebdb4 100644 --- a/spec/support/javascript_fixtures_helpers.rb +++ b/spec/support/javascript_fixtures_helpers.rb @@ -31,6 +31,9 @@ module JavaScriptFixturesHelpers if response_mime_type.html? doc = Nokogiri::HTML::DocumentFragment.parse(fixture) + link_tags = doc.css('link') + link_tags.remove + scripts = doc.css('script') scripts.remove -- cgit v1.2.1 From 1e66f35c560b5a0067851cabb792fe23281cfd51 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Mon, 28 Nov 2016 16:50:08 +0800 Subject: Pass `--load-images=no` to PhantomJS via Capybara/Poltergeist We were unintentionally hitting `gravatar.com` whenever a test that used Poltergeist was run. This was certainly wasting their resources and slowing down our tests even further, for no reason. --- spec/support/capybara.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb index e1f90e17cce..16d5f2bf0b8 100644 --- a/spec/support/capybara.rb +++ b/spec/support/capybara.rb @@ -7,7 +7,15 @@ timeout = (ENV['CI'] || ENV['CI_SERVER']) ? 90 : 10 Capybara.javascript_driver = :poltergeist Capybara.register_driver :poltergeist do |app| - Capybara::Poltergeist::Driver.new(app, js_errors: true, timeout: timeout, window_size: [1366, 768]) + Capybara::Poltergeist::Driver.new( + app, + js_errors: true, + timeout: timeout, + window_size: [1366, 768], + phantomjs_options: [ + '--load-images=no' + ] + ) end Capybara.default_max_wait_time = timeout -- cgit v1.2.1 From 13ad9a745a392e0bf0cedd0e1f318c1acee9b969 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Mon, 28 Nov 2016 13:08:14 +0800 Subject: Speed up Project security access specs Prior, every single test was creating four `ProjectMember` objects, each of which created one `User` record, even though each test only used _one_ of those Users, if any. Now each test only creates the single user record it needs, if it needs one. This shaves minutes off of each spec file changed here. --- spec/support/matchers/access_matchers.rb | 35 +++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'spec/support') diff --git a/spec/support/matchers/access_matchers.rb b/spec/support/matchers/access_matchers.rb index 0497e391860..691d7e05f57 100644 --- a/spec/support/matchers/access_matchers.rb +++ b/spec/support/matchers/access_matchers.rb @@ -7,7 +7,7 @@ module AccessMatchers extend RSpec::Matchers::DSL include Warden::Test::Helpers - def emulate_user(user) + def emulate_user(user, project = nil) case user when :user login_as(create(:user)) @@ -18,6 +18,18 @@ module AccessMatchers when :external login_as(create(:user, external: true)) when User + login_as(user) + when :owner + raise ArgumentError, "cannot emulate owner without project" unless project + + login_as(project.owner) + when *Gitlab::Access.sym_options.keys + raise ArgumentError, "cannot emulate user #{user} without project" unless project + + role = user + user = create(:user) + project.public_send(:"add_#{role}", user) + login_as(user) else raise ArgumentError, "cannot emulate user #{user}" @@ -26,8 +38,7 @@ module AccessMatchers def description_for(user, type) if user.kind_of?(User) - # User#inspect displays too much information for RSpec's description - # messages + # User#inspect displays too much information for RSpec's descriptions "be #{type} for the specified user" else "be #{type} for #{user}" @@ -36,21 +47,31 @@ module AccessMatchers matcher :be_allowed_for do |user| match do |url| - emulate_user(user) - visit url + emulate_user(user, @project) + visit(url) + status_code != 404 && current_path != new_user_session_path end + chain :of do |project| + @project = project + end + description { description_for(user, 'allowed') } end matcher :be_denied_for do |user| match do |url| - emulate_user(user) - visit url + emulate_user(user, @project) + visit(url) + status_code == 404 || current_path == new_user_session_path end + chain :of do |project| + @project = project + end + description { description_for(user, 'denied') } end end -- cgit v1.2.1 From f7351b040b0b0737372d18201ddc955942e0e016 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Tue, 29 Nov 2016 15:08:11 +0800 Subject: Speed up Group security access specs This is the Group equivalent of 13ad9a745a392e0bf0cedd0e1f318c1acee9b969 --- spec/support/matchers/access_matchers.rb | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'spec/support') diff --git a/spec/support/matchers/access_matchers.rb b/spec/support/matchers/access_matchers.rb index 691d7e05f57..ceddb656596 100644 --- a/spec/support/matchers/access_matchers.rb +++ b/spec/support/matchers/access_matchers.rb @@ -7,7 +7,7 @@ module AccessMatchers extend RSpec::Matchers::DSL include Warden::Test::Helpers - def emulate_user(user, project = nil) + def emulate_user(user, membership = nil) case user when :user login_as(create(:user)) @@ -19,16 +19,17 @@ module AccessMatchers login_as(create(:user, external: true)) when User login_as(user) - when :owner - raise ArgumentError, "cannot emulate owner without project" unless project - - login_as(project.owner) - when *Gitlab::Access.sym_options.keys - raise ArgumentError, "cannot emulate user #{user} without project" unless project + when *Gitlab::Access.sym_options_with_owner.keys + raise ArgumentError, "cannot emulate #{user} without membership parent" unless membership role = user - user = create(:user) - project.public_send(:"add_#{role}", user) + + if role == :owner && membership.owner + user = membership.owner + else + user = create(:user) + membership.public_send(:"add_#{role}", user) + end login_as(user) else @@ -47,14 +48,14 @@ module AccessMatchers matcher :be_allowed_for do |user| match do |url| - emulate_user(user, @project) + emulate_user(user, @membership) visit(url) status_code != 404 && current_path != new_user_session_path end - chain :of do |project| - @project = project + chain :of do |membership| + @membership = membership end description { description_for(user, 'allowed') } @@ -62,14 +63,14 @@ module AccessMatchers matcher :be_denied_for do |user| match do |url| - emulate_user(user, @project) + emulate_user(user, @membership) visit(url) status_code == 404 || current_path == new_user_session_path end - chain :of do |project| - @project = project + chain :of do |membership| + @membership = membership end description { description_for(user, 'denied') } -- cgit v1.2.1 From fbbf177e3b604bebce3b10f8eea8920ff5606fca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Wed, 28 Sep 2016 12:45:46 +0200 Subject: New `gitlab:workhorse:install` rake task MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- spec/support/stub_configuration.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'spec/support') diff --git a/spec/support/stub_configuration.rb b/spec/support/stub_configuration.rb index f40ee862df8..c0847bbd7f1 100644 --- a/spec/support/stub_configuration.rb +++ b/spec/support/stub_configuration.rb @@ -17,6 +17,10 @@ module StubConfiguration allow(Gitlab.config.gravatar).to receive_messages(messages) end + def stub_gitlab_workhorse_setting(messages) + allow(Gitlab.config.gitlab_workhorse).to receive_messages(messages) + end + def stub_incoming_email_setting(messages) allow(Gitlab.config.incoming_email).to receive_messages(messages) end -- cgit v1.2.1 From b193e8497444a19e4ea541f73f82eb7e21aa8879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Tue, 29 Nov 2016 19:21:25 +0100 Subject: Move task helpers to a module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- spec/support/rake_helpers.rb | 4 ++-- spec/support/stub_configuration.rb | 4 ---- 2 files changed, 2 insertions(+), 6 deletions(-) (limited to 'spec/support') diff --git a/spec/support/rake_helpers.rb b/spec/support/rake_helpers.rb index 52d80c69835..4a8158ed79b 100644 --- a/spec/support/rake_helpers.rb +++ b/spec/support/rake_helpers.rb @@ -1,7 +1,7 @@ module RakeHelpers - def run_rake_task(task_name) + def run_rake_task(task_name, *args) Rake::Task[task_name].reenable - Rake.application.invoke_task task_name + Rake.application.invoke_task("#{task_name}[#{args.join(',')}]") end def stub_warn_user_is_not_gitlab diff --git a/spec/support/stub_configuration.rb b/spec/support/stub_configuration.rb index c0847bbd7f1..f40ee862df8 100644 --- a/spec/support/stub_configuration.rb +++ b/spec/support/stub_configuration.rb @@ -17,10 +17,6 @@ module StubConfiguration allow(Gitlab.config.gravatar).to receive_messages(messages) end - def stub_gitlab_workhorse_setting(messages) - allow(Gitlab.config.gitlab_workhorse).to receive_messages(messages) - end - def stub_incoming_email_setting(messages) allow(Gitlab.config.incoming_email).to receive_messages(messages) end -- cgit v1.2.1 From 567b8a99fe0e454a6d2c0b79cde86ee2dea806f3 Mon Sep 17 00:00:00 2001 From: Mike Greiling Date: Mon, 21 Nov 2016 13:48:29 -0600 Subject: update gitlab flavored markdown tests to reflect namespaced ids --- spec/support/matchers/markdown_matchers.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'spec/support') diff --git a/spec/support/matchers/markdown_matchers.rb b/spec/support/matchers/markdown_matchers.rb index 8c98b1f988c..a44bd2601c1 100644 --- a/spec/support/matchers/markdown_matchers.rb +++ b/spec/support/matchers/markdown_matchers.rb @@ -38,9 +38,9 @@ module MarkdownMatchers set_default_markdown_messages match do |actual| - expect(actual).to have_selector('h1 a#gitlab-markdown') - expect(actual).to have_selector('h2 a#markdown') - expect(actual).to have_selector('h3 a#autolinkfilter') + expect(actual).to have_selector('h1 a#user-content_gitlab-markdown') + expect(actual).to have_selector('h2 a#user-content_markdown') + expect(actual).to have_selector('h3 a#user-content_autolinkfilter') end end -- cgit v1.2.1 From 131a04d7962b01daa58b8e5efe3f1359a3e73fe1 Mon Sep 17 00:00:00 2001 From: Mike Greiling Date: Tue, 29 Nov 2016 13:07:38 -0600 Subject: remove underscore from user-content id namespace --- spec/support/matchers/markdown_matchers.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'spec/support') diff --git a/spec/support/matchers/markdown_matchers.rb b/spec/support/matchers/markdown_matchers.rb index a44bd2601c1..97b8b342eb2 100644 --- a/spec/support/matchers/markdown_matchers.rb +++ b/spec/support/matchers/markdown_matchers.rb @@ -38,9 +38,9 @@ module MarkdownMatchers set_default_markdown_messages match do |actual| - expect(actual).to have_selector('h1 a#user-content_gitlab-markdown') - expect(actual).to have_selector('h2 a#user-content_markdown') - expect(actual).to have_selector('h3 a#user-content_autolinkfilter') + expect(actual).to have_selector('h1 a#user-content-gitlab-markdown') + expect(actual).to have_selector('h2 a#user-content-markdown') + expect(actual).to have_selector('h3 a#user-content-autolinkfilter') end end -- cgit v1.2.1 From a610e0dcfc178ad31bcf2392e0d103e2c4ef8407 Mon Sep 17 00:00:00 2001 From: Sean McGivern Date: Mon, 28 Nov 2016 16:48:10 +0000 Subject: Fix have_issuable_counts matcher on failure --- spec/support/matchers/have_issuable_counts.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'spec/support') diff --git a/spec/support/matchers/have_issuable_counts.rb b/spec/support/matchers/have_issuable_counts.rb index 02605d6b70e..92cf3de5448 100644 --- a/spec/support/matchers/have_issuable_counts.rb +++ b/spec/support/matchers/have_issuable_counts.rb @@ -1,9 +1,9 @@ RSpec::Matchers.define :have_issuable_counts do |opts| - match do |actual| - expected_counts = opts.map do |state, count| - "#{state.to_s.humanize} #{count}" - end + expected_counts = opts.map do |state, count| + "#{state.to_s.humanize} #{count}" + end + match do |actual| actual.within '.issues-state-filters' do expected_counts.each do |expected_count| expect(actual).to have_content(expected_count) -- cgit v1.2.1 From 4e249d5baea99a52915025fc2827d01ab2d77c18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Fri, 2 Dec 2016 13:54:57 +0100 Subject: Use :maximum instead of :within for length validators with a 0..N range MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- spec/support/matchers/is_within.rb | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 spec/support/matchers/is_within.rb (limited to 'spec/support') diff --git a/spec/support/matchers/is_within.rb b/spec/support/matchers/is_within.rb deleted file mode 100644 index 0c35fc7e899..00000000000 --- a/spec/support/matchers/is_within.rb +++ /dev/null @@ -1,9 +0,0 @@ -# Extend shoulda-matchers -module Shoulda::Matchers::ActiveModel - class ValidateLengthOfMatcher - # Shortcut for is_at_least and is_at_most - def is_within(range) - is_at_least(range.min) && is_at_most(range.max) - end - end -end -- cgit v1.2.1 From 593c912151eaef865d31fc2a8307ef6d337c2349 Mon Sep 17 00:00:00 2001 From: Robert Schilling Date: Mon, 5 Dec 2016 15:40:53 +0100 Subject: Grapify the service API --- spec/support/services_shared_context.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'spec/support') diff --git a/spec/support/services_shared_context.rb b/spec/support/services_shared_context.rb index d1c999cad4d..66c93890e31 100644 --- a/spec/support/services_shared_context.rb +++ b/spec/support/services_shared_context.rb @@ -16,8 +16,14 @@ Service.available_services_names.each do |service| hash.merge!(k => 'secrettoken') elsif k =~ /^(.*_url|url|webhook)/ hash.merge!(k => "http://example.com") + elsif service_klass.method_defined?("#{k}?") + hash.merge!(k => true) elsif service == 'irker' && k == :recipients hash.merge!(k => 'irc://irc.network.net:666/#channel') + elsif service == 'irker' && k == :server_port + hash.merge!(k => 1234) + elsif service == 'jira' && k == :jira_issue_transition_id + hash.merge!(k => 1234) else hash.merge!(k => "someword") end -- cgit v1.2.1 From 3c36d9dc9b3c2db45df6dce19357e9c4bdde366f Mon Sep 17 00:00:00 2001 From: jnoortheen Date: Tue, 6 Dec 2016 22:32:30 +0530 Subject: fix: removed signed_out notification test: replaced signed_out message check with check for sign_in button fixes #25294 --- spec/support/login_helpers.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'spec/support') diff --git a/spec/support/login_helpers.rb b/spec/support/login_helpers.rb index c0b3e83244d..ad1eed5b369 100644 --- a/spec/support/login_helpers.rb +++ b/spec/support/login_helpers.rb @@ -75,7 +75,8 @@ module LoginHelpers def logout find(".header-user-dropdown-toggle").click click_link "Sign out" - expect(page).to have_content('Signed out successfully') + # check the sign_in button + expect(page).to have_button('Sign in') end # Logout without JavaScript driver -- cgit v1.2.1 From 6731ab5d76c34462f0b4424ff03c9646ad916b76 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Thu, 18 Aug 2016 16:31:44 +0200 Subject: Add Gitlab::Middleware::Multipart --- spec/support/upload_helpers.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 spec/support/upload_helpers.rb (limited to 'spec/support') diff --git a/spec/support/upload_helpers.rb b/spec/support/upload_helpers.rb new file mode 100644 index 00000000000..5eead80c935 --- /dev/null +++ b/spec/support/upload_helpers.rb @@ -0,0 +1,16 @@ +require 'fileutils' + +module UploadHelpers + extend self + + def uploaded_image_temp_path + basename = 'banana_sample.gif' + orig_path = File.join(Rails.root, 'spec', 'fixtures', basename) + tmp_path = File.join(Rails.root, 'tmp', 'tests', basename) + # Because we use 'move_to_store' on all uploaders, we create a new + # tempfile on each call: the file we return here will be renamed in most + # cases. + FileUtils.copy(orig_path, tmp_path) + tmp_path + end +end -- cgit v1.2.1 From 141faaacf9119ce5d765efe73c6509030ba078cd Mon Sep 17 00:00:00 2001 From: Felipe Artur Date: Fri, 25 Nov 2016 17:36:37 -0200 Subject: Mattermost Notifications Service --- spec/support/slack_mattermost_shared_examples.rb | 328 +++++++++++++++++++++++ 1 file changed, 328 insertions(+) create mode 100644 spec/support/slack_mattermost_shared_examples.rb (limited to 'spec/support') diff --git a/spec/support/slack_mattermost_shared_examples.rb b/spec/support/slack_mattermost_shared_examples.rb new file mode 100644 index 00000000000..56d4965f74d --- /dev/null +++ b/spec/support/slack_mattermost_shared_examples.rb @@ -0,0 +1,328 @@ +Dir[Rails.root.join("app/models/project_services/chat_message/*.rb")].each { |f| require f } + +RSpec.shared_examples 'slack or mattermost' do + let(:chat_service) { described_class.new } + let(:webhook_url) { 'https://example.gitlab.com/' } + + describe "Associations" do + it { is_expected.to belong_to :project } + it { is_expected.to have_one :service_hook } + end + + describe 'Validations' do + context 'when service is active' do + before { subject.active = true } + + it { is_expected.to validate_presence_of(:webhook) } + it_behaves_like 'issue tracker service URL attribute', :webhook + end + + context 'when service is inactive' do + before { subject.active = false } + + it { is_expected.not_to validate_presence_of(:webhook) } + end + end + + describe "#execute" do + let(:user) { create(:user) } + let(:project) { create(:project) } + let(:username) { 'slack_username' } + let(:channel) { 'slack_channel' } + + let(:push_sample_data) do + Gitlab::DataBuilder::Push.build_sample(project, user) + end + + before do + allow(chat_service).to receive_messages( + project: project, + project_id: project.id, + service_hook: true, + webhook: webhook_url + ) + + WebMock.stub_request(:post, webhook_url) + + opts = { + title: 'Awesome issue', + description: 'please fix' + } + + issue_service = Issues::CreateService.new(project, user, opts) + @issue = issue_service.execute + @issues_sample_data = issue_service.hook_data(@issue, 'open') + + opts = { + title: 'Awesome merge_request', + description: 'please fix', + source_branch: 'feature', + target_branch: 'master' + } + merge_service = MergeRequests::CreateService.new(project, + user, opts) + @merge_request = merge_service.execute + @merge_sample_data = merge_service.hook_data(@merge_request, + 'open') + + opts = { + title: "Awesome wiki_page", + content: "Some text describing some thing or another", + format: "md", + message: "user created page: Awesome wiki_page" + } + + wiki_page_service = WikiPages::CreateService.new(project, user, opts) + @wiki_page = wiki_page_service.execute + @wiki_page_sample_data = wiki_page_service.hook_data(@wiki_page, 'create') + end + + it "calls Slack/Mattermost API for push events" do + chat_service.execute(push_sample_data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + + it "calls Slack/Mattermost API for issue events" do + chat_service.execute(@issues_sample_data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + + it "calls Slack/Mattermost API for merge requests events" do + chat_service.execute(@merge_sample_data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + + it "calls Slack/Mattermost API for wiki page events" do + chat_service.execute(@wiki_page_sample_data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + + it 'uses the username as an option for slack when configured' do + allow(chat_service).to receive(:username).and_return(username) + + expect(Slack::Notifier).to receive(:new). + with(webhook_url, username: username, channel: chat_service.default_channel). + and_return( + double(:slack_service).as_null_object + ) + + chat_service.execute(push_sample_data) + end + + it 'uses the channel as an option when it is configured' do + allow(chat_service).to receive(:channel).and_return(channel) + expect(Slack::Notifier).to receive(:new). + with(webhook_url, channel: channel). + and_return( + double(:slack_service).as_null_object + ) + chat_service.execute(push_sample_data) + end + + context "event channels" do + it "uses the right channel for push event" do + chat_service.update_attributes(push_channel: "random") + + expect(Slack::Notifier).to receive(:new). + with(webhook_url, channel: "random"). + and_return( + double(:slack_service).as_null_object + ) + + chat_service.execute(push_sample_data) + end + + it "uses the right channel for merge request event" do + chat_service.update_attributes(merge_request_channel: "random") + + expect(Slack::Notifier).to receive(:new). + with(webhook_url, channel: "random"). + and_return( + double(:slack_service).as_null_object + ) + + chat_service.execute(@merge_sample_data) + end + + it "uses the right channel for issue event" do + chat_service.update_attributes(issue_channel: "random") + + expect(Slack::Notifier).to receive(:new). + with(webhook_url, channel: "random"). + and_return( + double(:slack_service).as_null_object + ) + + chat_service.execute(@issues_sample_data) + end + + it "uses the right channel for wiki event" do + chat_service.update_attributes(wiki_page_channel: "random") + + expect(Slack::Notifier).to receive(:new). + with(webhook_url, channel: "random"). + and_return( + double(:slack_service).as_null_object + ) + + chat_service.execute(@wiki_page_sample_data) + end + + context "note event" do + let(:issue_note) do + create(:note_on_issue, project: project, note: "issue note") + end + + it "uses the right channel" do + chat_service.update_attributes(note_channel: "random") + + note_data = Gitlab::DataBuilder::Note.build(issue_note, user) + + expect(Slack::Notifier).to receive(:new). + with(webhook_url, channel: "random"). + and_return( + double(:slack_service).as_null_object + ) + + chat_service.execute(note_data) + end + end + end + end + + describe "Note events" do + let(:user) { create(:user) } + let(:project) { create(:project, creator_id: user.id) } + + before do + allow(chat_service).to receive_messages( + project: project, + project_id: project.id, + service_hook: true, + webhook: webhook_url + ) + + WebMock.stub_request(:post, webhook_url) + end + + context 'when commit comment event executed' do + let(:commit_note) do + create(:note_on_commit, author: user, + project: project, + commit_id: project.repository.commit.id, + note: 'a comment on a commit') + end + + it "calls Slack/Mattermost API for commit comment events" do + data = Gitlab::DataBuilder::Note.build(commit_note, user) + chat_service.execute(data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + end + + context 'when merge request comment event executed' do + let(:merge_request_note) do + create(:note_on_merge_request, project: project, + note: "merge request note") + end + + it "calls Slack API for merge request comment events" do + data = Gitlab::DataBuilder::Note.build(merge_request_note, user) + chat_service.execute(data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + end + + context 'when issue comment event executed' do + let(:issue_note) do + create(:note_on_issue, project: project, note: "issue note") + end + + it "calls Slack API for issue comment events" do + data = Gitlab::DataBuilder::Note.build(issue_note, user) + chat_service.execute(data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + end + + context 'when snippet comment event executed' do + let(:snippet_note) do + create(:note_on_project_snippet, project: project, + note: "snippet note") + end + + it "calls Slack API for snippet comment events" do + data = Gitlab::DataBuilder::Note.build(snippet_note, user) + chat_service.execute(data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + end + end + + describe 'Pipeline events' do + let(:user) { create(:user) } + let(:project) { create(:project) } + + let(:pipeline) do + create(:ci_pipeline, + project: project, status: status, + sha: project.commit.sha, ref: project.default_branch) + end + + before do + allow(chat_service).to receive_messages( + project: project, + service_hook: true, + webhook: webhook_url + ) + end + + shared_examples 'call Slack/Mattermost API' do + before do + WebMock.stub_request(:post, webhook_url) + end + + it 'calls Slack/Mattermost API for pipeline events' do + data = Gitlab::DataBuilder::Pipeline.build(pipeline) + chat_service.execute(data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + end + + context 'with failed pipeline' do + let(:status) { 'failed' } + + it_behaves_like 'call Slack/Mattermost API' + end + + context 'with succeeded pipeline' do + let(:status) { 'success' } + + context 'with default to notify_only_broken_pipelines' do + it 'does not call Slack/Mattermost API for pipeline events' do + data = Gitlab::DataBuilder::Pipeline.build(pipeline) + result = chat_service.execute(data) + + expect(result).to be_falsy + end + end + + context 'with setting notify_only_broken_pipelines to false' do + before do + chat_service.notify_only_broken_pipelines = false + end + + it_behaves_like 'call Slack/Mattermost API' + end + end + end +end -- cgit v1.2.1 From 9f97fa4d9f4e86e8f1ff1db4621bcf81390936da Mon Sep 17 00:00:00 2001 From: Mark Fletcher Date: Wed, 14 Dec 2016 20:45:39 +0000 Subject: Ensure issuable state changes only fire webhooks once * Webhooks for close and reopen events now fired in respective services only * Prevents generic 'update' webhooks firing too --- .../services/issuable_update_service_shared_examples.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 spec/support/services/issuable_update_service_shared_examples.rb (limited to 'spec/support') diff --git a/spec/support/services/issuable_update_service_shared_examples.rb b/spec/support/services/issuable_update_service_shared_examples.rb new file mode 100644 index 00000000000..a3336755773 --- /dev/null +++ b/spec/support/services/issuable_update_service_shared_examples.rb @@ -0,0 +1,17 @@ +shared_examples 'issuable update service' do + context 'changing state' do + before { expect(project).to receive(:execute_hooks).once } + + context 'to reopened' do + it 'executes hooks only once' do + described_class.new(project, user, state_event: 'reopen').execute(closed_issuable) + end + end + + context 'to closed' do + it 'executes hooks only once' do + described_class.new(project, user, state_event: 'close').execute(open_issuable) + end + end + end +end -- cgit v1.2.1