From b80bd744ad873f6fc43018bc4bfb90677de167bd Mon Sep 17 00:00:00 2001 From: Stefan Wintermeyer Date: Mon, 17 Dec 2012 12:01:45 +0100 Subject: Start of GS5. --- test/unit/phone_book_test.rb | 119 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 test/unit/phone_book_test.rb (limited to 'test/unit/phone_book_test.rb') diff --git a/test/unit/phone_book_test.rb b/test/unit/phone_book_test.rb new file mode 100644 index 0000000..7db48f8 --- /dev/null +++ b/test/unit/phone_book_test.rb @@ -0,0 +1,119 @@ +require 'test_helper' + +class PhoneBookTest < ActiveSupport::TestCase + def test_should_have_a_valid_factory + assert Factory.build(:phone_book).valid? + end + + def test_should_have_unique_name_depending_on_type + user1 = Factory.create(:user) + user2 = Factory.create(:user) + tenant = Factory.create(:tenant) + + phonebook = Factory.create(:phone_book, :phone_bookable => user1) + assert !user1.phone_books.build(:name => phonebook.name).valid? + assert user2.phone_books.build(:name => phonebook.name).valid? + assert tenant.phone_books.build(:name => phonebook.name).valid? + end + + # TODO Create a real system for the phone_book tests and than test again. + + + # test "User gets a private phone book with rw rights" do + # user = Factory.create(:user) + # assert_equal 1, user.phone_books.count + + # phone_book = user.phone_books.first + + # user_ability = Ability.new( user ) + + # [ :show, :destroy, :edit ].each { |action| + # assert user_ability.can?( action, phone_book ), "should be able to #{action}" + # } + + # # Lets test some stuff about the phone_book_entries + # assert_equal 0, phone_book.phone_book_entries.count + + # entry1 = Factory.create(:phone_book_entry, :phone_book_id => phone_book.id) + # entry2 = Factory.create(:phone_book_entry, :phone_book_id => phone_book.id) + # entry3 = Factory.create(:phone_book_entry, :phone_book_id => phone_book.id) + # assert_equal 3, phone_book.phone_book_entries.count + + # assert_equal 1, PhoneBookEntry.where(:id => entry1.id).count + # assert_equal 1, PhoneBookEntry.where(:id => entry2.id).count + # assert_equal 1, PhoneBookEntry.where(:id => entry3.id).count + # user.phone_books.first.destroy + # assert_equal 0, user.phone_books.count + # assert_equal 0, PhoneBookEntry.where(:id => entry1.id).count + # assert_equal 0, PhoneBookEntry.where(:id => entry2.id).count + # assert_equal 0, PhoneBookEntry.where(:id => entry3.id).count + # end + + test "Tenant gets automatically one phone book and can destroy it" do + tenant = Factory.create(:tenant) + assert_equal 1, tenant.phone_books.count + tenant.phone_books.first.destroy + assert_equal 0, tenant.phone_books.count + end + + # test "only tenant members can read a tenant phone book" do + # tenant = Factory.create(:tenant) + # user = Factory.create(:user) + # tenant.users << user + # tenant.save + # user.current_tenant = tenant + # user.save + # phone_book = Factory.create(:phone_book, :phone_bookable_type => 'Tenant', :phone_bookable_id => tenant.id) + + # evil_user = Factory.create(:user) + + # user_ability = Ability.new( user ) + # evil_user_ability = Ability.new( evil_user ) + + # [ :show ].each { |action| + # assert user_ability.can?( action, phone_book ), "should be able to #{action}" + # assert evil_user_ability.cannot?( action, phone_book ), "should not be able to #{action}" + # } + # end + + + + # test "tenant's phone book can not be edited by tenant members" do + # tenant = Factory.create(:tenant) + # user = Factory.create(:user) + # tenant.users << user + # phone_book = Factory.create(:phone_book, :phone_bookable_type => 'Tenant', :phone_bookable_id => tenant.id) + + # evil_user = Factory.create(:user) + + # user_ability = Ability.new( user ) + # evil_user_ability = Ability.new( evil_user ) + + # [ :edit, :destroy ].each { |action| + # assert user_ability.cannot?( action, phone_book ), "should not be able to #{action}" + # assert evil_user_ability.cannot?( action, phone_book ), "should not be able to #{action}" + # } + # end + + # test "only user can manage his private phone book after creating it" do + # user = Factory.create(:user) + # phone_book = Factory.create(:phone_book, :phone_bookable_type => 'User', :phone_bookable_id => user.id) + + # evil_user = Factory.create(:user) + + # user_ability = Ability.new( user ) + # evil_user_ability = Ability.new( evil_user ) + + # [ :show, :destroy, :edit ].each { |action| + # assert user_ability.can?( action, phone_book ), "should be able to #{action}" + # assert evil_user_ability.cannot?( action, phone_book ), "should not be able to #{action}" + # } + # end + + def test_that_the_initial_state_should_be_active + @phone_book = Factory.create(:phone_book) + assert_equal 'active', @phone_book.state + assert @phone_book.active? + end + +end -- cgit v1.2.3