OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/spec/controllers/projects_controller_spec.rb

202 lines
5.6 KiB

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2021 the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++
require 'spec_helper'
describe ProjectsController, type: :controller do
shared_let(:admin) { FactoryBot.create :admin }
let(:non_member) { FactoryBot.create :non_member }
before do
allow(@controller).to receive(:set_localization)
login_as admin
@params = {}
end
describe '#new' do
it "renders 'new'" do
get 'new', params: @params
expect(response).to be_successful
expect(response).to render_template 'new'
end
context 'by non-admin user with add_project permission' do
let(:non_member_user) { FactoryBot.create :user }
before do
non_member.add_permission! :add_project
login_as non_member_user
end
it 'should accept get' do
get :new
expect(response).to be_successful
expect(response).to render_template 'new'
end
end
end
describe 'index.html' do
let(:project_a) { FactoryBot.create(:project, name: 'Project A', public: false, active: true) }
let(:project_b) { FactoryBot.create(:project, name: 'Project B', public: false, active: true) }
let(:project_c) { FactoryBot.create(:project, name: 'Project C', public: true, active: true) }
let(:project_d) { FactoryBot.create(:project, name: 'Project D', public: true, active: false) }
let(:projects) { [project_a, project_b, project_c, project_d] }
before do
Role.anonymous
Role.non_member
projects
login_as(user)
get 'index'
end
shared_examples_for 'successful index' do
it 'is success' do
expect(response).to be_successful
end
it 'renders the index template' do
expect(response).to render_template 'index'
end
end
context 'as admin' do
let(:user) { FactoryBot.build(:admin) }
it_behaves_like 'successful index'
it "shows all active projects" do
expect(assigns[:projects])
.to match_array [project_a, project_b, project_c]
end
end
context 'as anonymous user' do
let(:user) { User.anonymous }
it_behaves_like 'successful index'
it "shows only (active) public projects" do
expect(assigns[:projects])
.to match_array [project_c]
end
end
context 'as user' do
User custom fields in the invite user modal (#9220) * Projects form working with formly 50% * Removed console.log * Working with formattable * Working with formattable * Input with id and label * Input with id and label * Useless dependencies removed * Saving forms + required labels with * * First backend validation approach * Removed reload on type change + keep model on route changes * Handlig backend validations with setError * Formatting the form model to submit * Make up refactor * working with op-form-field * Form creation moved to the service * Working with op-form-field wrapper * Working with validation and op-form-field * Working with []CustomFields * Clean up * Clean up * Clean up * Clean up * Form routing working * Notification on form error and success * Refactor + removed useless dynamic form observable * DynamicFieldsService with tests * Refactor: inputs catalog + catch form load error * Filter out non writable fields * Refactor: naming consistency * Cleaning comments * dynamic-fields-service tests + wrapper component * DynamicForm Tests * @ngx-formly/core dependency added * Cleaning up * Provide DynamicFieldsService in root so it can be used independently * DynamicForm working as a FormControl * Getting route params sync * Global FormsService: submit + formatting + error handling * Fix: @Optional() FormGroupDirective in OpFormFieldComponent * Code climate fix * Removed CdkTextareaAutosize because of CDK issue 22469 * DynamicFormComponent tests * Dynamic input test helpers + boolean and text tests * Refactor edit fields to avoid circular dependencies in the dynamic forms * Naming fix * IntegerInputComponent tests * SelectInputComponent tests * Fix: duplicated identifier on inputs * Extract toolbar to be reused for now Still TBD whether we want to move them right now to the frontend? * Create new project route and redirect to rails view after saving * fieldsSettingsPipe + hide 'identifier' on projects * Handling multi-values (also as links) and passwords * Some TODOs removed * FormattableTextareaInputComponent tests * Projects form working with formly 50% * Removed console.log * Working with formattable * Working with formattable * Input with id and label * Input with id and label * Useless dependencies removed * Saving forms + required labels with * * First backend validation approach * Removed reload on type change + keep model on route changes * Handlig backend validations with setError * Formatting the form model to submit * Make up refactor * working with op-form-field * Form creation moved to the service * Working with op-form-field wrapper * Working with validation and op-form-field * Working with []CustomFields * Clean up * Clean up * Clean up * Clean up * Form routing working * Notification on form error and success * Refactor + removed useless dynamic form observable * DynamicFieldsService with tests * Refactor: inputs catalog + catch form load error * Filter out non writable fields * Refactor: naming consistency * Cleaning comments * dynamic-fields-service tests + wrapper component * DynamicForm Tests * @ngx-formly/core dependency added * Cleaning up * DynamicForm working as a FormControl * Getting route params sync * Global FormsService: submit + formatting + error handling * Fix: @Optional() FormGroupDirective in OpFormFieldComponent * Code climate fix * Removed CdkTextareaAutosize because of CDK issue 22469 * DynamicFormComponent tests * Dynamic input test helpers + boolean and text tests * Refactor edit fields to avoid circular dependencies in the dynamic forms * Naming fix * IntegerInputComponent tests * SelectInputComponent tests * Fix: duplicated identifier on inputs * Extract toolbar to be reused for now Still TBD whether we want to move them right now to the frontend? * Create new project route and redirect to rails view after saving * fieldsSettingsPipe + hide 'identifier' on projects * Handling multi-values (also as links) and passwords * Some TODOs removed * FormattableTextareaInputComponent tests * _isResourceSchema based on parent?.location * Scope DynamicFieldsService to DynamicFormComponent * Added backend validation method to FormsService * Removed projects routes and ruby template * Removed projects routes and dynamic forms from Projects * Revert "Provide DynamicFieldsService in root so it can be used independently" This reverts commit ab56f3c56f6c8c3c3b791c081c762aa713e83a41. * Provide DynamicFieldsService in root so it can be used independently * TODO: test ProjectsComponent * Code climate fixes (remove TODOs) * Default OpFormFieldComponent.inlineLabel to false * Dynamic components tests xkipped * Typing improvements * DynamicFormComponent working as a FormControl * Global FormsService: submit + formatting + error handling * Fix: @Optional() FormGroupDirective in OpFormFieldComponent * Code climate fixes * noWrapLabel default to false * Started adding user custom fields to the ium * Import the dynamic-forms module into the common module * Refactor edit fields to avoid circular dependencies in the dynamic forms * Using DynamicFormsModule in OpenprojectInviteUserModalModule * Add formly form * Update principal name filter * Dynamic form field is rendering * Handling multi-values (also as links) and passwords * Added backend validation method to FormsService * Remove form from DynamicForm when not isStandaloneForm * Allow multiple form keys to validate * Remove form from non standalone forms * Remove duplicated button * Moved to FormGroup input for dynamic form * Custom field happy path is done * Add explanatory comment to payload structure transformation * add op-form class to ium steps * Add shrinkwrap back in * Fix test, fix dynamic form resource path * gimme a shirnkwrap * Remove failing tests * Remove another failing test * Remove more failing specs * Fix double loading of principals * Add custom field spec * Fix spec * Reset shrinkwrap * Forbid Factory.build(:user, member_in_project) If you use the trait member_in_project(s), the user is implicitly saved to create the member. This is very confusing if trying to use required custom fields, as this will fail with the Member#user_id foreign key being nil, as the user cannot be saved. Instead, raise an error when trying to use this factory trait * Change additional spec factory Co-authored-by: Aleix Suau <info@macrofonoestudio.es> Co-authored-by: Oliver Günther <mail@oliverguenther.de>
4 years ago
let(:user) { FactoryBot.create(:user, member_in_project: project_b) }
it_behaves_like 'successful index'
it "shows (active) public projects and those in which the user is member of" do
expect(assigns[:projects])
.to match_array [project_b, project_c]
end
end
end
describe '#destroy' do
render_views
let(:project) { FactoryBot.build_stubbed(:project) }
let(:request) { delete :destroy, params: { id: project.id } }
let(:service_result) { ::ServiceResult.new(success: success) }
before do
allow(Project).to receive(:find).and_return(project)
deletion_service = instance_double(::Projects::ScheduleDeletionService,
call: service_result)
allow(::Projects::ScheduleDeletionService)
.to receive(:new)
.with(user: admin, model: project)
.and_return(deletion_service)
end
context 'when service call succeeds' do
let(:success) { true }
it 'prints success' do
request
expect(response).to be_redirect
expect(flash[:notice]).to be_present
end
end
context 'when service call fails' do
let(:success) { false }
it 'prints fail' do
request
expect(response).to be_redirect
expect(flash[:error]).to be_present
end
end
end
describe 'with an existing project' do
let(:project) { FactoryBot.create :project, identifier: 'blog' }
it 'should get destroy info' do
get :destroy_info, params: { id: project.id }
expect(response).to be_successful
expect(response).to render_template 'destroy_info'
expect { project.reload }.not_to raise_error
end
end
describe '#copy' do
let(:project) { FactoryBot.create :project, identifier: 'blog' }
it "renders 'copy'" do
get 'copy', params: { id: project.id }
expect(response).to be_successful
expect(response).to render_template 'copy'
end
context 'as non authorized user' do
let(:user) { FactoryBot.build_stubbed :user }
before do
login_as user
end
it "shows an error" do
get 'copy', params: { id: project.id }
expect(response.status).to eq 403
end
end
end
end