File: record_user_last_activity_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (119 lines) | stat: -rw-r--r-- 3,444 bytes parent folder | download
1
2
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe RecordUserLastActivity, feature_category: :seat_cost_management do
  let_it_be(:group) { create(:group) }
  let_it_be(:project) { create(:project, group: group) }
  let_it_be(:user) { create(:user) }

  let(:controller_class) do
    # rubocop:disable Rails/ApplicationController -- This is a test controller
    Class.new(ActionController::Base) do
      include RecordUserLastActivity

      def initialize(user, group, project)
        @current_user = user
        @group = group
        @project = project
      end

      def request
        @request ||= Struct.new(:get?, :env).new(true, { 'HTTP_CF_IPCOUNTRY' => 'US' })
      end
    end
    # rubocop:enable Rails/ApplicationController
  end

  subject(:controller) { controller_class.new(user, group, project) }

  shared_examples 'does not update the user activity timestamp' do
    it 'does not update the user last activity' do
      expect { controller.set_user_last_activity }.not_to change { user.reload.last_activity_on }
    end
  end

  describe '#set_user_last_activity' do
    context 'when the request is a GET request' do
      it 'updates the user last activity' do
        expect { controller.set_user_last_activity }.to change { user.reload.last_activity_on }
      end
    end

    context 'when the request is not a GET request' do
      before do
        allow(controller.request).to receive(:get?).and_return(false)
      end

      it_behaves_like 'does not update the user activity timestamp'
    end

    context 'when the database is read-only' do
      before do
        allow(Gitlab::Database).to receive(:read_only?).and_return(true)
      end

      it_behaves_like 'does not update the user activity timestamp'
    end

    context 'when there is no current user' do
      before do
        allow(controller).to receive(:current_user).and_return(nil)
      end

      it_behaves_like 'does not update the user activity timestamp'
    end
  end

  describe '#set_member_last_activity' do
    shared_examples 'does not update publish an activity event' do
      it do
        expect { controller.set_member_last_activity }.not_to publish_event(Users::ActivityEvent)
      end
    end

    shared_examples 'publishes an activity event' do
      it 'publishes a Users::ActivityEvent' do
        expect { controller.set_member_last_activity }
          .to publish_event(Users::ActivityEvent)
          .with({
            user_id: user.id,
            namespace_id: context.root_ancestor.id
          })
      end
    end

    context 'when the request is a GET request' do
      context 'when a group is available' do
        let(:context) { group }

        it_behaves_like 'publishes an activity event'
      end

      context 'when no group is available' do
        let(:group) { nil }

        context 'when a project is available' do
          let(:context) { project }

          it_behaves_like 'publishes an activity event'
        end
      end

      context 'when there is no group or project' do
        let(:group) { nil }
        let(:project) { nil }

        it_behaves_like 'does not update publish an activity event'
      end
    end

    context 'when the request is not a GET request' do
      before do
        allow(controller.request).to receive(:get?).and_return(false)
      end

      it_behaves_like 'does not update publish an activity event'
    end
  end
end