Skip to content

Commit cd1c64b

Browse files
authored
Fix mocked service (#3)
* 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
1 parent 27e877f commit cd1c64b

File tree

3 files changed

+76
-18
lines changed

3 files changed

+76
-18
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,35 @@ before do
5151
end
5252
```
5353

54+
If your'e testing a sign-in flow:
55+
56+
```ruby
57+
scenario 'sign-in' do
58+
# Simulate that the user will sign-in at the SSO site:
59+
Icalia::StubbedSSOService.sign_in_on_authorize
60+
visit root_path # or any path in your app that requires authentication
61+
62+
#...
63+
end
64+
```
65+
66+
If your'e testing a sign-out flow:
67+
68+
```ruby
69+
scenario 'sign-out' do
70+
# Simulate that the user will sign-in at the SSO site:
71+
Icalia::StubbedSSOService.sign_in_on_authorize
72+
visit root_path # or any path in your app that requires authentication
73+
74+
# Simulate that the user won't sign-in at the SSO site:
75+
Icalia::StubbedSSOService.do_not_sign_in_on_authorize
76+
click_link 'Logout'
77+
78+
# The message coming from Artanis & the Fake Artanis "StubbedSSOService":
79+
expect(page).to have_content 'Signed out successfully.'
80+
end
81+
```
82+
5483
## Development
5584

5685
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.

lib/icalia/stubbed_sso_service.rb

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class StubbedSSOService < Sinatra::Base
1010
FIND_AVAILABLE_PORT = 0
1111
CODE = 'icalia_oauth_authorization_code'.freeze
1212

13+
@@sign_in_user_on_authorize = false
14+
1315
post '/oauth/token' do
1416
if params[:code] == CODE
1517
response.headers['Content-Type'] = 'application/json'
@@ -30,7 +32,18 @@ class StubbedSSOService < Sinatra::Base
3032
end
3133
end
3234

35+
get '/sign-in' do
36+
'Signed out successfully.'
37+
end
38+
39+
get '/sign-out' do
40+
uri = URI(params[:redirect_uri])
41+
redirect uri
42+
end
43+
3344
get '/oauth/authorize' do
45+
return redirect '/sign-in' unless @@sign_in_user_on_authorize
46+
3447
store_oauth_flow_data params
3548
uri = URI(params[:redirect_uri])
3649
uri.query = URI.encode_www_form(state: params[:state], code: CODE)
@@ -116,37 +129,53 @@ def example_resource_owner_full_name
116129
def store_oauth_flow_data(data)
117130
oauth_flows << data
118131
end
132+
133+
def url
134+
"http://localhost:#{server_port}"
135+
end
136+
137+
def sign_in_url
138+
"#{url}/sign-in"
139+
end
119140

120141
# Taken from FakeStripe.stub_stripe at fake_stripe gem:
121142
def prepare
122143
reset
123-
144+
124145
yield self if block_given?
125-
146+
126147
# Since the OAuth flow is performed by the browser, we'll need to boot
127148
# the Sinatra app instead of just stubbing the app with WebMock...
128149
boot_once
129-
130-
oauth_host = "http://localhost:#{server_port}"
131150

132-
OmniAuth::Strategies::Icalia.instances.each do |options|
133-
client_options = options.client_options
134-
client_options.site = oauth_host
135-
client_options.token_url = "#{oauth_host}/oauth/token"
136-
client_options.authorize_url = "#{oauth_host}/oauth/authorize"
151+
OmniAuth::Strategies::Icalia.instances.each do |strategy|
152+
strategy.options.client_options.tap do |options|
153+
options.site = url
154+
options.token_url = "#{oauth_host}/oauth/token"
155+
options.authorize_url = "#{oauth_host}/oauth/authorize"
156+
end
137157
end
138158
end
139159

160+
def sign_in_on_authorize
161+
@@sign_in_user_on_authorize = true
162+
end
163+
164+
def do_not_sign_in_on_authorize
165+
@@sign_in_user_on_authorize = false
166+
end
167+
140168
def teardown
141169
default_client_options = OmniAuth::Strategies::Icalia
142170
.default_options
143171
.client_options
144-
145-
OmniAuth::Strategies::Icalia.instances.each do |options|
146-
client_options = options.client_options
147-
client_options.site = default_client_options.site
148-
client_options.token_url = default_client_options.token_url
149-
client_options.authorize_url = default_client_options.authorize_url
172+
173+
OmniAuth::Strategies::Icalia.instances.each do |strategy|
174+
strategy.options.client_options.tap do |options|
175+
options.site = default_client_options.site
176+
options.token_url = default_client_options.token_url
177+
options.authorize_url = default_client_options.authorize_url
178+
end
150179
end
151180
end
152181

lib/omniauth/strategies/icalia.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ def self.instances
1919
end
2020

2121
def initialize(*args)
22-
instance = super(*args)
23-
@@instances << instance
24-
instance
22+
ret = super
23+
@@instances << self
24+
ret
2525
end
2626

2727
def request_phase

0 commit comments

Comments
 (0)