BigW Consortium Gitlab

service_spec.rb 6.59 KB
Newer Older
1 2 3 4
# == Schema Information
#
# Table name: services
#
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)
Stan Hu committed
18
#  note_events           :boolean          default(TRUE), not null
Stan Hu committed
19
#  build_events          :boolean          default(FALSE), not null
20 21 22 23
#

require 'spec_helper'

Douwe Maan committed
24
describe Service, models: true do
25

26
  describe "Associations" do
27 28
    it { is_expected.to belong_to :project }
    it { is_expected.to have_one :service_hook }
29 30 31 32
  end

  describe "Mass assignment" do
  end
33 34 35 36 37 38 39

  describe "Test Button" do
    before do
      @service = Service.new
    end

    describe "Testable" do
40
      let(:project) { create :project }
41 42

      before do
43
        allow(@service).to receive(:project).and_return(project)
44 45 46 47
        @testable = @service.can_test?
      end

      describe :can_test do
48
        it { expect(@testable).to eq(true) }
49
      end
50 51 52 53 54 55 56 57 58 59

      describe :test do
        let(:data) { 'test' }

        it 'test runs execute' do
          expect(@service).to receive(:execute).with(data)

          @service.test(data)
        end
      end
60 61 62
    end

    describe "With commits" do
63
      let(:project) { create :project }
64 65

      before do
66
        allow(@service).to receive(:project).and_return(project)
67 68 69 70
        @testable = @service.can_test?
      end

      describe :can_test do
71
        it { expect(@testable).to eq(true) }
72 73 74
      end
    end
  end
75 76 77

  describe "Template" do
    describe "for pushover service" do
78 79 80 81 82 83 84 85 86 87
      let(:service_template) do
        PushoverService.create(
          template: true,
          properties: {
            device: 'MyDevice',
            sound: 'mic',
            priority: 4,
            api_key: '123456789'
          })
      end
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
      let(:project) { create(:project) }

      describe 'should be prefilled for projects pushover service' do
        before do
          service_template
          project.build_missing_services
        end

        it "should have all fields prefilled" do
          service = project.pushover_service
          expect(service.template).to eq(false)
          expect(service.device).to eq('MyDevice')
          expect(service.sound).to eq('mic')
          expect(service.priority).to eq(4)
          expect(service.api_key).to eq('123456789')
        end
      end
    end
  end
107

108
  describe "{property}_changed?" do
109 110 111 112 113 114 115 116 117 118 119
    let(:service) do
      BambooService.create(
        project: create(:project),
        properties: {
          bamboo_url: 'http://gitlab.com',
          username: 'mic',
          password: "password"
        }
      )
    end

120
    it "returns false when the property has not been assigned a new value" do
121
      service.username = "key_changed"
122
      expect(service.bamboo_url_changed?).to be_falsy
123 124
    end

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 160 161 162 163 164 165 166 167 168 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 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    it "returns true when the property has been assigned a different value" do
      service.bamboo_url = "http://example.com"
      expect(service.bamboo_url_changed?).to be_truthy
    end

    it "returns true when the property has been assigned a different value twice" do
      service.bamboo_url = "http://example.com"
      service.bamboo_url = "http://example.com"
      expect(service.bamboo_url_changed?).to be_truthy
    end

    it "returns false when the property has been re-assigned the same value" do
      service.bamboo_url = 'http://gitlab.com'
      expect(service.bamboo_url_changed?).to be_falsy
    end

    it "returns false when the property has been assigned a new value then saved" do
      service.bamboo_url = 'http://example.com'
      service.save
      expect(service.bamboo_url_changed?).to be_falsy
    end
  end

  describe "{property}_touched?" do
    let(:service) do
      BambooService.create(
        project: create(:project),
        properties: {
          bamboo_url: 'http://gitlab.com',
          username: 'mic',
          password: "password"
        }
      )
    end

    it "returns false when the property has not been assigned a new value" do
      service.username = "key_changed"
      expect(service.bamboo_url_touched?).to be_falsy
    end

    it "returns true when the property has been assigned a different value" do
      service.bamboo_url = "http://example.com"
      expect(service.bamboo_url_touched?).to be_truthy
    end

    it "returns true when the property has been assigned a different value twice" do
      service.bamboo_url = "http://example.com"
      service.bamboo_url = "http://example.com"
      expect(service.bamboo_url_touched?).to be_truthy
    end

    it "returns true when the property has been re-assigned the same value" do
      service.bamboo_url = 'http://gitlab.com'
      expect(service.bamboo_url_touched?).to be_truthy
    end

    it "returns false when the property has been assigned a new value then saved" do
      service.bamboo_url = 'http://example.com'
      service.save
      expect(service.bamboo_url_changed?).to be_falsy
    end
  end

  describe "{property}_was" do
    let(:service) do
      BambooService.create(
        project: create(:project),
        properties: {
          bamboo_url: 'http://gitlab.com',
          username: 'mic',
          password: "password"
        }
      )
    end


    it "returns nil when the property has not been assigned a new value" do
      service.username = "key_changed"
      expect(service.bamboo_url_was).to be_nil
    end

    it "returns the previous value when the property has been assigned a different value" do
      service.bamboo_url = "http://example.com"
      expect(service.bamboo_url_was).to eq('http://gitlab.com')
    end

    it "returns initial value when the property has been re-assigned the same value" do
      service.bamboo_url = 'http://gitlab.com'
      expect(service.bamboo_url_was).to eq('http://gitlab.com')
    end

    it "returns initial value when the property has been assigned multiple values" do
      service.bamboo_url = "http://example.com"
      service.bamboo_url = "http://example2.com"
      expect(service.bamboo_url_was).to eq('http://gitlab.com')
    end

    it "returns nil when the property has been assigned a new value then saved" do
      service.bamboo_url = 'http://example.com'
      service.save
      expect(service.bamboo_url_was).to be_nil
226 227
    end
  end
228
end