Commit ac661769 authored by Rémy Coutable's avatar Rémy Coutable

Fix and improve spec/controllers/autocomplete_controller_spec.rb

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 0ca8a36a
require 'spec_helper'
describe AutocompleteController do
let!(:project) { create(:project) }
let!(:user) { create(:user) }
let(:project) { create(:project) }
let(:user) { project.owner }
context 'GET users' do
let!(:user2) { create(:user) }
......@@ -11,8 +11,6 @@ describe AutocompleteController do
context 'project members' do
before do
sign_in(user)
project.add_master(user)
project.add_developer(user2)
end
describe 'GET #users with project ID' do
......@@ -20,11 +18,11 @@ describe AutocompleteController do
get(:users, project_id: project.id)
end
let(:body) { JSON.parse(response.body) }
it { expect(body).to be_kind_of(Array) }
it { expect(body.size).to eq 3 }
it { expect(body.map { |u| u["username"] }).to match_array([project.owner.username, user.username, user2.username]) }
it 'returns the project members' do
expect(json_response).to be_kind_of(Array)
expect(json_response.size).to eq(1)
expect(json_response.map { |u| u["username"] }).to include(user.username)
end
end
describe 'GET #users with unknown project' do
......@@ -34,65 +32,26 @@ describe AutocompleteController do
it { expect(response).to have_http_status(404) }
end
describe "GET #users that can push to protected branches" do
before do
get(:users, project_id: project.id, push_code_to_protected_branches: 'true')
end
let(:body) { JSON.parse(response.body) }
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([project.owner.username, user.username]) }
end
describe "GET #users that can push code" do
let(:reporter_user) { create(:user) }
before do
project.team << [reporter_user, :reporter]
get(:users, project_id: project.id, push_code: 'true')
end
let(:body) { JSON.parse(response.body) }
it { expect(body).to be_kind_of(Array) }
it { expect(body.size).to eq 3 }
it { expect(body.map { |user| user["username"] }).to match_array([project.owner.username, user.username, user2.username]) }
end
describe "GET #users that can push to protected branches, including the current user" do
before do
get(:users, project_id: project.id, push_code_to_protected_branches: true, current_user: true)
end
let(:body) { JSON.parse(response.body) }
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([project.owner.username, user.username]) }
end
end
context 'group members' do
let(:group) { create(:group) }
before do
sign_in(user)
group.add_owner(user)
sign_in(user)
end
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 }
it 'returns the group members' do
expect(json_response).to be_kind_of(Array)
expect(json_response.size).to eq(1)
expect(json_response.first["username"]).to eq user.username
end
end
describe 'GET #users with unknown group ID' do
......@@ -105,23 +64,22 @@ describe AutocompleteController do
end
context 'non-member login for public project' do
let!(:project) { create(:project, :public) }
let(:project) { create(:project, :public) }
before do
sign_in(non_member)
project.add_master(user)
end
let(:body) { JSON.parse(response.body) }
describe 'GET #users with project ID' do
before do
get(:users, project_id: project.id, current_user: true)
end
it { expect(body).to be_kind_of(Array) }
it { expect(body.size).to eq 3 }
it { expect(body.map { |u| u['username'] }).to include(user.username, non_member.username) }
it 'returns the project members and non-members' do
expect(json_response).to be_kind_of(Array)
expect(json_response.size).to eq(2)
expect(json_response.map { |u| u['username'] }).to include(user.username, non_member.username)
end
end
end
......@@ -131,10 +89,8 @@ describe AutocompleteController do
get(:users)
end
let(:body) { JSON.parse(response.body) }
it { expect(body).to be_kind_of(Array) }
it { expect(body.size).to eq User.count }
it { expect(json_response).to be_kind_of(Array) }
it { expect(json_response.size).to eq User.count }
end
context 'user order' do
......@@ -146,7 +102,7 @@ describe AutocompleteController do
sign_in(user)
get(:users, search: 'user')
response_usernames = JSON.parse(response.body).map { |user| user['username'] }
response_usernames = json_response.map { |user| user['username'] }
expect(response_usernames.take(3)).to match_array([user.username, reported_user.username, user1.username])
end
......@@ -160,15 +116,12 @@ describe AutocompleteController do
get(:users, per_page: per_page)
end
let(:body) { JSON.parse(response.body) }
it { expect(body).to be_kind_of(Array) }
it { expect(body.size).to eq per_page }
it { expect(json_response).to be_kind_of(Array) }
it { expect(json_response.size).to eq(per_page) }
end
context 'unauthenticated user' do
let(:public_project) { create(:project, :public) }
let(:body) { JSON.parse(response.body) }
describe 'GET #users with public project' do
before do
......@@ -176,8 +129,8 @@ describe AutocompleteController do
get(:users, project_id: public_project.id)
end
it { expect(body).to be_kind_of(Array) }
it { expect(body.size).to eq 2 }
it { expect(json_response).to be_kind_of(Array) }
it { expect(json_response.size).to eq 2 }
end
describe 'GET #users with project' do
......@@ -210,8 +163,8 @@ describe AutocompleteController do
get(:users)
end
it { expect(body).to be_kind_of(Array) }
it { expect(body.size).to eq 0 }
it { expect(json_response).to be_kind_of(Array) }
it { expect(json_response).to be_empty }
end
describe 'GET #users with todo filter' do
......@@ -219,14 +172,12 @@ describe AutocompleteController do
get :users, todo_filter: true
expect(response.status).to eq 200
expect(body).to be_kind_of(Array)
expect(json_response).to be_kind_of(Array)
end
end
end
context 'author of issuable included' do
let(:body) { JSON.parse(response.body) }
context 'authenticated' do
before do
sign_in(user)
......@@ -235,13 +186,13 @@ describe AutocompleteController do
it 'includes the author' do
get(:users, author_id: non_member.id)
expect(body.first["username"]).to eq non_member.username
expect(json_response.first["username"]).to eq non_member.username
end
it 'rejects non existent user ids' do
get(:users, author_id: 99999)
expect(body.collect { |u| u['id'] }).not_to include(99999)
expect(json_response.collect { |u| u['id'] }).not_to include(99999)
end
end
......@@ -249,7 +200,7 @@ describe AutocompleteController do
it 'returns empty result' do
get(:users, author_id: non_member.id)
expect(body).to be_empty
expect(json_response).to be_empty
end
end
end
......@@ -262,10 +213,9 @@ describe AutocompleteController do
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'] }
response_user_ids = json_response.map { |user| user['id'] }
expect(response_user_ids).to contain_exactly(*other_user_ids)
expect(response_user_ids).to contain_exactly(non_member.id)
end
end
end
......@@ -289,17 +239,15 @@ describe AutocompleteController 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 2
it 'returns projects' do
expect(json_response).to be_kind_of(Array)
expect(json_response.size).to eq(2)
expect(body.first['id']).to eq 0
expect(body.first['name_with_namespace']).to eq 'No project'
expect(json_response.first['id']).to eq(0)
expect(json_response.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
expect(json_response.last['id']).to eq authorized_project.id
expect(json_response.last['name_with_namespace']).to eq authorized_project.name_with_namespace
end
end
end
......@@ -315,14 +263,12 @@ describe AutocompleteController do
get(:projects, project_id: project.id, search: 'rugged')
end
let(:body) { JSON.parse(response.body) }
it 'returns projects' do
expect(json_response).to be_kind_of(Array)
expect(json_response.size).to eq(2)
it do
expect(body).to be_kind_of(Array)
expect(body.size).to eq 2
expect(body.last['id']).to eq authorized_search_project.id
expect(body.last['name_with_namespace']).to eq authorized_search_project.name_with_namespace
expect(json_response.last['id']).to eq authorized_search_project.id
expect(json_response.last['name_with_namespace']).to eq authorized_search_project.name_with_namespace
end
end
end
......@@ -344,11 +290,9 @@ describe AutocompleteController 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
it 'returns projects' do
expect(json_response).to be_kind_of(Array)
expect(json_response.size).to eq 3 # Of a total of 4
end
end
end
......@@ -368,11 +312,9 @@ describe AutocompleteController 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
it 'returns "No project"' do
expect(json_response.detect { |item| item['id'] == 0 }).to be_nil # 'No project' is not there
expect(json_response.detect { |item| item['id'] == authorized_project.id }).to be_nil # Offset project is not there either
end
end
end
......@@ -389,55 +331,12 @@ describe AutocompleteController 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
it 'returns a single "No project"' do
expect(json_response).to be_kind_of(Array)
expect(json_response.size).to eq(1) # 'No project'
expect(json_response.first['id']).to eq 0
end
end
end
end
context "groups" do
let(:matching_group) { create(:group) }
let(:non_matching_group) { create(:group) }
context "while fetching all groups belonging to a project" do
before do
project.team << [user, :developer]
project.invited_groups << matching_group
sign_in(user)
get(:project_groups, project_id: project.id)
end
let(:body) { JSON.parse(response.body) }
it { expect(body).to be_kind_of(Array) }
it { expect(body.size).to eq 1 }
it { expect(body.first.values_at('id', 'name')).to eq [matching_group.id, matching_group.name] }
end
context "while fetching all groups belonging to a project the current user cannot access" do
before do
project.invited_groups << matching_group
sign_in(user)
get(:project_groups, project_id: project.id)
end
it { expect(response).to be_not_found }
end
context "while fetching all groups belonging to an invalid project ID" do
before do
project.invited_groups << matching_group
sign_in(user)
get(:project_groups, project_id: 'invalid')
end
it { expect(response).to be_not_found }
end
end
end
require 'spec_helper'
describe AutocompleteController do
let(:project) { create(:project) }
let(:user) { project.owner }
context 'GET users' do
let!(:user2) { create(:user) }
let!(:non_member) { create(:user) }
context 'project members' do
before do
project.add_developer(user2)
sign_in(user)
end
describe "GET #users that can push to protected branches" do
before do
get(:users, project_id: project.id, push_code_to_protected_branches: 'true')
end
it 'returns authorized users' do
expect(json_response).to be_kind_of(Array)
expect(json_response.size).to eq(1)
expect(json_response.map { |u| u["username"] }).to match_array([user.username])
end
end
describe "GET #users that can push code" do
let(:reporter_user) { create(:user) }
before do
project.add_reporter(reporter_user)
get(:users, project_id: project.id, push_code: 'true')
end
it 'returns authorized users' do
expect(json_response).to be_kind_of(Array)
expect(json_response.size).to eq(2)
expect(json_response.map { |user| user["username"] }).to match_array([user.username, user2.username])
end
end
describe "GET #users that can push to protected branches, including the current user" do
before do
get(:users, project_id: project.id, push_code_to_protected_branches: true, current_user: true)
end
it 'returns authorized users' do
expect(json_response).to be_kind_of(Array)
expect(json_response.size).to eq(1)
expect(json_response.map { |u| u["username"] }).to match_array([user.username])
end
end
end
end
context "groups" do
let(:matching_group) { create(:group) }
let(:non_matching_group) { create(:group) }
let(:user2) { create(:user) }
before do
project.invited_groups << matching_group
end
context "while fetching all groups belonging to a project" do
before do
sign_in(user)
get(:project_groups, project_id: project.id)
end
it 'returns a single group' do
expect(json_response).to be_kind_of(Array)
expect(json_response.size).to eq(1)
expect(json_response.first.values_at('id', 'name')).to eq [matching_group.id, matching_group.name]
end
end
context "while fetching all groups belonging to a project the current user cannot access" do
before do
sign_in(user2)
get(:project_groups, project_id: project.id)
end
it { expect(response).to be_not_found }
end
context "while fetching all groups belonging to an invalid project ID" do
before do
sign_in(user)
get(:project_groups, project_id: 'invalid')
end
it { expect(response).to be_not_found }
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment