2022-01-24 23:09:14 +00:00
|
|
|
from bs4 import BeautifulSoup
|
2022-01-25 18:38:19 +00:00
|
|
|
from imaginaerraum_door_admin.door_handle import DoorHandle
|
2022-01-24 23:09:14 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-01-25 18:38:19 +00:00
|
|
|
def headless_login(client):
|
2022-01-24 23:09:14 +00:00
|
|
|
# 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'
|
|
|
|
}
|
2022-01-25 18:38:19 +00:00
|
|
|
return client.post('/login', data=payload, follow_redirects=True)
|
|
|
|
|
|
|
|
def test_login_headless(client):
|
|
|
|
response = headless_login(client)
|
2022-01-24 23:09:14 +00:00
|
|
|
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'})])
|
|
|
|
|
2022-01-25 18:38:19 +00:00
|
|
|
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')
|