Module: Panda::Core::Testing::OmniAuthHelpers

Defined in:
lib/panda/core/testing/omniauth_helpers.rb

Instance Method Summary collapse

Instance Method Details

#login_as_admin(email: "admin@example.com", name: "Admin User") ⇒ Object



29
30
31
32
33
# File 'lib/panda/core/testing/omniauth_helpers.rb', line 29

def (email: "admin@example.com", name: "Admin User")
  user = (email: email, name: name, is_admin: true)
  visit "/admin/auth/google_oauth2"
  user
end

#login_as_user(email: "user@example.com", name: "Regular User") ⇒ Object



35
36
37
38
39
# File 'lib/panda/core/testing/omniauth_helpers.rb', line 35

def (email: "user@example.com", name: "Regular User")
  user = (email: email, name: name, is_admin: false)
  visit "/admin/auth/google_oauth2"
  user
end

#logoutObject



41
42
43
# File 'lib/panda/core/testing/omniauth_helpers.rb', line 41

def logout
  visit "/admin/logout"
end

#mock_omniauth_failure(message = "Authentication failed") ⇒ Object



45
46
47
48
# File 'lib/panda/core/testing/omniauth_helpers.rb', line 45

def mock_omniauth_failure(message = "Authentication failed")
  OmniAuth.config.test_mode = true
  OmniAuth.config.mock_auth[:google_oauth2] = :invalid_credentials
end

#mock_omniauth_login(email: "admin@example.com", name: "Test User", provider: :google_oauth2, uid: "123456789", is_admin: true) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/panda/core/testing/omniauth_helpers.rb', line 7

def (email: "admin@example.com", name: "Test User", provider: :google_oauth2, uid: "123456789", is_admin: true)
  OmniAuth.config.test_mode = true
  OmniAuth.config.mock_auth[provider] = OmniAuth::AuthHash.new(
    provider: provider.to_s,
    uid: uid,
    info: {
      email: email,
      name: name,
      image: "https://example.com/avatar.jpg"
    }
  )

  # Create or update the user in the test database
  Panda::Core::User.find_or_create_by(email: email) do |u|
    # Split name into firstname and lastname
    parts = name.split(" ", 2)
    u.firstname = parts[0] || name
    u.lastname = parts[1] || ""
    u.admin = is_admin
  end
end