diff options
author | Azul <azul@leap.se> | 2013-01-14 16:24:25 +0100 |
---|---|---|
committer | Azul <azul@leap.se> | 2013-01-14 16:24:25 +0100 |
commit | a9ef046a596b5876792b1cc4ab0d26b9b255c342 (patch) | |
tree | fe691c72f36b42f74a1d850f2e406c50500036e3 | |
parent | 3cd3c261bdf02b2da5217fa1c469d30f9d92c9a3 (diff) |
tickets controller tests passing
-rw-r--r-- | help/app/views/tickets/_ticket_data.html.haml | 2 | ||||
-rw-r--r-- | help/test/functional/tickets_controller_test.rb | 69 | ||||
-rw-r--r-- | users/test/factories.rb | 1 | ||||
-rw-r--r-- | users/test/support/auth_test_helper.rb | 6 | ||||
-rw-r--r-- | users/test/support/stub_record_helper.rb | 13 |
5 files changed, 49 insertions, 42 deletions
diff --git a/help/app/views/tickets/_ticket_data.html.haml b/help/app/views/tickets/_ticket_data.html.haml index 3d301be..fee080f 100644 --- a/help/app/views/tickets/_ticket_data.html.haml +++ b/help/app/views/tickets/_ticket_data.html.haml @@ -22,4 +22,4 @@ = button_to 'Close', {:post => {:is_open => false}}, :method => :put, :class => 'btn btn-small' - else = 'closed' - = button_to 'Open', {:post => {:is_open => true}}, :method => :put, :class => 'btn btn-small'
\ No newline at end of file + = button_to 'Open', {:post => {:is_open => true}}, :method => :put, :class => 'btn btn-small' diff --git a/help/test/functional/tickets_controller_test.rb b/help/test/functional/tickets_controller_test.rb index 7060936..97e3308 100644 --- a/help/test/functional/tickets_controller_test.rb +++ b/help/test/functional/tickets_controller_test.rb @@ -3,21 +3,17 @@ require 'test_helper' class TicketsControllerTest < ActionController::TestCase setup do - User.create(User.valid_attributes_hash.merge({:login => 'first_test'})) - User.create(User.valid_attributes_hash.merge({:login => 'different'})) - Ticket.create( {:title => "stub test ticket", :id => 'stubtestticketid', :comments_attributes => {"0" => {"body" =>"body of stubbed test ticket"}}}) - Ticket.create( {:title => "stub test ticket two", :id => 'stubtestticketid2', :comments_attributes => {"0" => {"body" =>"body of second stubbed test ticket"}}}) + @user = FactoryGirl.create :user + @other_user = FactoryGirl.create :user end teardown do - User.find_by_login('first_test').destroy - User.find_by_login('different').destroy - Ticket.find('stubtestticketid').destroy - Ticket.find('stubtestticketid2').destroy + @user.destroy + @other_user.destroy end test "should get index if logged in" do - login :is_admin? => false + login get :index assert_response :success assert_not_nil assigns(:tickets) @@ -35,29 +31,32 @@ class TicketsControllerTest < ActionController::TestCase assert_response :success end - test "ticket show access" do - ticket = Ticket.first - ticket.created_by = nil # TODO: hacky, but this makes sure this ticket is an unauthenticated one - ticket.save + test "unauthenticated tickets are visible" do + ticket = find_record :ticket, :created_by => nil get :show, :id => ticket.id assert_response :success + end - ticket.created_by = User.last.id - ticket.save + test "user tickets are not visible without login" do + ticket = find_record :ticket, :created_by => @user.id get :show, :id => ticket.id assert_response :redirect assert_redirected_to login_url + end - login(User.last) + test "user tickets are visible to creator" do + ticket = find_record :ticket, :created_by => @user.id + login @user get :show, :id => ticket.id assert_response :success + end - login(User.first) #assumes User.first != User.last: - assert_not_equal User.first, User.last + test "user tickets are not visible to other user" do + ticket = find_record :ticket, :created_by => @user.id + login @other_user get :show, :id => ticket.id assert_response :redirect assert_redirected_to root_url - end test "should create unauthenticated ticket" do @@ -80,7 +79,7 @@ class TicketsControllerTest < ActionController::TestCase params = {:title => "auth ticket test title", :comments_attributes => {"0" => {"body" =>"body of test ticket"}}} - login :email => "test@email.net" + login :email => Faker::Internet.user_name + '@' + APP_CONFIG[:domain] assert_difference('Ticket.count') do post :create, :ticket => params @@ -237,30 +236,32 @@ class TicketsControllerTest < ActionController::TestCase end test "tickets for regular user" do - login :is_admin? => false, :email => nil + login + ticket = FactoryGirl.create :ticket + other_ticket = FactoryGirl.create :ticket - put :update, :id => 'stubtestticketid',:ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}} } + put :update, :id => ticket.id, + :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}} } assert_not_nil assigns(:ticket).comments.last.posted_by assert_equal assigns(:ticket).comments.last.posted_by, @current_user.id get :index, {:open_status => "open"} assert assigns(:all_tickets).count > 0 - assert assigns(:all_tickets).include?(Ticket.find('stubtestticketid')) - - assert !assigns(:all_tickets).include?(Ticket.find('stubtestticketid2')) + assert assigns(:all_tickets).include?(ticket) + assert !assigns(:all_tickets).include?(other_ticket) # user should have one more ticket if a new tick gets a comment by this user assert_difference('assigns[:all_tickets].count') do - put :update, :id => 'stubtestticketid2' , :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}}} + put :update, :id => other_ticket.id, :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}}} get :index, {:open_status => "open"} end - assert assigns(:all_tickets).include?(Ticket.find('stubtestticketid2')) + assert assigns(:all_tickets).include?(other_ticket) # if we close one ticket, the user should have 1 less open ticket assert_difference('assigns[:all_tickets].count', -1) do - t = Ticket.find('stubtestticketid2') - t.close - t.save + other_ticket.reload + other_ticket.close + other_ticket.save get :index, {:open_status => "open"} end @@ -268,14 +269,14 @@ class TicketsControllerTest < ActionController::TestCase # look at closed tickets: get :index, {:open_status => "closed"} - assert assigns(:all_tickets).include?(Ticket.find('stubtestticketid2')) - assert !assigns(:all_tickets).include?(Ticket.find('stubtestticketid')) + assert !assigns(:all_tickets).include?(ticket) + assert assigns(:all_tickets).include?(other_ticket) number_closed_tickets = assigns(:all_tickets).count # all tickets should equal closed + open get :index, {:open_status => "all"} - assert assigns(:all_tickets).include?(Ticket.find('stubtestticketid2')) - assert assigns(:all_tickets).include?(Ticket.find('stubtestticketid')) + assert assigns(:all_tickets).include?(ticket) + assert assigns(:all_tickets).include?(other_ticket) assert_equal assigns(:all_tickets).count, number_closed_tickets + number_open_tickets end diff --git a/users/test/factories.rb b/users/test/factories.rb index 6b094bd..4bf7e62 100644 --- a/users/test/factories.rb +++ b/users/test/factories.rb @@ -7,6 +7,7 @@ FactoryGirl.define do factory :user_with_settings do email_forward { Faker::Internet.email } + email { Faker::Internet.user_name + '@' + APP_CONFIG[:domain] } email_aliases_attributes do {:a => Faker::Internet.user_name + '@' + APP_CONFIG[:domain]} end diff --git a/users/test/support/auth_test_helper.rb b/users/test/support/auth_test_helper.rb index c9f5612..c0fcf3a 100644 --- a/users/test/support/auth_test_helper.rb +++ b/users/test/support/auth_test_helper.rb @@ -10,10 +10,10 @@ module AuthTestHelper end def login(user_or_method_hash = {}) - @current_user = stub_record(User, user_or_method_hash) - unless @current_user.respond_to? :is_admin? - @current_user.stubs(:is_admin?).returns(false) + if user_or_method_hash.respond_to?(:reverse_merge) + user_or_method_hash.reverse_merge! :is_admin? => false end + @current_user = stub_record(:user, user_or_method_hash, true) request.env['warden'] = stub :user => @current_user return @current_user end diff --git a/users/test/support/stub_record_helper.rb b/users/test/support/stub_record_helper.rb index 18db5ae..168a827 100644 --- a/users/test/support/stub_record_helper.rb +++ b/users/test/support/stub_record_helper.rb @@ -4,10 +4,11 @@ module StubRecordHelper # return the record given. # If no record is given but a hash or nil will create a stub based on # that instead and returns the stub. - def find_record(factory) - record = stub_record factory + def find_record(factory, attribs_hash = {}) + attribs_hash.reverse_merge!(:id => Random.rand(10000).to_s) + record = stub_record factory, attribs_hash klass = record.class - finder = klass.respond_to?(:find_by_param) ? :find_by_param : :find_by_id + finder = klass.respond_to?(:find_by_param) ? :find_by_param : :find klass.expects(finder).with(record.to_param.to_s).returns(record) return record end @@ -18,11 +19,15 @@ module StubRecordHelper # If the second parameter is a record we return the record itself. # This way you can build functions that either take a record or a # method hash to stub from. See find_record for an example. - def stub_record(factory, record_or_method_hash = {}) + def stub_record(factory, record_or_method_hash = {}, persisted=false) if record_or_method_hash && !record_or_method_hash.is_a?(Hash) return record_or_method_hash end FactoryGirl.build_stubbed(factory).tap do |record| + if persisted or record.persisted? + record_or_method_hash.reverse_merge! :created_at => Time.now, + :updated_at => Time.now, :id => Random.rand(100000).to_s + end record.stubs(record_or_method_hash) if record_or_method_hash.present? end end |