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
58 59 60 61 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 58 def admin_user ensure_columns_loaded @admin_user ||= Panda::Core::User.find_by(email: "[email protected]") || create_admin_user end |
#clear_omniauth_config ⇒ Object
OMNIAUTH HELPERS
72 73 74 75 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 72 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 33 34 35 |
# 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" Panda::Core::User.find_or_create_by!(id: admin_id) do |user| user.email = attributes[:email] || "[email protected]" user.name = attributes[:name] || "Admin User" user.image_url = attributes[:image_url] || default_image_url # Use is_admin for the actual column, but support both for compatibility if user.respond_to?(:is_admin=) user.is_admin = attributes.fetch(:admin, true) elsif user.respond_to?(:admin=) user.admin = attributes.fetch(:admin, true) end # 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=) end end |
#create_regular_user(attributes = {}) ⇒ Object
Create a regular user with fixed ID for consistent fixture references
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 38 def create_regular_user(attributes = {}) ensure_columns_loaded regular_id = "9a8b7c6d-5e4f-3a2b-1c0d-9e8f7a6b5c4d" Panda::Core::User.find_or_create_by!(id: regular_id) do |user| user.email = attributes[:email] || "[email protected]" user.name = attributes[:name] || "Regular User" user.image_url = attributes[:image_url] || default_image_url(dark: true) # Use is_admin for the actual column, but support both for compatibility if user.respond_to?(:is_admin=) user.is_admin = attributes.fetch(:admin, false) elsif user.respond_to?(:admin=) user.admin = attributes.fetch(:admin, false) end # 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=) end end |
#login_as_admin(attributes = {}) ⇒ Object
High-level helper: Login as admin (creates user if needed)
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 162 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)
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 175 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)
152 153 154 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 152 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)
147 148 149 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 147 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)
157 158 159 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 157 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).
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 124 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
189 190 191 192 193 194 195 196 197 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 189 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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 77 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
63 64 65 66 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 63 def regular_user ensure_columns_loaded @regular_user ||= Panda::Core::User.find_by(email: "[email protected]") || create_regular_user end |
#sign_in_as(user) ⇒ Object
For request specs - set session directly (fast, no HTTP requests)
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 100 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 |