public
Description: OneBody is free, open-source, web-based social networking and online directory software for churches.
Homepage: http://beonebody.com
Clone URL: git://github.com/seven1m/onebody.git
onebody / test / unit / person_test.rb
100644 130 lines (114 sloc) 4.995 kb
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
120
121
122
123
124
125
126
127
128
129
130
require File.dirname(__FILE__) + '/../test_helper'
 
class PersonTest < Test::Unit::TestCase
  fixtures :people, :families
  
  should "know which groups the person belongs to" do
    Group.find(:all).each do |group|
      group.people.each do |person|
        assert person.member_of?(group)
      end
    end
  end
  
  should "not allow someone outside the family to share the same email address" do
    # tim already has email address
    p = people(:peter)
    p.email = people(:tim).email
    p.save
    assert !p.valid?
    assert_equal 'already taken by someone else.', p.errors[:email]
  end
  
  should "validate format of email address" do
    # test every character allowed
    p = people(:peter)
    p.email = 'abcdefghijklmnopqrstuvwxyz0123456789._%-@abcdefghijklmnopqrstuvwxyz0123456789.-.com'
    p.save
    assert_nil p.errors[:email]
    # test what we have in our fixtures
    Person.find(:all).select { |p| p.email }.each do |p|
      p.save
      assert_nil p.errors[:email]
    end
    # test a bad one
    p = people(:peter)
    p.email = 'bad address@example.com'
    p.save
    assert !p.valid?
    assert_equal 'not a valid email address.', p.errors[:email]
  end
  
  should "validate format of website" do
    Person.logged_in = people(:peter)
    # good
    p = people(:peter)
    p.website = 'http://goodwebsite.com/a/path?some=args'
    p.save
    assert_nil p.errors[:website]
    # bad
    p.website = "javascript://void(alert('do evil stuff'))"
    p.save
    assert !p.valid?
    assert_equal "has an incorrect format (are you missing 'http://' at the beginning?)", p.errors[:website]
  end
  
  should "inherit attribute sharing from family" do
    # update_attribute to nil doesn't seem to work for booleans on fixture instantiated instances
    f = families(:morgan); p = Person.find(people(:jennie).id)
    # family = true, person = false, peter should not see
    f.update_attribute :share_mobile_phone, true
    p.update_attribute :share_mobile_phone, false
    assert !p.share_mobile_phone_with(people(:peter))
    # family = false, person = true, peter should see
    f.update_attribute :share_mobile_phone, false
    p.update_attribute :share_mobile_phone, true
    assert p.share_mobile_phone_with(people(:peter))
    # family = true, person = nil, peter should see
    f.update_attribute :share_mobile_phone, true
    p.update_attribute :share_mobile_phone, nil
    assert Person.find(people(:jennie).id).share_mobile_phone_with(people(:peter))
    # family = false, person = nil, peter should not see
    f.update_attribute :share_mobile_phone, false
    p.update_attribute :share_mobile_phone, nil
    assert !Person.find(people(:jennie).id).share_mobile_phone_with(people(:peter))
  end
  
  should "create an update" do
    tim = {
      'person' => partial_fixture('people', 'tim', %w(first_name last_name suffix gender mobile_phone work_phone fax birthday anniversary)),
      'family' => partial_fixture('families', 'morgan', %w(name last_name home_phone address1 address2 city state zip))
    }
 
    (tim_change_first_name = tim.clone)['person']['first_name'] = 'Timothy'
    update = Update.create_from_params(tim_change_first_name, people(:tim))
    assert_equal 'Timothy', update.first_name
    assert_equal '04/28/1981', update.birthday.strftime('%m/%d/%Y')
    assert_equal '08/11/2001', update.anniversary.strftime('%m/%d/%Y')
 
    (tim_change_birthday = tim.clone)['person']['birthday'] = '06/24/1980'
    update = Update.create_from_params(tim_change_birthday, people(:tim))
    assert_equal '06/24/1980', update.birthday.strftime('%m/%d/%Y')
    assert_equal '08/11/2001', update.anniversary.strftime('%m/%d/%Y')
    
    (tim_remove_anniversary = tim.clone)['person']['anniversary'] = ''
    update = Update.create_from_params(tim_remove_anniversary, people(:tim))
    assert_equal '06/24/1980', update.birthday.strftime('%m/%d/%Y')
    assert_equal nil, update.anniversary
  end
  
  should "mark email_changed when email address changes" do
    Person.logged_in = people(:tim)
    people(:tim).email = 'change@example.com'
    assert !people(:tim).email_changed?
    people(:tim).save
    assert people(:tim).email_changed?
  end
  
  should "generate a custom directory pdf" do
    assert_match /PDF\-1\.3/, people(:tim).generate_directory_pdf.to_s[0..100]
  end
  
  should "know when a birthday is coming up" do
    people(:tim).update_attributes!(:birthday => Date.today + 5)
    assert people(:tim).reload.birthday_soon?
    people(:tim).update_attributes!(:birthday => Date.today + BIRTHDAY_SOON_DAYS + 1)
    assert !people(:tim).reload.birthday_soon?
  end
  
  private
  
    def partial_fixture(table, name, valid_attributes)
      returning YAML::load(File.open(File.join(RAILS_ROOT, "test/fixtures/#{table}.yml")))[name] do |fixture|
        fixture.delete_if { |key, val| !valid_attributes.include? key }
        fixture.each do |key, val|
          fixture[key] = val.to_s
        end
      end
    end
end