jira_service_spec.rb 7.9 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2 3 4
# == Schema Information
#
# Table name: services
#
D
Dmitriy Zaporozhets 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17
#  id                    :integer          not null, primary key
#  type                  :string(255)
#  title                 :string(255)
#  project_id            :integer
#  created_at            :datetime
#  updated_at            :datetime
#  active                :boolean          default(FALSE), not null
#  properties            :text
#  template              :boolean          default(FALSE)
#  push_events           :boolean          default(TRUE)
#  issues_events         :boolean          default(TRUE)
#  merge_requests_events :boolean          default(TRUE)
#  tag_push_events       :boolean          default(TRUE)
S
Stan Hu 已提交
18
#  note_events           :boolean          default(TRUE), not null
D
Dmitriy Zaporozhets 已提交
19 20
#

21 22
require 'spec_helper'

D
Douwe Maan 已提交
23
describe JiraService, models: true do
24
  describe "Associations" do
25 26
    it { is_expected.to belong_to :project }
    it { is_expected.to have_one :service_hook }
27 28
  end

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  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

D
Drew Blessing 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  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
79 80
      @jira_service.execute(merge_request,
                            ExternalIssue.new("JIRA-123", project))
D
Drew Blessing 已提交
81 82 83 84 85 86 87
      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'
88 89
      @jira_service.execute(merge_request,
                            ExternalIssue.new("JIRA-123", project))
D
Drew Blessing 已提交
90 91 92 93 94 95 96 97 98 99 100
      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
101
        @jira_service = JiraService.create!(
D
Drew Blessing 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 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 149 150 151 152 153 154 155 156 157 158 159
          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

160 161 162 163 164 165
  describe "Validations" do
    context "active" do
      before do
        subject.active = true
      end

166 167 168
      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 }
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    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

      it 'should be initialized' do
        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

      it "should be correct" do
        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 已提交
212 213 214 215 216 217
        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"
218 219
          }
        }
220
        allow(Gitlab.config).to receive(:issues_tracker).and_return(settings)
221 222 223 224 225 226 227 228
        @service = project.create_jira_service(active: true)
      end

      after do
        @service.destroy!
      end

      it 'should be prepopulated with the settings' do
V
Valery Sizov 已提交
229 230 231
        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")
232 233 234 235
      end
    end
  end
end