From c8680b33d485a23c23fac55483aabdb792df0821 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Wed, 22 Jun 2022 12:03:17 -0400 Subject: [PATCH] Added a session id. --- src/main.rs | 10 +++++++++- tests/features/session.feature | 8 ++++++++ tests/step_defs/page.py | 10 +++++++++- tests/step_defs/test_session.py | 25 +++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 tests/features/session.feature create mode 100644 tests/step_defs/test_session.py diff --git a/src/main.rs b/src/main.rs index 4ea03ae..b59ea4a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,8 @@ -use tide::{http::StatusCode, Request, Response}; +use tide::{ + http::StatusCode, + sessions::{MemoryStore, SessionMiddleware}, + Request, Response, +}; mod settings; @@ -16,6 +20,10 @@ async fn main() -> tide::Result<()> { async fn app_setup() -> tide::Server<()> { let mut app = tide::new(); app.at("/").get(home); + app.with(SessionMiddleware::new( + MemoryStore::new(), + b"361f953f-56ba-45e6-86ab-9efbf61b745d", + )); return app; } diff --git a/tests/features/session.feature b/tests/features/session.feature new file mode 100644 index 0000000..cb51f09 --- /dev/null +++ b/tests/features/session.feature @@ -0,0 +1,8 @@ +Feature: Session + + Browser session requirements + + Scenario: Session ID is set + Given a running server + When the home page is accessed + Then there is a session id diff --git a/tests/step_defs/page.py b/tests/step_defs/page.py index 017ba77..6c5aad9 100644 --- a/tests/step_defs/page.py +++ b/tests/step_defs/page.py @@ -3,6 +3,7 @@ from urllib import request from urllib.error import HTTPError + class Page: """Class for getting an individual page.""" @@ -13,10 +14,17 @@ class Page: def request_url(self, url): """Make a page request.""" try: - self.res = request.urlopen(url) + page = request.urlopen(url) except HTTPError as err: self.res = err + else: + with page: + self.res = page def get_status_code(self): """Return the status code.""" return self.res.status + + def get_header(self, header): + """Did a session id get set.""" + return self.res.headers[header] diff --git a/tests/step_defs/test_session.py b/tests/step_defs/test_session.py new file mode 100644 index 0000000..cb83de9 --- /dev/null +++ b/tests/step_defs/test_session.py @@ -0,0 +1,25 @@ +"""tests for server sessions.""" + +from pytest_bdd import given, scenarios, then, when + +scenarios("../features/session.feature") + + +@given("a running server") +def start_server(server): + """Make a running server.""" + server.set_safe_port() + server.start() + + +@when("the home page is accessed") +def access_home_page(server, page): + """Access the home page.""" + url = f"http://{server.base_url}/" + page.request_url(url) + + +@then("there is a session id") +def confirm_session(page): + """Confirm session id exists.""" + assert page.get_header("set-cookie")