From a9ef046a596b5876792b1cc4ab0d26b9b255c342 Mon Sep 17 00:00:00 2001
From: Azul <azul@leap.se>
Date: Mon, 14 Jan 2013 16:24:25 +0100
Subject: tickets controller tests passing

---
 help/test/functional/tickets_controller_test.rb | 69 +++++++++++++------------
 1 file changed, 35 insertions(+), 34 deletions(-)

(limited to 'help/test')

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
-- 
cgit v1.2.3


From 632a31cf48f41b3d0ea52f3b741407d65ab64139 Mon Sep 17 00:00:00 2001
From: Azul <azul@leap.se>
Date: Mon, 14 Jan 2013 16:30:32 +0100
Subject: forgot to include the factory for tickets

---
 help/test/factories.rb | 10 ++++++++++
 1 file changed, 10 insertions(+)
 create mode 100644 help/test/factories.rb

(limited to 'help/test')

diff --git a/help/test/factories.rb b/help/test/factories.rb
new file mode 100644
index 0000000..919882a
--- /dev/null
+++ b/help/test/factories.rb
@@ -0,0 +1,10 @@
+FactoryGirl.define do
+
+  factory :ticket do
+    title { Faker::Lorem.sentence }
+    comments_attributes do
+      { "0" => { "body" => Faker::Lorem.sentences } }
+    end
+  end
+
+end
-- 
cgit v1.2.3


From 581bcc0507b2f2385bf1f6d82bb119afa2f00716 Mon Sep 17 00:00:00 2001
From: Azul <azul@leap.se>
Date: Mon, 14 Jan 2013 16:59:55 +0100
Subject: little bit of cleanup

* checking records are actually getting created / destroyed
* simplify creation where possible
---
 help/test/functional/tickets_controller_test.rb | 39 +++++++++++--------------
 1 file changed, 17 insertions(+), 22 deletions(-)

(limited to 'help/test')

diff --git a/help/test/functional/tickets_controller_test.rb b/help/test/functional/tickets_controller_test.rb
index 97e3308..794b119 100644
--- a/help/test/functional/tickets_controller_test.rb
+++ b/help/test/functional/tickets_controller_test.rb
@@ -98,11 +98,9 @@ class TicketsControllerTest < ActionController::TestCase
   end
 
   test "add comment to unauthenticated ticket" do
-    ticket = Ticket.find('stubtestticketid')
-    ticket.created_by = nil # TODO: hacky, but this makes sure this ticket is an unauthenticated one
-    ticket.save
+    ticket = FactoryGirl.create :ticket, :created_by => nil
 
-    assert_difference('Ticket.find("stubtestticketid").comments.count') do
+    assert_difference('Ticket.find(ticket.id).comments.count') do
       put :update, :id => ticket.id,
         :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}} }
     end
@@ -110,24 +108,24 @@ class TicketsControllerTest < ActionController::TestCase
     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)
 
+    assigns(:ticket).destroy
   end
 
 
   test "add comment to own authenticated ticket" do
 
     login User.last
-    ticket = Ticket.find('stubtestticketid')
-    ticket.created_by = @current_user.id # TODO: hacky, but confirms it is their ticket
-    ticket.save
+    ticket = FactoryGirl.create :ticket, :created_by => @current_user.id
 
     #they should be able to comment if it is their ticket:
-    assert_difference('Ticket.find("stubtestticketid").comments.count') do
+    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
+    assigns(:ticket).destroy
 
   end
 
@@ -135,17 +133,13 @@ class TicketsControllerTest < ActionController::TestCase
   test "cannot comment if it is not your ticket" do
 
     login :is_admin? => false, :email => nil
-    ticket = Ticket.first
-
-    assert_not_nil User.first.id
-    ticket.created_by = User.first.id
-    ticket.save
+    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, assigns(:ticket).comments
+    assert_equal ticket.comments.map(&:body), assigns(:ticket).comments.map(&:body)
 
   end
 
@@ -154,14 +148,10 @@ class TicketsControllerTest < ActionController::TestCase
 
     login :is_admin? => true
 
-    ticket = Ticket.find('stubtestticketid')
-    assert_not_nil User.last.id
-    ticket.created_by = User.last.id # TODO: hacky, but confirms it somebody elses ticket:
-    assert_not_equal User.last.id, @current_user.id
-    ticket.save
+    ticket = FactoryGirl.create :ticket, :created_by => @other_user.id
 
     #admin should be able to comment:
-    assert_difference('Ticket.find("stubtestticketid").comments.count') do
+    assert_difference('Ticket.find(ticket.id).comments.count') do
       put :update, :id => ticket.id,
         :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}} }
     end
@@ -169,9 +159,11 @@ class TicketsControllerTest < ActionController::TestCase
     assert_not_nil assigns(:ticket).comments.last.posted_by
     assert_equal assigns(:ticket).comments.last.posted_by, @current_user.id
 
+    assigns(:ticket).destroy
   end
 
   test "tickets by admin" do
+    ticket = FactoryGirl.create :ticket, :created_by => @other_user.id
 
     login :is_admin? => true, :email => nil
 
@@ -188,17 +180,18 @@ class TicketsControllerTest < ActionController::TestCase
 
 
   test "admin_status mine vs all" do
-    testticket = Ticket.create :title => 'temp testytest'
+    testticket = FactoryGirl.create :ticket
     login :is_admin? => true, :email => nil
 
     get :index, {:admin_status => "all", :open_status => "open"}
     assert assigns(:all_tickets).include?(testticket)
     get :index, {:admin_status => "mine", :open_status => "open"}
     assert !assigns(:all_tickets).include?(testticket)
+    testticket.destroy
   end
 
   test "commenting on a ticket adds to tickets that are mine" do
-    testticket = Ticket.create :title => 'temp testytest'
+    testticket = FactoryGirl.create :ticket
     login :is_admin? => true, :email => nil
 
     get :index, {:admin_status => "mine", :open_status => "open"}
@@ -215,6 +208,7 @@ class TicketsControllerTest < ActionController::TestCase
   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'}
@@ -233,6 +227,7 @@ class TicketsControllerTest < ActionController::TestCase
     assert_not_equal first_tick, assigns(:all_tickets).first
     assert_not_equal last_tick, assigns(:all_tickets).last
 
+    tickets.each {|ticket| ticket.destroy}
   end
 
   test "tickets for regular user" do
-- 
cgit v1.2.3


From 7b323c5dbd7171cf98f69ca159b5aa2174a5d069 Mon Sep 17 00:00:00 2001
From: Azul <azul@leap.se>
Date: Mon, 14 Jan 2013 20:02:17 +0100
Subject: minor: further cleanup - try to leave no record behind

---
 help/test/factories.rb                          | 2 +-
 help/test/functional/tickets_controller_test.rb | 2 ++
 help/test/unit/ticket_comment_test.rb           | 5 +++--
 help/test/unit/ticket_test.rb                   | 1 +
 4 files changed, 7 insertions(+), 3 deletions(-)

(limited to 'help/test')

diff --git a/help/test/factories.rb b/help/test/factories.rb
index 919882a..5b38952 100644
--- a/help/test/factories.rb
+++ b/help/test/factories.rb
@@ -3,7 +3,7 @@ FactoryGirl.define do
   factory :ticket do
     title { Faker::Lorem.sentence }
     comments_attributes do
-      { "0" => { "body" => Faker::Lorem.sentences } }
+      { "0" => { "body" => Faker::Lorem.sentences.join(" ") } }
     end
   end
 
diff --git a/help/test/functional/tickets_controller_test.rb b/help/test/functional/tickets_controller_test.rb
index 794b119..0c462a9 100644
--- a/help/test/functional/tickets_controller_test.rb
+++ b/help/test/functional/tickets_controller_test.rb
@@ -274,6 +274,8 @@ class TicketsControllerTest < ActionController::TestCase
     assert assigns(:all_tickets).include?(other_ticket)
     assert_equal assigns(:all_tickets).count, number_closed_tickets + number_open_tickets
 
+    assigns(:all_tickets).each {|t| t.destroy}
+
   end
 
 end
diff --git a/help/test/unit/ticket_comment_test.rb b/help/test/unit/ticket_comment_test.rb
index 1fe1fe2..44865ed 100644
--- a/help/test/unit/ticket_comment_test.rb
+++ b/help/test/unit/ticket_comment_test.rb
@@ -21,11 +21,11 @@ class TicketCommentTest < ActiveSupport::TestCase
 
     #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
@@ -49,6 +49,7 @@ class TicketCommentTest < ActiveSupport::TestCase
     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
diff --git a/help/test/unit/ticket_test.rb b/help/test/unit/ticket_test.rb
index ac6426e..ce35e1d 100644
--- a/help/test/unit/ticket_test.rb
+++ b/help/test/unit/ticket_test.rb
@@ -32,6 +32,7 @@ class TicketTest < ActiveSupport::TestCase
     #p t.email_address
     #p t.email_address.strip =~ RFC822::EmailAddress
     assert !t.valid?
+    t.reload.destroy
   end
 
   test "creation validated" do
-- 
cgit v1.2.3