61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
from bs4 import BeautifulSoup
|
|
from imaginaerraum_door_admin.door_handle import DoorHandle
|
|
|
|
def test_login(browser, live_server):
|
|
response = browser.get(f'http://localhost:{live_server.port}')
|
|
|
|
assert '<h1>Space Zugangsverwaltung</h1>' in browser.page_source
|
|
|
|
response = browser.get(f'http://localhost:{live_server.port}/login')
|
|
|
|
email_form = browser.find_element_by_id('email').send_keys('gandalf@shire.me')
|
|
password_form = browser.find_element_by_id('password').send_keys('shadowfax')
|
|
submit_button = browser.find_element_by_id('submit').click()
|
|
|
|
assert 'Tür öffnen' in browser.page_source
|
|
|
|
|
|
def headless_login(client):
|
|
# extract csrf token from the login page source
|
|
response = client.get('/login')
|
|
soup = BeautifulSoup(response.data)
|
|
csrf_token = soup.find('input', attrs={'id': 'csrf_token'})['value']
|
|
|
|
# send login information
|
|
payload = {
|
|
'csrf_token': csrf_token,
|
|
'email': 'gandalf@shire.me',
|
|
'password': 'shadowfax'
|
|
}
|
|
return client.post('/login', data=payload, follow_redirects=True)
|
|
|
|
def test_login_headless(client):
|
|
response = headless_login(client)
|
|
soup = BeautifulSoup(response.data)
|
|
|
|
# make sure login succeeded -> Tür öffnen button will appear
|
|
assert any(['Tür öffnen' in link.contents[0] for link in soup.findAll('a', attrs={'class': ['btn'], 'role': 'button'})])
|
|
|
|
def test_open_door_button(client, mocker):
|
|
mocker.patch('imaginaerraum_door_admin.door_handle.DoorHandle.open_door')
|
|
|
|
# we need to log in in order to open the door
|
|
response = headless_login(client)
|
|
|
|
# visit route for open
|
|
client.get('/open')
|
|
|
|
# make sure the open method was called
|
|
DoorHandle.open_door.assert_called_once_with(user='gandalf')
|
|
|
|
def test_close_door_button(client, mocker):
|
|
mocker.patch('imaginaerraum_door_admin.door_handle.DoorHandle.close_door')
|
|
|
|
# we need to log in in order to open the door
|
|
response = headless_login(client)
|
|
|
|
# visit route for open
|
|
client.get('/close')
|
|
|
|
# make sure the open method was called
|
|
DoorHandle.close_door.assert_called_once_with(user='gandalf') |