Skip to content

Commit

Permalink
Introduce before_request configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainEndelin committed Aug 30, 2023
1 parent af559f6 commit cd34512
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [1.16.0]
[Compare]: https://github.com/shakacode/cypress-on-rails/compare/v1.15.1...v1.16.0

### Added
* Add support for `before_request` options on the middleware, for authentication [PR 138](https://github.com/shakacode/cypress-on-rails/pull/138) by [RomainEndelin]

## [1.15.1]
[Compare]: https://github.com/shakacode/cypress-on-rails/compare/v1.15.0...v1.15.1

Expand Down
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,47 @@ describe('My First Test', () => {
})
```

## `before_request` configuration

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.

You should get familiar with [Rack middlewares](https://www.rubyguides.com/2018/09/rack-middleware/).
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`.

### Authenticate CypressOnRails

```ruby
CypressOnRails.configure do |c|
# ...

# Refer to https://www.rubydoc.info/gems/rack/Rack/Request for the `request` argument.
c.before_request = lambda { |request|
body = JSON.parse(request.body.string)
if body['cypress_token'] != ENV.fetch('SWEEP_CYPRESS_SECRET_TOKEN')
# You may also use warden for authentication:
# if !request.env['warden'].authenticate(:secret_key)
return [401, {}, ['unauthorized']]
end

}
end
```

### Send usage metrics

```ruby
CypressOnRails.configure do |c|
# ...

# Refer to https://www.rubydoc.info/gems/rack/Rack/Request for the `request` argument.
c.before_request = lambda { |request|
statsd = Datadog::Statsd.new('localhost', 8125)

statsd.increment('cypress_on_rails.requests')
}
end
```

## Usage with other rack applications

Add CypressOnRails to your config.ru
Expand Down
2 changes: 2 additions & 0 deletions lib/cypress_on_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Configuration
attr_accessor :install_folder
attr_accessor :use_middleware
attr_accessor :use_vcr_middleware
attr_accessor :before_request
attr_accessor :logger

# Attributes for backwards compatibility
Expand All @@ -30,6 +31,7 @@ def reset
self.install_folder = 'spec/e2e'
self.use_middleware = true
self.use_vcr_middleware = false
self.before_request = -> (request) {}
self.logger = Logger.new(STDOUT)
end

Expand Down
4 changes: 4 additions & 0 deletions lib/cypress_on_rails/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def file_path
end

def handle_command(req)
maybe_env = configuration.before_request.call(req)
# Halt the middleware if an Rack Env was returned by `before_request`
return maybe_env unless maybe_env.nil?

body = JSON.parse(req.body.read)
logger.info "handle_command: #{body}"
commands = Command.from_body(body, configuration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ if defined?(CypressOnRails)
c.use_middleware = !Rails.env.production?
<% unless options.experimental %># <% end %> c.use_vcr_middleware = !Rails.env.production?
c.logger = Rails.logger

# If you want to enable a before_request logic, such as authentication, logging, sending metrics, etc.
# Refer to https://www.rubydoc.info/gems/rack/Rack/Request for the `request` argument.
# Return nil to continue through the Cypress command. Return a response [status, header, body] to halt.
# c.before_request = lambda { |request|
# unless request.env['warden'].authenticate(:secret_key)
# return [403, {}, ["forbidden"]]
# end
# }
end

# # if you compile your asssets on CI
Expand Down
4 changes: 4 additions & 0 deletions spec/cypress_on_rails/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
expect(CypressOnRails.configuration.install_folder).to eq('spec/e2e')
expect(CypressOnRails.configuration.use_middleware?).to eq(true)
expect(CypressOnRails.configuration.logger).to_not be_nil
expect(CypressOnRails.configuration.before_request).to_not be_nil
end

it 'can be configured' do
my_logger = Logger.new(STDOUT)
before_request_lambda = -> (_) { return [200, {}, ['hello world']] }
CypressOnRails.configure do |config|
config.api_prefix = '/api'
config.install_folder = 'my/path'
config.use_middleware = false
config.logger = my_logger
config.before_request = before_request_lambda
end
expect(CypressOnRails.configuration.api_prefix).to eq('/api')
expect(CypressOnRails.configuration.install_folder).to eq('my/path')
expect(CypressOnRails.configuration.use_middleware?).to eq(false)
expect(CypressOnRails.configuration.logger).to eq(my_logger)
expect(CypressOnRails.configuration.before_request).to eq(before_request_lambda)
end
end

0 comments on commit cd34512

Please sign in to comment.