Read about our upcoming Code of Conduct on this issue

session.py 3.83 KB
Newer Older
1
import logging
2
from selenium.webdriver.common.keys import Keys
3
import time
4
5

from .selenium import webdriver_wait_get
6
from .user import default_password, User
7

8

9
10
11
logger = logging.getLogger(__name__)


12
def initialize_root(driver, heptapod, password):
13
14
    # Create initial password
    elem = driver.find_element_by_name('user[password]')
15
    elem.send_keys(password)
16
    elem = driver.find_element_by_name('user[password_confirmation]')
17
    elem.send_keys(password)
18
19
    elem.send_keys(Keys.RETURN)

20
    sign_in_page_login(driver, heptapod, 'root', password=password)
21
22


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def finalize_password(driver, heptapod, user, password, current_password=None):
    """Enter the final password, if already on the "Set up new password" page.
    """
    if current_password is None:
        current_password = password

    elem = driver.find_element_by_name('user[current_password]')
    elem.send_keys(current_password)
    elem = driver.find_element_by_name('user[password]')
    elem.send_keys(password)
    elem = driver.find_element_by_name('user[password_confirmation]')
    elem.send_keys(password)
    elem.send_keys(Keys.RETURN)


38
def sign_in_page_login(driver, heptapod, user, password=None):
39
    """Perform login as user, with webdriver already on the signin page.
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

    If password is not specified, it is read from the heptapod object.
    If it is, it'll be stored in the heptapod object
    """
    elem = driver.find_element_by_name('user[login]')
    elem.send_keys(user)
    elem = driver.find_element_by_name('user[password]')
    if password is None:
        password = heptapod.users[user]['password']
    else:
        heptapod.users.setdefault(user, {})['password'] = password
    elem.send_keys(password)
    elem.send_keys(Keys.RETURN)


55
56
57
58
59
60
61
62
63
64
def page_is_password_setup(driver):
    """Return if the current page is the password setup page.

    This happens for instance after first login with a password considered
    to be temporary
    """
    # plural form to get an empty list instead of an exception
    return bool(driver.find_elements_by_name('user[password_confirmation]'))


65
def login_as_root(driver, heptapod, password):
66
    start = time.time()
67
    webdriver_wait_get(heptapod, driver, relative_uri='/users/sign_in')
68
69
70
71
    assert 'GitLab' in driver.title
    html = driver.find_element_by_tag_name('html').get_attribute('innerHTML')

    if 'Please create a password for your new account.' in html:
72
        initialize_root(driver, heptapod, password)
73
74
    else:
        sign_in_page_login(driver, heptapod, 'root', password)
75
76
77
        if page_is_password_setup(driver):
            finalize_password(driver, heptapod, 'root', password)
            sign_in_page_login(driver, heptapod, 'root', password)
78
79
        logger.info("Made signed-in webdriver for user %r in %.2f seconds",
                    'root', time.time() - start)
80
81


82
def ensure_user(heptapod, name, password=None, fullname=None):
83
84
    """Retrieve user or create it.

85
    :returns: `User` instance
86
87
88
89
90
    """
    if password is None:
        password = default_password(password)
    user = User.search(heptapod, name)
    if user is None:
91
92
        user = User.create(heptapod, name,
                           password=password, fullname=fullname)
93
94
95
    else:
        user.password = password
        user.store_in_heptapod()
96

97
98
99
100
101
    user.ensure_private_token()
    return user


def make_webdriver(heptapod, user_name, password):
102
    start = time.time()
103
    driver = heptapod.new_webdriver()
104
    webdriver_wait_get(heptapod, driver, relative_uri='/users/sign_in')
105
    sign_in_page_login(driver, heptapod, user_name, password=password)
106
    logger.info("Made signed-in webdriver for user %s in %.2f seconds",
107
108
                user_name, time.time() - start)
    return driver
109
110
111
112
113
114
115


def login_as_user(user):
    """Return a Selenium webdriver, authentified as user

    :param user: a `User` instance
    """
116
    return user.heptapod.get_user_webdriver(user.name)