From 26689f3bd284c4eac1e40e1bcf0236e6889fa9ed Mon Sep 17 00:00:00 2001 From: George Vinogradov Date: Wed, 7 Dec 2011 23:51:08 +0400 Subject: [PATCH] Start writing Group tests. --- app/controllers/groups_controller.rb | 4 +- spec/controllers/groups_controller_spec.rb | 42 +++++++++++++++++++ spec/controllers/members_controller_spec.rb | 5 +++ spec/factories/group_factory.rb | 5 +++ .../shared_examples/groups_controller.rb | 15 +++++++ .../shared_examples/members_controller.rb | 6 +++ 6 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 spec/controllers/groups_controller_spec.rb create mode 100644 spec/controllers/members_controller_spec.rb create mode 100644 spec/factories/group_factory.rb create mode 100644 spec/support/shared_examples/groups_controller.rb create mode 100644 spec/support/shared_examples/members_controller.rb diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index e46615661..db45df1e5 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -47,7 +47,7 @@ class GroupsController < ApplicationController if @group.save flash[:notice] = t('flash.group.saved') - redirect_to edit_group_path(@group) + redirect_to group_path(@group) else flash[:error] = t('flash.group.save_error') flash[:warning] = @group.errors[:base] @@ -58,7 +58,7 @@ class GroupsController < ApplicationController def update if @group.update_attributes(params[:group]) flash[:notice] = t('flash.group.saved') - redirect_to groups_path + redirect_to group_path(@group) else flash[:error] = t('flash.group.save_error') render :action => :edit diff --git a/spec/controllers/groups_controller_spec.rb b/spec/controllers/groups_controller_spec.rb new file mode 100644 index 000000000..d9b3a897f --- /dev/null +++ b/spec/controllers/groups_controller_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +describe GroupsController do + before(:each) do + @group = Factory(:group) + @another_user = Factory(:user) + @create_params = {:group => {:name => 'grp1', :uname => 'un_grp1'}} + @update_params = {:group => {:name => 'grp2'}} + end + + context 'for guest' do + it 'should not be able to perform index action' do + get :index + response.should redirect_to(new_user_session_path) + end + + it 'should not be able to perform update action' do + put :update, {:id => @group.id}.merge(@update_params) + response.should redirect_to(new_user_session_path) + end + end + + context 'for admin' do + before(:each) do + @admin = Factory(:admin) + set_session_for(@admin) + end + + it_should_behave_like 'be_able_to_perform_index#groups' + it_should_behave_like 'be_able_to_perform_update#groups' + it_should_behave_like 'update_member_relation' + + it 'should be able to perform create action' do + post :create, @create_params + response.should redirect_to(group_path( Group.last.id )) + end + + it 'should change objects count on create' do + lambda { post :create, @create_params }.should change{ Group.count }.by(1) + end + end +end diff --git a/spec/controllers/members_controller_spec.rb b/spec/controllers/members_controller_spec.rb new file mode 100644 index 000000000..3b886638e --- /dev/null +++ b/spec/controllers/members_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe MembersController do + +end diff --git a/spec/factories/group_factory.rb b/spec/factories/group_factory.rb new file mode 100644 index 000000000..c451ff2b3 --- /dev/null +++ b/spec/factories/group_factory.rb @@ -0,0 +1,5 @@ +Factory.define(:group) do |g| + g.name { Factory.next(:string) } + g.uname { Factory.next(:uname) } + g.association :owner, :factory => :user +end diff --git a/spec/support/shared_examples/groups_controller.rb b/spec/support/shared_examples/groups_controller.rb new file mode 100644 index 000000000..e9b560ce1 --- /dev/null +++ b/spec/support/shared_examples/groups_controller.rb @@ -0,0 +1,15 @@ +shared_examples_for 'be_able_to_perform_index#groups' do + it 'should be able to perform index action' do + get :index + response.should render_template(:index) + end +end + +shared_examples_for 'be_able_to_perform_update#groups' do + it 'should be able to perform update action' do + put :update, {:id => @group.id}.merge(@update_params) + response.should redirect_to(group_path(@group)) + end +end + + diff --git a/spec/support/shared_examples/members_controller.rb b/spec/support/shared_examples/members_controller.rb new file mode 100644 index 000000000..dde99c184 --- /dev/null +++ b/spec/support/shared_examples/members_controller.rb @@ -0,0 +1,6 @@ +shared_examples_for 'update_member_relation' do + it 'should update member relation' do + @another_user.relations.exists? :target_id => @group.id, :target_type => 'Group', :role => 'read' + end +end +