From ea0503defdec63ccc03d9f708c9aaa8d0c4d794c Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Fri, 24 Jun 2022 14:49:46 -0400 Subject: [PATCH] Confirmed restart did not change session id. --- tests/features/session.feature | 6 ++++ tests/step_defs/conftest.py | 12 ++++++-- tests/step_defs/test_session.py | 49 ++++++++++++++++++++++++++++----- 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/tests/features/session.feature b/tests/features/session.feature index 8fed299..ebe631f 100644 --- a/tests/features/session.feature +++ b/tests/features/session.feature @@ -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 diff --git a/tests/step_defs/conftest.py b/tests/step_defs/conftest.py index c7b8879..29d80d6 100644 --- a/tests/step_defs/conftest.py +++ b/tests/step_defs/conftest.py @@ -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 {} diff --git a/tests/step_defs/test_session.py b/tests/step_defs/test_session.py index f6d07b2..81553d0 100644 --- a/tests/step_defs/test_session.py +++ b/tests/step_defs/test_session.py @@ -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']}"