jira_service_spec.rb 7.4 KB
Newer Older
1 2
require 'spec_helper'

D
Douwe Maan 已提交
3
describe JiraService, models: true do
4
  describe "Associations" do
5 6
    it { is_expected.to belong_to :project }
    it { is_expected.to have_one :service_hook }
7 8
  end

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
  describe 'Validations' do
    context 'when service is active' do
      before { subject.active = true }

      it { is_expected.to validate_presence_of(:api_url) }
      it { is_expected.to validate_presence_of(:project_url) }
      it { is_expected.to validate_presence_of(:issues_url) }
      it { is_expected.to validate_presence_of(:new_issue_url) }
      it_behaves_like 'issue tracker service URL attribute', :api_url
      it_behaves_like 'issue tracker service URL attribute', :project_url
      it_behaves_like 'issue tracker service URL attribute', :issues_url
      it_behaves_like 'issue tracker service URL attribute', :new_issue_url
    end

    context 'when service is inactive' do
      before { subject.active = false }

      it { is_expected.not_to validate_presence_of(:api_url) }
      it { is_expected.not_to validate_presence_of(:project_url) }
      it { is_expected.not_to validate_presence_of(:issues_url) }
      it { is_expected.not_to validate_presence_of(:new_issue_url) }
    end
  end

33 34 35 36 37 38 39 40 41
  describe '#reference_pattern' do
    it_behaves_like 'allows project key on reference pattern'

    it 'does not allow # on the code' do
      expect(subject.reference_pattern.match('#123')).to be_nil
      expect(subject.reference_pattern.match('1#23#12')).to be_nil
    end
  end

D
Drew Blessing 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  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
58
      @sample_data = Gitlab::DataBuilder::Push.build_sample(project, user)
D
Drew Blessing 已提交
59 60 61 62 63 64 65 66
      # 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

67
    it "calls JIRA API" do
68 69
      @jira_service.execute(merge_request,
                            ExternalIssue.new("JIRA-123", project))
D
Drew Blessing 已提交
70 71 72 73 74 75 76
      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'
77 78
      @jira_service.execute(merge_request,
                            ExternalIssue.new("JIRA-123", project))
D
Drew Blessing 已提交
79 80 81 82 83 84 85 86 87 88 89
      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
90
        @jira_service = JiraService.create!(
D
Drew Blessing 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
          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

120
      it "resets password if url changed, even if setter called multiple times" do
D
Drew Blessing 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        @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

149 150 151 152 153 154
  describe "Validations" do
    context "active" do
      before do
        subject.active = true
      end

155 156 157
      it { is_expected.to validate_presence_of :project_url }
      it { is_expected.to validate_presence_of :issues_url }
      it { is_expected.to validate_presence_of :new_issue_url }
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    end
  end

  describe 'description and title' do
    let(:project) { create(:project) }

    context 'when it is not set' do
      before do
        @service = project.create_jira_service(active: true)
      end

      after do
        @service.destroy!
      end

173
      it 'is initialized' do
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
        expect(@service.title).to eq('JIRA')
        expect(@service.description).to eq("Jira issue tracker")
      end
    end

    context 'when it is set' do
      before do
        properties = { 'title' => 'Jira One', 'description' => 'Jira One issue tracker' }
        @service = project.create_jira_service(active: true, properties: properties)
      end

      after do
        @service.destroy!
      end

189
      it "is correct" do
190 191 192 193 194 195 196 197 198 199 200
        expect(@service.title).to eq('Jira One')
        expect(@service.description).to eq('Jira One issue tracker')
      end
    end
  end

  describe 'project and issue urls' do
    let(:project) { create(:project) }

    context 'when gitlab.yml was initialized' do
      before do
D
Drew Blessing 已提交
201 202 203 204 205 206
        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"
207 208
          }
        }
209
        allow(Gitlab.config).to receive(:issues_tracker).and_return(settings)
210 211 212 213 214 215 216
        @service = project.create_jira_service(active: true)
      end

      after do
        @service.destroy!
      end

217
      it 'is prepopulated with the settings' do
V
Valery Sizov 已提交
218 219 220
        expect(@service.properties["project_url"]).to eq('http://jira.sample/projects/project_a')
        expect(@service.properties["issues_url"]).to eq("http://jira.sample/issues/:id")
        expect(@service.properties["new_issue_url"]).to eq("http://jira.sample/projects/project_a/issues/new")
221 222 223 224
      end
    end
  end
end