added tests for admin management operations, streamlined testing a bit

This commit is contained in:
Simon Pirkelmann 2022-01-29 23:46:20 +01:00
parent 8f8bdb8cc3
commit 97957e389c

View File

@ -1,5 +1,7 @@
import pytest
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from imaginaerraum_door_admin.door_handle import DoorHandle from imaginaerraum_door_admin.door_handle import DoorHandle
import re
def test_login(browser, live_server): def test_login(browser, live_server):
response = browser.get(f'http://localhost:{live_server.port}') response = browser.get(f'http://localhost:{live_server.port}')
@ -15,20 +17,26 @@ def test_login(browser, live_server):
assert 'Tür öffnen' in browser.page_source assert 'Tür öffnen' in browser.page_source
def headless_login(client): def extract_csrf_token(response):
# extract csrf token from the login page source
response = client.get('/login')
soup = BeautifulSoup(response.data) soup = BeautifulSoup(response.data)
csrf_token = soup.find('input', attrs={'id': 'csrf_token'})['value'] csrf_token = soup.find('input', attrs={'id': 'csrf_token'})['value']
return csrf_token
def headless_login(client, user='gandalf@shire.me', password='shadowfax'):
# extract csrf token from the login page source
response = client.get('/login')
csrf_token = extract_csrf_token(response)
# send login information # send login information
payload = { payload = {
'csrf_token': csrf_token, 'csrf_token': csrf_token,
'email': 'gandalf@shire.me', 'email': user,
'password': 'shadowfax' 'password': password
} }
return client.post('/login', data=payload, follow_redirects=True) return client.post('/login', data=payload, follow_redirects=True)
def test_login_headless(client): def test_login_headless(client):
response = headless_login(client) response = headless_login(client)
soup = BeautifulSoup(response.data) soup = BeautifulSoup(response.data)
@ -36,26 +44,76 @@ def test_login_headless(client):
# make sure login succeeded -> Tür öffnen button will appear # 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'})]) 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):
@pytest.fixture
def client_authenticated(client):
# log in using admin account for testing
headless_login(client)
yield client
def test_open_door_button(client_authenticated, mocker):
mocker.patch('imaginaerraum_door_admin.door_handle.DoorHandle.open_door') 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 # visit route for open
client.get('/open') client_authenticated.get('/open')
# make sure the open method was called # make sure the open method was called
DoorHandle.open_door.assert_called_once_with(user='gandalf') DoorHandle.open_door.assert_called_once_with(user='gandalf')
def test_close_door_button(client, mocker):
def test_close_door_button(client_authenticated, mocker):
mocker.patch('imaginaerraum_door_admin.door_handle.DoorHandle.close_door') 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 # visit route for open
client.get('/close') client_authenticated.get('/close')
# make sure the open method was called # make sure the open method was called
DoorHandle.close_door.assert_called_once_with(user='gandalf') DoorHandle.close_door.assert_called_once_with(user='gandalf')
def test_manage_admins(client_authenticated):
# visit admin management page
response = client_authenticated.get('/manage_admins')
assert "Nutzer Übersicht" in response.data.decode()
assert "gandalf" in response.data.decode()
assert "gandalf@shire.me" in response.data.decode()
def test_create_admin(client_authenticated):
# visit admin management page
response = client_authenticated.get('/manage_admins')
csrf_token = extract_csrf_token(response)
# post data for creating a new admin
payload = {'name': 'bilbo',
'email': 'bilbo@shire.me',
'csrf_token': csrf_token}
response = client_authenticated.post('/manage_admins', data=payload,
follow_redirects=True)
# after the new admin user is created, we should have been redirected to the
# /manage_admin page. there, the password for login is displayed
# we test if the newly created user can log in with that password
# extract password displayed on the page
match = re.search('Passwort (?P<password>.*) um', response.data.decode())
assert match is not None
extracted_password = match['password']
# log out current user
response = client_authenticated.get('/logout')
# try to log in new user using the extracted password
response = headless_login(client_authenticated, user='bilbo',
password=extracted_password)
# - see if it works
soup = BeautifulSoup(response.data)
# make sure login succeeded
# -> username should be displayed
assert 'Benutzer <span>bilbo</span>' in soup.decode()
# -> 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'})])