debian-mirror-gitlab/spec/controllers/autocomplete_controller_spec.rb

314 lines
8.2 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
require 'spec_helper'
describe AutocompleteController do
let!(:project) { create(:project) }
2016-09-13 17:45:13 +05:30
let!(:user) { create(:user) }
2015-04-26 12:48:37 +05:30
2016-09-13 17:45:13 +05:30
context 'users and members' do
let!(:user2) { create(:user) }
let!(:non_member) { create(:user) }
2015-04-26 12:48:37 +05:30
2016-09-13 17:45:13 +05:30
context 'project members' do
2015-09-11 14:41:01 +05:30
before do
2016-09-13 17:45:13 +05:30
sign_in(user)
project.team << [user, :master]
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
describe 'GET #users with project ID' do
before do
get(:users, project_id: project.id)
end
2016-06-02 11:05:42 +05:30
2016-09-13 17:45:13 +05:30
let(:body) { JSON.parse(response.body) }
2015-09-11 14:41:01 +05:30
2016-09-13 17:45:13 +05:30
it { expect(body).to be_kind_of(Array) }
it { expect(body.size).to eq 1 }
it { expect(body.map { |u| u["username"] }).to include(user.username) }
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
describe 'GET #users with unknown project' do
before do
get(:users, project_id: 'unknown')
end
2015-04-26 12:48:37 +05:30
2016-09-13 17:45:13 +05:30
it { expect(response).to have_http_status(404) }
end
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
context 'group members' do
let(:group) { create(:group) }
2015-09-11 14:41:01 +05:30
before do
2016-09-13 17:45:13 +05:30
sign_in(user)
group.add_owner(user)
2015-09-11 14:41:01 +05:30
end
2015-04-26 12:48:37 +05:30
2016-09-13 17:45:13 +05:30
let(:body) { JSON.parse(response.body) }
describe 'GET #users with group ID' do
before do
get(:users, group_id: group.id)
end
it { expect(body).to be_kind_of(Array) }
it { expect(body.size).to eq 1 }
it { expect(body.first["username"]).to eq user.username }
end
describe 'GET #users with unknown group ID' do
before do
get(:users, group_id: 'unknown')
end
it { expect(response).to have_http_status(404) }
end
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
context 'non-member login for public project' do
let!(:project) { create(:project, :public) }
2015-09-11 14:41:01 +05:30
before do
2016-09-13 17:45:13 +05:30
sign_in(non_member)
project.team << [user, :master]
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
let(:body) { JSON.parse(response.body) }
2015-09-11 14:41:01 +05:30
2016-09-13 17:45:13 +05:30
describe 'GET #users with project ID' do
before do
get(:users, project_id: project.id, current_user: true)
end
2015-09-11 14:41:01 +05:30
2016-09-13 17:45:13 +05:30
it { expect(body).to be_kind_of(Array) }
it { expect(body.size).to eq 2 }
it { expect(body.map { |u| u['username'] }).to match_array([user.username, non_member.username]) }
end
2015-04-26 12:48:37 +05:30
end
2016-09-13 17:45:13 +05:30
context 'all users' do
2015-09-11 14:41:01 +05:30
before do
2016-09-13 17:45:13 +05:30
sign_in(user)
get(:users)
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
let(:body) { JSON.parse(response.body) }
2015-09-11 14:41:01 +05:30
it { expect(body).to be_kind_of(Array) }
2016-09-13 17:45:13 +05:30
it { expect(body.size).to eq User.count }
2015-09-11 14:41:01 +05:30
end
2015-04-26 12:48:37 +05:30
2016-09-13 17:45:13 +05:30
context 'unauthenticated user' do
let(:public_project) { create(:project, :public) }
let(:body) { JSON.parse(response.body) }
2015-04-26 12:48:37 +05:30
2016-09-13 17:45:13 +05:30
describe 'GET #users with public project' do
before do
public_project.team << [user, :guest]
get(:users, project_id: public_project.id)
end
2015-04-26 12:48:37 +05:30
2016-09-13 17:45:13 +05:30
it { expect(body).to be_kind_of(Array) }
it { expect(body.size).to eq 1 }
end
2015-09-11 14:41:01 +05:30
2016-09-13 17:45:13 +05:30
describe 'GET #users with project' do
before do
get(:users, project_id: project.id)
end
2015-09-11 14:41:01 +05:30
2016-09-13 17:45:13 +05:30
it { expect(response).to have_http_status(404) }
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
describe 'GET #users with unknown project' do
before do
get(:users, project_id: 'unknown')
end
2015-09-11 14:41:01 +05:30
2016-09-13 17:45:13 +05:30
it { expect(response).to have_http_status(404) }
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
describe 'GET #users with inaccessible group' do
before do
project.team << [user, :guest]
get(:users, group_id: user.namespace.id)
end
2015-09-11 14:41:01 +05:30
2016-09-13 17:45:13 +05:30
it { expect(response).to have_http_status(404) }
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
describe 'GET #users with no project' do
before do
get(:users)
end
it { expect(body).to be_kind_of(Array) }
it { expect(body.size).to eq 0 }
end
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
context 'author of issuable included' do
2015-09-11 14:41:01 +05:30
before do
2016-09-13 17:45:13 +05:30
sign_in(user)
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
let(:body) { JSON.parse(response.body) }
2015-09-11 14:41:01 +05:30
2016-09-13 17:45:13 +05:30
it 'includes the author' do
get(:users, author_id: non_member.id)
expect(body.first["username"]).to eq non_member.username
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
it 'rejects non existent user ids' do
get(:users, author_id: 99999)
expect(body.collect { |u| u['id'] }).not_to include(99999)
end
end
context 'skip_users parameter included' do
before { sign_in(user) }
it 'skips the user IDs passed' do
get(:users, skip_users: [user, user2].map(&:id))
other_user_ids = [non_member, project.owner, project.creator].map(&:id)
response_user_ids = JSON.parse(response.body).map { |user| user['id'] }
expect(response_user_ids).to contain_exactly(*other_user_ids)
end
2015-09-11 14:41:01 +05:30
end
2015-04-26 12:48:37 +05:30
end
2016-06-02 11:05:42 +05:30
2016-09-13 17:45:13 +05:30
context 'projects' do
let(:authorized_project) { create(:project) }
let(:authorized_search_project) { create(:project, name: 'rugged') }
2016-06-02 11:05:42 +05:30
before do
sign_in(user)
2016-09-13 17:45:13 +05:30
project.team << [user, :master]
2016-06-02 11:05:42 +05:30
end
2016-09-13 17:45:13 +05:30
context 'authorized projects' do
before do
authorized_project.team << [user, :master]
end
describe 'GET #projects with project ID' do
before do
get(:projects, project_id: project.id)
end
2016-06-02 11:05:42 +05:30
2016-09-13 17:45:13 +05:30
let(:body) { JSON.parse(response.body) }
2016-06-02 11:05:42 +05:30
2016-09-13 17:45:13 +05:30
it do
expect(body).to be_kind_of(Array)
expect(body.size).to eq 2
expect(body.first['id']).to eq 0
expect(body.first['name_with_namespace']).to eq 'No project'
expect(body.last['id']).to eq authorized_project.id
expect(body.last['name_with_namespace']).to eq authorized_project.name_with_namespace
end
end
2016-06-02 11:05:42 +05:30
end
2016-09-13 17:45:13 +05:30
context 'authorized projects and search' do
before do
authorized_project.team << [user, :master]
authorized_search_project.team << [user, :master]
end
describe 'GET #projects with project ID and search' do
before do
get(:projects, project_id: project.id, search: 'rugged')
end
let(:body) { JSON.parse(response.body) }
it do
expect(body).to be_kind_of(Array)
expect(body.size).to eq 2
2016-06-02 11:05:42 +05:30
2016-09-13 17:45:13 +05:30
expect(body.last['id']).to eq authorized_search_project.id
expect(body.last['name_with_namespace']).to eq authorized_search_project.name_with_namespace
end
end
end
2016-09-29 09:46:39 +05:30
context 'authorized projects apply limit' do
before do
authorized_project2 = create(:project)
authorized_project3 = create(:project)
authorized_project.team << [user, :master]
authorized_project2.team << [user, :master]
authorized_project3.team << [user, :master]
stub_const 'MoveToProjectFinder::PAGE_SIZE', 2
end
describe 'GET #projects with project ID' do
before do
get(:projects, project_id: project.id)
end
let(:body) { JSON.parse(response.body) }
it do
expect(body).to be_kind_of(Array)
expect(body.size).to eq 3 # Of a total of 4
end
end
end
context 'authorized projects with offset' do
before do
authorized_project2 = create(:project)
authorized_project3 = create(:project)
authorized_project.team << [user, :master]
authorized_project2.team << [user, :master]
authorized_project3.team << [user, :master]
end
describe 'GET #projects with project ID and offset_id' do
before do
get(:projects, project_id: project.id, offset_id: authorized_project.id)
end
let(:body) { JSON.parse(response.body) }
it do
expect(body.detect { |item| item['id'] == 0 }).to be_nil # 'No project' is not there
expect(body.detect { |item| item['id'] == authorized_project.id }).to be_nil # Offset project is not there either
end
end
end
2016-09-13 17:45:13 +05:30
context 'authorized projects without admin_issue ability' do
before(:each) do
authorized_project.team << [user, :guest]
expect(user.can?(:admin_issue, authorized_project)).to eq(false)
end
describe 'GET #projects with project ID' do
before do
get(:projects, project_id: project.id)
end
let(:body) { JSON.parse(response.body) }
it do
expect(body).to be_kind_of(Array)
expect(body.size).to eq 1 # 'No project'
expect(body.first['id']).to eq 0
end
end
2016-06-02 11:05:42 +05:30
end
end
2015-04-26 12:48:37 +05:30
end