From b2ce3643e27db4cc0ad30cc09d651c00ec799887 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Fri, 26 Mar 2021 17:34:57 +0000 Subject: Add latest changes from gitlab-org/gitlab@13-10-stable-ee --- spec/fixtures/rails_sample.bmp | Bin 0 -> 356502 bytes spec/fixtures/rails_sample.png | Bin 0 -> 85809 bytes spec/fixtures/rails_sample.tif | Bin 0 -> 359036 bytes spec/fixtures/sample.ico | Bin 0 -> 4286 bytes spec/lib/gitlab/utils/mime_type_spec.rb | 57 ++++ .../project_services/hipchat_service_spec.rb | 321 +-------------------- .../upload_type_check_shared_context.rb | 3 +- 7 files changed, 60 insertions(+), 321 deletions(-) create mode 100644 spec/fixtures/rails_sample.bmp create mode 100644 spec/fixtures/rails_sample.png create mode 100644 spec/fixtures/rails_sample.tif create mode 100644 spec/fixtures/sample.ico create mode 100644 spec/lib/gitlab/utils/mime_type_spec.rb (limited to 'spec') diff --git a/spec/fixtures/rails_sample.bmp b/spec/fixtures/rails_sample.bmp new file mode 100644 index 00000000000..1871273cf98 Binary files /dev/null and b/spec/fixtures/rails_sample.bmp differ diff --git a/spec/fixtures/rails_sample.png b/spec/fixtures/rails_sample.png new file mode 100644 index 00000000000..d076b02bab2 Binary files /dev/null and b/spec/fixtures/rails_sample.png differ diff --git a/spec/fixtures/rails_sample.tif b/spec/fixtures/rails_sample.tif new file mode 100644 index 00000000000..04b70192ebb Binary files /dev/null and b/spec/fixtures/rails_sample.tif differ diff --git a/spec/fixtures/sample.ico b/spec/fixtures/sample.ico new file mode 100644 index 00000000000..a1fb6e91d65 Binary files /dev/null and b/spec/fixtures/sample.ico differ diff --git a/spec/lib/gitlab/utils/mime_type_spec.rb b/spec/lib/gitlab/utils/mime_type_spec.rb new file mode 100644 index 00000000000..b2b6bd6c4e3 --- /dev/null +++ b/spec/lib/gitlab/utils/mime_type_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require "fast_spec_helper" +require "rspec/parameterized" + +RSpec.describe Gitlab::Utils::MimeType do + describe ".from_io" do + subject { described_class.from_io(io) } + + context "input isn't an IO" do + let(:io) { "test" } + + it "returns nil" do + expect(subject).to be_nil + end + end + + context "input is a file" do + using RSpec::Parameterized::TableSyntax + + where(:fixture, :mime_type) do + "banana_sample.gif" | "image/gif" + "rails_sample.jpg" | "image/jpeg" + "rails_sample.png" | "image/png" + "rails_sample.bmp" | "image/bmp" + "rails_sample.tif" | "image/tiff" + "sample.ico" | "image/vnd.microsoft.icon" + "blockquote_fence_before.md" | "text/plain" + "csv_empty.csv" | "application/x-empty" + end + + with_them do + let(:io) { File.open(File.join(__dir__, "../../../fixtures", fixture)) } + + it { is_expected.to eq(mime_type) } + end + end + end + + describe ".from_string" do + subject { described_class.from_string(str) } + + context "input isn't a string" do + let(:str) { nil } + + it "returns nil" do + expect(subject).to be_nil + end + end + + context "input is a string" do + let(:str) { "plain text" } + + it { is_expected.to eq('text/plain') } + end + end +end diff --git a/spec/models/project_services/hipchat_service_spec.rb b/spec/models/project_services/hipchat_service_spec.rb index 667e6cc85ab..82a4cde752b 100644 --- a/spec/models/project_services/hipchat_service_spec.rb +++ b/spec/models/project_services/hipchat_service_spec.rb @@ -49,307 +49,8 @@ RSpec.describe HipchatService do WebMock.stub_request(:post, api_url) end - it 'tests and return errors' do - allow(hipchat).to receive(:execute).and_raise(StandardError, 'no such room') - result = hipchat.test(push_sample_data) - - expect(result[:success]).to be_falsey - expect(result[:result].to_s).to eq('no such room') - end - - it 'uses v1 if version is provided' do - allow(hipchat).to receive(:api_version).and_return('v1') - expect(HipChat::Client).to receive(:new).with( - token, - api_version: 'v1', - server_url: server_url - ).and_return(double(:hipchat_service).as_null_object) - hipchat.execute(push_sample_data) - end - - it 'uses v2 as the version when nothing is provided' do - allow(hipchat).to receive(:api_version).and_return('') - expect(HipChat::Client).to receive(:new).with( - token, - api_version: 'v2', - server_url: server_url - ).and_return(double(:hipchat_service).as_null_object) - hipchat.execute(push_sample_data) - end - - context 'push events' do - it "calls Hipchat API for push events" do - hipchat.execute(push_sample_data) - - expect(WebMock).to have_requested(:post, api_url).once - end - - it "creates a push message" do - message = hipchat.send(:create_push_message, push_sample_data) - - push_sample_data[:object_attributes] - branch = push_sample_data[:ref].gsub('refs/heads/', '') - expect(message).to include("#{user.name} pushed to branch " \ - "#{branch} of " \ - "#{project_name}") - end - end - - context 'tag_push events' do - let(:push_sample_data) do - Gitlab::DataBuilder::Push.build( - project: project, - user: user, - oldrev: Gitlab::Git::BLANK_SHA, - newrev: '1' * 40, - ref: 'refs/tags/test') - end - - it "calls Hipchat API for tag push events" do - hipchat.execute(push_sample_data) - - expect(WebMock).to have_requested(:post, api_url).once - end - - it "creates a tag push message" do - message = hipchat.send(:create_push_message, push_sample_data) - - push_sample_data[:object_attributes] - expect(message).to eq("#{user.name} pushed new tag " \ - "test to " \ - "#{project_name}\n") - end - end - - context 'issue events' do - let(:issue) { create(:issue, title: 'Awesome issue', description: '**please** fix') } - let(:issue_service) { Issues::CreateService.new(project, user) } - let(:issues_sample_data) { issue_service.hook_data(issue, 'open') } - - it "calls Hipchat API for issue events" do - hipchat.execute(issues_sample_data) - - expect(WebMock).to have_requested(:post, api_url).once - end - - it "creates an issue message" do - message = hipchat.send(:create_issue_message, issues_sample_data) - - obj_attr = issues_sample_data[:object_attributes] - expect(message).to eq("#{user.name} opened " \ - "issue ##{obj_attr["iid"]} in " \ - "#{project_name}: " \ - "Awesome issue" \ - "
please fix
") - end - end - - context 'merge request events' do - let(:merge_request) { create(:merge_request, description: '**please** fix', title: 'Awesome merge request', target_project: project, source_project: project) } - let(:merge_service) { MergeRequests::CreateService.new(project, user) } - let(:merge_sample_data) { merge_service.hook_data(merge_request, 'open') } - - it "calls Hipchat API for merge requests events" do - hipchat.execute(merge_sample_data) - - expect(WebMock).to have_requested(:post, api_url).once - end - - it "creates a merge request message" do - message = hipchat.send(:create_merge_request_message, - merge_sample_data) - - obj_attr = merge_sample_data[:object_attributes] - expect(message).to eq("#{user.name} opened " \ - "merge request !#{obj_attr["iid"]} in " \ - "#{project_name}: " \ - "Awesome merge request" \ - "
please fix
") - end - end - - context "Note events" do - let(:user) { create(:user) } - let(:project) { create(:project, :repository, creator: user) } - - context 'when commit comment event triggered' 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 Hipchat API for commit comment events" do - data = Gitlab::DataBuilder::Note.build(commit_note, user) - hipchat.execute(data) - - expect(WebMock).to have_requested(:post, api_url).once - - message = hipchat.send(:create_message, data) - - obj_attr = data[:object_attributes] - commit_id = Commit.truncate_sha(data[:commit][:id]) - title = hipchat.send(:format_title, data[:commit][:message]) - - expect(message).to eq("#{user.name} commented on " \ - "commit #{commit_id} in " \ - "#{project_name}: " \ - "#{title}" \ - "
a comment on a commit
") - end - end - - context 'when merge request comment event triggered' do - let(:merge_request) do - create(:merge_request, source_project: project, - target_project: project) - end - - let(:merge_request_note) do - create(:note_on_merge_request, noteable: merge_request, - project: project, - note: "merge request **note**") - end - - it "calls Hipchat API for merge request comment events" do - data = Gitlab::DataBuilder::Note.build(merge_request_note, user) - hipchat.execute(data) - - expect(WebMock).to have_requested(:post, api_url).once - - message = hipchat.send(:create_message, data) - - obj_attr = data[:object_attributes] - merge_id = data[:merge_request]['iid'] - title = data[:merge_request]['title'] - - expect(message).to eq("#{user.name} commented on " \ - "merge request !#{merge_id} in " \ - "#{project_name}: " \ - "#{title}" \ - "
merge request note
") - end - end - - context 'when issue comment event triggered' do - let(:issue) { create(:issue, project: project) } - let(:issue_note) do - create(:note_on_issue, noteable: issue, project: project, - note: "issue **note**") - end - - it "calls Hipchat API for issue comment events" do - data = Gitlab::DataBuilder::Note.build(issue_note, user) - hipchat.execute(data) - - message = hipchat.send(:create_message, data) - - obj_attr = data[:object_attributes] - issue_id = data[:issue]['iid'] - title = data[:issue]['title'] - - expect(message).to eq("#{user.name} commented on " \ - "issue ##{issue_id} in " \ - "#{project_name}: " \ - "#{title}" \ - "
issue note
") - end - - context 'with confidential issue' do - before do - issue.update!(confidential: true) - end - - it 'calls Hipchat API with issue comment' do - data = Gitlab::DataBuilder::Note.build(issue_note, user) - hipchat.execute(data) - - message = hipchat.send(:create_message, data) - - expect(message).to include("
issue note
") - end - end - end - - context 'when snippet comment event triggered' do - let(:snippet) { create(:project_snippet, project: project) } - let(:snippet_note) do - create(:note_on_project_snippet, noteable: snippet, - project: project, - note: "snippet note") - end - - it "calls Hipchat API for snippet comment events" do - data = Gitlab::DataBuilder::Note.build(snippet_note, user) - hipchat.execute(data) - - expect(WebMock).to have_requested(:post, api_url).once - - message = hipchat.send(:create_message, data) - - obj_attr = data[:object_attributes] - snippet_id = data[:snippet]['id'] - title = data[:snippet]['title'] - - expect(message).to eq("#{user.name} commented on " \ - "snippet ##{snippet_id} in " \ - "#{project_name}: " \ - "#{title}" \ - "
snippet note
") - end - end - end - - context 'pipeline events' do - let(:pipeline) { create(:ci_empty_pipeline, user: project.owner) } - let(:data) { Gitlab::DataBuilder::Pipeline.build(pipeline) } - - context 'for failed' do - before do - pipeline.drop - end - - it "calls Hipchat API" do - hipchat.execute(data) - - expect(WebMock).to have_requested(:post, api_url).once - end - - it "creates a build message" do - message = hipchat.__send__(:create_pipeline_message, data) - - project_url = project.web_url - project_name = project.full_name.gsub(/\s/, '') - pipeline_attributes = data[:object_attributes] - ref = pipeline_attributes[:ref] - ref_type = pipeline_attributes[:tag] ? 'tag' : 'branch' - duration = pipeline_attributes[:duration] - user_name = data[:user][:name] - - expect(message).to eq("#{project_name}: " \ - "Pipeline ##{pipeline.id} " \ - "of #{ref} #{ref_type} " \ - "by #{user_name} failed in #{duration} second(s)") - end - end - - context 'for succeeded' do - before do - pipeline.succeed - end - - it "calls Hipchat API" do - hipchat.notify_only_broken_pipelines = false - hipchat.execute(data) - expect(WebMock).to have_requested(:post, api_url).once - end - - it "notifies only broken" do - hipchat.notify_only_broken_pipelines = true - hipchat.execute(data) - expect(WebMock).not_to have_requested(:post, api_url).once - end - end + it 'does nothing' do + expect { hipchat.execute(push_sample_data) }.not_to raise_error end describe "#message_options" do @@ -388,22 +89,4 @@ RSpec.describe HipchatService do end end end - - context 'with UrlBlocker' do - let(:user) { create(:user) } - let(:project) { create(:project, :repository) } - let(:hipchat) { create(:hipchat_service, project: project, properties: { room: 'test' }) } - let(:push_sample_data) { Gitlab::DataBuilder::Push.build_sample(project, user) } - - describe '#execute' do - before do - hipchat.server = 'http://localhost:9123' - end - - it 'raises UrlBlocker for localhost' do - expect(Gitlab::UrlBlocker).to receive(:validate!).and_call_original - expect { hipchat.execute(push_sample_data) }.to raise_error(Gitlab::HTTP::BlockedUrlError) - end - end - end end diff --git a/spec/support/shared_contexts/upload_type_check_shared_context.rb b/spec/support/shared_contexts/upload_type_check_shared_context.rb index f168cad961c..5fce31b4a15 100644 --- a/spec/support/shared_contexts/upload_type_check_shared_context.rb +++ b/spec/support/shared_contexts/upload_type_check_shared_context.rb @@ -13,7 +13,6 @@ end # @param mime_type [String] mime type to forcibly detect. RSpec.shared_context 'force content type detection to mime_type' do before do - magic_mime_obj = MimeMagic.new(mime_type) - allow(MimeMagic).to receive(:by_magic).with(anything).and_return(magic_mime_obj) + allow(Gitlab::Utils::MimeType).to receive(:from_io).and_return(mime_type) end end -- cgit v1.2.3