def test_check_csrf_incorrect_signature(self):
     """Test check_csrf when signature doesn't match."""
     with unittest.mock.patch(
             "swift_browser_ui._convenience.setd",
             new={
                 "auth_endpoint_url":
                 "http://example-auth.exampleosep.com:5001/v3"
             }):
         testreq = get_request_with_fernet()
         cookie, _ = generate_cookie(testreq)
         cookie = add_csrf_to_cookie(cookie, testreq, bad_sign=True)
         encrypt_cookie(cookie, testreq)
         testreq.headers["Referer"] = "http://localhost:8080"
         with self.assertRaises(HTTPForbidden):
             check_csrf(testreq)
 def test_check_csrf_no_referer(self):
     """Test check_csrf when no Referer header is present."""
     with unittest.mock.patch(
             "swift_browser_ui._convenience.setd",
             new={
                 "auth_endpoint_url":
                 "http://example-auth.exampleosep.com:5001/v3"
             }):
         testreq = get_request_with_fernet()
         cookie, _ = generate_cookie(testreq)
         cookie = add_csrf_to_cookie(cookie, testreq)
         encrypt_cookie(cookie, testreq)
         self.assertTrue(check_csrf(testreq))
 def test_check_csrf_correct_referer(self):
     """Test check_csrf when the session is valid."""
     with unittest.mock.patch(
             "swift_browser_ui._convenience.setd",
             new={
                 "auth_endpoint_url":
                 "http://example-auth.exampleosep.com:5001/v3"
             }):
         testreq = get_request_with_fernet()
         cookie, _ = generate_cookie(testreq)
         cookie = add_csrf_to_cookie(cookie, testreq)
         encrypt_cookie(cookie, testreq)
         testreq.headers["Referer"] = "http://localhost:8080"
         self.assertTrue(check_csrf(testreq))
 def test_check_csrf_os_skip(self):
     """Test check_csrf when skipping referer from OS."""
     with unittest.mock.patch(
             "swift_browser_ui._convenience.setd",
             new={
                 "auth_endpoint_url":
                 "http://example-auth.exampleosep.com:5001/v3"
             }):
         testreq = get_request_with_fernet()
         cookie, _ = generate_cookie(testreq)
         cookie = add_csrf_to_cookie(cookie, testreq)
         encrypt_cookie(cookie, testreq)
         testreq.headers["Referer"] = "http://example-auth.exampleosep.com"
         self.assertTrue(check_csrf(testreq))