Module: Panda::Core::AuthenticationTestHelpers

Defined in:
lib/panda/core/testing/support/authentication_test_helpers.rb

Instance Method Summary collapse

Instance Method Details

#admin_userObject

Backwards compatibility with fixture access patterns



48
49
50
51
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 48

def admin_user
  ensure_columns_loaded
  @admin_user ||= Panda::Core::User.find_by(email: "admin@example.com") || create_admin_user
end

#clear_omniauth_configObject

============================================================================

OMNIAUTH HELPERS



62
63
64
65
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 62

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
# 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] || "admin@example.com"
    user.name = attributes[:name] || "Admin User"
    user.image_url = attributes[:image_url] || default_image_url
    user.admin = attributes[:admin] || true
    # 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



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 33

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] || "user@example.com"
    user.name = attributes[:name] || "Regular User"
    user.image_url = attributes[:image_url] || default_image_url(dark: true)
    user.admin = attributes[:admin] || false
    # 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)



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 152

def (attributes = {})
  user = create_admin_user(attributes)
  if respond_to?(:visit)
    # System spec - use test endpoint
    (user, expect_success: true)
  else
    # Request spec - use direct session
    (user)
  end
  user
end

#login_as_user(attributes = {}) ⇒ Object

High-level helper: Login as regular user (creates user if needed)



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 165

def (attributes = {})
  user = create_regular_user(attributes)
  if respond_to?(:visit)
    # System spec - regular users get redirected to login
    (user, expect_success: false)
  else
    # Request spec - use direct session
    (user)
  end
  user
end

#login_with_github(user, expect_success: true) ⇒ Object

Convenience method: Login with GitHub OAuth provider (using test endpoint)



142
143
144
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 142

def (user, expect_success: true)
  (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)



137
138
139
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 137

def (user, expect_success: true)
  (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)



147
148
149
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 147

def (user, expect_success: true)
  (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).



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 114

def (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



179
180
181
182
183
184
185
186
187
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 179

def (user, provider: :google_oauth2)
  mock_oauth_for_user(user, provider: provider)

  visit 
  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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 67

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_userObject



53
54
55
56
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 53

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)



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/panda/core/testing/support/authentication_test_helpers.rb', line 90

def (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