Module: Panda::Core::AuthenticationTestHelpers
- Defined in:
- lib/panda/core/testing/support/authentication_test_helpers.rb
Instance Method Summary collapse
-
#admin_user ⇒ Object
Backwards compatibility with fixture access patterns.
-
#clear_omniauth_config ⇒ Object
OMNIAUTH HELPERS ============================================================================.
-
#create_admin_user(attributes = {}) ⇒ Object
Create an admin user with fixed ID for consistent fixture references.
-
#create_regular_user(attributes = {}) ⇒ Object
Create a regular user with fixed ID for consistent fixture references.
-
#login_as_admin(attributes = {}) ⇒ Object
High-level helper: Login as admin (creates user if needed).
-
#login_as_user(attributes = {}) ⇒ Object
High-level helper: Login as regular user (creates user if needed).
-
#login_with_github(user, expect_success: true) ⇒ Object
Convenience method: Login with GitHub OAuth provider (using test endpoint).
-
#login_with_google(user, expect_success: true) ⇒ Object
Convenience method: Login with Google OAuth provider (using test endpoint).
-
#login_with_microsoft(user, expect_success: true) ⇒ Object
Convenience method: Login with Microsoft OAuth provider (using test endpoint).
-
#login_with_test_endpoint(user, return_to: nil, expect_success: true) ⇒ Object
For system specs - use test session endpoint (works across processes) This uses the TestSessionsController which is only available in test environment.
-
#manual_login_with_oauth(user, provider: :google_oauth2) ⇒ Object
Manual OAuth login (slower, but tests actual OAuth flow) Use this when you need to test the OAuth callback handler itself.
- #mock_oauth_for_user(user, provider: :google_oauth2) ⇒ Object
- #regular_user ⇒ Object
-
#sign_in_as(user) ⇒ Object
For request specs - set session directly (fast, no HTTP requests).
Instance Method Details
#admin_user ⇒ Object
Backwards compatibility with fixture access patterns
52 53 54 55 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 52 def admin_user ensure_columns_loaded @admin_user ||= Panda::Core::User.find_by(email: "admin@example.com") || create_admin_user end |
#clear_omniauth_config ⇒ Object
============================================================================
OMNIAUTH HELPERS
66 67 68 69 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 66 def clear_omniauth_config OmniAuth.config.mock_auth.clear Rails.application.env_config.delete("omniauth.auth") if defined?(Rails.application) end |
#create_admin_user(attributes = {}) ⇒ Object
Create an admin user with fixed ID for consistent fixture references
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 18 def create_admin_user(attributes = {}) ensure_columns_loaded admin_id = "8f481fcb-d9c8-55d7-ba17-5ea5d9ed8b7a" user = Panda::Core::User.find_or_initialize_by(id: admin_id) user.email = attributes[:email] || "admin@example.com" user.name = attributes[:name] || "Admin User" user.image_url = attributes[:image_url] || default_image_url user.admin = attributes.fetch(:admin, true) user.enabled = attributes.fetch(:enabled, true) if user.respond_to?(:enabled=) # Only set OAuth fields if they exist on the model user.uid = attributes[:uid] || "admin_oauth_uid_123" if user.respond_to?(:uid=) user.provider = attributes[:provider] || "google_oauth2" if user.respond_to?(:provider=) user.save! if user.new_record? || user.changed? user end |
#create_regular_user(attributes = {}) ⇒ Object
Create a regular user with fixed ID for consistent fixture references
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 35 def create_regular_user(attributes = {}) ensure_columns_loaded regular_id = "9a8b7c6d-5e4f-3a2b-1c0d-9e8f7a6b5c4d" user = Panda::Core::User.find_or_initialize_by(id: regular_id) user.email = attributes[:email] || "user@example.com" user.name = attributes[:name] || "Regular User" user.image_url = attributes[:image_url] || default_image_url(dark: true) user.admin = attributes.fetch(:admin, false) user.enabled = attributes.fetch(:enabled, true) if user.respond_to?(:enabled=) # Only set OAuth fields if they exist on the model user.uid = attributes[:uid] || "user_oauth_uid_456" if user.respond_to?(:uid=) user.provider = attributes[:provider] || "google_oauth2" if user.respond_to?(:provider=) user.save! if user.new_record? || user.changed? user end |
#login_as_admin(attributes = {}) ⇒ Object
High-level helper: Login as admin (creates user if needed)
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 156 def login_as_admin(attributes = {}) user = create_admin_user(attributes) if respond_to?(:visit) # System spec - use test endpoint login_with_test_endpoint(user, expect_success: true) else # Request spec - use direct session sign_in_as(user) end user end |
#login_as_user(attributes = {}) ⇒ Object
High-level helper: Login as regular user (creates user if needed)
169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 169 def login_as_user(attributes = {}) user = create_regular_user(attributes) if respond_to?(:visit) # System spec - regular users get redirected to login login_with_test_endpoint(user, expect_success: false) else # Request spec - use direct session sign_in_as(user) end user end |
#login_with_github(user, expect_success: true) ⇒ Object
Convenience method: Login with GitHub OAuth provider (using test endpoint)
146 147 148 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 146 def login_with_github(user, expect_success: true) login_with_test_endpoint(user, return_to: determine_default_redirect_path, expect_success: expect_success) end |
#login_with_google(user, expect_success: true) ⇒ Object
Convenience method: Login with Google OAuth provider (using test endpoint)
141 142 143 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 141 def login_with_google(user, expect_success: true) login_with_test_endpoint(user, return_to: determine_default_redirect_path, expect_success: expect_success) end |
#login_with_microsoft(user, expect_success: true) ⇒ Object
Convenience method: Login with Microsoft OAuth provider (using test endpoint)
151 152 153 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 151 def login_with_microsoft(user, expect_success: true) login_with_test_endpoint(user, return_to: determine_default_redirect_path, expect_success: expect_success) end |
#login_with_test_endpoint(user, return_to: nil, expect_success: true) ⇒ Object
For system specs - use test session endpoint (works across processes) This uses the TestSessionsController which is only available in test environment
NOTE: Due to Cuprite's redirect handling, we visit the target path directly after setting up the session via the test endpoint. Flash messages won't be available in system tests due to cross-process timing. Use request specs to test flash messages (see authentication_request_spec.rb).
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 118 def login_with_test_endpoint(user, return_to: nil, expect_success: true) return_path = return_to || determine_default_redirect_path # Visit the test login endpoint (sets session via Redis) # Note: Capybara/Cuprite may not follow the redirect properly, so we # manually navigate to the expected destination visit "/admin/test_login/#{user.id}?return_to=#{return_path}" # Wait briefly for session to be set # sleep 0.2 # Manually visit the destination since Cuprite doesn't reliably follow redirects if expect_success visit return_path # Wait for page to load # sleep 0.2 # Verify we're on the expected path expect(page).to have_current_path(return_path, wait: 2) end end |
#manual_login_with_oauth(user, provider: :google_oauth2) ⇒ Object
Manual OAuth login (slower, but tests actual OAuth flow) Use this when you need to test the OAuth callback handler itself
183 184 185 186 187 188 189 190 191 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 183 def manual_login_with_oauth(user, provider: :google_oauth2) mock_oauth_for_user(user, provider: provider) visit admin_login_path expect(page).to have_css("#button-sign-in-#{provider}") find("#button-sign-in-#{provider}").click Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[provider] end |
#mock_oauth_for_user(user, provider: :google_oauth2) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 71 def mock_oauth_for_user(user, provider: :google_oauth2) clear_omniauth_config OmniAuth.config.test_mode = true OmniAuth.config.mock_auth[provider] = OmniAuth::AuthHash.new({ provider: provider.to_s, uid: (user.respond_to?(:uid) ? user.uid : nil) || user.id, info: { email: user.email, name: user.name, image: user.respond_to?(:image_url) ? user.image_url : nil }, credentials: { token: "mock_token_#{user.id}", expires_at: Time.now + 1.week } }) end |
#regular_user ⇒ Object
57 58 59 60 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 57 def regular_user ensure_columns_loaded @regular_user ||= Panda::Core::User.find_by(email: "user@example.com") || create_regular_user end |
#sign_in_as(user) ⇒ Object
For request specs - set session directly (fast, no HTTP requests)
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 94 def sign_in_as(user) # This works in request specs where we have direct access to the session begin if respond_to?(:session) session[:user_id] = user.id end rescue NoMethodError # Session method doesn't exist in this context (e.g., system specs) end Panda::Core::Current.user = user user end |