yus 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,132 @@
1
+ #!/usr/bin/env ruby
2
+ # TestServer -- yus -- 01.06.2006 -- hwyss@ywesee.com
3
+
4
+ $: << File.expand_path('../lib', File.dirname(__FILE__))
5
+
6
+ require 'test/unit'
7
+ require 'yus/server'
8
+ require 'flexmock'
9
+
10
+ module Yus
11
+ class Server
12
+ public :authenticate, :clean
13
+ end
14
+ class TestServer < Test::Unit::TestCase
15
+ def setup
16
+ @config = FlexMock.new
17
+ @config.should_receive(:cleaner_interval).and_return { 100000000 }
18
+ digest = FlexMock.new
19
+ digest.should_receive(:hexdigest).and_return { |input| input }
20
+ @config.should_receive(:digest).and_return { digest }
21
+ @config.should_receive(:session_timeout).and_return { 0.5 }
22
+ @config.should_receive(:root_name).and_return { 'admin' }
23
+ @config.should_receive(:root_pass).and_return { 'admin' }
24
+ @logger = FlexMock.new
25
+ @logger.should_receive(:info)
26
+ @logger.should_receive(:debug)
27
+ @persistence = FlexMock.new
28
+ @server = Server.new(@persistence, @config, @logger)
29
+ end
30
+ def test_authenticate__no_user
31
+ @logger.should_receive(:warn).times(1)
32
+ @persistence.should_receive(:find_entity).times(1)
33
+ assert_raises(UnknownEntityError) {
34
+ @server.authenticate('name', 'password')
35
+ }
36
+ end
37
+ def test_authenticate__wrong_password
38
+ @logger.should_receive(:warn).times(1)
39
+ user = FlexMock.new
40
+ user.should_receive(:authenticate).and_return { false }
41
+ @persistence.should_receive(:find_entity).times(1).and_return { user }
42
+ assert_raises(AuthenticationError) {
43
+ @server.authenticate('name', 'password')
44
+ }
45
+ end
46
+ def test_authenticate__success
47
+ user = FlexMock.new
48
+ user.should_receive(:authenticate).and_return { |pass|
49
+ assert_equal('password', pass)
50
+ true
51
+ }
52
+ @persistence.should_receive(:find_entity).times(1).and_return { user }
53
+ assert_nothing_raised {
54
+ result = @server.authenticate('name', 'password')
55
+ assert_equal(user, result)
56
+ }
57
+ end
58
+ def test_autosession
59
+ @server.autosession('domain') { |session|
60
+ assert_instance_of(AutoSession, session)
61
+ }
62
+ end
63
+ def test_login__success
64
+ user = FlexMock.new
65
+ user.should_receive(:authenticate).and_return { |pass|
66
+ assert_equal('password', pass)
67
+ true
68
+ }
69
+ user.should_receive(:login)
70
+ user.should_receive(:get_preference).and_return { |key, domain|
71
+ {
72
+ 'session_timeout' => 0.5,
73
+ }[key]
74
+ }
75
+ @persistence.should_receive(:find_entity).times(1).and_return { user }
76
+ @persistence.should_receive(:save_entity).times(1)
77
+ assert_nothing_raised {
78
+ session = @server.login('name', 'password', 'domain')
79
+ assert_instance_of(EntitySession, session)
80
+ assert_kind_of(DRb::DRbUndumped, session)
81
+ assert_equal([session], @server.instance_variable_get('@sessions'))
82
+ }
83
+ end
84
+ def test_logout
85
+ needle = FlexMock.new
86
+ needle.should_receive(:config).and_return { @config }
87
+ @config.should_receive(:session_timeout).and_return { 200 }
88
+ sessions = @server.instance_variable_get('@sessions')
89
+ session = RootSession.new(needle)
90
+ sessions.push(session)
91
+ @server.logout(session)
92
+ assert_equal([], sessions)
93
+ end
94
+ def test_login__root
95
+ session = @server.login('admin', 'admin', 'domain')
96
+ assert_instance_of(RootSession, session)
97
+ end
98
+ def test_ping
99
+ assert(@server.ping)
100
+ end
101
+ def test_clean
102
+ needle = FlexMock.new
103
+ needle.should_receive(:config).and_return { @config }
104
+ @config.should_receive(:session_timeout).and_return { 0.5 }
105
+ sessions = @server.instance_variable_get('@sessions')
106
+ session = RootSession.new(needle)
107
+ sessions.push(session)
108
+ sleep(1)
109
+ @server.clean
110
+ assert_equal([], sessions)
111
+ end
112
+ end
113
+ class TestServerCleaner < Test::Unit::TestCase
114
+ def test_autoclean
115
+ config = FlexMock.new
116
+ config.should_receive(:cleaner_interval).and_return { 0.5 }
117
+ config.should_receive(:session_timeout).and_return { 0.5 }
118
+ logger = FlexMock.new
119
+ logger.should_receive(:info)
120
+ logger.should_receive(:debug)
121
+ needle = FlexMock.new
122
+ needle.should_receive(:config).and_return { config }
123
+ persistence = FlexMock.new
124
+ server = Server.new(persistence, config, logger)
125
+ sessions = server.instance_variable_get('@sessions')
126
+ session = RootSession.new(needle)
127
+ sessions.push(session)
128
+ sleep(2)
129
+ assert_equal([], sessions)
130
+ end
131
+ end
132
+ end