Skip to content

Commit b412e20

Browse files
author
farkwun
committed
Adding tests for user model
1 parent ccc3499 commit b412e20

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

test/models/user_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require File.expand_path('../../test_helper', __FILE__)
2+
3+
class UserTest < ActiveSupport::TestCase
4+
include TestSetupMethods
5+
include TestHelperMethods
6+
def setup
7+
@show_debug = false
8+
end
9+
10+
test "parent_id_required?" do
11+
@alice = create_user('alice', '[email protected]')
12+
assert @alice.errors.any?,
13+
"User should have failed creation - #{@alice.inspect}"
14+
end
15+
16+
test "cannot_set_parent_id_to_child_id" do
17+
flunk('Functionality does not exist') # comment this out to test actual code
18+
19+
@alice = create_user('alice','[email protected]', 1)
20+
@bob = create_user('bob','[email protected]', @alice.id)
21+
22+
find_user(@alice).update({:parent_id => @bob.id.to_i})
23+
24+
refute find_user(@alice).parent_id.to_i == @bob.id.to_i,
25+
"Should not allow update with child as parent - #{find_user(@alice).inspect}"
26+
end
27+
end

test/test_helper.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,44 @@
11
# Load the Redmine helper
22
require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper')
3+
4+
module TestSetupMethods
5+
def create_user(login, email, parent_id = nil)
6+
pwd = repeat_str('123')
7+
mock_user = User.create() do |u|
8+
u.login = login
9+
u.password = pwd
10+
u.password_confirmation = pwd
11+
u.firstname = login
12+
u.lastname = 'doe'
13+
u.mail = email
14+
u.language = 'en'
15+
u.mail_notification = 'only_my_events'
16+
u.must_change_passwd = false
17+
u.parent_id = parent_id
18+
u.status = 1
19+
u.auth_source_id = nil
20+
end
21+
# block below prints out all errors if validation fails
22+
if mock_user.errors.any? && @show_debug
23+
mock_user.errors.each do |attribute, message|
24+
puts "Error - #{attribute} : #{message}"
25+
end
26+
end
27+
mock_user
28+
end
29+
end
30+
31+
module TestHelperMethods
32+
def repeat_str(input, length = Setting.password_min_length)
33+
puts input if @show_debug
34+
repeated_input = ""
35+
while repeated_input.length < length.to_i
36+
repeated_input << input
37+
end
38+
repeated_input
39+
end
40+
41+
def find_user(user)
42+
User.find_by_login(user.login)
43+
end
44+
end

0 commit comments

Comments
 (0)