def test_invalid_value(self): """ Initializing the Identity object with an invalid Base6č encoded payload should raise a ValueError. """ with self.assertRaises(ValueError): from_encoded("invalid Base64")
def test_invalid_type(self): """ Initializing the Identity object with an invalid type that can’t be a Base64 encoded payload should raise a TypeError. """ with self.assertRaises(TypeError): from_encoded(["not", "a", "string"])
def test_invalid_format(self): """ Initializing the Identity object with an valid Base64 encoded payload that does not contain the "identity" field. """ identity = self._identity() dict_ = identity._asdict() json = dumps(dict_) base64 = b64encode(json.encode()) with self.assertRaises(KeyError): from_encoded(base64)
def _pick_identity(): if os.getenv("FLASK_DEBUG") and os.getenv("NOAUTH"): return Identity(account_number="0000001") else: try: payload = request.headers[_IDENTITY_HEADER] except KeyError: abort(Forbidden.code) try: return from_encoded(payload) except (KeyError, TypeError, ValueError): abort(Forbidden.code)
def test_valid(self): """ Initialize the Identity object with an encoded payload – a base64-encoded JSON. That would typically be a raw HTTP header content. """ identity = self._identity() dict_ = {"identity": identity._asdict()} json = dumps(dict_) base64 = b64encode(json.encode()) try: self.assertEqual(identity, from_encoded(base64)) except (TypeError, ValueError): self.fail()