scinote-web/spec/controllers/projects_controller_spec.rb

169 lines
5.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require 'rails_helper'
describe ProjectsController, type: :controller do
login_user
include_context 'reference_project_structure', {
projects: 3,
skip_my_module: true
}
describe '#index' do
2020-10-26 23:17:39 +08:00
let(:params) { { team: team.id, sort: 'atoz' } }
2020-10-26 23:17:39 +08:00
it 'returns success response' do
get :index, params: params
expect(response).to have_http_status(:success)
end
end
describe 'POST create' do
context 'in JSON format' do
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 }
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'
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'
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
end
end
describe '#edit' do
context 'in JSON format' do
let(:params) { { id: projects.first.id } }
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'
end
end
end
describe 'PUT update' do
context 'in HTML format' do
let(:action) { put :update, params: params }
let(:params) do
{ id: projects.first.id,
project: { name: projects.first.name,
team_id: projects.first.team.id,
visibility: projects.first.visibility,
default_public_user_role_id: projects.first.default_public_user_role.id } }
end
it 'returns redirect response' do
action
expect(response).to have_http_status(:redirect)
2019-09-12 23:21:48 +08:00
expect(response.media_type).to eq 'text/html'
end
2019-03-08 00:26:42 +08:00
it 'calls create activity service (project_grant_access_to_all_team_members)' do
params[:project][:visibility] = 'visible'
2019-03-08 00:26:42 +08:00
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :project_grant_access_to_all_team_members))
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))
action
2019-03-08 00:26:42 +08:00
end
it 'calls create activity service (restore_project)' do
projects.first.update(archived: true)
2019-03-08 00:26:42 +08:00
params[:project][:archived] = false
expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :restore_project))
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))
action
end
2019-03-08 00:26:42 +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
end
end
describe '#show' do
context 'in HTML format' do
let(:params) do
{ id: projects.first.id, sort: 'old',
project: { name: 'test project A1', team_id: team.id,
visibility: 'visible' } }
end
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'
end
end
end
describe '#notifications' do
context 'in JSON format' do
let(:params) do
{ id: projects.first.id,
project: { name: 'test project A1', team_id: team.id,
visibility: 'visible' } }
end
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'
end
end
end
describe '#experiment_archive' do
context 'in HTML format' do
let(:params) do
{ id: projects.first.id,
2021-02-26 23:29:56 +08:00
view_mode: :archived,
project: { name: 'test project A1', team_id: team.id,
visibility: 'visible' } }
end
it 'returns success response' do
2021-02-26 23:29:56 +08:00
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'
end
end
end
end