Confirmed restart did not change session id.

This commit is contained in:
Jeff Baskin 2022-06-24 14:49:46 -04:00
parent d68988b2d4
commit ea0503defd
3 changed files with 58 additions and 9 deletions

View File

@ -6,3 +6,9 @@ Feature: Session
Given a running server
When the home page is accessed
Then there is a default session id
Scenario: Session ID is retained after restart
Given a running server
And the home page is accessed
When the server is restarted
Then the session id remains the same

View File

@ -4,6 +4,7 @@ from pytest import fixture
from .server import Server
from .page import Page
@fixture
def server():
"""Create a server instance."""
@ -11,7 +12,14 @@ def server():
yield serv
serv.destroy()
@fixture
def page():
pg = Page()
return pg
"""Return a page for testing."""
return Page()
@fixture
def data_store():
"""Return a dictionary to hold between function test data."""
return {}

View File

@ -1,10 +1,23 @@
"""tests for server sessions."""
from time import sleep
from pytest_bdd import given, scenarios, then, when
SESSION_NAME = "morethantext.sid"
scenarios("../features/session.feature")
def get_session_id(page):
"""Pulls the session id cookie."""
cookies = page.get_cookies()
id_cookie = None
for holder in cookies:
if holder.name == SESSION_NAME:
id_cookie = holder
return id_cookie
@given("a running server")
def start_server(server):
"""Make a running server."""
@ -12,6 +25,14 @@ def start_server(server):
server.start()
@given("the home page is accessed")
def store_session_id(server, page, data_store):
"""Store the session id."""
page.request_url(f"http://{server.base_url}/")
cookie = get_session_id(page)
data_store["session"] = cookie.value
@when("the home page is accessed")
def access_home_page(server, page):
"""Access the home page."""
@ -19,13 +40,27 @@ def access_home_page(server, page):
page.request_url(url)
@when("the server is restarted")
def restart_server(server):
"""Restarts the server"""
server.stop()
sleep(1)
server.start()
@then("there is a default session id")
def confirm_session(page):
"""Confirm session id exists."""
cookies = page.get_cookies()
name = "morethantext.sid"
cookie = None
for holder in cookies:
if holder.name == name:
cookie = holder
assert cookie, f"Did not find cookie {name}, but retrieved {cookies}"
assert get_session_id(
page
), f"Did not find cookie {SESSION_NAME}, but retrieved {page.get_cookies()}"
@then("the session id remains the same")
def check_session_id(page, data_store):
"""Checks the new session with the old."""
cookie = get_session_id(page)
session = cookie.value
assert (
session == data_store["session"]
), f"Session id became {session} but should have been {data_store['session']}"