diff options
author | Achilleas Pipinellis <axilleas@axilleas.me> | 2015-12-25 01:31:02 +0200 |
---|---|---|
committer | Achilleas Pipinellis <axilleas@axilleas.me> | 2015-12-25 01:31:02 +0200 |
commit | d18fd3f6489ad2a8065f8ddd80ca51100eb50b5e (patch) | |
tree | 1e809b9c4f694e20e9cc7dff06a9cb7f2f56aef2 /spec/models | |
parent | e74affcfa84acaddc236d6dfed7be1a61470dc0e (diff) | |
parent | 350d65503f0fa34ae397942578c5ea8b2a46a629 (diff) | |
download | gitlab-ce-ci_triggers_docs.tar.gz |
Merge branch 'master' into ci_triggers_docsci_triggers_docs
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/build_spec.rb | 70 | ||||
-rw-r--r-- | spec/models/concerns/mentionable_spec.rb | 13 | ||||
-rw-r--r-- | spec/models/concerns/token_authenticatable_spec.rb | 23 | ||||
-rw-r--r-- | spec/models/global_milestone_spec.rb | 10 | ||||
-rw-r--r-- | spec/models/jira_issue_spec.rb | 30 | ||||
-rw-r--r-- | spec/models/merge_request_spec.rb | 11 | ||||
-rw-r--r-- | spec/models/note_spec.rb | 4 | ||||
-rw-r--r-- | spec/models/project_services/jira_service_spec.rb | 118 | ||||
-rw-r--r-- | spec/models/project_spec.rb | 24 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 1 |
10 files changed, 291 insertions, 13 deletions
diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb index 96b6f1dbca6..1c22e3cb7c4 100644 --- a/spec/models/build_spec.rb +++ b/spec/models/build_spec.rb @@ -189,6 +189,12 @@ describe Ci::Build, models: true do it { is_expected.to eq(98.29) } end + + context 'using a regex capture' do + subject { build.extract_coverage('TOTAL 9926 3489 65%', 'TOTAL\s+\d+\s+\d+\s+(\d{1,3}\%)') } + + it { is_expected.to eq(65) } + end end describe :variables do @@ -390,4 +396,68 @@ describe Ci::Build, models: true do it { is_expected.to include('gitlab-ci-token') } it { is_expected.to include(project.web_url[7..-1]) } end + + def create_mr(build, commit, factory: :merge_request, created_at: Time.now) + FactoryGirl.create(factory, + source_project_id: commit.gl_project_id, + target_project_id: commit.gl_project_id, + source_branch: build.ref, + created_at: created_at) + end + + describe :merge_request do + context 'when a MR has a reference to the commit' do + before do + @merge_request = create_mr(build, commit, factory: :merge_request) + + commits = [double(id: commit.sha)] + allow(@merge_request).to receive(:commits).and_return(commits) + allow(MergeRequest).to receive_message_chain(:includes, :where, :reorder).and_return([@merge_request]) + end + + it 'returns the single associated MR' do + expect(build.merge_request.id).to eq(@merge_request.id) + end + end + + context 'when there is not a MR referencing the commit' do + it 'returns nil' do + expect(build.merge_request).to be_nil + end + end + + context 'when more than one MR have a reference to the commit' do + before do + @merge_request = create_mr(build, commit, factory: :merge_request) + @merge_request.close! + @merge_request2 = create_mr(build, commit, factory: :merge_request) + + commits = [double(id: commit.sha)] + allow(@merge_request).to receive(:commits).and_return(commits) + allow(@merge_request2).to receive(:commits).and_return(commits) + allow(MergeRequest).to receive_message_chain(:includes, :where, :reorder).and_return([@merge_request, @merge_request2]) + end + + it 'returns the first MR' do + expect(build.merge_request.id).to eq(@merge_request.id) + end + end + + context 'when a Build is created after the MR' do + before do + @merge_request = create_mr(build, commit, factory: :merge_request_with_diffs) + commit2 = FactoryGirl.create :ci_commit, project: project + @build2 = FactoryGirl.create :ci_build, commit: commit2 + + commits = [double(id: commit.sha), double(id: commit2.sha)] + allow(@merge_request).to receive(:commits).and_return(commits) + allow(MergeRequest).to receive_message_chain(:includes, :where, :reorder).and_return([@merge_request]) + end + + it 'returns the current MR' do + expect(@build2.merge_request.id).to eq(@merge_request.id) + end + end + + end end diff --git a/spec/models/concerns/mentionable_spec.rb b/spec/models/concerns/mentionable_spec.rb index 6179882e935..6653621a83e 100644 --- a/spec/models/concerns/mentionable_spec.rb +++ b/spec/models/concerns/mentionable_spec.rb @@ -1,5 +1,18 @@ require 'spec_helper' +describe Mentionable do + include Mentionable + + describe :references do + let(:project) { create(:project) } + + it 'excludes JIRA references' do + allow(project).to receive_messages(jira_tracker?: true) + expect(referenced_mentionables(project, 'JIRA-123')).to be_empty + end + end +end + describe Issue, "Mentionable" do describe '#mentioned_users' do let!(:user) { create(:user, username: 'stranger') } diff --git a/spec/models/concerns/token_authenticatable_spec.rb b/spec/models/concerns/token_authenticatable_spec.rb index a9b0b64e5de..30c0a04b840 100644 --- a/spec/models/concerns/token_authenticatable_spec.rb +++ b/spec/models/concerns/token_authenticatable_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' shared_examples 'TokenAuthenticatable' do describe 'dynamically defined methods' do - it { expect(described_class).to be_private_method_defined(:generate_token_for) } + it { expect(described_class).to be_private_method_defined(:generate_token) } + it { expect(described_class).to be_private_method_defined(:write_new_token) } it { expect(described_class).to respond_to("find_by_#{token_field}") } it { is_expected.to respond_to("ensure_#{token_field}") } it { is_expected.to respond_to("reset_#{token_field}!") } @@ -24,11 +25,11 @@ describe ApplicationSetting, 'TokenAuthenticatable' do it_behaves_like 'TokenAuthenticatable' describe 'generating new token' do - subject { described_class.new } - let(:token) { subject.send(token_field) } - context 'token is not generated yet' do - it { expect(token).to be nil } + describe 'token field accessor' do + subject { described_class.new.send(token_field) } + it { is_expected.to_not be_blank } + end describe 'ensured token' do subject { described_class.new.send("ensure_#{token_field}") } @@ -36,11 +37,21 @@ describe ApplicationSetting, 'TokenAuthenticatable' do it { is_expected.to be_a String } it { is_expected.to_not be_blank } end + + describe 'ensured! token' do + subject { described_class.new.send("ensure_#{token_field}!") } + + it 'should persist new token' do + expect(subject).to eq described_class.current[token_field] + end + end end context 'token is generated' do before { subject.send("reset_#{token_field}!") } - it { expect(token).to be_a String } + it 'persists a new token 'do + expect(subject.send(:read_attribute, token_field)).to be_a String + end end end diff --git a/spec/models/global_milestone_spec.rb b/spec/models/global_milestone_spec.rb index ba03e6aabd0..197c99cd007 100644 --- a/spec/models/global_milestone_spec.rb +++ b/spec/models/global_milestone_spec.rb @@ -62,4 +62,14 @@ describe GlobalMilestone, models: true do expect(@global_milestone.milestones.count).to eq(3) end end + + describe :safe_title do + let(:milestone) { create(:milestone, title: "git / test", project: project1) } + + it 'should strip out slashes and spaces' do + global_milestone = GlobalMilestone.new(milestone.title, [milestone]) + + expect(global_milestone.safe_title).to eq('git-test') + end + end end diff --git a/spec/models/jira_issue_spec.rb b/spec/models/jira_issue_spec.rb new file mode 100644 index 00000000000..1634265b439 --- /dev/null +++ b/spec/models/jira_issue_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +describe JiraIssue do + let(:project) { create(:project) } + subject { JiraIssue.new('JIRA-123', project) } + + describe 'id' do + subject { super().id } + it { is_expected.to eq('JIRA-123') } + end + + describe 'iid' do + subject { super().iid } + it { is_expected.to eq('JIRA-123') } + end + + describe 'to_s' do + subject { super().to_s } + it { is_expected.to eq('JIRA-123') } + end + + describe :== do + specify { expect(subject).to eq(JiraIssue.new('JIRA-123', project)) } + specify { expect(subject).not_to eq(JiraIssue.new('JIRA-124', project)) } + + it 'only compares with JiraIssues' do + expect(subject).not_to eq('JIRA-123') + end + end +end diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb index 1aeba9b2b3b..e0653a8327d 100644 --- a/spec/models/merge_request_spec.rb +++ b/spec/models/merge_request_spec.rb @@ -164,6 +164,17 @@ describe MergeRequest, models: true do expect(subject.closes_issues).to include(issue2) end + + context 'for a project with JIRA integration' do + let(:issue0) { JiraIssue.new('JIRA-123', subject.project) } + let(:issue1) { JiraIssue.new('FOOBAR-4567', subject.project) } + + it 'returns sorted JiraIssues' do + allow(subject.project).to receive_messages(default_branch: subject.target_branch) + + expect(subject.closes_issues).to eq([issue0, issue1]) + end + end end describe "#work_in_progress?" do diff --git a/spec/models/note_spec.rb b/spec/models/note_spec.rb index 216c7dabae0..b7006fa5e68 100644 --- a/spec/models/note_spec.rb +++ b/spec/models/note_spec.rb @@ -164,8 +164,8 @@ describe Note, models: true do let(:issue) { create :issue } it "converts aliases to actual name" do - note = create :note, note: ":thumbsup:", noteable: issue - expect(note.reload.note).to eq("+1") + note = create :note, note: ":+1:", noteable: issue + expect(note.reload.note).to eq("thumbsup") end end end diff --git a/spec/models/project_services/jira_service_spec.rb b/spec/models/project_services/jira_service_spec.rb index 7d91ebe9ce6..2f8193170ae 100644 --- a/spec/models/project_services/jira_service_spec.rb +++ b/spec/models/project_services/jira_service_spec.rb @@ -26,6 +26,113 @@ describe JiraService, models: true do it { is_expected.to have_one :service_hook } end + describe "Execute" do + let(:user) { create(:user) } + let(:project) { create(:project) } + let(:merge_request) { create(:merge_request) } + + before do + @jira_service = JiraService.new + allow(@jira_service).to receive_messages( + project_id: project.id, + project: project, + service_hook: true, + project_url: 'http://jira.example.com', + username: 'gitlab_jira_username', + password: 'gitlab_jira_password' + ) + @jira_service.save # will build API URL, as api_url was not specified above + @sample_data = Gitlab::PushDataBuilder.build_sample(project, user) + # https://github.com/bblimke/webmock#request-with-basic-authentication + @api_url = 'http://gitlab_jira_username:gitlab_jira_password@jira.example.com/rest/api/2/issue/JIRA-123/transitions' + @comment_url = 'http://gitlab_jira_username:gitlab_jira_password@jira.example.com/rest/api/2/issue/JIRA-123/comment' + + WebMock.stub_request(:post, @api_url) + WebMock.stub_request(:post, @comment_url) + end + + it "should call JIRA API" do + @jira_service.execute(merge_request, JiraIssue.new("JIRA-123", project)) + expect(WebMock).to have_requested(:post, @comment_url).with( + body: /Issue solved with/ + ).once + end + + it "calls the api with jira_issue_transition_id" do + @jira_service.jira_issue_transition_id = 'this-is-a-custom-id' + @jira_service.execute(merge_request, JiraIssue.new("JIRA-123", project)) + expect(WebMock).to have_requested(:post, @api_url).with( + body: /this-is-a-custom-id/ + ).once + end + end + + describe "Stored password invalidation" do + let(:project) { create(:project) } + + context "when a password was previously set" do + before do + @jira_service = JiraService.create( + project: create(:project), + properties: { + api_url: 'http://jira.example.com/rest/api/2', + username: 'mic', + password: "password" + } + ) + end + + it "reset password if url changed" do + @jira_service.api_url = 'http://jira_edited.example.com/rest/api/2' + @jira_service.save + expect(@jira_service.password).to be_nil + end + + it "does not reset password if username changed" do + @jira_service.username = "some_name" + @jira_service.save + expect(@jira_service.password).to eq("password") + end + + it "does not reset password if new url is set together with password, even if it's the same password" do + @jira_service.api_url = 'http://jira_edited.example.com/rest/api/2' + @jira_service.password = 'password' + @jira_service.save + expect(@jira_service.password).to eq("password") + expect(@jira_service.api_url).to eq("http://jira_edited.example.com/rest/api/2") + end + + it "should reset password if url changed, even if setter called multiple times" do + @jira_service.api_url = 'http://jira1.example.com/rest/api/2' + @jira_service.api_url = 'http://jira1.example.com/rest/api/2' + @jira_service.save + expect(@jira_service.password).to be_nil + end + end + + context "when no password was previously set" do + before do + @jira_service = JiraService.create( + project: create(:project), + properties: { + api_url: 'http://jira.example.com/rest/api/2', + username: 'mic' + } + ) + end + + it "saves password if new url is set together with password" do + @jira_service.api_url = 'http://jira_edited.example.com/rest/api/2' + @jira_service.password = 'password' + @jira_service.save + expect(@jira_service.password).to eq("password") + expect(@jira_service.api_url).to eq("http://jira_edited.example.com/rest/api/2") + end + + end + end + + describe "Validations" do context "active" do before do @@ -78,11 +185,12 @@ describe JiraService, models: true do context 'when gitlab.yml was initialized' do before do - settings = { "jira" => { - "title" => "Jira", - "project_url" => "http://jira.sample/projects/project_a", - "issues_url" => "http://jira.sample/issues/:id", - "new_issue_url" => "http://jira.sample/projects/project_a/issues/new" + settings = { + "jira" => { + "title" => "Jira", + "project_url" => "http://jira.sample/projects/project_a", + "issues_url" => "http://jira.sample/issues/:id", + "new_issue_url" => "http://jira.sample/projects/project_a/issues/new" } } allow(Gitlab.config).to receive(:issues_tracker).and_return(settings) diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index c4d3813e9c9..400bdf2d962 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -552,4 +552,28 @@ describe Project, models: true do end end end + + describe '#visibility_level_allowed?' do + let(:project) { create :project, visibility_level: Gitlab::VisibilityLevel::INTERNAL } + + context 'when checking on non-forked project' do + it { expect(project.visibility_level_allowed?(Gitlab::VisibilityLevel::PRIVATE)).to be_truthy } + it { expect(project.visibility_level_allowed?(Gitlab::VisibilityLevel::INTERNAL)).to be_truthy } + it { expect(project.visibility_level_allowed?(Gitlab::VisibilityLevel::PUBLIC)).to be_truthy } + end + + context 'when checking on forked project' do + let(:forked_project) { create :forked_project_with_submodules } + + before do + forked_project.build_forked_project_link(forked_to_project_id: forked_project.id, forked_from_project_id: project.id) + forked_project.save + end + + it { expect(forked_project.visibility_level_allowed?(Gitlab::VisibilityLevel::PRIVATE)).to be_truthy } + it { expect(forked_project.visibility_level_allowed?(Gitlab::VisibilityLevel::INTERNAL)).to be_truthy } + it { expect(forked_project.visibility_level_allowed?(Gitlab::VisibilityLevel::PUBLIC)).to be_falsey } + end + + end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 376266c0955..2f184bbaf92 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -26,6 +26,7 @@ # bio :string(255) # failed_attempts :integer default(0) # locked_at :datetime +# unlock_token :string(255) # username :string(255) # can_create_group :boolean default(TRUE), not null # can_create_team :boolean default(TRUE), not null |