summaryrefslogtreecommitdiff
path: root/engines/support/test
diff options
context:
space:
mode:
authorazul <azul@riseup.net>2014-04-17 10:12:05 +0200
committerazul <azul@riseup.net>2014-04-17 10:12:05 +0200
commit3513ad74f950b113af1ba1e3d06bc6a55c48fde5 (patch)
treedb49ebd4428053d5c8d720275b77594a531a1ad1 /engines/support/test
parentcb6442c344d6bdaf52c3878b2de2fcf4d85f2648 (diff)
parent3d3688647fab7049e5b531c45b85c1e46a1d528f (diff)
Merge pull request #146 from azul/refactor/engines
Refactor/engines
Diffstat (limited to 'engines/support/test')
-rw-r--r--engines/support/test/factories.rb18
-rw-r--r--engines/support/test/functional/tickets_controller_test.rb273
-rw-r--r--engines/support/test/integration/navigation_test.rb9
-rw-r--r--engines/support/test/leap_web_help_test.rb7
-rw-r--r--engines/support/test/test_helper.rb15
-rw-r--r--engines/support/test/unit/account_extension_test.rb12
-rw-r--r--engines/support/test/unit/ticket_comment_test.rb59
-rw-r--r--engines/support/test/unit/ticket_test.rb88
8 files changed, 481 insertions, 0 deletions
diff --git a/engines/support/test/factories.rb b/engines/support/test/factories.rb
new file mode 100644
index 0000000..be04f15
--- /dev/null
+++ b/engines/support/test/factories.rb
@@ -0,0 +1,18 @@
+FactoryGirl.define do
+
+ factory :ticket do
+ subject { Faker::Lorem.sentence }
+ email { Faker::Internet.email }
+
+ factory :ticket_with_comment do
+ comments_attributes do
+ { "0" => { "body" => Faker::Lorem.sentences.join(" ") } }
+ end
+ end
+
+ factory :ticket_with_creator do
+ created_by { FactoryGirl.create(:user).id }
+ end
+ end
+
+end
diff --git a/engines/support/test/functional/tickets_controller_test.rb b/engines/support/test/functional/tickets_controller_test.rb
new file mode 100644
index 0000000..416fb73
--- /dev/null
+++ b/engines/support/test/functional/tickets_controller_test.rb
@@ -0,0 +1,273 @@
+require 'test_helper'
+
+class TicketsControllerTest < ActionController::TestCase
+
+ teardown do
+ # destroy all tickets that were created during the test
+ Ticket.all.each{|t| t.destroy}
+ end
+
+ test "should get index if logged in" do
+ login
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:tickets)
+ end
+
+ test "no index if not logged in" do
+ get :index
+ assert_response :redirect
+ assert_nil assigns(:tickets)
+ end
+
+ test "should get new" do
+ get :new
+ assert_equal Ticket, assigns(:ticket).class
+ assert_response :success
+ end
+
+ test "unauthenticated tickets are visible" do
+ ticket = find_record :ticket, :created_by => nil
+ get :show, :id => ticket.id
+ assert_response :success
+ end
+
+ test "user tickets are not visible without login" do
+ user = find_record :user
+ ticket = find_record :ticket, :created_by => user.id
+ get :show, :id => ticket.id
+ assert_response :redirect
+ assert_redirected_to login_url
+ end
+
+ test "user tickets are visible to creator" do
+ user = find_record :user
+ ticket = find_record :ticket, :created_by => user.id
+ login user
+ get :show, :id => ticket.id
+ assert_response :success
+ end
+
+ test "other users tickets are not visible" do
+ other_user = find_record :user
+ ticket = find_record :ticket, :created_by => other_user.id
+ login
+ get :show, :id => ticket.id
+ assert_response :redirect
+ assert_redirected_to home_url
+ end
+
+ test "should create unauthenticated ticket" do
+ params = {:subject => "unauth ticket test subject", :comments_attributes => {"0" => {"body" =>"body of test ticket"}}}
+
+ assert_difference('Ticket.count') do
+ post :create, :ticket => params
+ end
+
+ assert_response :redirect
+ assert_nil assigns(:ticket).created_by
+
+ assert_equal 1, assigns(:ticket).comments.count
+ assert_nil assigns(:ticket).comments.first.posted_by
+
+ end
+
+ test "should create authenticated ticket" do
+
+ params = {:subject => "auth ticket test subject", :comments_attributes => {"0" => {"body" =>"body of test ticket"}}}
+
+ login
+
+ assert_difference('Ticket.count') do
+ post :create, :ticket => params
+ end
+
+ assert_response :redirect
+
+ assert_not_nil assigns(:ticket).created_by
+ assert_equal assigns(:ticket).created_by, @current_user.id
+ assert_equal assigns(:ticket).email, @current_user.email_address
+
+ 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
+ end
+
+ test "add comment to unauthenticated ticket" do
+ ticket = FactoryGirl.create :ticket, :created_by => nil
+
+ assert_difference('Ticket.find(ticket.id).comments.count') do
+ put :update, :id => ticket.id,
+ :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}} }
+ end
+
+ assert_equal ticket, assigns(:ticket) # still same ticket, with different comments
+ assert_not_equal ticket.comments, assigns(:ticket).comments # ticket == assigns(:ticket), but they have different comments (which we want)
+
+ end
+
+
+ test "add comment to own authenticated ticket" do
+
+ login
+ ticket = FactoryGirl.create :ticket, :created_by => @current_user.id
+
+ #they should be able to comment if it is their ticket:
+ assert_difference('Ticket.find(ticket.id).comments.count') do
+ put :update, :id => ticket.id,
+ :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}} }
+ end
+ assert_not_equal ticket.comments, assigns(:ticket).comments
+ assert_not_nil assigns(:ticket).comments.last.posted_by
+ assert_equal assigns(:ticket).comments.last.posted_by, @current_user.id
+
+ end
+
+
+ test "cannot comment if it is not your ticket" do
+
+ other_user = find_record :user
+ login :is_admin? => false, :email => nil
+ ticket = FactoryGirl.create :ticket, :created_by => other_user.id
+ # they should *not* be able to comment if it is not their ticket
+ put :update, :id => ticket.id, :ticket => {:comments_attributes => {"0" => {"body" =>"not allowed comment"}} }
+ assert_response :redirect
+ assert_access_denied
+
+ assert_equal ticket.comments.map(&:body), assigns(:ticket).comments.map(&:body)
+
+ end
+
+
+ test "admin add comment to authenticated ticket" do
+
+ other_user = find_record :user
+ login :is_admin? => true
+
+ ticket = FactoryGirl.create :ticket, :created_by => other_user.id
+
+ #admin should be able to comment:
+ assert_difference('Ticket.find(ticket.id).comments.count') do
+ put :update, :id => ticket.id,
+ :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}} }
+ end
+ assert_not_equal ticket.comments, assigns(:ticket).comments
+ assert_not_nil assigns(:ticket).comments.last.posted_by
+ assert_equal assigns(:ticket).comments.last.posted_by, @current_user.id
+ end
+
+ test "tickets by admin" do
+ other_user = find_record :user
+ ticket = FactoryGirl.create :ticket, :created_by => other_user.id
+
+ login :is_admin? => true
+
+ get :index, {:admin_status => "all", :open_status => "open"}
+ assert assigns(:all_tickets).count > 0
+
+ # if we close one ticket, the admin should have 1 less open ticket
+ assert_difference('assigns[:all_tickets].count', -1) do
+ assigns(:tickets).first.close
+ assigns(:tickets).first.save
+ get :index, {:admin_status => "all", :open_status => "open"}
+ end
+ end
+
+
+ test "admin_status mine vs all" do
+ testticket = FactoryGirl.create :ticket
+ user = find_record :user
+ login :is_admin? => true, :email => nil
+
+ get :index, {:open_status => "open"}
+ assert assigns(:all_tickets).include?(testticket)
+ get :index, {:user_id => user.id, :open_status => "open"}
+ assert !assigns(:all_tickets).include?(testticket)
+ end
+
+ test "commenting on a ticket adds to tickets that are mine" do
+ testticket = FactoryGirl.create :ticket
+ user = find_record :admin_user
+ login user
+ get :index, {:user_id => user.id, :open_status => "open"}
+ assert_difference('assigns[:all_tickets].count') do
+ put :update, :id => testticket.id, :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}}}
+ get :index, {:user_id => user.id, :open_status => "open"}
+ end
+
+ assert assigns(:all_tickets).include?(assigns(:ticket))
+ assert_not_nil assigns(:ticket).comments.last.posted_by
+ assert_equal assigns(:ticket).comments.last.posted_by, @current_user.id
+ end
+
+ test "admin ticket ordering" do
+ tickets = FactoryGirl.create_list :ticket, 2
+
+ login :is_admin? => true, :email => nil
+ get :index, {:admin_status => "all", :open_status => "open", :sort_order => 'created_at_desc'}
+
+ # this will consider all tickets, not just those on first page
+ first_tick = assigns(:all_tickets).all.first
+ last_tick = assigns(:all_tickets).all.last
+ assert first_tick.created_at > last_tick.created_at
+
+ # and now reverse order:
+ get :index, {:admin_status => "all", :open_status => "open", :sort_order => 'created_at_asc'}
+
+ assert_equal first_tick, assigns(:all_tickets).last
+ assert_equal last_tick, assigns(:all_tickets).first
+
+ assert_not_equal first_tick, assigns(:all_tickets).first
+ assert_not_equal last_tick, assigns(:all_tickets).last
+
+ end
+
+ test "tickets for regular user" do
+ login
+ ticket = FactoryGirl.create :ticket
+ other_ticket = FactoryGirl.create :ticket
+
+ 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)
+ 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 => other_ticket.id, :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}}}
+ get :index, {:open_status => "open"}
+ end
+ 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
+ other_ticket.reload
+ other_ticket.close
+ other_ticket.save
+ get :index, {:open_status => "open"}
+ end
+
+ number_open_tickets = assigns(:all_tickets).count
+
+ # look at closed tickets:
+ get :index, {:open_status => "closed"}
+ 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)
+ assert assigns(:all_tickets).include?(other_ticket)
+ assert_equal assigns(:all_tickets).count, number_closed_tickets + number_open_tickets
+
+
+ end
+
+end
+
diff --git a/engines/support/test/integration/navigation_test.rb b/engines/support/test/integration/navigation_test.rb
new file mode 100644
index 0000000..eec8c0e
--- /dev/null
+++ b/engines/support/test/integration/navigation_test.rb
@@ -0,0 +1,9 @@
+require 'test_helper'
+
+class NavigationTest < ActionDispatch::IntegrationTest
+
+ # test "the truth" do
+ # assert true
+ # end
+end
+
diff --git a/engines/support/test/leap_web_help_test.rb b/engines/support/test/leap_web_help_test.rb
new file mode 100644
index 0000000..d74c087
--- /dev/null
+++ b/engines/support/test/leap_web_help_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class LeapWebHelpTest < ActiveSupport::TestCase
+ test "truth" do
+ assert_kind_of Module, LeapWebHelp
+ end
+end
diff --git a/engines/support/test/test_helper.rb b/engines/support/test/test_helper.rb
new file mode 100644
index 0000000..fff9173
--- /dev/null
+++ b/engines/support/test/test_helper.rb
@@ -0,0 +1,15 @@
+# Configure Rails Environment
+ENV["RAILS_ENV"] = "test"
+
+require File.expand_path('../../../../test/dummy/config/environment', __FILE__)
+require "rails/test_help"
+
+Rails.backtrace_cleaner.remove_silencers!
+
+# Load support files
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
+
+# Load fixtures from the engine
+if ActiveSupport::TestCase.method_defined?(:fixture_path=)
+ ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
+end
diff --git a/engines/support/test/unit/account_extension_test.rb b/engines/support/test/unit/account_extension_test.rb
new file mode 100644
index 0000000..aba162c
--- /dev/null
+++ b/engines/support/test/unit/account_extension_test.rb
@@ -0,0 +1,12 @@
+require 'test_helper'
+
+class AccountExtensionTest < ActiveSupport::TestCase
+
+ test "destroying an account triggers ticket destruction" do
+ t = FactoryGirl.create :ticket_with_creator
+ u = t.created_by_user
+ Account.new(u).destroy
+ assert_equal nil, Ticket.find(t.id)
+ end
+
+end
diff --git a/engines/support/test/unit/ticket_comment_test.rb b/engines/support/test/unit/ticket_comment_test.rb
new file mode 100644
index 0000000..fe8cc95
--- /dev/null
+++ b/engines/support/test/unit/ticket_comment_test.rb
@@ -0,0 +1,59 @@
+require 'test_helper'
+
+class TicketCommentTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+
+=begin
+ setup do
+ @sample_ticket = Ticket.create :title => 'test ticket'
+ @sample_ticket.save
+ end
+=end
+
+ test "create" do
+
+ comment2 = TicketComment.new :body => "help my email is broken!"
+ assert comment2.valid?
+ #assert_not_nil comment2.posted_at #?
+ #assert_nil comment2.posted_by #if not logged in #TODO
+
+ #comment.ticket = testticket #Ticket.find_by_title("testing")
+ #assert_equal testticket.title, comment.ticket.title
+
+ #tc.ticket = Ticket.find_by_title("test title")
+ #tc.ticket.title
+ end
+
+=begin
+ test "create authenticated comment" do
+ User.current = 4
+ comment2 = TicketComment.new :body => "help my email is broken!"
+ comment2.valid? #save # should not save comment
+ assert_not_nil comment2.posted_by
+ end
+=end
+
+ test "add comments" do
+ testticket = Ticket.create :subject => "testing"
+ assert_equal testticket.comments.count, 0
+ comment = TicketComment.new :body => "my email broke"
+ #assert comment.valid? #validating or saving necessary for setting posted_at
+ #assert_not_nil comment.posted_at
+
+ testticket.comments << comment
+ assert_equal testticket.comments.count, 1
+ sleep(1) # so first comment has earlier posted_at time
+ comment2 = TicketComment.new :body => "my email broke"
+ testticket.comments << comment2 #this should validate comment2
+ testticket.valid?
+ assert_equal testticket.comments.count, 2
+ testticket.reload.destroy
+ # where should posted_at be set?
+ #assert_not_nil comment.posted_at
+ #assert_not_nil testticket.comments.last.posted_at
+ #assert testticket.comments.first.posted_at < testticket.comments.last.posted_at
+ end
+
+end
diff --git a/engines/support/test/unit/ticket_test.rb b/engines/support/test/unit/ticket_test.rb
new file mode 100644
index 0000000..f5e6ea7
--- /dev/null
+++ b/engines/support/test/unit/ticket_test.rb
@@ -0,0 +1,88 @@
+require 'test_helper'
+
+class TicketTest < ActiveSupport::TestCase
+
+ test "ticket with default attribs is valid" do
+ t = FactoryGirl.build :ticket
+ assert t.valid?
+ end
+
+ test "ticket without email is valid" do
+ t = FactoryGirl.build :ticket, email: ""
+ assert t.valid?
+ end
+
+ test "ticket validates email format" do
+ t = FactoryGirl.build :ticket, email: "aswerssfd"
+ assert !t.valid?
+ end
+
+ test "ticket open states" do
+ t = FactoryGirl.build :ticket
+ assert t.is_open
+ t.close
+ assert !t.is_open
+ t.reopen
+ assert t.is_open
+ end
+
+ test "creation validated" do
+ @sample = Ticket.new
+ assert !@sample.is_creator_validated?
+ #p current_user
+ @sample.created_by = 22 #current_user
+ assert @sample.is_creator_validated?
+ end
+
+ test "destroy all tickets from a user" do
+ t = FactoryGirl.create :ticket_with_creator
+ u = t.created_by_user
+ Ticket.destroy_all_from(u)
+ assert_equal nil, Ticket.find(t.id)
+ end
+=begin
+# TODO: do once have current_user stuff in order
+ test "code if & only if not creator-validated" do
+ User.current_test = nil
+ t1 = Ticket.create :subject => 'test title'
+ assert_not_nil t1.code
+ assert_nil t1.created_by
+
+ User.current_test = 4
+ t2 = Ticket.create :subject => 'test title'
+ assert_nil t2.code
+ assert_not_nil t2.created_by
+ end
+=end
+
+
+ test "find tickets user commented on" do
+
+ # clear old tickets just in case
+ # this will cause RestClient::ResourceNotFound errors if there are multiple copies of the same ticket returned
+ Ticket.by_includes_post_by.key('123').each {|t| t.destroy}
+ # TODO: the by_includes_post_by view is only used for tests. Maybe we should get rid of it and change the test to including ordering?
+
+
+ testticket = Ticket.create :subject => "test retrieving commented tickets"
+ comment = TicketComment.new :body => "my email broke", :posted_by => "123"
+ assert_equal 0, testticket.comments.count
+ assert_equal [], Ticket.by_includes_post_by.key('123').all
+
+ testticket.comments << comment
+ testticket.save
+ assert_equal 1, testticket.reload.comments.count
+ assert_equal [testticket], Ticket.by_includes_post_by.key('123').all
+
+ comment = TicketComment.new :body => "another comment", :posted_by => "123"
+ testticket.comments << comment
+ testticket.save
+
+ # this will ensure that the ticket is only included once, even though the user has commented on the ticket twice:
+ assert_equal [testticket], Ticket.by_includes_post_by.key('123').all
+
+ testticket.destroy
+ assert_equal [], Ticket.by_includes_post_by.key('123').all;
+ end
+
+end