Skip to content

Commit

Permalink
Fix mocked service (#3)
Browse files Browse the repository at this point in the history
* Yield after all required configuration is made on .prepare

* Use #tap to change strategy client options

* Fix strategy instances being collected as options

* Expose the stubbed service url with the #url method

* Ability for mock to either authenticate or not

* Add comment on readme about simulating sign-in and sign-out flows
  • Loading branch information
vovimayhem authored Feb 29, 2020
1 parent 27e877f commit cd1c64b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 18 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,35 @@ before do
end
```

If your'e testing a sign-in flow:

```ruby
scenario 'sign-in' do
# Simulate that the user will sign-in at the SSO site:
Icalia::StubbedSSOService.sign_in_on_authorize
visit root_path # or any path in your app that requires authentication

#...
end
```

If your'e testing a sign-out flow:

```ruby
scenario 'sign-out' do
# Simulate that the user will sign-in at the SSO site:
Icalia::StubbedSSOService.sign_in_on_authorize
visit root_path # or any path in your app that requires authentication

# Simulate that the user won't sign-in at the SSO site:
Icalia::StubbedSSOService.do_not_sign_in_on_authorize
click_link 'Logout'

# The message coming from Artanis & the Fake Artanis "StubbedSSOService":
expect(page).to have_content 'Signed out successfully.'
end
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
59 changes: 44 additions & 15 deletions lib/icalia/stubbed_sso_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class StubbedSSOService < Sinatra::Base
FIND_AVAILABLE_PORT = 0
CODE = 'icalia_oauth_authorization_code'.freeze

@@sign_in_user_on_authorize = false

post '/oauth/token' do
if params[:code] == CODE
response.headers['Content-Type'] = 'application/json'
Expand All @@ -30,7 +32,18 @@ class StubbedSSOService < Sinatra::Base
end
end

get '/sign-in' do
'Signed out successfully.'
end

get '/sign-out' do
uri = URI(params[:redirect_uri])
redirect uri
end

get '/oauth/authorize' do
return redirect '/sign-in' unless @@sign_in_user_on_authorize

store_oauth_flow_data params
uri = URI(params[:redirect_uri])
uri.query = URI.encode_www_form(state: params[:state], code: CODE)
Expand Down Expand Up @@ -116,37 +129,53 @@ def example_resource_owner_full_name
def store_oauth_flow_data(data)
oauth_flows << data
end

def url
"http://localhost:#{server_port}"
end

def sign_in_url
"#{url}/sign-in"
end

# Taken from FakeStripe.stub_stripe at fake_stripe gem:
def prepare
reset

yield self if block_given?

# Since the OAuth flow is performed by the browser, we'll need to boot
# the Sinatra app instead of just stubbing the app with WebMock...
boot_once

oauth_host = "http://localhost:#{server_port}"

OmniAuth::Strategies::Icalia.instances.each do |options|
client_options = options.client_options
client_options.site = oauth_host
client_options.token_url = "#{oauth_host}/oauth/token"
client_options.authorize_url = "#{oauth_host}/oauth/authorize"
OmniAuth::Strategies::Icalia.instances.each do |strategy|
strategy.options.client_options.tap do |options|
options.site = url
options.token_url = "#{oauth_host}/oauth/token"
options.authorize_url = "#{oauth_host}/oauth/authorize"
end
end
end

def sign_in_on_authorize
@@sign_in_user_on_authorize = true
end

def do_not_sign_in_on_authorize
@@sign_in_user_on_authorize = false
end

def teardown
default_client_options = OmniAuth::Strategies::Icalia
.default_options
.client_options

OmniAuth::Strategies::Icalia.instances.each do |options|
client_options = options.client_options
client_options.site = default_client_options.site
client_options.token_url = default_client_options.token_url
client_options.authorize_url = default_client_options.authorize_url

OmniAuth::Strategies::Icalia.instances.each do |strategy|
strategy.options.client_options.tap do |options|
options.site = default_client_options.site
options.token_url = default_client_options.token_url
options.authorize_url = default_client_options.authorize_url
end
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/omniauth/strategies/icalia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def self.instances
end

def initialize(*args)
instance = super(*args)
@@instances << instance
instance
ret = super
@@instances << self
ret
end

def request_phase
Expand Down

0 comments on commit cd1c64b

Please sign in to comment.