drone_ci_service_spec.rb 3.32 KB
Newer Older
Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# == Schema Information
#
# Table name: services
#
#  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)
#  note_events           :boolean          default(TRUE), not null
#

require 'spec_helper'

Douwe Maan's avatar
Douwe Maan committed
23
describe DroneCiService, models: true do
Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
24 25 26 27 28 29 30
  describe 'associations' do
    it { is_expected.to belong_to(:project) }
    it { is_expected.to have_one(:service_hook) }
  end

  describe 'validations' do
    context 'active' do
31
      before { subject.active = true }
Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
32 33 34

      it { is_expected.to validate_presence_of(:token) }
      it { is_expected.to validate_presence_of(:drone_url) }
35
      it_behaves_like 'issue tracker service URL attribute', :drone_url
Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
36 37 38
    end

    context 'inactive' do
39
      before { subject.active = false }
Kirilll Zaitsev's avatar
Kirilll Zaitsev committed
40 41 42 43 44 45 46 47 48 49 50 51 52 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

      it { is_expected.not_to validate_presence_of(:token) }
      it { is_expected.not_to validate_presence_of(:drone_url) }
    end
  end

  shared_context :drone_ci_service do
    let(:drone)      { DroneCiService.new }
    let(:project)    { create(:project, name: 'project') }
    let(:path)       { "#{project.namespace.path}/#{project.path}" }
    let(:drone_url)  { 'http://drone.example.com' }
    let(:sha)        { '2ab7834c' }
    let(:branch)     { 'dev' }
    let(:token)      { 'secret' }
    let(:iid)        { rand(1..9999) }

    before(:each) do
      allow(drone).to receive_messages(
        project_id: project.id,
        project: project,
        active: true,
        drone_url: drone_url,
        token: token
      )
    end
  end

  describe "service page/path methods" do
    include_context :drone_ci_service

    # URL's
    let(:commit_page) { "#{drone_url}/gitlab/#{path}/redirect/commits/#{sha}?branch=#{branch}" }
    let(:merge_request_page) { "#{drone_url}/gitlab/#{path}/redirect/pulls/#{iid}" }
    let(:commit_status_path) { "#{drone_url}/gitlab/#{path}/commits/#{sha}?branch=#{branch}&access_token=#{token}" }
    let(:merge_request_status_path) { "#{drone_url}/gitlab/#{path}/pulls/#{iid}?access_token=#{token}" }

    it { expect(drone.build_page(sha, branch)).to eq(commit_page) }
    it { expect(drone.commit_page(sha, branch)).to eq(commit_page) }
    it { expect(drone.merge_request_page(iid, sha, branch)).to eq(merge_request_page) }
    it { expect(drone.commit_status_path(sha, branch)).to eq(commit_status_path) }
    it { expect(drone.merge_request_status_path(iid, sha, branch)).to eq(merge_request_status_path)  }
  end

  describe "execute" do
    include_context :drone_ci_service

    let(:user)    { create(:user, username: 'username') }
    let(:push_sample_data) { Gitlab::PushDataBuilder.build_sample(project, user) }

    it do
      service_hook = double
      expect(service_hook).to receive(:execute)
      expect(drone).to receive(:service_hook).and_return(service_hook)

      drone.execute(push_sample_data)
    end
  end
end