2019-08-03 06:57:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-02-05 06:49:58 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-03 14:08:28 -04:00
|
|
|
RSpec.describe Projects::StarrersController do
|
2019-08-14 16:29:41 -04:00
|
|
|
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) }
|
2022-10-21 05:10:32 -04:00
|
|
|
let(:blocked_user) { create(:user, state: 'blocked') }
|
2019-07-27 02:26:53 -04:00
|
|
|
let(:admin) { create(:user, admin: true) }
|
2019-08-14 16:29:41 -04:00
|
|
|
let(:project) { create(:project, :public) }
|
2019-02-05 06:49:58 -05:00
|
|
|
|
|
|
|
before do
|
2019-08-14 16:29:41 -04:00
|
|
|
user_1.toggle_star(project)
|
|
|
|
user_2.toggle_star(project)
|
2019-02-05 06:49:58 -05:00
|
|
|
private_user.toggle_star(project)
|
2022-10-21 05:10:32 -04:00
|
|
|
blocked_user.toggle_star(project)
|
2019-02-05 06:49:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET index' do
|
2019-08-14 16:29:41 -04:00
|
|
|
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
|
2019-02-05 06:49:58 -05:00
|
|
|
end
|
|
|
|
|
2019-08-19 16:33:07 -04:00
|
|
|
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
|
|
|
|
|
2019-02-05 06:49:58 -05:00
|
|
|
context 'when project is public' do
|
|
|
|
before do
|
|
|
|
project.update_attribute(:visibility_level, Project::PUBLIC)
|
|
|
|
end
|
|
|
|
|
2019-07-27 02:26:53 -04:00
|
|
|
context 'when no user is logged in' do
|
2019-08-14 16:29:41 -04:00
|
|
|
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
|
|
|
|
|
2022-10-21 05:10:32 -04:00
|
|
|
it 'non-active users are not visible' do
|
|
|
|
expect(user_ids).not_to include(blocked_user.id)
|
|
|
|
end
|
|
|
|
|
2019-08-14 16:29:41 -04:00
|
|
|
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
|
2019-07-27 02:26:53 -04:00
|
|
|
before do
|
2019-08-14 16:29:41 -04:00
|
|
|
sign_in(user_1)
|
2019-07-27 02:26:53 -04:00
|
|
|
end
|
2019-02-05 06:49:58 -05:00
|
|
|
|
2019-08-14 16:29:41 -04:00
|
|
|
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'
|
2019-07-27 02:26:53 -04:00
|
|
|
end
|
|
|
|
|
2019-08-14 16:29:41 -04:00
|
|
|
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'
|
2019-07-27 02:26:53 -04:00
|
|
|
end
|
2019-02-05 06:49:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when private user is logged in' do
|
|
|
|
before do
|
|
|
|
sign_in(private_user)
|
|
|
|
end
|
|
|
|
|
2019-08-14 16:29:41 -04:00
|
|
|
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'
|
2019-07-27 02:26:53 -04:00
|
|
|
end
|
|
|
|
|
2019-08-14 16:29:41 -04:00
|
|
|
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'
|
2019-07-27 02:26:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when admin is logged in' do
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
2019-02-05 06:49:58 -05:00
|
|
|
|
2019-08-14 16:29:41 -04:00
|
|
|
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'
|
2019-02-05 06:49:58 -05:00
|
|
|
end
|
2019-07-27 02:26:53 -04:00
|
|
|
|
2019-08-14 16:29:41 -04:00
|
|
|
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'
|
2019-07-27 02:26:53 -04:00
|
|
|
end
|
2019-02-05 06:49:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project is private' do
|
|
|
|
before do
|
2021-03-30 08:10:51 -04:00
|
|
|
project.update!(visibility_level: Project::PRIVATE)
|
2019-02-05 06:49:58 -05:00
|
|
|
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
|
2019-08-14 16:29:41 -04:00
|
|
|
end
|
2019-02-05 06:49:58 -05:00
|
|
|
|
2019-08-14 16:29:41 -04:00
|
|
|
it 'only users with public profiles are visible' do
|
|
|
|
expect(user_ids).to contain_exactly(user_1.id, user_2.id)
|
2019-02-05 06:49:58 -05:00
|
|
|
end
|
2019-08-14 16:29:41 -04:00
|
|
|
|
|
|
|
include_examples 'starrers counts'
|
2019-02-05 06:49:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|