| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 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
 98
 99
 100
 
 | # frozen_string_literal: true
require 'spec_helper'
RSpec.describe AnalyticsBuildEntity do
  let(:entity) do
    described_class.new(build, request: double)
  end
  context 'build with an author' do
    let(:user) { create(:user) }
    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) }
    subject { entity.as_json }
    around do |example|
      freeze_time { example.run }
    end
    it 'contains the URL' do
      expect(subject).to include(:url)
    end
    it 'contains the author' do
      expect(subject).to include(:author)
    end
    it 'contains the project path' do
      expect(subject).to include(:project_path)
    end
    it 'contains the namespace full path' do
      expect(subject).to include(:namespace_full_path)
    end
    it 'does not contain sensitive information' do
      expect(subject).not_to include(/token/)
      expect(subject).not_to include(/variables/)
    end
    it 'contains the right started at' do
      expect(subject[:date]).to eq('about 2 hours ago')
    end
    it 'contains the duration' do
      expect(subject[:total_time]).to eq(hours: 1 )
    end
    context 'no started at or finished at date' do
      let(:started_at) { nil }
      let(:finished_at) { nil }
      it 'does not blow up' do
        expect { subject[:date] }.not_to raise_error
      end
      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
    end
    context 'no started at date' do
      let(:started_at) { nil }
      it 'does not blow up' do
        expect { subject[:date] }.not_to raise_error
      end
      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
    end
    context 'no finished at date' do
      let(:finished_at) { nil }
      it 'does not blow up' do
        expect { subject[:date] }.not_to raise_error
      end
      it 'shows the right message' do
        expect(subject[:date]).to eq('about 2 hours ago')
      end
      it 'shows the right total time' do
        expect(subject[:total_time]).to eq({ hours: 2 })
      end
    end
  end
end
 |