2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
RSpec.describe Dashboard::TodosController do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:author) { create(:user) }
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { create(:project) }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:todo_service) { TodoService.new }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
2018-03-17 18:26:18 +05:30
|
|
|
project.add_developer(user)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET #index' do
|
|
|
|
context 'project authorization' do
|
|
|
|
it 'renders 404 when user does not have read access on given project' do
|
2017-09-10 17:25:29 +05:30
|
|
|
unauthorized_project = create(:project, :private)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
get :index, params: { project_id: unauthorized_project.id }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders 404 when given project does not exists' do
|
2020-04-22 19:07:51 +05:30
|
|
|
get :index, params: { project_id: non_existing_record_id }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders 200 when filtering for "any project" todos' do
|
2019-02-15 15:39:39 +05:30
|
|
|
get :index, params: { project_id: '' }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders 200 when user has access on given project' do
|
2017-09-10 17:25:29 +05:30
|
|
|
authorized_project = create(:project, :public)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
get :index, params: { project_id: authorized_project.id }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
context "with render_views" do
|
|
|
|
render_views
|
|
|
|
|
|
|
|
it 'avoids N+1 queries', :request_store do
|
|
|
|
merge_request = create(:merge_request, source_project: project)
|
|
|
|
create(:todo, project: project, author: author, user: user, target: merge_request)
|
|
|
|
create(:issue, project: project, assignees: [user])
|
|
|
|
|
|
|
|
group = create(:group)
|
|
|
|
group.add_owner(user)
|
|
|
|
|
|
|
|
get :index
|
|
|
|
|
|
|
|
control = ActiveRecord::QueryRecorder.new { get :index }
|
|
|
|
|
|
|
|
create(:issue, project: project, assignees: [user])
|
|
|
|
group_2 = create(:group)
|
|
|
|
group_2.add_owner(user)
|
|
|
|
project_2 = create(:project)
|
|
|
|
project_2.add_developer(user)
|
|
|
|
merge_request_2 = create(:merge_request, source_project: project_2)
|
|
|
|
create(:todo, project: project, author: author, user: user, target: merge_request_2)
|
|
|
|
|
|
|
|
expect { get :index }.not_to exceed_query_limit(control)
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-29 20:51:05 +05:30
|
|
|
context 'group authorization' do
|
|
|
|
it 'renders 404 when user does not have read access on given group' do
|
|
|
|
unauthorized_group = create(:group, :private)
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
get :index, params: { group_id: unauthorized_group.id }
|
2018-11-29 20:51:05 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2018-11-29 20:51:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
it_behaves_like 'paginated collection' do
|
2018-03-17 18:26:18 +05:30
|
|
|
let!(:issues) { create_list(:issue, 3, project: project, assignees: [user]) }
|
2019-12-04 20:38:33 +05:30
|
|
|
let(:collection) { user.todos }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
issues.each { |issue| todo_service.new_issue(issue, user) }
|
2018-03-17 18:26:18 +05:30
|
|
|
allow(Kaminari.config).to receive(:default_per_page).and_return(2)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
context 'when providing no filters' do
|
|
|
|
it 'does not perform a query to get the page count, but gets that from the user' do
|
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
|
|
|
|
|
|
|
expect(user).to receive(:todos_pending_count).and_call_original
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
get :index, params: { page: (last_page + 1).to_param, sort: :created_asc }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
expect(response).to redirect_to(dashboard_todos_path(page: last_page, sort: :created_asc))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when providing filters' do
|
|
|
|
it 'performs a query to get the correct page count' do
|
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
|
|
|
|
|
|
|
expect(user).not_to receive(:todos_pending_count)
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
get :index, params: { page: (last_page + 1).to_param, project_id: project.id }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
expect(response).to redirect_to(dashboard_todos_path(page: last_page, project_id: project.id))
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
context 'external authorization' do
|
|
|
|
subject { get :index }
|
|
|
|
|
|
|
|
it_behaves_like 'disabled when using an external authorization service'
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PATCH #restore' do
|
|
|
|
let(:todo) { create(:todo, :done, user: user, project: project, author: author) }
|
|
|
|
|
|
|
|
it 'restores the todo to pending state' do
|
2019-02-15 15:39:39 +05:30
|
|
|
patch :restore, params: { id: todo.id }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(todo.reload).to be_pending
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(json_response).to eq({ "count" => 1, "done_count" => 0 })
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PATCH #bulk_restore' do
|
|
|
|
let(:todos) { create_list(:todo, 2, :done, user: user, project: project, author: author) }
|
|
|
|
|
|
|
|
it 'restores the todos to pending state' do
|
2019-02-15 15:39:39 +05:30
|
|
|
patch :bulk_restore, params: { ids: todos.map(&:id) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
todos.each do |todo|
|
|
|
|
expect(todo.reload).to be_pending
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(json_response).to eq({ 'count' => 2, 'done_count' => 0 })
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|