summaryrefslogtreecommitdiff
path: root/test/unit/phone_book_test.rb
blob: 7db48f88d618f96cdfe297ca29e10a5538db8d3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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