wovnrb 0.2.26 → 0.2.27

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 31142a2c019c93131fd423b8019e803dc7729193
4
- data.tar.gz: 1753cc34aaedf94b5d7bb3bc21a70a792cb8e1c8
3
+ metadata.gz: 38efdfd2c6d0513313fda62527545ff41ccd34a9
4
+ data.tar.gz: e41a5805138544e774fd122f476066d73dde80b9
5
5
  SHA512:
6
- metadata.gz: 34165658b41c519a54d8efae36bba7a45b3c242b03d76ac2b82666d97e77ff2006f138b51db3313a7916dd8fa1339ff522645bed7beee7acc253e5d82d45a04e
7
- data.tar.gz: 39145d2508fbbff2281332a63e68b93359f55671f054d9c85af858bb765bba1fdb2e1bdeff57c46f469c22f724bbb89fd30b60df5fd12787a9e68e35d89878a8
6
+ metadata.gz: 48705abcdbb30187115939810538da2d929dd598ab0b0cf9247710cbd0b560ae20b4c0b3e693e9d064de5cf2bd0db2f08cc639e6b9ebe27658811c6980e86cd3
7
+ data.tar.gz: 3893de97cf771a6abaac622f32b35d4c25996e15c1a677dbdeb5a8c6a7d739aba68878f68fadcfc05906404c1780feb3903972b918bec6c74dc22cb3bf15355b
@@ -31,7 +31,7 @@ module Wovnrb
31
31
 
32
32
  @@cache_prefix = 'api::cache::'
33
33
  def to_key(url)
34
- "#{@@cache_prefix}#{url}"
34
+ "::" + @store.settings['project_token'] + "::#{@@cache_prefix}#{url}"
35
35
  end
36
36
 
37
37
  def build_api_uri
data/lib/wovnrb/store.rb CHANGED
@@ -48,13 +48,21 @@ module Wovnrb
48
48
  @config_loaded = false
49
49
  end
50
50
 
51
+ # Returns true or false based on whether the token is valid or not
52
+ #
53
+ # @return [Boolean] Returns true if the token is valid, and false if it is not
54
+ def valid_token?(token)
55
+ return !token.nil? && (token.length == 5 || token.length == 6)
56
+ end
57
+
51
58
  # Returns true or false based on whether the settings are valid or not, logs any invalid settings to ../error.log
52
59
  #
53
60
  # @return [Boolean] Returns true if the settings are valid, and false if they are not
54
61
  def valid_settings?
55
62
  valid = true
56
63
  errors = [];
57
- if !settings.has_key?('project_token') || settings['project_token'].length < 5 || settings['project_token'].length > 6
64
+ #if valid_token?(!settings.has_key?('project_token') || settings['project_token'].length < 5 || settings['project_token'].length > 6
65
+ if !valid_token?(settings['project_token'])
58
66
  valid = false
59
67
  errors.push("Project token #{settings['project_token']} is not valid.")
60
68
  end
@@ -1,3 +1,3 @@
1
1
  module Wovnrb
2
- VERSION = "0.2.26"
2
+ VERSION = "0.2.27"
3
3
  end
data/lib/wovnrb.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'rack'
1
2
  require 'wovnrb/store'
2
3
  require 'wovnrb/headers'
3
4
  require 'wovnrb/lang'
@@ -47,6 +48,11 @@ module Wovnrb
47
48
 
48
49
  if res_headers["Content-Type"] =~ /html/
49
50
  unless @store.settings['ignore_globs'].any?{|g| g.match?(headers.pathname)}
51
+ # If the user defines a token for this request, use it instead of the config token
52
+ request = Rack::Request.new(env)
53
+ if @store.valid_token?(request.params['wovn_token'])
54
+ @store.settings['project_token'] = request.params['wovn_token']
55
+ end
50
56
  # ApiData creates request for external server, but cannot use async.
51
57
  # Because some server not allow multi thread. (env['async.callback'] is not supported at all Server).
52
58
  api_data = ApiData.new(headers.redis_url, @store)
@@ -25,7 +25,7 @@ module Wovnrb
25
25
  stub_request(:get, "https://api.wovn.io/v0/values?token=#{token}&url=#{url}").
26
26
  to_return(:body => '{"test_body": "a"}')
27
27
  store = Wovnrb::Store.instance
28
- store.settings('user_token' => token)
28
+ store.settings('project_token' => token)
29
29
  api_data = Wovnrb::ApiData.new(url, store)
30
30
 
31
31
  assert_equal({'test_body' => 'a'}, api_data.get_data)
@@ -37,7 +37,7 @@ module Wovnrb
37
37
  stub = stub_request(:get, "https://api.wovn.io/v0/values?token=#{token}&url=#{url}").
38
38
  to_return(:body => '{"test_body": "a"}')
39
39
  store = Wovnrb::Store.instance
40
- store.settings('user_token' => token)
40
+ store.settings('project_token' => token)
41
41
  api_data = Wovnrb::ApiData.new(url, store)
42
42
 
43
43
  assert_equal({'test_body' => 'a'}, api_data.get_data)
@@ -45,13 +45,34 @@ module Wovnrb
45
45
  assert_requested(stub, :times => 1)
46
46
  end
47
47
 
48
+ def test_get_data_cache_with_different_project_tokens
49
+ token = 'a'
50
+ url = 'url'
51
+ stub_a = stub_request(:get, "https://api.wovn.io/v0/values?token=#{token}&url=#{url}").
52
+ to_return(:body => '{"test_body": "a"}')
53
+ store = Wovnrb::Store.instance
54
+ api_data = Wovnrb::ApiData.new(url, store)
55
+
56
+ store.settings('project_token' => token)
57
+ assert_equal({'test_body' => 'a'}, api_data.get_data)
58
+
59
+ token = 'b'
60
+ stub_b = stub_request(:get, "https://api.wovn.io/v0/values?token=#{token}&url=#{url}").
61
+ to_return(:body => '{"test_body": "a"}')
62
+ store.settings('project_token' => token)
63
+ assert_equal({'test_body' => 'a'}, api_data.get_data)
64
+
65
+ assert_requested(stub_a, :times => 1)
66
+ assert_requested(stub_b, :times => 1)
67
+ end
68
+
48
69
  def test_get_data_fail
49
70
  token = 'a'
50
71
  url = 'url'
51
72
  stub_request(:get, "https://api.wovn.io/v0/values?token=#{token}&url=#{url}").
52
73
  to_return(:status => [500, "Internal Server Error"])
53
74
  store = Wovnrb::Store.instance
54
- store.settings('user_token' => token)
75
+ store.settings('project_token' => token)
55
76
  api_data = Wovnrb::ApiData.new(url, store)
56
77
  log_mock = Wovnrb::LogMock.mock_log
57
78
 
@@ -104,6 +104,42 @@ module Wovnrb
104
104
  assert_equal([], s.settings['ignore_globs'])
105
105
  end
106
106
 
107
+ def test_valid_user_token
108
+ mock = LogMock.mock_log
109
+ store = Wovnrb::Store.instance
110
+
111
+ assert_equal(true, store.valid_token?('12345'))
112
+ end
113
+
114
+ def test_valid_project_token
115
+ mock = LogMock.mock_log
116
+ store = Wovnrb::Store.instance
117
+
118
+ assert_equal(true, store.valid_token?('123456'))
119
+ end
120
+
121
+ def test_invalid_token_nil
122
+ mock = LogMock.mock_log
123
+ store = Wovnrb::Store.instance
124
+ settings = {'not_a_token' => '12345'}
125
+
126
+ assert_equal(false, store.valid_token?(settings['token']))
127
+ end
128
+
129
+ def test_invalid_token_too_short
130
+ mock = LogMock.mock_log
131
+ store = Wovnrb::Store.instance
132
+
133
+ assert_equal(false, store.valid_token?('hi'))
134
+ end
135
+
136
+ def test_invalid_token_too_long
137
+ mock = LogMock.mock_log
138
+ store = Wovnrb::Store.instance
139
+
140
+ assert_equal(false, store.valid_token?('1234567'))
141
+ end
142
+
107
143
  def test_add_custom_lang_aliases_empty
108
144
  s = Wovnrb::Store.instance
109
145
  s.settings({'custom_lang_aliases' => {}})
@@ -3,6 +3,7 @@ require 'wovnrb'
3
3
  require 'wovnrb/headers'
4
4
  require 'minitest/autorun'
5
5
  require 'webmock/minitest'
6
+ require 'rack'
6
7
  require 'pry'
7
8
 
8
9
  class WovnrbTest < Minitest::Test
@@ -43,13 +44,43 @@ class WovnrbTest < Minitest::Test
43
44
  stub = stub_request(:get, "#{settings['api_url']}?token=#{token}&url=#{url}").
44
45
  to_return(:body => '{"test_body": "a"}')
45
46
 
46
- i = Wovnrb::Interceptor.new(RackMock.new, settings)
47
+ i = Wovnrb::Interceptor.new(get_app, settings)
47
48
 
48
49
  i.call(Wovnrb.get_env)
49
50
  i.call(Wovnrb.get_env)
50
51
  assert_requested(stub, :times => 1)
51
52
  end
52
53
 
54
+ def test_request_wovn_token
55
+ settings = Wovnrb.get_settings
56
+ settings['project_token'] = 'token0'
57
+ request_token = 'token1'
58
+ url = 'wovn.io/dashboard'
59
+ stub = stub_request(:get, "#{settings['api_url']}?token=#{request_token}&url=#{url}").
60
+ to_return(:body => '{"test_body": "a"}')
61
+
62
+ i = Wovnrb::Interceptor.new(get_app(:params => {'wovn_token' => request_token}), settings)
63
+
64
+ env = Wovnrb.get_env
65
+ i.call(env)
66
+ assert_requested(stub, :times => 1)
67
+ end
68
+
69
+ def test_request_invalid_wovn_token
70
+ settings = Wovnrb.get_settings
71
+ settings['project_token'] = 'token0'
72
+ request_token = 'invalidtoken1'
73
+ url = 'wovn.io/dashboard'
74
+ stub = stub_request(:get, "#{settings['api_url']}?token=#{settings['project_token']}&url=#{url}").
75
+ to_return(:body => '{"test_body": "a"}')
76
+
77
+ i = Wovnrb::Interceptor.new(get_app(:params => {'wovn_token' => settings['project_token']}), settings)
78
+
79
+ env = Wovnrb.get_env
80
+ i.call(env)
81
+ assert_requested(stub, :times => 1)
82
+ end
83
+
53
84
  def test_switch_lang
54
85
  i = Wovnrb::Interceptor.new(get_app)
55
86
  h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
@@ -274,8 +305,8 @@ HTML
274
305
  assert_equal([expected_body], swapped_body)
275
306
  end
276
307
 
277
- def get_app
278
- RackMock.new
308
+ def get_app(opts={})
309
+ RackMock.new(opts)
279
310
  end
280
311
 
281
312
  def generate_values
@@ -286,8 +317,29 @@ HTML
286
317
  end
287
318
 
288
319
  class RackMock
320
+ def initialize(opts={})
321
+ @params = {}
322
+ if opts.has_key?(:params) && opts[:params].class == Hash
323
+ opts[:params].each do |key, val|
324
+ @params[key] = val
325
+ end
326
+ end
327
+ @request_headers = {}
328
+ end
329
+
289
330
  def call(env)
331
+ @env = env
332
+ if @params.length > 0
333
+ req = Rack::Request.new(@env)
334
+ @params.each do |key, val|
335
+ req.update_param(key, val)
336
+ end
337
+ end
290
338
  [200, {'Content-Type' => 'text/html'}, ['']]
291
339
  end
340
+
341
+ def [](key)
342
+ @env[key]
343
+ end
292
344
  end
293
345
  end
data/test/test_helper.rb CHANGED
@@ -73,12 +73,12 @@ module Wovnrb
73
73
  env['SERVER_NAME'] = 'wovn.io'
74
74
  env['HTTP_COOKIE'] = "olfsk=olfsk021093478426337242; hblid=KB8AAMzxzu2DSxnB4X7BJ26rBGVeF0yJ; optimizelyEndUserId=oeu1426233718869r0.5398541854228824; __zlcmid=UFeZqrVo6Mv3Yl; wovn_selected_lang=en; optimizelySegments=%7B%7D; optimizelyBuckets=%7B%7D; _equalizer_session=eDFwM3M2QUZJZFhoby9JZlArckcvSUJwNFRINXhUeUxtNnltQXZhV0tqdGhZQjJMZ01URnZTK05ydFVWYmM3U0dtMVN0M0Z0UnNDVG8vdUNDTUtPc21jY0FHREgrZ05CUnBTb0hyUlkvYlBWQVhQR3RZdnhjMWsrRW5rOVp1Z3V3bkgyd3NpSlRZQWU1dlZvNmM1THp6aUZVeE83Y1pWWENRNTBUVFIrV05WeTdDMlFlem1tUzdxaEtndFZBd2dtUjU2ak5EUmJPa3RWWmMyT1pSVWdMTm8zOVZhUWhHdGQ3L1c5bm91RmNSdFRrcC90Tml4N2t3ZWlBaDRya2lLT1I0S0J2TURhUWl6Uk5rOTQ4Y1MwM3VKYnlLMUYraEt5clhRdFd1eGdEWXdZd3pFbWQvdE9vQndhdDVQbXNLcHBURm9CbnZKenU2YnNXRFdqRVl0MVV3bmRyYjhvMDExcGtUVU9tK1lqUGswM3p6M05tbVRnTjE3TUl5cEdpTTZ4a2gray8xK0FvTC9wUDVka1JSeE5GM1prZmRjWDdyVzRhWW5uS2Mxc1BxOEVVTTZFS3N5bTlVN2p5eE5YSjNZWGI2UHd3Vzc0bDM5QjIwL0l5Mm85NmQyWFAwdVQ3ZzJYYk1QOHY2NVJpY2c9LS1KNU96eHVycVJxSDJMbEc4Rm9KVXpBPT0%3D--17e47555d692fb9cde20ef78a09a5eabbf805bb3; mp_a0452663eb7abb7dfa9c94007ebb0090_mixpanel=%7B%22distinct_id%22%3A%20%2253ed9ffa4a65662e37000000%22%2C%22%24initial_referrer%22%3A%20%22http%3A%2F%2Fp.dev-wovn.io%3A8080%2Fhttp%3A%2F%2Fdev-wovn.io%3A3000%22%2C%22%24initial_referring_domain%22%3A%20%22p.dev-wovn.io%3A8080%22%2C%22__mps%22%3A%20%7B%7D%2C%22__mpso%22%3A%20%7B%7D%2C%22__mpa%22%3A%20%7B%7D%2C%22__mpu%22%3A%20%7B%7D%2C%22__mpap%22%3A%20%5B%5D%7D"
75
75
  env['HTTP_ACCEPT_LANGUAGE'] = 'ja,en-US;q=0.8,en;q=0.6'
76
- env['QUERY_STRING'] = 'param=val&hey=you'
77
76
  env['ORIGINAL_FULLPATH'] = '/dashboard?param=val&hey=you'
78
77
  #env['HTTP_REFERER'] =
79
78
  env['REQUEST_PATH'] = '/dashboard'
80
79
  env['PATH_INFO'] = '/dashboard'
81
80
 
81
+ url = options['url'] || "http://wovn.io/dashboard?param=val&hey=you"
82
82
  if options['url']
83
83
  url = URI.parse(options['url'])
84
84
  env['rack.url_scheme'] = url.scheme
@@ -93,7 +93,8 @@ module Wovnrb
93
93
  env['REQUEST_PATH'] = url.path
94
94
  env['PATH_INFO'] = url.path
95
95
  end
96
- return env.merge(options)
96
+
97
+ return Rack::MockRequest.env_for(url, env.merge(options))
97
98
  end
98
99
 
99
100
  def to_dom(html)
data/wovnrb.gemspec CHANGED
@@ -30,6 +30,7 @@ Gem::Specification.new do |spec|
30
30
  spec.add_dependency "activesupport"
31
31
  spec.add_dependency "lz4-ruby"
32
32
 
33
+ spec.add_dependency "rack", "~>1.5.2"
33
34
  spec.add_development_dependency "bundler", "~> 1.7"
34
35
  spec.add_development_dependency "rake", "~> 10.0"
35
36
  spec.add_development_dependency "listen", "~> 3.0.6"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wovnrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.26
4
+ version: 0.2.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Sandford
@@ -67,6 +67,20 @@ dependencies:
67
67
  - - ">="
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rack
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: 1.5.2
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: 1.5.2
70
84
  - !ruby/object:Gem::Dependency
71
85
  name: bundler
72
86
  requirement: !ruby/object:Gem::Requirement