Merge pull request #878 from opf/feature/rewrite_members_controller_tests

Rewrote members_controller_tests into specs so they can be disabled by
pull/705/merge
cratz 11 years ago
commit c44ff964c9
  1. 114
      spec/controllers/members_controller_spec.rb
  2. 114
      test/functional/members_controller_test.rb

@ -29,6 +29,7 @@
require 'spec_helper'
describe MembersController do
let(:admin) {FactoryGirl.create(:admin)}
let(:user) { FactoryGirl.create(:user) }
let(:project) { FactoryGirl.create(:project) }
let(:role) { FactoryGirl.create(:role) }
@ -37,35 +38,136 @@ describe MembersController do
:roles => [role]) }
before do
User.stub(:current).and_return(user)
User.stub(:current).and_return(admin)
end
describe :autocomplete_for_member do
let(:params) { ActionController::Parameters.new({ "id" => project.identifier.to_s }) }
before do
User.stub(:current).and_return(user)
end
describe "WHEN the user is authorized
WHEN a project is provided" do
before do
role.permissions << :manage_members
role.save!
member
post :autocomplete_for_member, params, :format => :xhr
end
it "should be success" do
post :autocomplete_for_member, params, :format => :xhr
response.should be_success
end
end
describe "WHEN the user is not authorized" do
before do
it "should be forbidden" do
post :autocomplete_for_member, params, :format => :xhr
response.response_code.should == 403
end
end
end
describe :create do
render_views
let(:user2) { FactoryGirl.create(:user) }
let(:user3) { FactoryGirl.create(:user) }
let(:user4) { FactoryGirl.create(:user) }
let(:valid_params) { { :format => "js",
:project_id => project.id,
:member => {:role_ids => [role.id],
:user_ids => [user2.id, user3.id, user4.id]}}
}
let(:invalid_params) { { :format => "js",
:project_id => project.id,
:member => {:role_ids => [],
:user_ids => [user2.id, user3.id, user4.id]}}
}
context "post :create" do
context "single member" do
let(:action) { post :create, :project_id => project.id, :member => {:role_ids => [role.id], :user_id => user2.id} }
it "should add a member" do
expect{action}.to change {Member.count}.by(1)
expect(response).to redirect_to(settings_project_path(project)+'/members')
expect(user2).to be_member_of(project)
end
end
it "should be forbidden" do
response.response_code.should == 403
context "multiple members" do
let(:action) { post :create, :project_id => project.id, :member => {:role_ids => [role.id], :user_ids => [user2.id, user3.id, user4.id] } }
it "should add all members" do
expect{action}.to change {Member.count}.by(3)
expect(response).to redirect_to(settings_project_path(project)+'/members')
expect(user2).to be_member_of(project)
expect(user3).to be_member_of(project)
expect(user4).to be_member_of(project)
end
end
end
context "post :create in JS format" do
context "with successful saves" do
before do
post :create, valid_params
end
it "should add members" do
user2.should be_member_of(project)
user3.should be_member_of(project)
user4.should be_member_of(project)
end
it "should replace the tab with RJS" do
assert_select_rjs :replace_html, 'tab-content-members'
end
end
end
context "with a failed save" do
it "should not replace the tab with RJS" do
post :create, invalid_params
assert_select '#tab-content-members', 0
end
it "should show an error message" do
post :create, invalid_params
assert_select_rjs :insert_html, :top do
assert_select '#errorExplanation'
end
end
end
end
describe :destroy do
let(:action) { post :destroy, :id => member.id }
before do
member
end
it "should destroy a member" do
expect{action}.to change {Member.count}.by(-1)
expect(response).to redirect_to(settings_project_path(project)+'/members')
expect(user).to_not be_member_of(project)
end
end
describe :update do
let(:action) { post :update, :id => member.id, :member => {:role_ids => [role2.id], :user_id => user.id} }
let(:role2) { FactoryGirl.create(:role) }
before do
member
end
it "should update the member" do
expect{action}.to_not change {Member.count}
expect(response).to redirect_to(settings_project_path(project)+'/members')
end
end
end

@ -1,114 +0,0 @@
#-- encoding: UTF-8
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2013 the OpenProject Foundation (OPF)
#
# 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 doc/COPYRIGHT.rdoc for more details.
#++
require File.expand_path('../../test_helper', __FILE__)
require 'members_controller'
# Re-raise errors caught by the controller.
class MembersController; def rescue_action(e) raise e end; end
class MembersControllerTest < ActionController::TestCase
fixtures :all
def setup
super
@controller = MembersController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
User.current = nil
@request.session[:user_id] = 2
end
def test_create
assert_difference 'Member.count' do
post :create, :id => 1, :member => {:role_ids => [1], :user_id => 7}
end
assert_redirected_to '/projects/ecookbook/settings/members'
assert User.find(7).member_of?(Project.find(1))
end
def test_create_multiple
assert_difference 'Member.count', 3 do
post :create, :id => 1, :member => {:role_ids => [1], :user_ids => [7, 8, 9]}
end
assert_redirected_to '/projects/ecookbook/settings/members'
assert User.find(7).member_of?(Project.find(1))
end
context "post :create in JS format" do
context "with successful saves" do
should "add membership for each user" do
post :create, :format => "js", :id => 1, :member => {:role_ids => [1], :user_ids => [7, 8, 9]}
assert User.find(7).member_of?(Project.find(1))
assert User.find(8).member_of?(Project.find(1))
assert User.find(9).member_of?(Project.find(1))
end
should "replace the tab with RJS" do
post :create, :format => "js", :id => 1, :member => {:role_ids => [1], :user_ids => [7, 8, 9]}
assert_select_rjs :replace_html, 'tab-content-members'
end
end
context "with a failed save" do
should "not replace the tab with RJS" do
post :create, :format => "js", :id => 1, :member => {:role_ids => [], :user_ids => [7, 8, 9]}
assert_select '#tab-content-members', 0
end
should "show an error message" do
post :create, :format => "js", :id => 1, :member => {:role_ids => [], :user_ids => [7, 8, 9]}
assert_select_rjs :insert_html, :top do
assert_select '#errorExplanation'
end
end
end
end
def test_update
assert_no_difference 'Member.count' do
put :update, :id => 2, :member => {:role_ids => [1], :user_id => 3}
end
assert_redirected_to '/projects/ecookbook/settings/members'
end
def test_destroy
assert_difference 'Member.count', -1 do
delete :destroy, :id => 2
end
assert_redirected_to '/projects/ecookbook/settings/members'
assert !User.find(3).member_of?(Project.find(1))
end
end
Loading…
Cancel
Save