debian-mirror-gitlab/spec/controllers/projects/starrers_controller_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

203 lines
5.1 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe Projects::StarrersController do
2019-10-12 21:52:04 +05:30
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) }
2023-01-13 00:05:48 +05:30
let(:blocked_user) { create(:user, state: 'blocked') }
2019-10-12 21:52:04 +05:30
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)
2023-01-13 00:05:48 +05:30
blocked_user.toggle_star(project)
2019-10-12 21:52:04 +05:30
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
2019-12-04 20:38:33 +05:30
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-10-12 21:52:04 +05:30
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
2023-01-13 00:05:48 +05:30
it 'non-active users are not visible' do
expect(user_ids).not_to include(blocked_user.id)
end
2019-10-12 21:52:04 +05:30
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
2021-04-29 21:17:54 +05:30
project.update!(visibility_level: Project::PRIVATE)
2019-10-12 21:52:04 +05:30
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