audit_events_helper_spec.rb 2.02 KB
Newer Older
1 2 3 4
require 'spec_helper'

describe AuditEventsHelper do
  describe '#human_text' do
5
    let(:target_type) { 'User' }
6 7 8 9
    let(:details) do
      {
        author_name: 'John Doe',
        target_id: 1,
10
        target_type: target_type,
11 12 13 14
        target_details: 'Michael'
      }
    end

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
    subject { human_text(details) }

    context 'when message consist of hash keys' do
      subject { human_text({ remove: 'user_access' }.merge(details))}

      it 'ignores keys that start with start with author_, or target_' do
        expect(subject).to eq 'Remove <strong>user access</strong>    '
      end
    end

    context 'when details contain custom message' do
      let(:custom_message) { 'Custom message <strong>with tags</strong>' }

      subject { human_text( { custom_message: custom_message }.merge(details)) }

      it 'returns custom message' do
        expect(subject).to eq(custom_message)
      end

34 35
      context 'when message relates to feature flags' do
        let(:custom_message) { "Feature flag my_feature_flag was updated" }
36
        let(:target_type) { 'Operations::FeatureFlag' }
37

38 39
        it 'shows message as is' do
          is_expected.to eq(custom_message)
40 41 42 43 44 45 46 47 48 49 50 51
        end
      end

      context 'when the target_type is not Operations::FeatureFlag' do
        let(:target_type) { 'User' }

        context 'when custom message contains "_"' do
          let(:custom_message) { "message_with_spaces" }

          it 'replaces them with spaces' do
            expect(subject).to eq("message with spaces")
          end
52 53
        end
      end
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    end
  end

  describe '#select_keys' do
    it 'returns empty string if key starts with author_' do
      expect(select_keys('author_name', 'John Doe')).to eq ''
    end

    it 'returns empty string if key starts with target_' do
      expect(select_keys('target_name', 'John Doe')).to eq ''
    end

    it 'returns formatted text if key does not start with author_, or target_' do
      expect(select_keys('remove', 'user_access')).to eq 'remove <strong>user_access</strong>'
    end
  end
end