2018-10-10 06:09:32 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe ProjectsController, type: :controller do
|
|
|
|
login_user
|
|
|
|
render_views
|
|
|
|
|
|
|
|
PROJECTS_CNT = 26
|
|
|
|
time = Time.new(2015, 8, 1, 14, 35, 0)
|
|
|
|
let!(:user) { User.first }
|
|
|
|
let!(:team) { create :team, created_by: user }
|
|
|
|
let!(:user_team) { create :user_team, team: team, user: user }
|
2018-10-24 04:24:08 +08:00
|
|
|
before do
|
|
|
|
@projects_overview = ProjectsOverviewService.new(team, user, params)
|
2018-10-10 06:09:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
let!(:project_1) do
|
|
|
|
create :project, name: 'test project D', visibility: 1, team: team,
|
|
|
|
archived: false, created_at: time.advance(hours: 2),
|
|
|
|
created_by: user
|
|
|
|
end
|
|
|
|
let!(:project_2) do
|
|
|
|
create :project, name: 'test project B', visibility: 1, team: team,
|
|
|
|
archived: true, created_at: time, created_by: user
|
|
|
|
end
|
|
|
|
let!(:project_3) do
|
|
|
|
create :project, name: 'test project C', visibility: 1, team: team,
|
|
|
|
archived: false, created_at: time.advance(hours: 3),
|
|
|
|
created_by: user
|
|
|
|
end
|
|
|
|
let!(:project_4) do
|
|
|
|
create :project, name: 'test project A', visibility: 1, team: team,
|
|
|
|
archived: true, created_at: time.advance(hours: 1),
|
|
|
|
created_by: user
|
|
|
|
end
|
|
|
|
let!(:project_5) do
|
|
|
|
create :project, name: 'test project E', visibility: 1, team: team,
|
|
|
|
archived: true, created_at: time.advance(hours: 5),
|
|
|
|
created_by: user
|
|
|
|
end
|
|
|
|
let!(:project_6) do
|
|
|
|
create :project, name: 'test project F', visibility: 0, team: team,
|
|
|
|
archived: false, created_at: time.advance(hours: 4),
|
|
|
|
created_by: user
|
|
|
|
end
|
|
|
|
(7..PROJECTS_CNT).each do |i|
|
|
|
|
let!("project_#{i}") do
|
|
|
|
create :project, name: "test project #{(64 + i).chr}",
|
|
|
|
visibility: 1,
|
|
|
|
team: team, archived: i % 2,
|
|
|
|
created_at: time.advance(hours: 6, minutes: i),
|
|
|
|
created_by: user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-15 20:45:05 +08:00
|
|
|
# rubocop:disable Security/Eval
|
|
|
|
# rubocop:disable Style/EvalWithLocation
|
2018-10-10 06:09:32 +08:00
|
|
|
(1..PROJECTS_CNT).each do |i|
|
|
|
|
let!("user_projects_#{i}") do
|
2019-01-15 20:45:05 +08:00
|
|
|
create :user_project, :owner, project: eval("project_#{i}"), user: user
|
2018-10-10 06:09:32 +08:00
|
|
|
end
|
|
|
|
end
|
2019-01-15 20:45:05 +08:00
|
|
|
# rubocop:enable Security/Eval
|
|
|
|
# rubocop:enable Style/EvalWithLocation
|
2018-10-10 06:09:32 +08:00
|
|
|
|
|
|
|
describe '#index' do
|
|
|
|
context 'in JSON format' do
|
2018-10-24 04:24:08 +08:00
|
|
|
let(:params) { { team: team.id, sort: 'atoz' } }
|
|
|
|
|
2018-10-10 06:09:32 +08:00
|
|
|
it 'returns success response' do
|
|
|
|
get :index, params: params, format: :json
|
|
|
|
expect(response).to have_http_status(:success)
|
2019-09-12 23:21:48 +08:00
|
|
|
expect(response.media_type).to eq 'application/json'
|
2018-10-10 06:09:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#index_dt' do
|
|
|
|
context 'in JSON format' do
|
2018-10-24 04:24:08 +08:00
|
|
|
let(:params) do
|
|
|
|
{ start: 1, length: 2, order: { '0': { dir: 'ASC', column: '2' } } }
|
|
|
|
end
|
|
|
|
|
2018-10-10 06:09:32 +08:00
|
|
|
it 'returns success response' do
|
|
|
|
get :index_dt, params: params, format: :json
|
|
|
|
expect(response).to have_http_status(:success)
|
2019-09-12 23:21:48 +08:00
|
|
|
expect(response.media_type).to eq 'application/json'
|
2018-10-10 06:09:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-27 18:24:44 +08:00
|
|
|
describe 'POST create' do
|
2018-10-10 06:09:32 +08:00
|
|
|
context 'in JSON format' do
|
2018-10-24 04:24:08 +08:00
|
|
|
let(:params) do
|
|
|
|
{ project: { name: 'test project A1', team_id: team.id,
|
|
|
|
visibility: 'visible', archived: false } }
|
|
|
|
end
|
2019-03-13 16:06:48 +08:00
|
|
|
let(:action) { post :create, params: params, format: :json }
|
2018-10-24 04:24:08 +08:00
|
|
|
|
2018-10-10 06:09:32 +08:00
|
|
|
it 'returns success response, then unprocessable_entity on second run' do
|
|
|
|
get :create, params: params, format: :json
|
|
|
|
expect(response).to have_http_status(:success)
|
2019-09-12 23:21:48 +08:00
|
|
|
expect(response.media_type).to eq 'application/json'
|
2018-10-10 06:09:32 +08:00
|
|
|
get :create, params: params, format: :json
|
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
2019-09-12 23:21:48 +08:00
|
|
|
expect(response.media_type).to eq 'application/json'
|
2018-10-10 06:09:32 +08:00
|
|
|
end
|
2019-02-27 14:30:43 +08:00
|
|
|
|
2019-03-13 16:06:48 +08:00
|
|
|
it 'calls create activity for creating project' do
|
|
|
|
expect(Activities::CreateActivityService)
|
|
|
|
.to(receive(:call)
|
|
|
|
.with(hash_including(activity_type: :create_project)))
|
2019-02-27 14:30:43 +08:00
|
|
|
|
2019-03-13 16:06:48 +08:00
|
|
|
action
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds activity in DB' do
|
|
|
|
expect { action }
|
|
|
|
.to(change { Activity.count })
|
2019-02-27 14:30:43 +08:00
|
|
|
end
|
2018-10-10 06:09:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#edit' do
|
|
|
|
context 'in JSON format' do
|
2018-10-24 04:24:08 +08:00
|
|
|
let(:params) { { id: project_1.id } }
|
|
|
|
|
2018-10-10 06:09:32 +08:00
|
|
|
it 'returns success response' do
|
|
|
|
get :edit, params: params, format: :json
|
|
|
|
expect(response).to have_http_status(:success)
|
2019-09-12 23:21:48 +08:00
|
|
|
expect(response.media_type).to eq 'application/json'
|
2018-10-10 06:09:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-27 18:24:44 +08:00
|
|
|
describe 'PUT update' do
|
2018-10-10 06:09:32 +08:00
|
|
|
context 'in HTML format' do
|
2019-03-27 18:24:44 +08:00
|
|
|
let(:action) { put :update, params: params }
|
2018-10-24 04:24:08 +08:00
|
|
|
let(:params) do
|
|
|
|
{ id: project_1.id,
|
2019-03-08 00:26:42 +08:00
|
|
|
project: { name: project_1.name, team_id: project_1.team.id,
|
|
|
|
visibility: project_1.visibility } }
|
2018-10-24 04:24:08 +08:00
|
|
|
end
|
|
|
|
|
2018-10-10 06:09:32 +08:00
|
|
|
it 'returns redirect response' do
|
2019-03-27 18:24:44 +08:00
|
|
|
action
|
2018-10-10 06:09:32 +08:00
|
|
|
expect(response).to have_http_status(:redirect)
|
2019-09-12 23:21:48 +08:00
|
|
|
expect(response.media_type).to eq 'text/html'
|
2018-10-10 06:09:32 +08:00
|
|
|
end
|
2019-03-08 00:26:42 +08:00
|
|
|
|
|
|
|
it 'calls create activity service (change_project_visibility)' do
|
|
|
|
params[:project][:visibility] = 'hidden'
|
|
|
|
expect(Activities::CreateActivityService).to receive(:call)
|
|
|
|
.with(hash_including(activity_type: :change_project_visibility))
|
2019-03-27 18:24:44 +08:00
|
|
|
action
|
2019-03-08 00:26:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls create activity service (rename_project)' do
|
|
|
|
params[:project][:name] = 'test project changed'
|
|
|
|
expect(Activities::CreateActivityService).to receive(:call)
|
|
|
|
.with(hash_including(activity_type: :rename_project))
|
2019-03-27 18:24:44 +08:00
|
|
|
action
|
2019-03-08 00:26:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls create activity service (restore_project)' do
|
|
|
|
project_1.update(archived: true)
|
|
|
|
params[:project][:archived] = false
|
|
|
|
expect(Activities::CreateActivityService).to receive(:call)
|
|
|
|
.with(hash_including(activity_type: :restore_project))
|
2019-03-27 18:24:44 +08:00
|
|
|
action
|
2019-03-08 00:26:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls create activity service (archive_project)' do
|
|
|
|
params[:project][:archived] = true
|
|
|
|
expect(Activities::CreateActivityService).to receive(:call)
|
|
|
|
.with(hash_including(activity_type: :archive_project))
|
2019-03-27 18:24:44 +08:00
|
|
|
action
|
|
|
|
end
|
2019-03-08 00:26:42 +08:00
|
|
|
|
2019-03-27 18:24:44 +08:00
|
|
|
it 'adds activity in DB' do
|
|
|
|
params[:project][:archived] = true
|
|
|
|
expect { action }
|
|
|
|
.to(change { Activity.count })
|
2019-03-08 00:26:42 +08:00
|
|
|
end
|
2018-10-10 06:09:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#show' do
|
|
|
|
context 'in HTML format' do
|
2018-10-24 04:24:08 +08:00
|
|
|
let(:params) do
|
|
|
|
{ id: project_1.id, sort: 'old',
|
|
|
|
project: { name: 'test project A1', team_id: team.id,
|
|
|
|
visibility: 'visible' } }
|
|
|
|
end
|
|
|
|
|
2018-10-10 06:09:32 +08:00
|
|
|
it 'returns success response' do
|
|
|
|
get :show, params: params
|
|
|
|
expect(response).to have_http_status(:success)
|
2019-09-12 23:21:48 +08:00
|
|
|
expect(response.media_type).to eq 'text/html'
|
2018-10-10 06:09:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#notifications' do
|
|
|
|
context 'in JSON format' do
|
2018-10-24 04:24:08 +08:00
|
|
|
let(:params) do
|
|
|
|
{ id: project_1.id,
|
|
|
|
project: { name: 'test project A1', team_id: team.id,
|
|
|
|
visibility: 'visible' } }
|
|
|
|
end
|
|
|
|
|
2018-10-10 06:09:32 +08:00
|
|
|
it 'returns success response' do
|
|
|
|
get :notifications, format: :json, params: params
|
|
|
|
expect(response).to have_http_status(:success)
|
2019-09-12 23:21:48 +08:00
|
|
|
expect(response.media_type).to eq 'application/json'
|
2018-10-10 06:09:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#experiment_archive' do
|
|
|
|
context 'in HTML format' do
|
2018-10-24 04:24:08 +08:00
|
|
|
let(:params) do
|
|
|
|
{ id: project_1.id,
|
|
|
|
project: { name: 'test project A1', team_id: team.id,
|
|
|
|
visibility: 'visible' } }
|
|
|
|
end
|
|
|
|
|
2018-10-10 06:09:32 +08:00
|
|
|
it 'returns success response' do
|
|
|
|
get :experiment_archive, params: params
|
|
|
|
expect(response).to have_http_status(:success)
|
2019-09-12 23:21:48 +08:00
|
|
|
expect(response.media_type).to eq 'text/html'
|
2018-10-10 06:09:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|