def test_api_check_raise_on_invalid_cookie(self):
     """Test raise if there's an invalid session cookie."""
     testreq = get_request_with_fernet()
     _, testreq.cookies['S3BROW_SESSION'] = generate_cookie(testreq)
     testreq.app['Sessions'] = []
     with self.assertRaises(HTTPUnauthorized):
         api_check(testreq)
 def test_api_check_raise_on_invalid_fernet(self):
     """Test raise if the cryptographic key has changed."""
     testreq = get_request_with_fernet()
     _, testreq.cookies['S3BROW_SESSION'] = generate_cookie(testreq)
     testreq.app['Crypt'] = cryptography.fernet.Fernet(
         cryptography.fernet.Fernet.generate_key())
     with self.assertRaises(HTTPUnauthorized):
         api_check(testreq)
 def test_api_check_raise_on_no_avail(self):
     """Test raise if the availability wasn't checked before an API call."""
     testreq = get_request_with_fernet()
     cookie, _ = generate_cookie(testreq)
     testreq.cookies['S3BROW_SESSION'] = \
         get_full_crypted_session_cookie(cookie, testreq.app)
     session = cookie["id"]
     testreq.app['Creds'][session] = {}
     testreq.app['Sessions'] = [session]
     testreq.app['Creds'][session]['ST_conn'] = "placeholder"
     testreq.app['Creds'][session]['OS_sess'] = "placeholder"
     with self.assertRaises(HTTPUnauthorized):
         api_check(testreq)
 def test_api_check_raise_on_no_session(self):
     """Test raise if there's no established OS session on an API call."""
     testreq = get_request_with_fernet()
     cookie, _ = generate_cookie(testreq)
     testreq.cookies['S3BROW_SESSION'] = \
         get_full_crypted_session_cookie(cookie, testreq.app)
     session = cookie["id"]
     testreq.app['Sessions'] = [session]
     testreq.app['Creds'][session] = {}
     testreq.app['Creds'][session]['ST_conn'] = "placeholder"
     testreq.app['Creds'][session]['Avail'] = "placeholder"
     with self.assertRaises(HTTPUnauthorized):
         api_check(testreq)
 def test_api_check_success(self):
     """Test that the api_check function runs with correct input."""
     testreq = get_request_with_fernet()
     cookie, _ = generate_cookie(testreq)
     testreq.cookies['S3BROW_SESSION'] = \
         get_full_crypted_session_cookie(cookie, testreq.app)
     session = cookie["id"]
     testreq.app['Sessions'] = [session]
     testreq.app['Creds'][session] = {}
     testreq.app['Creds'][session]['Avail'] = "placeholder"
     testreq.app['Creds'][session]['OS_sess'] = "placeholder"
     testreq.app['Creds'][session]['ST_conn'] = "placeholder"
     ret = api_check(testreq)
     self.assertEqual(ret, cookie["id"])