From d620fd42925f1d3d29a66bb61a75bed8c2bdaf8f Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 6 Nov 2013 10:40:32 +0100 Subject: refactor: split up and cleaned up ticket validation tests --- help/test/factories.rb | 8 ++++++-- help/test/unit/ticket_test.rb | 41 +++++++++++++++++++---------------------- 2 files changed, 25 insertions(+), 24 deletions(-) (limited to 'help') diff --git a/help/test/factories.rb b/help/test/factories.rb index 5b38952..5368ce6 100644 --- a/help/test/factories.rb +++ b/help/test/factories.rb @@ -2,8 +2,12 @@ FactoryGirl.define do factory :ticket do title { Faker::Lorem.sentence } - comments_attributes do - { "0" => { "body" => Faker::Lorem.sentences.join(" ") } } + email { Faker::Internet.email } + + factory :ticket_with_comment do + comments_attributes do + { "0" => { "body" => Faker::Lorem.sentences.join(" ") } } + end end end diff --git a/help/test/unit/ticket_test.rb b/help/test/unit/ticket_test.rb index ce35e1d..04832d9 100644 --- a/help/test/unit/ticket_test.rb +++ b/help/test/unit/ticket_test.rb @@ -1,41 +1,38 @@ require 'test_helper' class TicketTest < ActiveSupport::TestCase - #test "the truth" do - # assert true - #end - setup do - @sample = Ticket.new + test "ticket with default attribs is valid" do + t = FactoryGirl.build :ticket + assert t.valid? end - test "validity" do - t = Ticket.create :title => 'test title', :email => 'blah@blah.com' + test "ticket without email is valid" do + t = FactoryGirl.build :ticket, email: "" assert t.valid? - assert_equal t.title, 'test title' + end + + test "ticket validates email format" do + t = FactoryGirl.build :ticket, email: "aswerssfd" + assert !t.valid? + end + test "ticket allows for multiple email addresses" do + t = FactoryGirl.build :ticket, email: 'blah@blah.com, bb@jjj.org' + 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 - #user = LeapWebHelp::User.new(User.valid_attributes_hash) - #user = LeapWebUsers::User.create - - #t.user = user - - #t.email = '' #invalid - #assert !t.valid? - #t.email = 'blah@blah.com, bb@jjj.org' - #assert t.valid? - t.email = 'bdlfjlkasfjklasjf' #invalid - #p t.email_address - #p t.email_address.strip =~ RFC822::EmailAddress - assert !t.valid? - t.reload.destroy end test "creation validated" do + @sample = Ticket.new assert !@sample.is_creator_validated? #p current_user @sample.created_by = 22 #current_user -- cgit v1.2.3 From ebc60f3aba1ca08e454ba5e91f49905df2e5fa13 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 6 Nov 2013 10:55:25 +0100 Subject: Ticket.destroy_all_from(user) - remove all tickets created by a user We'll use this to clean up after user destruction --- help/app/models/ticket.rb | 7 +++++++ help/test/factories.rb | 4 ++++ help/test/unit/ticket_test.rb | 6 ++++++ 3 files changed, 17 insertions(+) (limited to 'help') diff --git a/help/app/models/ticket.rb b/help/app/models/ticket.rb index 8066d0d..74f2050 100644 --- a/help/app/models/ticket.rb +++ b/help/app/models/ticket.rb @@ -24,6 +24,7 @@ class Ticket < CouchRest::Model::Base design do view :by_updated_at view :by_created_at + view :by_created_by view :by_is_open_and_created_at view :by_is_open_and_updated_at @@ -40,6 +41,12 @@ class Ticket < CouchRest::Model::Base @selection.tickets end + def self.destroy_all_from(user) + self.by_created_by.key(user.id).each do |ticket| + ticket.destroy + end + end + def is_creator_validated? !!created_by end diff --git a/help/test/factories.rb b/help/test/factories.rb index 5368ce6..bce3af1 100644 --- a/help/test/factories.rb +++ b/help/test/factories.rb @@ -9,6 +9,10 @@ FactoryGirl.define 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/help/test/unit/ticket_test.rb b/help/test/unit/ticket_test.rb index 04832d9..6c29d11 100644 --- a/help/test/unit/ticket_test.rb +++ b/help/test/unit/ticket_test.rb @@ -39,6 +39,12 @@ class TicketTest < ActiveSupport::TestCase 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 -- cgit v1.2.3 From 24598b5c5e4df20c423ec74ea8e9df1592483c6b Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 6 Nov 2013 11:26:46 +0100 Subject: destroy all tickets created by a user when account is destroyed In order to keep the users engine independent of the tickets engine i added a generic load hook to the account model. The tickets engine then monkeypatches the account destruction and destroys all tickets before the user is destroyed. The tickets are destroyed first so that even if things break there should never be tickets with an outdated user id. I would have prefered to use super over using an alias_method_chain but I have not been able to figure out a way to make account a superclass of the account extension and still refer to Account from the users engine. --- help/app/models/account_extension/tickets.rb | 13 +++++++++++++ help/config/initializers/account_lifecycle.rb | 3 +++ help/test/unit/account_extension_test.rb | 12 ++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 help/app/models/account_extension/tickets.rb create mode 100644 help/config/initializers/account_lifecycle.rb create mode 100644 help/test/unit/account_extension_test.rb (limited to 'help') diff --git a/help/app/models/account_extension/tickets.rb b/help/app/models/account_extension/tickets.rb new file mode 100644 index 0000000..f898b56 --- /dev/null +++ b/help/app/models/account_extension/tickets.rb @@ -0,0 +1,13 @@ +module AccountExtension::Tickets + extend ActiveSupport::Concern + + def destroy_with_tickets + Ticket.destroy_all_from(self.user) + destroy_without_tickets + end + + included do + alias_method_chain :destroy, :tickets + end + +end diff --git a/help/config/initializers/account_lifecycle.rb b/help/config/initializers/account_lifecycle.rb new file mode 100644 index 0000000..d9f04c1 --- /dev/null +++ b/help/config/initializers/account_lifecycle.rb @@ -0,0 +1,3 @@ +ActiveSupport.on_load(:account) do + include AccountExtension::Tickets +end diff --git a/help/test/unit/account_extension_test.rb b/help/test/unit/account_extension_test.rb new file mode 100644 index 0000000..aba162c --- /dev/null +++ b/help/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 -- cgit v1.2.3 From 69a41bee2548fa8743dd3188b0ebfc84dac17062 Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 8 Nov 2013 09:48:57 +0100 Subject: removed outdated test. --- help/test/unit/ticket_test.rb | 5 ----- 1 file changed, 5 deletions(-) (limited to 'help') diff --git a/help/test/unit/ticket_test.rb b/help/test/unit/ticket_test.rb index 6c29d11..751fe70 100644 --- a/help/test/unit/ticket_test.rb +++ b/help/test/unit/ticket_test.rb @@ -17,11 +17,6 @@ class TicketTest < ActiveSupport::TestCase assert !t.valid? end - test "ticket allows for multiple email addresses" do - t = FactoryGirl.build :ticket, email: 'blah@blah.com, bb@jjj.org' - assert !t.valid? - end - test "ticket open states" do t = FactoryGirl.build :ticket assert t.is_open -- cgit v1.2.3