sbsm 1.3.4 → 1.3.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -124,6 +124,7 @@ module SBSM
124
124
  end
125
125
  end
126
126
  def extend(mod)
127
+ binding.pry
127
128
  if(mod.constants.include?(:VIRAL))
128
129
  @viral_modules.push(mod)
129
130
  end
@@ -28,7 +28,6 @@
28
28
  require 'digest/md5'
29
29
  require 'mail'
30
30
  require 'date'
31
- require 'drb/drb'
32
31
  require 'uri'
33
32
  require 'stringio'
34
33
  require 'hpricot'
@@ -97,6 +96,7 @@ module SBSM
97
96
  def validate(key, value)
98
97
  value = value.pop if value.is_a? Array
99
98
  return nil if value.nil?
99
+ # SBSM.info("validate #{key} #{value.class} #{value}")
100
100
  if value.is_a?(StringIO)
101
101
  if(@files.include?(key))
102
102
  return validate_file(key, value)
@@ -107,7 +107,7 @@ module SBSM
107
107
  p e
108
108
  end
109
109
  end
110
- elsif(value.is_a? DRb::DRbObject)
110
+ elsif(value.is_a?(Array)) # TODO::
111
111
  value = value[0]
112
112
  if(@files.include?(key))
113
113
  return validate_file(key, value)
@@ -123,7 +123,8 @@ module SBSM
123
123
  end
124
124
  def perform_validation(key, value)
125
125
  value = value.to_s.strip
126
- begin
126
+ # require 'pry'; binding.pry if /event/.match(key.to_s)
127
+ begin
127
128
  if(key==:event)
128
129
  symbol = value.to_sym
129
130
  symbol if @events.include?(symbol)
@@ -1,3 +1,3 @@
1
1
  module SBSM
2
- VERSION = '1.3.4'
2
+ VERSION = '1.3.5'
3
3
  end
@@ -42,7 +42,7 @@ module SBSM
42
42
  infect(newstate)
43
43
  end
44
44
  newstate
45
- rescue DRb::DRbError, RangeError
45
+ rescue RangeError
46
46
  @session.logout
47
47
  home
48
48
  end
@@ -0,0 +1,11 @@
1
+ #\ -w -p 6789
2
+ # 6789 must be in sync with TEST_APP_URI from test_application.rb
3
+ root_dir = File.expand_path(File.join(__FILE__, '..', '..'))
4
+ $LOAD_PATH << File.join(root_dir, 'test')
5
+ require 'simple_sbsm'
6
+ use Rack::CommonLogger, TEST_LOGGER
7
+ use Rack::Reloader, 0
8
+ use Rack::ContentLength
9
+ use(Rack::Static, urls: ["/doc/"])
10
+ app = Rack::ShowExceptions.new(Rack::Lint.new(Demo::SimpleSBSM.new(cookie_name: ::TEST_COOKIE_NAME)))
11
+ run app
@@ -1,15 +1,16 @@
1
1
  #!/usr/local/bin/ruby
2
2
  # Niklaus Giger, November 2016
3
3
  # A simple example on how to use SBSM with webrick
4
- require 'drb/drb'
5
4
  require 'sbsm/logger'
6
- require 'sbsm/session'
7
- require 'sbsm/validator'
8
- require 'sbsm/drbserver'
9
- require 'sbsm/state'
5
+ require 'sbsm/app'
10
6
 
11
- SERVER_URI = 'druby://localhost:9876'
7
+ root_dir = File.expand_path(File.join(__FILE__, '..', '..'))
8
+ TEST_LOGGER = ChronoLogger.new(File.join(root_dir, 'test.log'))
9
+ SBSM.logger=TEST_LOGGER
10
+
11
+ TEST_APP_URI = 'druby://localhost:9876'
12
12
  SERVER_NAME = 'localhost:9878'
13
+ TEST_COOKIE_NAME = 'test-cookie'
13
14
 
14
15
  # Some string shared with the unit test
15
16
  HOME_HTML_CONTENT = 'Überall zu Hause' # mit UTF-8!
@@ -19,6 +20,10 @@ FEEDBACK_HTML_CONTENT = 'Give us your feedback about SBSM'
19
20
  CONFIRM_HTML_CONTENT = 'Please confirm your feedback'
20
21
  SENT_HTML_CONTENT = 'Thanks for you feedback! Hope to see you soon'
21
22
 
23
+ begin
24
+ require 'pry'
25
+ rescue LoadError
26
+ end
22
27
 
23
28
  module Demo
24
29
  class GlobalState < SBSM::State
@@ -45,7 +50,7 @@ module Demo
45
50
  }
46
51
  @@class_counter = 0
47
52
  def initialize(session, user)
48
- SBSM.info "HomeState #{session} DRb.front is #{DRb.front}"
53
+ SBSM.info "HomeState #{session}"
49
54
  @session = session
50
55
  @member_counter = 0
51
56
  super(session, user)
@@ -135,7 +140,7 @@ module Demo
135
140
  def ready?
136
141
  unless @email
137
142
  false
138
- else
143
+ elseustomized
139
144
  true
140
145
  end
141
146
  end
@@ -211,18 +216,15 @@ module Demo
211
216
  end
212
217
  class Session < SBSM::Session
213
218
  DEFAULT_STATE = HomeState
214
- def initialize(key, app, validator=Validator.new)
215
- SBSM.info "Session #{app}"
216
- super(key, app, validator)
217
- end
218
219
  end
220
+
219
221
  class SimpleSBSM < SBSM::App
220
222
  SESSION = Session
221
- attr_reader :drb_uri
222
223
  def initialize(cookie_name: nil)
223
- @drb_uri = SERVER_URI
224
224
  SBSM.info "SimpleSBSM.new"
225
- super(:app => self, :validator => Validator.new, :trans_handler => SBSM::TransHandler.instance, :drb_uri => SERVER_URI,
225
+ super(validator: Validator.new,
226
+ trans_handler: SBSM::TransHandler.instance,
227
+ session_class: SESSION,
226
228
  cookie_name: cookie_name)
227
229
  end
228
230
  end
@@ -12,58 +12,47 @@ require 'sbsm/app'
12
12
  require 'sbsm/session'
13
13
  require 'simple_sbsm'
14
14
  require 'nokogiri'
15
- begin
16
- require 'pry'
17
- rescue LoadError
18
- end
19
15
 
20
- class AppVariantTest < MiniTest::Unit::TestCase
16
+ RUN_ALL_TESTS=true
17
+ # Here we test, whether setting various class constant have the desired effect
18
+
19
+ class AppVariantTest < Minitest::Test
21
20
  include Rack::Test::Methods
22
- MY_COOKIE_NAME = 'test-cookie'
21
+ attr_reader :app
23
22
  def setup
24
- @@myapp = Demo::SimpleSBSM.new(cookie_name: MY_COOKIE_NAME)
25
- SBSM.info msg = "Starting #{SERVER_URI}"
26
- DRb.start_service(SERVER_URI, @@myapp)
27
- sleep(0.1)
28
- end
29
- def teardown
30
- DRb.stop_service
23
+ @app = Demo::SimpleSBSM.new(cookie_name: TEST_COOKIE_NAME)
31
24
  end
32
- def app
33
- @@myapp
34
- end
35
-
36
25
  def test_post_feedback
37
26
  get '/de/page' do # needed to set cookie
38
- last_response.set_cookie(MY_COOKIE_NAME, :value => Hash.new('anrede' => 'value2'))
27
+ last_response.set_cookie(TEST_COOKIE_NAME, :value => Hash.new('anrede' => 'value2'))
39
28
  end
40
29
  get '/de/page/feedback' do
41
30
  end
42
- assert_equal ["_session_id", MY_COOKIE_NAME], last_request.cookies.keys
43
- skip "Cannot test cookie_input"
44
- assert_equal ['anrede', 'name'], @@myapp.proxy.cookie_input.keys
45
- assert_equal 'xxx', @@myapp.proxy.persistent_user_input(:anrede)
31
+ assert_equal ["_session_id", TEST_COOKIE_NAME], last_request.cookies.keys
32
+ skip "Cannot content for test cookie_input"
33
+ assert_equal ['anrede', 'name'], @app.cookie_input.keys
34
+ assert_equal 'xxx', @app.persistent_user_input(:anrede)
46
35
 
47
- assert_equal ['value2', 'value3'], @@myapp.proxy.cookie_input.values
36
+ assert_equal ['value2', 'value3'], @app.cookie_input.values
48
37
  assert_match /anrede=value2/, CGI.unescape(last_response.headers['Set-Cookie'])
49
38
  end
50
- end
39
+ end if RUN_ALL_TESTS
51
40
 
52
- class AppTest < MiniTest::Unit::TestCase
41
+ class AppTest < Minitest::Test
53
42
  include Rack::Test::Methods
43
+ attr_reader :app
54
44
 
55
45
  def setup
56
- @@myapp = Demo::SimpleSBSM.new
57
- SBSM.info msg = "Starting #{SERVER_URI}"
58
- DRb.start_service(SERVER_URI, @@myapp)
59
- sleep(0.1)
60
- end
61
- def teardown
62
- DRb.stop_service
63
- end
64
- def app
65
- @@myapp
46
+ @app = Demo::SimpleSBSM.new
66
47
  end
48
+
49
+ def test_session_redirect
50
+ get '/de/page/redirect'
51
+ assert_equal 303,last_response.status
52
+ assert_equal 'feedback',last_response.headers['Location']
53
+ assert_match REDIRECT_HTML_CONTENT, last_response.body
54
+ assert_match /utf-8/i, last_response.headers['Content-Type']
55
+ end if RUN_ALL_TESTS
67
56
  def test_post_feedback
68
57
  get '/de/page' do # needed to set cookie
69
58
  last_response.set_cookie(SBSM::Session::PERSISTENT_COOKIE_NAME, :value => Hash.new('anrede' => 'value2', 'name' => 'values'))
@@ -76,10 +65,10 @@ class AppTest < MiniTest::Unit::TestCase
76
65
  skip "Cannot test cookie_input"
77
66
  assert_match /anrede.*=.*value2/, CGI.unescape(last_response.headers['Set-Cookie'])
78
67
  assert_match FEEDBACK_HTML_CONTENT, last_response.body
79
- assert_equal ['anrede'], @@myapp.proxy.cookie_input.keys
80
- assert_equal ['value2'], @@myapp.proxy.cookie_input.values
81
- assert_equal ['anrede', 'name'], @@myapp.proxy.cookie_input.keys
82
- assert_equal ['value2', 'value3'], @@myapp.proxy.cookie_input.values
68
+ assert_equal ['anrede'], @app.cookie_input.keys
69
+ assert_equal ['value2'], @app.cookie_input.values
70
+ assert_equal ['anrede', 'name'], @app.cookie_input.keys
71
+ assert_equal ['value2', 'value3'], @app.cookie_input.values
83
72
  page = Nokogiri::HTML(last_response.body)
84
73
  x = page.css('div')
85
74
  skip 'We must add here an input form or we cannot continue testing'
@@ -95,7 +84,7 @@ class AppTest < MiniTest::Unit::TestCase
95
84
  assert last_response.ok?
96
85
  assert_match CONFIRM_DONE_HTML_CONTENT, last_response.body
97
86
  end
98
-
87
+ if RUN_ALL_TESTS
99
88
  def test_session_home
100
89
  get '/home'
101
90
  assert last_response.ok?
@@ -149,6 +138,7 @@ class AppTest < MiniTest::Unit::TestCase
149
138
  assert_match /^request_path is \/$/, body
150
139
  assert_match /member_counter is 1$/, body
151
140
  assert_match HOME_HTML_CONTENT, body
141
+ # Getting the request a second time must increment the class, but not the member counter
152
142
  m = /class_counter is (\d+)$/.match(body)
153
143
  counter = m[1]
154
144
  assert_match /class_counter is (\d+)$/, body
@@ -156,9 +146,21 @@ class AppTest < MiniTest::Unit::TestCase
156
146
  assert last_response.ok?
157
147
  body = last_response.body.clone
158
148
  assert_match /^request_path is \/$/, body
159
- assert_match /member_counter is 2$/, body
160
- assert_match /class_counter is #{counter.to_i+1}$/, body
149
+ class_line = /class_counter.*/.match(body)[0]
150
+ assert_match /class_counter is #{counter.to_i+1}$/, class_line
151
+ member_line = /member_counter.*/.match(body)[0]
152
+ assert_match /member_counter is 1$/, member_line
153
+ end
154
+ def test_session_home_then_fr_about
155
+ get '/home'
156
+ assert last_response.ok?
157
+ assert_match /^request_path is \/home$/, last_response.body
158
+ assert_match HOME_HTML_CONTENT, last_response.body
159
+ get '/fr/page/about'
160
+ assert last_response.ok?
161
+ assert_match ABOUT_HTML_CONTENT, last_response.body
161
162
  end
163
+
162
164
  def test_session_home_then_fr_about
163
165
  get '/home'
164
166
  assert last_response.ok?
@@ -168,9 +170,18 @@ class AppTest < MiniTest::Unit::TestCase
168
170
  assert last_response.ok?
169
171
  assert_match ABOUT_HTML_CONTENT, last_response.body
170
172
  end
173
+ def test_session_about_then_root
174
+ get '/fr/page/about'
175
+ assert last_response.ok?
176
+ assert_match ABOUT_HTML_CONTENT, last_response.body
177
+ get '/'
178
+ assert last_response.ok?
179
+ assert_match HOME_HTML_CONTENT, last_response.body
180
+ end
171
181
 
172
182
  def test_show_stats
173
183
  # We add it here to get some more or less useful statistics
174
- Session.show_stats '/de/page'
175
- end
184
+ ::SBSM::Session.show_stats '/de/page'
185
+ end if RUN_ALL_TESTS
186
+ end
176
187
  end
@@ -0,0 +1,232 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+ $: << File.dirname(__FILE__)
4
+ $:.unshift File.expand_path('../lib', File.dirname(__FILE__))
5
+ require 'minitest/autorun'
6
+ require 'rack/test'
7
+ require 'sbsm/app'
8
+ require 'sbsm/session'
9
+ require 'nokogiri'
10
+ require 'sbsm/state'
11
+ require 'simple_sbsm'
12
+
13
+ ENV['RACK_ENV'] = 'test'
14
+ ENV['REQUEST_METHOD'] = 'GET'
15
+ RUN_ALL_TESTS = true
16
+
17
+ # Overriding some stuff from the simple_sbsm
18
+ module Demo
19
+ class AboutState < GlobalState
20
+ DIRECT_EVENT = :about
21
+ def initialize(session, user)
22
+ SBSM.info "AboutState #{session}"
23
+ super(session, user)
24
+ session.login
25
+ end
26
+ def http_headers
27
+ { "foo" => "bar" }
28
+ end
29
+ end
30
+
31
+
32
+ DEMO_PERSISTENT_COOKIE_NAME = 'demo-simple-sbsm'
33
+ class Demo::CustomizedSBSM_COOKIE < SBSM::App
34
+ PERSISTENT_COOKIE_NAME = DEMO_PERSISTENT_COOKIE_NAME
35
+ end
36
+
37
+ class CustomLookandfeel < SBSM::Lookandfeel
38
+ LANGUAGES = ['fr']
39
+
40
+ DICTIONARIES = {
41
+ 'fr' => {
42
+ hello: 'Bonjour'
43
+ }
44
+ }
45
+ end
46
+
47
+ class CustomizedValidator < SBSM::Validator
48
+ def initialize
49
+ SBSM.debug "CustomizedValidator init"
50
+ @@visited_init = true
51
+ super
52
+ end
53
+
54
+ def self.get_visited_init
55
+ defined?(@@visited_init) ? @@visited_init : :not_initialized
56
+ end
57
+ end
58
+
59
+ class CustomizedSession < SBSM::Session
60
+ DEFAULT_STATE = AboutState
61
+ SERVER_NAME = 'custom_server'
62
+ DEFAULT_ZONE = :custome_zone
63
+ DEFAULT_LANGUAGE = 'fr'
64
+ PERSISTENT_COOKIE_NAME = 'CustomizedSession-cookie'
65
+ LOOKANDFEEL = CustomLookandfeel
66
+
67
+ def initialize(args)
68
+ SBSM.debug "session args #{args}"
69
+ @@active_stated_visited = false
70
+ @@login_visited = false
71
+ @@visited_init = true
72
+ super(args)
73
+ end
74
+
75
+ def flavor
76
+ SBSM.info 'test-flavor'
77
+ 'test-flavor'
78
+ end
79
+
80
+ def active_state
81
+ SBSM.info '@active_stated_visited'
82
+ @@active_stated_visited = true
83
+ @active_state = super
84
+ end
85
+
86
+ def flavor
87
+ SBSM.info 'test-flavor'
88
+ 'test-flavor'
89
+ end
90
+
91
+ def login
92
+ @@login_visited = true
93
+ SBSM.info '@login_visited'
94
+ 'user'
95
+ end
96
+
97
+ def self.get_visited_init
98
+ defined?(@@visited_init) ? @@visited_init : :not_initialized
99
+ end
100
+
101
+ def self.get_visited_active_state
102
+ defined?(@@active_stated_visited) ? @@active_stated_visited : :not_initialized
103
+ end
104
+
105
+ def self.get_visited_login
106
+ defined?(@@login_visited) ? @@login_visited : :not_initialized
107
+ end
108
+ end
109
+
110
+ class CustomizedSBSM < SBSM::App
111
+ SESSION = CustomizedSession
112
+ def initialize(validator: CustomizedValidator.new,
113
+ trans_handler: SBSM::TransHandler.instance,
114
+ cookie_name: nil,
115
+ session_class: SESSION)
116
+ SBSM.info "CustomizedSBSM.new SESSION #{SESSION}"
117
+
118
+ super(validator: validator,
119
+ trans_handler: trans_handler,
120
+ cookie_name: cookie_name,
121
+ session_class: session_class)
122
+ end
123
+ end
124
+ end
125
+
126
+ class CustomizedAppInvalidValidator < Minitest::Test
127
+ include Rack::Test::Methods
128
+ attr_reader :app
129
+
130
+ def test_raise_exeption_if_class
131
+ @app = Demo::CustomizedSBSM.new(validator: ::SBSM::Validator)
132
+ assert_raises { get '/' do end }
133
+ end
134
+
135
+ def test_valid_if_validator
136
+ @app = Demo::CustomizedSBSM.new(validator: ::SBSM::Validator.new)
137
+ get '/' do end
138
+ end
139
+ end if RUN_ALL_TESTS
140
+
141
+ class CustomizedAppInvalidTranshandler < Minitest::Test
142
+ include Rack::Test::Methods
143
+ attr_reader :app
144
+
145
+ def test_raise_exeption_if_class
146
+ @app = Demo::CustomizedSBSM.new(trans_handler: ::SBSM::TransHandler)
147
+ assert_raises { get '/' do end }
148
+ end
149
+
150
+ def test_valid_if_validator_instance
151
+ @app = Demo::CustomizedSBSM.new(trans_handler: ::SBSM::TransHandler.instance)
152
+ get '/' do end
153
+ end
154
+ end if RUN_ALL_TESTS
155
+
156
+ class CustomizedAppSessionValidatorLnf < Minitest::Test
157
+ include Rack::Test::Methods
158
+ attr_reader :app
159
+ def setup
160
+ @app = Demo::CustomizedSBSM.new
161
+ end
162
+
163
+ def test_customized_active_state
164
+ get '/' do
165
+ end
166
+ assert last_response.ok?
167
+ assert_equal true, Demo::CustomizedSession.get_visited_active_state, 'Should have visited customized active_state'
168
+ end if RUN_ALL_TESTS
169
+ def test_customized_session_init
170
+ get '/' do
171
+ end
172
+ assert last_response.ok?
173
+ assert_equal true, Demo::CustomizedSession.get_visited_init, 'Should have visited customized session init'
174
+ end if RUN_ALL_TESTS
175
+
176
+ def test_customized_validator
177
+ # @app = Demo::CustomizedSBSM.new(validator: Demo::CustomizedValidator.new)
178
+ get '/' do
179
+ end
180
+ assert last_response.ok?
181
+ assert_equal true, Demo::CustomizedValidator.get_visited_init, 'Should have initialized customized validator'
182
+ end if RUN_ALL_TESTS
183
+
184
+ def test_customized_login
185
+ get '/fr/page/about' do # we patched to force a login
186
+ end
187
+ assert last_response.ok?
188
+ assert_equal true, Demo::CustomizedSession.get_visited_login, 'Should have visited customized login'
189
+ end
190
+
191
+ def test_customized_http_header
192
+ get '/fr/page/about' do # we patched to force a login
193
+ end
194
+ assert last_response.ok?
195
+ assert_equal 'bar', last_response.headers['foo']
196
+ end
197
+
198
+ def test_process_state
199
+ get '/fr/page/home' do # we patched to force a login
200
+ end
201
+ assert_equal Demo::AboutState, @app.last_session.active_state.class
202
+ assert_equal(1, @app.last_session.attended_states.size)
203
+ get '/fr/page/feedback' do # we patched to force a login
204
+ end
205
+ assert_equal(1, @app.last_session.attended_states.size)
206
+ assert_equal Demo::FeedbackState, @app.last_session.active_state.class
207
+ end
208
+
209
+ def test_customized_cookie_name
210
+ @app = Demo::CustomizedSBSM.new(cookie_name: TEST_COOKIE_NAME)
211
+ get '/' do
212
+ end
213
+ assert last_response.ok?
214
+ # TEST_COOKIE_NAME set via param to app
215
+ assert_equal TEST_COOKIE_NAME, last_response.get_header('Set-Cookie').split("\n").first.split('=').first
216
+ end if RUN_ALL_TESTS
217
+ end
218
+
219
+ class CustomizedAppCookieName < Minitest::Test
220
+ include Rack::Test::Methods
221
+ attr_reader :app
222
+ def setup
223
+ @app = SBSM::App.new(cookie_name: Demo::DEMO_PERSISTENT_COOKIE_NAME)
224
+ end
225
+ def test_customized_cookie_name
226
+ get '/' do
227
+ end
228
+ assert last_response.ok?
229
+ assert_equal Demo::DEMO_PERSISTENT_COOKIE_NAME, last_response.get_header('Set-Cookie').split("\n").first.split('=').first
230
+ end if RUN_ALL_TESTS
231
+ end
232
+