analytics_build_entity_spec.rb 2.13 KB
Newer Older
1 2 3 4 5 6 7
require 'spec_helper'

describe AnalyticsBuildEntity do
  let(:entity) do
    described_class.new(build, request: double)
  end

8
  context 'build with an author' do
9
    let(:user) { create(:user) }
10 11 12
    let(:started_at) { 2.hours.ago }
    let(:finished_at) { 1.hour.ago }
    let(:build) { create(:ci_build, author: user, started_at: started_at, finished_at: finished_at) }
13 14 15

    subject { entity.as_json }

Rémy Coutable's avatar
Rémy Coutable committed
16 17
    around do |example|
      Timecop.freeze { example.run }
18 19
    end

20 21
    it 'contains the URL' do
      expect(subject).to include(:url)
22 23
    end

24 25 26 27
    it 'contains the author' do
      expect(subject).to include(:author)
    end

28 29 30 31
    it 'does not contain sensitive information' do
      expect(subject).not_to include(/token/)
      expect(subject).not_to include(/variables/)
    end
32 33 34 35 36 37

    it 'contains the right started at' do
      expect(subject[:date]).to eq('about 2 hours ago')
    end

    it 'contains the duration' do
James Lopez's avatar
James Lopez committed
38
      expect(subject[:total_time]).to eq(hours: 1 )
39
    end
40 41 42 43 44 45

    context 'no started at or finished at date' do
      let(:started_at) { nil }
      let(:finished_at) { nil }

      it 'does not blow up' do
46
        expect { subject[:date] }.not_to raise_error
47 48
      end

49 50 51 52 53 54 55
      it 'shows the right message' do
        expect(subject[:date]).to eq('Not started')
      end

      it 'shows the right total time' do
        expect(subject[:total_time]).to eq({})
      end
56 57 58 59 60 61
    end

    context 'no started at date' do
      let(:started_at) { nil }

      it 'does not blow up' do
62
        expect { subject[:date] }.not_to raise_error
63
      end
64 65 66 67 68 69 70 71

      it 'shows the right message' do
        expect(subject[:date]).to eq('Not started')
      end

      it 'shows the right total time' do
        expect(subject[:total_time]).to eq({})
      end
72 73 74 75 76 77
    end

    context 'no finished at date' do
      let(:finished_at) { nil }

      it 'does not blow up' do
78
        expect { subject[:date] }.not_to raise_error
79
      end
80 81 82 83 84 85

      it 'shows the right message' do
        expect(subject[:date]).to eq('about 2 hours ago')
      end

      it 'shows the right total time' do
James Lopez's avatar
James Lopez committed
86
        expect(subject[:total_time]).to eq({ hours: 2 })
87
      end
88
    end
89 90
  end
end