diff options
-rw-r--r-- | help/app/models/ticket.rb | 10 | ||||
-rw-r--r-- | help/test/functional/tickets_controller_test.rb | 40 |
2 files changed, 30 insertions, 20 deletions
diff --git a/help/app/models/ticket.rb b/help/app/models/ticket.rb index 81cd08e..1ed2ed4 100644 --- a/help/app/models/ticket.rb +++ b/help/app/models/ticket.rb @@ -93,7 +93,7 @@ class Ticket < CouchRest::Model::Base # self.created_by = User.current if User.current #end - def self.for_user(user, options) + def self.for_user(user, options = {}) if options[:open_status] == 'open' Ticket.by_is_open_and_created_by.key([true, user.id]) elsif options[:open_status] == 'closed' @@ -106,9 +106,9 @@ class Ticket < CouchRest::Model::Base # @tickets = @tickets.sort{|x,y| x.updated_at <=> y.updated_at} end - def self.for_admin(user, options) + def self.for_admin(user, options = {}) if options[:admin_status] == 'mine' - self.tickets_by_admin(user.id) #returns Array so pagination does not work + self.tickets_by_admin(user.id, options) #returns Array so pagination does not work elsif options[:open_status] == 'open' Ticket.by_updated_at_and_is_open # Ticket.by_is_open.key(true) #returns CouchRest::Model::Designs::View @@ -122,12 +122,12 @@ class Ticket < CouchRest::Model::Base end #returns Array which doesn't work for pagination, as it is now. - def self.tickets_by_admin(id) + def self.tickets_by_admin(id, options = {}) admin_tickets = [] tickets = Ticket.all tickets.each do |ticket| ticket.comments.each do |comment| - if comment.posted_by == id and (params[:open_status] != 'open' or ticket.is_open) and (params[:open_status] != 'closed' or !ticket.is_open) #limit based on whether the ticket is open if open_status is set to open or closed + if comment.posted_by == id and (options[:open_status] != 'open' or ticket.is_open) and (options[:open_status] != 'closed' or !ticket.is_open) #limit based on whether the ticket is open if open_status is set to open or closed admin_tickets << ticket break end diff --git a/help/test/functional/tickets_controller_test.rb b/help/test/functional/tickets_controller_test.rb index 35901ca..cf123a9 100644 --- a/help/test/functional/tickets_controller_test.rb +++ b/help/test/functional/tickets_controller_test.rb @@ -2,7 +2,17 @@ require 'test_helper' class TicketsControllerTest < ActionController::TestCase - test "should get index if logged in" do + setup do + User.create(User.valid_attributes_hash.merge({:login => 'first_test'})) + User.create(User.valid_attributes_hash.merge({:login => 'different'})) + end + + teardown do + User.find_by_login('first_test').destroy + User.find_by_login('different').destroy + end + + test "should get index if logged in" do login(User.last) get :index assert_response :success @@ -17,7 +27,7 @@ class TicketsControllerTest < ActionController::TestCase 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.created_by = nil # TODO: hacky, but this makes sure this ticket is an unauthenticated one ticket.save get :show, :id => ticket.id assert_response :success @@ -28,7 +38,7 @@ class TicketsControllerTest < ActionController::TestCase assert_response :redirect assert_redirected_to login_url - login(User.last) + login(User.last) get :show, :id => ticket.id assert_response :success @@ -37,7 +47,7 @@ class TicketsControllerTest < ActionController::TestCase get :show, :id => ticket.id assert_response :redirect assert_redirected_to root_url - + end test "should create unauthenticated ticket" do @@ -67,10 +77,10 @@ class TicketsControllerTest < ActionController::TestCase assert_response :redirect - assert_not_nil assigns(:ticket).created_by + assert_not_nil assigns(:ticket).created_by assert_equal assigns(:ticket).created_by, @current_user.id assert_equal assigns(:ticket).email, @current_user.email - + assert_equal 1, assigns(:ticket).comments.count assert_not_nil assigns(:ticket).comments.first.posted_by assert_equal assigns(:ticket).comments.first.posted_by, @current_user.id @@ -79,7 +89,7 @@ class TicketsControllerTest < ActionController::TestCase test "add comment to unauthenticated ticket" do ticket = Ticket.last - ticket.created_by = nil # TODO: hacky, but this makes sure this ticket is an unauthenticated one + ticket.created_by = nil # TODO: hacky, but this makes sure this ticket is an unauthenticated one ticket.save assert_difference('Ticket.last.comments.count') do put :update, :id => ticket.id, @@ -127,7 +137,7 @@ class TicketsControllerTest < ActionController::TestCase assert_response :redirect assert_access_denied assert_equal ticket.comments, assigns(:ticket).comments - + end @@ -135,7 +145,7 @@ class TicketsControllerTest < ActionController::TestCase admin_login = APP_CONFIG['admins'].first admin_user = User.find_by_login(admin_login) #assumes that there is an admin login - login(admin_user) + login(admin_user) ticket = Ticket.last assert_not_nil User.last.id @@ -159,21 +169,21 @@ class TicketsControllerTest < ActionController::TestCase admin_login = APP_CONFIG['admins'].first admin_user = User.find_by_login(admin_login) #assumes that there is an admin login login(admin_user) - + post :create, :ticket => {:title => "test tick", :comments_attributes => {"0" => {"body" =>"body of test tick"}}} post :create, :ticket => {:title => "another test tick", :comments_attributes => {"0" => {"body" =>"body of another test tick"}}} assert_not_nil assigns(:ticket).created_by assert_equal assigns(:ticket).created_by, admin_user.id - get :index, {:status => "open tickets I admin"} + get :index, {:admin_status => "mine", :open_status => "open"} assert assigns(:tickets).count > 1 # at least 2 tickets # if we close one ticket, the admin should have 1 less open ticket they admin assert_difference('assigns[:tickets].count', -1) do assigns(:ticket).close assigns(:ticket).save - get :index, {:status => "open tickets I admin"} + get :index, {:admin_status => "mine", :open_status => "open"} end assigns(:ticket).destroy @@ -182,8 +192,8 @@ class TicketsControllerTest < ActionController::TestCase # admin should have one more ticket if a new tick gets an admin comment assert_difference('assigns[:tickets].count') do - put :update, :id => testticket.id, :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}}} - get :index, {:status => "open tickets I admin"} + put :update, :id => testticket.id, :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}}} + get :index, {:admin_status => "mine", :open_status => "open"} end assert assigns(:tickets).include?(assigns(:ticket)) @@ -191,7 +201,7 @@ class TicketsControllerTest < ActionController::TestCase assert_equal assigns(:ticket).comments.last.posted_by, admin_user.id assigns(:ticket).destroy - + end end |