Skip to content

Commit 1d27b96

Browse files
Introduce before_request configuration (shakacode#138)
1 parent af559f6 commit 1d27b96

File tree

6 files changed

+63
-0
lines changed

6 files changed

+63
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### Added
2+
* Add support for `before_request` options on the middleware, for authentication [PR 138](https://github.com/shakacode/cypress-on-rails/pull/138) by [RomainEndelin]
3+
14
## [1.15.1]
25
[Compare]: https://github.com/shakacode/cypress-on-rails/compare/v1.15.0...v1.15.1
36

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,47 @@ describe('My First Test', () => {
395395
})
396396
```
397397

398+
## `before_request` configuration
399+
400+
You may perform any custom action before running a CypressOnRails command, such as authentication, or sending metrics. Please set `before_request` as part of the CypressOnRails configuration.
401+
402+
You should get familiar with [Rack middlewares](https://www.rubyguides.com/2018/09/rack-middleware/).
403+
If your function returns a `[status, header, body]` response, CypressOnRails will halt, and your command will not be executed. To execute the command, `before_request` should return `nil`.
404+
405+
### Authenticate CypressOnRails
406+
407+
```ruby
408+
CypressOnRails.configure do |c|
409+
# ...
410+
411+
# Refer to https://www.rubydoc.info/gems/rack/Rack/Request for the `request` argument.
412+
c.before_request = lambda { |request|
413+
body = JSON.parse(request.body.string)
414+
if body['cypress_token'] != ENV.fetch('SWEEP_CYPRESS_SECRET_TOKEN')
415+
# You may also use warden for authentication:
416+
# if !request.env['warden'].authenticate(:secret_key)
417+
return [401, {}, ['unauthorized']]
418+
end
419+
420+
}
421+
end
422+
```
423+
424+
### Send usage metrics
425+
426+
```ruby
427+
CypressOnRails.configure do |c|
428+
# ...
429+
430+
# Refer to https://www.rubydoc.info/gems/rack/Rack/Request for the `request` argument.
431+
c.before_request = lambda { |request|
432+
statsd = Datadog::Statsd.new('localhost', 8125)
433+
434+
statsd.increment('cypress_on_rails.requests')
435+
}
436+
end
437+
```
438+
398439
## Usage with other rack applications
399440

400441
Add CypressOnRails to your config.ru

lib/cypress_on_rails/configuration.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Configuration
66
attr_accessor :install_folder
77
attr_accessor :use_middleware
88
attr_accessor :use_vcr_middleware
9+
attr_accessor :before_request
910
attr_accessor :logger
1011

1112
# Attributes for backwards compatibility
@@ -30,6 +31,7 @@ def reset
3031
self.install_folder = 'spec/e2e'
3132
self.use_middleware = true
3233
self.use_vcr_middleware = false
34+
self.before_request = -> (request) {}
3335
self.logger = Logger.new(STDOUT)
3436
end
3537

lib/cypress_on_rails/middleware.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def file_path
4747
end
4848

4949
def handle_command(req)
50+
maybe_env = configuration.before_request.call(req)
51+
# Halt the middleware if an Rack Env was returned by `before_request`
52+
return maybe_env unless maybe_env.nil?
53+
5054
body = JSON.parse(req.body.read)
5155
logger.info "handle_command: #{body}"
5256
commands = Command.from_body(body, configuration)

lib/generators/cypress_on_rails/templates/config/initializers/cypress_on_rails.rb.erb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ if defined?(CypressOnRails)
77
c.use_middleware = !Rails.env.production?
88
<% unless options.experimental %># <% end %> c.use_vcr_middleware = !Rails.env.production?
99
c.logger = Rails.logger
10+
11+
# If you want to enable a before_request logic, such as authentication, logging, sending metrics, etc.
12+
# Refer to https://www.rubydoc.info/gems/rack/Rack/Request for the `request` argument.
13+
# Return nil to continue through the Cypress command. Return a response [status, header, body] to halt.
14+
# c.before_request = lambda { |request|
15+
# unless request.env['warden'].authenticate(:secret_key)
16+
# return [403, {}, ["forbidden"]]
17+
# end
18+
# }
1019
end
1120

1221
# # if you compile your asssets on CI

spec/cypress_on_rails/configuration_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@
88
expect(CypressOnRails.configuration.install_folder).to eq('spec/e2e')
99
expect(CypressOnRails.configuration.use_middleware?).to eq(true)
1010
expect(CypressOnRails.configuration.logger).to_not be_nil
11+
expect(CypressOnRails.configuration.before_request).to_not be_nil
1112
end
1213

1314
it 'can be configured' do
1415
my_logger = Logger.new(STDOUT)
16+
before_request_lambda = -> (_) { return [200, {}, ['hello world']] }
1517
CypressOnRails.configure do |config|
1618
config.api_prefix = '/api'
1719
config.install_folder = 'my/path'
1820
config.use_middleware = false
1921
config.logger = my_logger
22+
config.before_request = before_request_lambda
2023
end
2124
expect(CypressOnRails.configuration.api_prefix).to eq('/api')
2225
expect(CypressOnRails.configuration.install_folder).to eq('my/path')
2326
expect(CypressOnRails.configuration.use_middleware?).to eq(false)
2427
expect(CypressOnRails.configuration.logger).to eq(my_logger)
28+
expect(CypressOnRails.configuration.before_request).to eq(before_request_lambda)
2529
end
2630
end

0 commit comments

Comments
 (0)