chat_name_spec.rb 1.49 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
require 'spec_helper'

5
RSpec.describe ChatName do
6
  let_it_be(:chat_name) { create(:chat_name) }
7

8
  subject { chat_name }
9

10
  it { is_expected.to belong_to(:integration) }
11 12 13
  it { is_expected.to belong_to(:user) }

  it { is_expected.to validate_presence_of(:user) }
14
  it { is_expected.to validate_presence_of(:integration) }
15 16 17 18 19
  it { is_expected.to validate_presence_of(:team_id) }
  it { is_expected.to validate_presence_of(:chat_id) }

  it { is_expected.to validate_uniqueness_of(:user_id).scoped_to(:service_id) }
  it { is_expected.to validate_uniqueness_of(:chat_id).scoped_to(:service_id, :team_id) }
20

21
  it 'is removed when the project is deleted' do
22
    expect { subject.reload.integration.project.delete }.to change { ChatName.count }.by(-1)
23 24 25 26

    expect(ChatName.where(id: subject.id)).not_to exist
  end

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  describe '#update_last_used_at', :clean_gitlab_redis_shared_state do
    it 'updates the last_used_at timestamp' do
      expect(subject.last_used_at).to be_nil

      subject.update_last_used_at

      expect(subject.last_used_at).to be_present
    end

    it 'does not update last_used_at if it was recently updated' do
      subject.update_last_used_at

      time = subject.last_used_at

      subject.update_last_used_at

      expect(subject.last_used_at).to eq(time)
    end
  end
46 47 48 49 50 51 52 53

  it_behaves_like 'it has loose foreign keys' do
    let(:factory_name) { :chat_name }

    before do
      Ci::PipelineChatData # ensure that the referenced model is loaded
    end
  end
54
end