Skip to content

Commit bf3fcf0

Browse files
committed
Configure OmniAuth for test mode and enhance OAuth test helpers
1 parent 8973482 commit bf3fcf0

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

spec/rails_helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@
4646
config.include Warden::Test::Helpers
4747
config.after { Warden.test_reset! }
4848

49+
# Configure OmniAuth for test mode
50+
config.before(:suite) do
51+
OmniAuth.config.test_mode = true
52+
end
53+
54+
config.after(:each) do
55+
OmniAuth.config.mock_auth[:github] = nil
56+
end
57+
4958
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
5059
config.fixture_paths = [Rails.root.join('spec/fixtures')]
5160

spec/support/oauth_test_helpers.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,76 @@ def mock_minimal_oauth_auth_hash(provider, options = {})
151151
}
152152
})
153153
end
154+
155+
# Ensure external platforms exist for OAuth tests
156+
def ensure_external_platforms_exist
157+
# Create external platforms needed for OAuth testing
158+
oauth_platforms = [
159+
{ identifier: 'github', name: 'GitHub' },
160+
{ identifier: 'google', name: 'Google' },
161+
{ identifier: 'facebook', name: 'Facebook' },
162+
{ identifier: 'linkedin', name: 'LinkedIn' }
163+
]
164+
165+
oauth_platforms.each do |platform_attrs|
166+
next if BetterTogether::Platform.external.find_by(identifier: platform_attrs[:identifier])
167+
168+
BetterTogether::Platform.create!(
169+
platform_attrs.merge(
170+
external: true,
171+
url: "https://#{platform_attrs[:identifier]}.com",
172+
privacy: 'public',
173+
time_zone: 'UTC'
174+
)
175+
)
176+
end
177+
end
178+
179+
# Setup complete OAuth test environment
180+
def setup_oauth_test_environment(provider, auth_hash = nil)
181+
# Ensure external platforms exist
182+
ensure_external_platforms_exist
183+
184+
# Setup OmniAuth test mode
185+
setup_omniauth_test_mode(provider, auth_hash)
186+
end
187+
188+
# Simplified mock for controller tests - matches RailsApps pattern
189+
def simple_oauth_mock(provider, options = {})
190+
defaults = {
191+
'provider' => provider.to_s,
192+
'uid' => options[:uid] || '123456',
193+
'info' => {
194+
'email' => options[:email] || "#{provider}[email protected]",
195+
'name' => options[:name] || "#{provider.to_s.capitalize} User",
196+
'nickname' => options[:nickname] || "#{provider}user",
197+
'image' => options[:image] || "https://#{provider}.com/avatar.jpg"
198+
},
199+
'credentials' => {
200+
'token' => options[:token] || 'mock_token_123',
201+
'secret' => options[:secret] || 'mock_secret_456',
202+
'expires_at' => options[:expires_at] || 1.hour.from_now.to_i
203+
}
204+
}
205+
206+
# Provider-specific adjustments
207+
case provider.to_sym
208+
when :github
209+
defaults['info']['urls'] = { 'GitHub' => 'https://github.com/testuser' }
210+
defaults.delete('secret') # GitHub doesn't use secret
211+
when :google, :google_oauth2
212+
defaults['extra'] = {
213+
'raw_info' => {
214+
'sub' => defaults['uid'],
215+
'email' => defaults['info']['email'],
216+
'email_verified' => true
217+
}
218+
}
219+
defaults.delete('secret') # Google doesn't use secret
220+
end
221+
222+
defaults.deep_merge(options[:extra_data] || {})
223+
end
154224
end
155225

156226
# Include the helper methods in RSpec

0 commit comments

Comments
 (0)