Module: Auth

Extended by:
LapisLazuli
Defined in:
lib/lapis_lazuli/generators/cucumber/template/features/helpers/authentication_helper.rb

Constant Summary collapse

@@user =
''

Constants included from LapisLazuli

LapisLazuli::CONFIG_OPTIONS, LapisLazuli::PLACEHOLDERS, LapisLazuli::VERSION

Constants included from LapisLazuli::ArgParse

LapisLazuli::ArgParse::ERROR_OPTIONS

Constants included from LapisLazuli::WorldModule::Hooks

LapisLazuli::WorldModule::Hooks::HOOK_QUEUES

Instance Attribute Summary

Attributes included from LapisLazuli

#software_versions

Class Method Summary collapse

Methods included from LapisLazuli

After, Before, Start, fetch_versions

Methods included from LapisLazuli::Assertions

#assertions, #assertions=

Methods included from LapisLazuli::GenericModule::XPath

#xp_and, #xp_contains, #xp_not, #xp_or

Methods included from LapisLazuli::WorldModule::API

#api, #has_api?

Methods included from LapisLazuli::WorldModule::Browser

#browser, #has_browser?

Methods included from LapisLazuli::WorldModule::Browser::ClassMethods

#browser_module, #browser_modules

Methods included from LapisLazuli::WorldModule::Proxy

#has_proxy?, #proxy

Methods included from LapisLazuli::WorldModule::Logging

#log

Methods included from LapisLazuli::WorldModule::Config

#add_config_from_file, #config, #current_env, #env, #env_or_config, #get_config_from_file, #has_config?, #has_env?, #has_env_or_config?, #init, #load_config, #metadata, #var_from_env

Methods included from LapisLazuli::WorldModule::Config::ClassMethods

#add_config, #config_file, #config_file=, #config_files

Methods included from LapisLazuli::WorldModule::Error

#error, #start_debugger

Methods included from LapisLazuli::WorldModule::Annotate

#annotate, #annotations

Methods included from LapisLazuli::ArgParse

#make_list_from_item, #make_list_from_nested, #parse_args

Methods included from LapisLazuli::WorldModule::Variable

#has_storage?, #scenario, #storage, #time, #uuid, #variable, #variable!

Methods included from LapisLazuli::WorldModule::Hooks

add_hook, #after_scenario_hook, #before_scenario_hook

Class Method Details

.ensure_log_in(user = 'default-user') ⇒ Object

Makes sure that a specific user is logged in, if it’s not already.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lapis_lazuli/generators/cucumber/template/features/helpers/authentication_helper.rb', line 51

def (user='default-user')
  Nav.to('training-page')
  unless Auth.is_logged_in?(user)
    # If the wrong user is logged in, we should ensure a log out action and then log in again
    Auth.ensure_log_out
    Auth.(user)
    # Double check if the login was successful, if not, throw an error.
    unless Auth.is_logged_in?(user)
      error "Failed to log in `#{user}`."
    end
  end
end

.ensure_log_outObject

Next are the functions called from the step definitions ‘ensure_something` is best practise to be used for functions that should get the test to a certain state. For example: `ensure_log_out` only logs out if you’re logged in ‘log_out` will blindly try to log out and fail if you’re already logged out



40
41
42
43
44
45
46
47
48
# File 'lib/lapis_lazuli/generators/cucumber/template/features/helpers/authentication_helper.rb', line 40

def ensure_log_out
  Nav.to('training-page')
  if Auth.is_logged_in?
    Auth.log_out
    if Auth.is_logged_in?
      error 'Page did not display in logged out state after logging out'
    end
  end
end

.form_containerObject

This is a list of elements relevant for this helper. The following is short notation, only use this if the element selector can be done in 1 line. @formatter:off



13
# File 'lib/lapis_lazuli/generators/cucumber/template/features/helpers/authentication_helper.rb', line 13

def form_container; browser.wait(:like => [:form, :id, 'form-login']); end

.is_logged_in?(user = nil) ⇒ Boolean

If user=nil, any logged in user is acceptable, else we want to make sure the username matches with the logged in user.

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lapis_lazuli/generators/cucumber/template/features/helpers/authentication_helper.rb', line 65

def is_logged_in?(user=nil)
  # For performance, we do a 0 second wait for the logged_out_element
  if Auth.logged_out_element(0, false)
    return false
  end
   = Auth.logged_in_element(5, false)
  if .nil?
    # Logged in element not found, check if the logged out element is present
    logout_elm = Auth.logged_out_element(0, false)
    if logout_elm.nil?
      # Neither of the elements were present, this should not be possible.
      error 'Failed to find the logged_out element and the logged_in element. The user is not logged in, nor logged out.'
    else
      # Logged out element was found the second time.
      return false
    end
  else
    # The logged in element was found, should we match the username?
    if user.nil?
      # No, any user is fine
      return true
    else
      # Yes, load the user data and match the username
      User.load_user_data(user)
      return .span(:class => ['username', 'ng-binding']).text == User.get('username')
    end
  end
end

.log_in(user = nil, renew_session = false) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/lapis_lazuli/generators/cucumber/template/features/helpers/authentication_helper.rb', line 103

def (user=nil, renew_session=false)
  # If user=nil, we expect that there already is user data loaded in a previous step.
  User.load_user_data(user) unless user.nil?

  Auth.username_field.to_subtype.to_subtype.set(User.get('username'))
  Auth.password_field.to_subtype.to_subtype.set(User.get('password'))
  Auth..click

  unless Auth.is_logged_in? user
    alert = browser.find(:like => [:div, :class, 'alert'], :throw => false)
    if alert.nil?
      error "Failed to log in user #{user}"
    else
      alert.flash
      error "Found error while logging in #{user}: `#{alert.html}`"
    end
  end
end

.log_outObject



94
95
96
97
98
99
100
101
# File 'lib/lapis_lazuli/generators/cucumber/template/features/helpers/authentication_helper.rb', line 94

def log_out
  Auth.logged_in_element.click
  dropdown = browser.wait(:like => [:ul, :class, 'dropdown-menu'])
  browser.find(
    :like => [:a, :id, 'link-logout'],
    :context => dropdown
  ).click
end

.logged_in_element(timeout = 10, throw = true) ⇒ Object

Following elements that need more advanced options/search patterns



20
21
22
23
24
25
26
# File 'lib/lapis_lazuli/generators/cucumber/template/features/helpers/authentication_helper.rb', line 20

def logged_in_element(timeout=10, throw=true)
  browser.wait(
    :like => [:a, :id, 'user_dropdown'],
    :timeout => timeout,
    :throw => throw
  )
end

.logged_out_element(timeout = 10, throw = true) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/lapis_lazuli/generators/cucumber/template/features/helpers/authentication_helper.rb', line 28

def logged_out_element(timeout=10, throw=true)
  browser.wait(
    :like => [:form, :id, 'form-login'],
    :timeout => timeout,
    :throw => throw
  )
end

.login_buttonObject



16
# File 'lib/lapis_lazuli/generators/cucumber/template/features/helpers/authentication_helper.rb', line 16

def ; browser.button(:id => 'button-login'); end

.password_fieldObject



15
# File 'lib/lapis_lazuli/generators/cucumber/template/features/helpers/authentication_helper.rb', line 15

def password_field; form_container.input(:id => 'login-password'); end

.username_fieldObject



14
# File 'lib/lapis_lazuli/generators/cucumber/template/features/helpers/authentication_helper.rb', line 14

def username_field; form_container.input(:xpath => '//*[@id="login-username"]'); end