aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/user_spec.rb
blob: 5f29ebeb693a21a44b5411a87c2ae4471309a2f7 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
require File.dirname(__FILE__) + '/../spec_helper'

describe User, " when authenticating" do
    before do
        @empty_user = User.new 

        @full_user = User.new
        @full_user.name = "Sensible User"
        @full_user.password = "foolishpassword"
        @full_user.email = "sensible@localhost"
        @full_user.save
    end

    it "should create a hashed password when the password is set" do
        @empty_user.hashed_password.should be_nil
        @empty_user.password = "a test password"
        @empty_user.hashed_password.should_not be_nil
    end

    it "should have errors when given the wrong password" do
        found_user = User.authenticate_from_form({ :email => "sensible@localhost", :password => "iownzyou" })
        found_user.errors.size.should > 0
    end

    it "should not find the user when given the wrong email" do
        found_user = User.authenticate_from_form( { :email => "soccer@localhost", :password => "foolishpassword" })
        found_user.errors.size.should > 0
    end

    it "should find the user when given the right email and password" do
        found_user = User.authenticate_from_form( { :email => "sensible@localhost", :password => "foolishpassword" })
        found_user.errors.size.should == 0
        found_user.should == (@full_user)
    end

end

describe User, " when saving" do
    before do
        @user = User.new 
    end

    it "should not save without setting some parameters" do
        lambda { @user.save! }.should raise_error(ActiveRecord::RecordInvalid)
    end

    it "should not save with misformatted email" do
        @user.name = "Mr. Silly"
        @user.password = "insecurepassword"  
        @user.email = "mousefooble"
        @user.should have(1).error_on(:email)
    end

    it "should not allow an email address as a name" do
        @user.name = "silly@example.com"
        @user.email = "silly@example.com"
        @user.password = "insecurepassword"  
        @user.should have(1).error_on(:name)
    end

    it "should not save with no password" do
        @user.name = "Mr. Silly"
        @user.password = ""  
        @user.email = "silly@localhost"
        @user.should have(1).error_on(:hashed_password)
    end

    it "should save with reasonable name, password and email" do
        @user.name = "Mr. Reasonable"
        @user.password = "insecurepassword"  
        @user.email = "reasonable@localhost"
        @user.save!
    end

    it "should let you make two users with same name" do
        @user.name = "Mr. Flobble"
        @user.password = "insecurepassword"  
        @user.email = "flobble@localhost"
        @user.save!

        @user2 = User.new 
        @user2.name = "Mr. Flobble"
        @user2.password = "insecurepassword"  
        @user2.email = "flobble2@localhost"
        @user2.save!
    end


end

describe User, "when reindexing referencing models" do 

    before do 
        @request_event = mock_model(InfoRequestEvent, :xapian_mark_needs_index => true)
        @request = mock_model(InfoRequest, :info_request_events => [@request_event])
        @comment_event = mock_model(InfoRequestEvent, :xapian_mark_needs_index => true)
        @comment = mock_model(Comment, :info_request_events => [@comment_event])
        @user = User.new(:comments => [@comment], :info_requests => [@request])
    end
    
    it 'should reindex events associated with that user\'s comments' do 
        @comment_event.should_receive(:xapian_mark_needs_index)
        @user.reindex_referencing_models
    end
    
    it 'should reindex events associated with that user\'s requests' do 
        @request_event.should_receive(:xapian_mark_needs_index)
        @user.reindex_referencing_models
    end
    
    describe 'when no_reindex is set' do 
        
        before do 
            @user.no_reindex = true
        end
            
        it 'should not reindex events associated with that user\'s comments' do 
            @comment_event.should_not_receive(:xapian_mark_needs_index)
            @user.reindex_referencing_models
        end
        
        it 'should not reindex events associated with that user\'s requests' do 
            @request_event.should_not_receive(:xapian_mark_needs_index)
            @user.reindex_referencing_models
        end
    
    end
    
end

describe User, "when checking abilities" do
    fixtures :users

    before do
        @user = users(:bob_smith_user)
    end

    it "should not get admin links" do
        @user.admin_page_links?.should be_false
    end

    it "should be able to file requests" do
        @user.can_file_requests?.should be_true
    end

end

describe User, 'when asked if a user owns every request' do 
    
    before do 
        @mock_user = mock_model(User)
    end
    
    it 'should return false if no user is passed' do 
        User.owns_every_request?(nil).should be_false
    end
    
    it 'should return true if the user has "requires admin" power' do 
        @mock_user.stub!(:owns_every_request?).and_return true
        User.owns_every_request?(@mock_user).should be_true
    end
    
    it 'should return false if the user does not have "requires admin" power' do 
        @mock_user.stub!(:owns_every_request?).and_return false
        User.owns_every_request?(@mock_user).should be_false
    end
    
end

describe User, " when making name and email address" do
    it "should generate a name and email" do
        @user = User.new
        @user.name = "Sensible User"
        @user.email = "sensible@localhost"

        @user.name_and_email.should == "Sensible User <sensible@localhost>"
    end

    it "should quote name and email with funny characters in the name" do
        @user = User.new
        @user.name = "Silly @ User"
        @user.email = "silly@localhost"

        @user.name_and_email.should == "\"Silly @ User\" <silly@localhost>"
    end
end