Add playwright tests for anonymous map

This commit is contained in:
Yohan Boniface 2023-09-12 11:07:10 +02:00
parent d902546da4
commit a15aa3566a
3 changed files with 193 additions and 64 deletions

View file

@ -0,0 +1,81 @@
import pytest
from django.core.signing import get_cookie_signer
from playwright.sync_api import expect
from umap.models import DataLayer
pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures("allow_anonymous")]
@pytest.fixture
def owner_session(anonymap, context, live_server):
key, value = anonymap.signed_cookie_elements
signed = get_cookie_signer(salt=key).sign(value)
context.add_cookies([{"name": key, "value": signed, "url": live_server.url}])
return context.new_page()
def test_map_load_with_owner(anonymap, live_server, owner_session):
owner_session.goto(f"{live_server.url}{anonymap.get_absolute_url()}")
map_el = owner_session.locator("#map")
expect(map_el).to_be_visible()
enable = owner_session.get_by_role("link", name="Edit")
expect(enable).to_be_visible()
enable.click()
disable = owner_session.get_by_role("link", name="Disable editing")
expect(disable).to_be_visible()
save = owner_session.get_by_title("Save current edits (Ctrl+S)")
expect(save).to_be_visible()
add_marker = owner_session.get_by_title("Draw a marker")
expect(add_marker).to_be_visible()
edit_settings = owner_session.get_by_title("Edit map settings")
expect(edit_settings).to_be_visible()
edit_permissions = owner_session.get_by_title("Update permissions and editors")
expect(edit_permissions).to_be_visible()
def test_map_load_with_anonymous(anonymap, live_server, page):
page.goto(f"{live_server.url}{anonymap.get_absolute_url()}")
map_el = page.locator("#map")
expect(map_el).to_be_visible()
enable = page.get_by_role("link", name="Edit")
expect(enable).to_be_hidden()
def test_map_load_with_anonymous_but_editable_layer(
anonymap, live_server, page, datalayer
):
datalayer.edit_status = DataLayer.ANONYMOUS
datalayer.save()
page.goto(f"{live_server.url}{anonymap.get_absolute_url()}")
map_el = page.locator("#map")
expect(map_el).to_be_visible()
enable = page.get_by_role("link", name="Edit")
expect(enable).to_be_visible()
enable.click()
disable = page.get_by_role("link", name="Disable editing")
expect(disable).to_be_visible()
save = page.get_by_title("Save current edits (Ctrl+S)")
expect(save).to_be_visible()
add_marker = page.get_by_title("Draw a marker")
expect(add_marker).to_be_visible()
edit_settings = page.get_by_title("Edit map settings")
expect(edit_settings).to_be_hidden()
edit_permissions = page.get_by_title("Update permissions and editors")
expect(edit_permissions).to_be_hidden()
def test_owner_permissions_form(map, datalayer, live_server, owner_session):
owner_session.goto(f"{live_server.url}{map.get_absolute_url()}?edit")
edit_permissions = owner_session.get_by_title("Update permissions and editors")
expect(edit_permissions).to_be_visible()
edit_permissions.click()
select = owner_session.locator(".umap-field-share_status select")
expect(select).to_be_hidden()
# expect(select).to_have_value(Map.PUBLIC) # Does not work
owner_field = owner_session.locator(".umap-field-owner")
expect(owner_field).to_be_hidden()
editors_field = owner_session.locator(".umap-field-editors input")
expect(editors_field).to_be_hidden()
datalayer_label = owner_session.get_by_text('Who can edit "Donau"')
expect(datalayer_label).to_be_visible()

View file

@ -1,64 +0,0 @@
from time import sleep
import pytest
from playwright.sync_api import expect
from umap.models import Map
pytestmark = pytest.mark.django_db
@pytest.fixture
def loggedin_page(context, user, settings, live_server):
settings.ENABLE_ACCOUNT_LOGIN = True
page = context.new_page()
page.goto(f"{live_server.url}/en/")
page.locator(".login").click()
page.get_by_placeholder("Username").fill("Gabriel")
page.get_by_placeholder("Password").fill("123123")
page.locator('#login_form input[type="submit"]').click()
sleep(1) # Time for ajax login POST to proceed
return page
def test_map_update_with_owner(map, live_server, loggedin_page):
loggedin_page.goto(f"{live_server.url}{map.get_absolute_url()}")
map_el = loggedin_page.locator("#map")
expect(map_el).to_be_visible()
enable = loggedin_page.get_by_role("link", name="Edit")
expect(enable).to_be_visible()
enable.click()
disable = loggedin_page.get_by_role("link", name="Disable editing")
expect(disable).to_be_visible()
save = loggedin_page.get_by_title("Save current edits (Ctrl+S)")
expect(save).to_be_visible()
add_marker = loggedin_page.get_by_title("Draw a marker")
expect(add_marker).to_be_visible()
edit_settings = loggedin_page.get_by_title("Edit map settings")
expect(edit_settings).to_be_visible()
edit_permissions = loggedin_page.get_by_title("Update permissions and editors")
expect(edit_permissions).to_be_visible()
def test_map_update_with_anonymous(map, live_server, page):
page.goto(f"{live_server.url}{map.get_absolute_url()}")
map_el = page.locator("#map")
expect(map_el).to_be_visible()
enable = page.get_by_role("link", name="Edit")
expect(enable).to_be_hidden()
def test_owner_permissions_form(map, datalayer, live_server, loggedin_page):
loggedin_page.goto(f"{live_server.url}{map.get_absolute_url()}?edit")
edit_permissions = loggedin_page.get_by_title("Update permissions and editors")
expect(edit_permissions).to_be_visible()
edit_permissions.click()
select = loggedin_page.locator(".umap-field-share_status select")
expect(select).to_be_visible()
# expect(select).to_have_value(Map.PUBLIC) # Does not work
owner_field = loggedin_page.locator(".umap-field-owner")
expect(owner_field).to_be_visible()
editors_field = loggedin_page.locator(".umap-field-editors input")
expect(editors_field).to_be_visible()
datalayer_label = loggedin_page.get_by_text('Who can edit "Donau"')
expect(datalayer_label).to_be_visible()

View file

@ -0,0 +1,112 @@
from time import sleep
import pytest
from playwright.sync_api import expect
from umap.models import DataLayer
pytestmark = pytest.mark.django_db
@pytest.fixture
def login(context, user, settings, live_server):
def do_login(username):
# TODO use storage state to do login only once per session
# https://playwright.dev/python/docs/auth
settings.ENABLE_ACCOUNT_LOGIN = True
page = context.new_page()
page.goto(f"{live_server.url}/en/")
page.locator(".login").click()
page.get_by_placeholder("Username").fill(username)
page.get_by_placeholder("Password").fill("123123")
page.locator('#login_form input[type="submit"]').click()
sleep(1) # Time for ajax login POST to proceed
return page
return do_login
def test_map_update_with_owner(map, live_server, login):
page = login(map.owner.username)
page.goto(f"{live_server.url}{map.get_absolute_url()}")
map_el = page.locator("#map")
expect(map_el).to_be_visible()
enable = page.get_by_role("link", name="Edit")
expect(enable).to_be_visible()
enable.click()
disable = page.get_by_role("link", name="Disable editing")
expect(disable).to_be_visible()
save = page.get_by_title("Save current edits (Ctrl+S)")
expect(save).to_be_visible()
add_marker = page.get_by_title("Draw a marker")
expect(add_marker).to_be_visible()
edit_settings = page.get_by_title("Edit map settings")
expect(edit_settings).to_be_visible()
edit_permissions = page.get_by_title("Update permissions and editors")
expect(edit_permissions).to_be_visible()
def test_map_update_with_anonymous(map, live_server, page):
page.goto(f"{live_server.url}{map.get_absolute_url()}")
map_el = page.locator("#map")
expect(map_el).to_be_visible()
enable = page.get_by_role("link", name="Edit")
expect(enable).to_be_hidden()
def test_map_update_with_anonymous_but_editable_datalayer(
map, datalayer, live_server, page
):
datalayer.edit_status = DataLayer.ANONYMOUS
datalayer.save()
page.goto(f"{live_server.url}{map.get_absolute_url()}")
map_el = page.locator("#map")
expect(map_el).to_be_visible()
enable = page.get_by_role("link", name="Edit")
expect(enable).to_be_visible()
enable.click()
add_marker = page.get_by_title("Draw a marker")
expect(add_marker).to_be_visible()
edit_settings = page.get_by_title("Edit map settings")
expect(edit_settings).to_be_hidden()
edit_permissions = page.get_by_title("Update permissions and editors")
expect(edit_permissions).to_be_hidden()
def test_owner_permissions_form(map, datalayer, live_server, login):
page = login(map.owner.username)
page.goto(f"{live_server.url}{map.get_absolute_url()}?edit")
edit_permissions = page.get_by_title("Update permissions and editors")
expect(edit_permissions).to_be_visible()
edit_permissions.click()
select = page.locator(".umap-field-share_status select")
expect(select).to_be_visible()
# expect(select).to_have_value(Map.PUBLIC) # Does not work
owner_field = page.locator(".umap-field-owner")
expect(owner_field).to_be_visible()
editors_field = page.locator(".umap-field-editors input")
expect(editors_field).to_be_visible()
datalayer_label = page.get_by_text('Who can edit "Donau"')
expect(datalayer_label).to_be_visible()
def test_map_update_with_editor(map, live_server, login, user):
map.editors.add(user)
map.save()
page = login(user.username)
page.goto(f"{live_server.url}{map.get_absolute_url()}")
map_el = page.locator("#map")
expect(map_el).to_be_visible()
enable = page.get_by_role("link", name="Edit")
expect(enable).to_be_visible()
enable.click()
disable = page.get_by_role("link", name="Disable editing")
expect(disable).to_be_visible()
save = page.get_by_title("Save current edits (Ctrl+S)")
expect(save).to_be_visible()
add_marker = page.get_by_title("Draw a marker")
expect(add_marker).to_be_visible()
edit_settings = page.get_by_title("Edit map settings")
expect(edit_settings).to_be_visible()
edit_permissions = page.get_by_title("Update permissions and editors")
expect(edit_permissions).to_be_visible()