Rack::AliceInExternal
Rack::AliceInExternal is a Rack middleware which provides rough and dirty stubbing for authentication via Sorcery external (GitHub OAuth only for the moment).
Assumes that you have built the OauthsController
following to https://github.com/NoamB/sorcery/wiki/External :
class OauthsController < ApplicationController
...
def oauth
...
end
def callback
...
end
...
end
Rack::AliceInExternal hooks requests for /oauth/[provider]
and returns the stubbed response which redirects you to /oauth/callback?provider=[provider]
.
Then callback
action tries to acquire the access token for authentication from the provider, Rack::AliceInExternal stubs those requests via WebMock and returns the response of the access token and the user data that can be appropriately handled by Sorcery.
This helps you to write feature tests in more user-story-friendly ways, like:
visit root_url
click_link 'Login with GitHub'
...
Installation
Add this line to your application's Gemfile:
gem 'rack-alice_in_external'
Usage
Rack::AliceInExternal stubs the external requests and returns the user information as a response which includes uid: 42
. So if the user with an association to the uid: 42
record in authentications
table exists, Sorcery makes that user login, otherwise a new user is created (and you cannot change uid
other than 42
).
Fixture examples:
users.yml
alice:
name: Alice
authentications.yml
alice_auth:
uid: 42
provider: github
user: alice
Add the middleware you want to use for stubbing in config/environments/test.rb
(add it in development.rb
as well if necessary):
Rails.application.configure do
...
# Stubbing OAuth authentication via Sorcery's GitHub external
config.middleware.use Rack::AliceInExternal::GithubMock
...
end
Then just write feature tests like the below, it tries to login as the user with uid: 42
:
visit root_url
click_link 'Login with GitHub' # <= the link to /oauth/[provider]
...
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/5t111111/rack-alice_in_external.