summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/starrers_controller_spec.rb
blob: 5774ff7c576f90d745826767d2ce6e92b1384f3b (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# frozen_string_literal: true

require 'spec_helper'

describe Projects::StarrersController do
  let(:user_1) { create(:user, name: 'John') }
  let(:user_2) { create(:user, name: 'Michael') }
  let(:private_user) { create(:user, name: 'Michael Douglas', private_profile: true) }
  let(:admin) { create(:user, admin: true) }
  let(:project) { create(:project, :public) }

  before do
    user_1.toggle_star(project)
    user_2.toggle_star(project)
    private_user.toggle_star(project)
  end

  describe 'GET index' do
    def get_starrers(search: nil)
      get :index, params: { namespace_id: project.namespace, project_id: project, search: search }
    end

    def user_ids
      assigns[:starrers].map { |s| s['user_id'] }
    end

    shared_examples 'starrers counts' do
      it 'starrers counts are correct' do
        expect(assigns[:total_count]).to eq(3)
        expect(assigns[:public_count]).to eq(2)
        expect(assigns[:private_count]).to eq(1)
      end
    end

    context 'N+1 queries' do
      render_views

      it 'avoids N+1s loading users', :request_store do
        get_starrers

        control_count = ActiveRecord::QueryRecorder.new { get_starrers }.count

        create_list(:user, 5).each { |user| user.toggle_star(project) }

        expect { get_starrers }.not_to exceed_query_limit(control_count)
      end
    end

    context 'when project is public' do
      before do
        project.update_attribute(:visibility_level, Project::PUBLIC)
      end

      context 'when no user is logged in' do
        context 'with no searching' do
          before do
            get_starrers
          end

          it 'only users with public profiles are visible' do
            expect(user_ids).to contain_exactly(user_1.id, user_2.id)
          end

          include_examples 'starrers counts'
        end

        context 'when searching by user' do
          before do
            get_starrers(search: 'Michael')
          end

          it 'only users with public profiles are visible' do
            expect(user_ids).to contain_exactly(user_2.id)
          end

          include_examples 'starrers counts'
        end
      end

      context 'when public user is logged in' do
        before do
          sign_in(user_1)
        end

        context 'with no searching' do
          before do
            get_starrers
          end

          it 'their star is also visible' do
            expect(user_ids).to contain_exactly(user_1.id, user_2.id)
          end

          include_examples 'starrers counts'
        end

        context 'when searching by user' do
          before do
            get_starrers(search: 'Michael')
          end

          it 'only users with public profiles are visible' do
            expect(user_ids).to contain_exactly(user_2.id)
          end

          include_examples 'starrers counts'
        end
      end

      context 'when private user is logged in' do
        before do
          sign_in(private_user)
        end

        context 'with no searching' do
          before do
            get_starrers
          end

          it 'their star is also visible' do
            expect(user_ids).to contain_exactly(user_1.id, user_2.id, private_user.id)
          end

          include_examples 'starrers counts'
        end

        context 'when searching by user' do
          before do
            get_starrers(search: 'Michael')
          end

          it 'only users with public profiles are visible' do
            expect(user_ids).to contain_exactly(user_2.id, private_user.id)
          end

          include_examples 'starrers counts'
        end
      end

      context 'when admin is logged in' do
        before do
          sign_in(admin)
        end

        context 'with no searching' do
          before do
            get_starrers
          end

          it 'all users are visible' do
            expect(user_ids).to include(user_1.id, user_2.id, private_user.id)
          end

          include_examples 'starrers counts'
        end

        context 'when searching by user' do
          before do
            get_starrers(search: 'Michael')
          end

          it 'public and private starrers are visible' do
            expect(user_ids).to contain_exactly(user_2.id, private_user.id)
          end

          include_examples 'starrers counts'
        end
      end
    end

    context 'when project is private' do
      before do
        project.update(visibility_level: Project::PRIVATE)
      end

      it 'starrers are not visible for non logged in users' do
        get_starrers

        expect(assigns[:starrers]).to be_blank
      end

      context 'when user is logged in' do
        before do
          sign_in(project.creator)
          get_starrers
        end

        it 'only users with public profiles are visible' do
          expect(user_ids).to contain_exactly(user_1.id, user_2.id)
        end

        include_examples 'starrers counts'
      end
    end
  end
end