macmillan-utils 1.0.40 → 1.0.41

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: 035e98cbc71c2bebdd28ae144cbcd2f67d4b4271
4
- data.tar.gz: 74d77b9604b18e7e0587c5b369197d9cb9273fc0
3
+ metadata.gz: 184123073d6983e967bbbce0a6b3c6fb931ecc75
4
+ data.tar.gz: d8c366efb96ee5dbc76d03fe7aeb61785cf5bbed
5
5
  SHA512:
6
- metadata.gz: c2787195fab5f2524f0d886f488580ea2f0b791d0f01e9f96fe2d4429d793d89be50003e97200c86c2a694c21553aef4b2498698e3d47ce16239fa0e4fb313ec
7
- data.tar.gz: b06b8e063a31ca8044e10fc8e9ab12b15e53011da44b4307dca0388185f93eb12adb8d6ebdae17200fa6569de7f5f642ff520189166d7686ceb4eafbea95bc0b
6
+ metadata.gz: 2aa234ff620d8a062eaf900be3b9d7d16dfba5b5c235fc305a6da172788425395e82d0fb15e28389f13c881fb8ef3deab29935aa56142f902609cbc16ffe9740
7
+ data.tar.gz: 7c291922805eaff74cc650b2591fbb64dc14d4f3395d85596c40a811cab66b80672caece13748fe08c098a26217273aa9f6a5db07fbccd669db114435a848cf1
data/.rubocop.yml CHANGED
@@ -7,6 +7,13 @@ Metrics/BlockLength:
7
7
  - describe
8
8
  - context
9
9
 
10
+ Metrics/MethodLength:
11
+ CountComments: false
12
+ Max: 20
13
+
14
+ Metrics/AbcSize:
15
+ Max: 40
16
+
10
17
  Style/Documentation:
11
18
  Enabled: false
12
19
 
@@ -16,6 +23,15 @@ Style/SpaceBeforeFirstArg:
16
23
  Style/BracesAroundHashParameters:
17
24
  Enabled: false
18
25
 
26
+ Style/CaseEquality:
27
+ Enabled: false
28
+
29
+ Style/GuardClause:
30
+ Enabled: false
31
+
32
+ Style/ConditionalAssignment:
33
+ Enabled: false
34
+
19
35
  Style/IndentHash:
20
36
  EnforcedStyle: consistent
21
37
 
@@ -1,6 +1,7 @@
1
1
  require 'rack/request'
2
2
  require 'rack/response'
3
3
  require 'uri'
4
+ require 'active_support/tagged_logging'
4
5
 
5
6
  module Macmillan
6
7
  module Utils
@@ -9,8 +10,17 @@ module Macmillan
9
10
  YEAR = 31_536_000
10
11
  COOKIE = 'euCookieNotice'.freeze
11
12
 
12
- def initialize(app)
13
+ def initialize(app, options = {})
13
14
  @app = app
15
+ @log_level = options[:log_level]
16
+
17
+ if (logger = options[:logger])
18
+ if logger.respond_to?(:tagged)
19
+ @logger = logger
20
+ else
21
+ @logger = ActiveSupport::TaggedLogging.new(logger)
22
+ end
23
+ end
14
24
  end
15
25
 
16
26
  def call(env)
@@ -26,41 +36,54 @@ module Macmillan
26
36
  private
27
37
 
28
38
  def cookies_accepted?(request)
29
-
30
- debug_log("request.post? IS #{request.post?.inspect}")
31
- debug_log("request.cookies[#{COOKIE}] IS #{request.cookies[COOKIE].inspect}")
32
- debug_log("request.params['cookies'] IS #{request.params['cookies'].inspect}")
33
- debug_log("request.cookies IS #{request.cookies.inspect}")
39
+ debug("request.post? IS #{request.post?.inspect}")
40
+ debug("request.cookies[#{COOKIE}] IS #{request.cookies[COOKIE].inspect}")
41
+ debug("request.params['cookies'] IS #{request.params['cookies'].inspect}")
42
+ debug("request.cookies IS #{request.cookies.inspect}")
34
43
 
35
44
  unless request.post?
36
- debug_log("request.post? (#{request.post?.inspect}) means passthru")
45
+ debug("request.post? (#{request.post?.inspect}) means passthru")
37
46
  return false
38
47
  end
48
+
39
49
  unless request.cookies[COOKIE] != 'accepted'
40
- debug_log("request.cookies['#{COOKIE}'] (#{request.cookies[COOKIE].inspect}) means passthru")
50
+ debug("request.cookies['#{COOKIE}'] (#{request.cookies[COOKIE].inspect}) means passthru")
41
51
  return false
42
52
  end
53
+
43
54
  unless request.params['cookies'] == 'accepted'
44
- debug_log("request.params['cookies'] (#{request.params['cookies'].inspect}) means passthru")
55
+ debug("request.params['cookies'] (#{request.params['cookies'].inspect}) means passthru")
45
56
  return false
46
57
  end
47
- debug_log('About to set the acceptance cookie and redirect')
58
+
59
+ debug('About to set the acceptance cookie and redirect')
48
60
  true
49
61
  end
50
62
 
51
- def debug_log(msg)
52
- logger.info("[Macmillan::Utils::Middleware::CookieMessage] #{msg}\n")
63
+ def debug(msg)
64
+ logger.tagged(self.class.name) { logger.debug(msg) }
53
65
  end
54
66
 
55
67
  def logger
56
- @logger ||= @request.logger || NullLogger.new
68
+ @logger ||= @request.logger || default_logger
69
+ end
70
+
71
+ def default_logger
72
+ logger = ::Logger.new($stdout)
73
+ logger.level = default_log_level
74
+
75
+ ActiveSupport::TaggedLogging.new(logger)
76
+ end
77
+
78
+ def default_log_level
79
+ @log_level || ::Logger::INFO
57
80
  end
58
81
 
59
82
  def redirect_back(request)
60
83
  response = Rack::Response.new
61
84
  location = build_location(request)
62
85
 
63
- debug_log("Redirecting to #{location}")
86
+ debug("Redirecting to #{location}")
64
87
 
65
88
  response.redirect(location)
66
89
  response.set_cookie(COOKIE, cookie_options(request))
@@ -79,31 +102,27 @@ module Macmillan
79
102
 
80
103
  def build_location(request)
81
104
  begin
82
- debug_log("Attempting to determine redirect by parsing referrer #{request.referrer}")
105
+ debug("Attempting to determine redirect by parsing referrer #{request.referrer}")
83
106
  uri = URI.parse(request.referrer.to_s)
84
107
  rescue URI::InvalidURIError
85
- debug_log("No that failed, attempting to determine redirect by parsing request.url #{request.url}")
108
+ debug("No that failed, attempting to determine redirect by parsing request.url #{request.url}")
86
109
  uri = URI.parse(request.url)
87
110
  end
88
111
 
89
112
  # Check that the redirect is an internal one for security reasons:
90
113
  # https://webmasters.googleblog.com/2009/01/open-redirect-urls-is-your-site-being.html
91
- unless internal_redirect?(request, uri)
92
- debug_log("Not internal redirect - so changing to #{request.url} instead of the above")
114
+ if internal_redirect?(request, uri)
115
+ uri.to_s
116
+ else
117
+ debug("Not internal redirect - so changing to #{request.url} instead of the above")
118
+ request.url
93
119
  end
94
- internal_redirect?(request, uri) ? uri.to_s : request.url
95
120
  end
96
121
 
97
122
  def internal_redirect?(request, uri)
98
- debug_log("Is redirect to #{uri.host}:#{uri.port} internal WRT #{request.host}:#{request.port}")
123
+ debug("Is redirect to #{uri.host}:#{uri.port} internal WRT #{request.host}:#{request.port}")
99
124
  request.host == uri.host # && request.port == uri.port
100
125
  end
101
-
102
- class NullLogger
103
- def method_missing(*args)
104
- nil
105
- end
106
- end
107
126
  end
108
127
  end
109
128
  end
@@ -31,4 +31,5 @@ Gem::Specification.new do |spec|
31
31
  spec.add_dependency 'syslog-logger'
32
32
  spec.add_dependency 'rubocop'
33
33
  spec.add_dependency 'colorize'
34
+ spec.add_dependency 'activesupport', '>= 3.2.0'
34
35
  end
@@ -44,7 +44,7 @@ RSpec.describe Macmillan::Utils::Middleware::CookieMessage do
44
44
  it 'sets the cookie' do
45
45
  expect(cookie).to match(/euCookieNotice=accepted;/)
46
46
  expect(cookie).to match(/domain=www\.nature\.com:80;/)
47
- expect(cookie).to match(/path=\/;/)
47
+ expect(cookie).to match(%r{path=/;})
48
48
  expect(cookie).to match(/expires=Wed, 31 Jan 2018 00:00:00 -0000/)
49
49
  end
50
50
 
@@ -93,4 +93,79 @@ RSpec.describe Macmillan::Utils::Middleware::CookieMessage do
93
93
  end
94
94
  end
95
95
  end
96
+
97
+ describe 'logging' do
98
+ let(:url) { 'http://www.nature.com/' }
99
+ let(:request_method) { 'GET' }
100
+ let(:output) { StringIO.new }
101
+
102
+ matcher :have_output do |expected|
103
+ match do
104
+ expected === output(actual)
105
+ end
106
+
107
+ failure_message do |actual|
108
+ "expected that #{output(actual)} would equal #{expected}"
109
+ end
110
+
111
+ def output(io)
112
+ io.rewind && io.read
113
+ end
114
+ end
115
+
116
+ context 'default logging' do
117
+ subject { described_class.new(app) }
118
+
119
+ around do |example|
120
+ begin
121
+ stdout = $stdout
122
+ $stdout = output
123
+
124
+ example.run
125
+ ensure
126
+ $stdout = stdout
127
+ end
128
+ end
129
+
130
+ it 'produces no output' do
131
+ expect(app).to receive(:call).with(env).and_call_original
132
+ expect(response).to eq([200, {}, %w[body]])
133
+ expect(output).to have_output('')
134
+ end
135
+ end
136
+
137
+ context 'custom log level' do
138
+ subject { described_class.new(app, log_level: ::Logger::DEBUG) }
139
+
140
+ around do |example|
141
+ begin
142
+ stdout = $stdout
143
+ $stdout = output
144
+
145
+ example.run
146
+ ensure
147
+ $stdout = stdout
148
+ end
149
+ end
150
+
151
+ it 'produces tagged output' do
152
+ expect(app).to receive(:call).with(env).and_call_original
153
+ expect(response).to eq([200, {}, %w[body]])
154
+ expect(output).to have_output(/\[Macmillan::Utils::Middleware::CookieMessage\]/)
155
+ expect(output).to have_output(/request.post\? \(false\) means passthru/)
156
+ end
157
+ end
158
+
159
+ context 'custom logger' do
160
+ let(:logger) { ::Logger.new(output) }
161
+ subject { described_class.new(app, logger: logger) }
162
+
163
+ it 'produces tagged output' do
164
+ expect(app).to receive(:call).with(env).and_call_original
165
+ expect(response).to eq([200, {}, %w[body]])
166
+ expect(output).to have_output(/\[Macmillan::Utils::Middleware::CookieMessage\]/)
167
+ expect(output).to have_output(/request.post\? \(false\) means passthru/)
168
+ end
169
+ end
170
+ end
96
171
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: macmillan-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.40
4
+ version: 1.0.41
5
5
  platform: ruby
6
6
  authors:
7
7
  - Springer Nature
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-22 00:00:00.000000000 Z
11
+ date: 2017-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -220,6 +220,20 @@ dependencies:
220
220
  - - ">="
221
221
  - !ruby/object:Gem::Version
222
222
  version: '0'
223
+ - !ruby/object:Gem::Dependency
224
+ name: activesupport
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - ">="
228
+ - !ruby/object:Gem::Version
229
+ version: 3.2.0
230
+ type: :runtime
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - ">="
235
+ - !ruby/object:Gem::Version
236
+ version: 3.2.0
223
237
  description:
224
238
  email:
225
239
  - npp-developers@macmillan.com