def from_token_payload(token_payload: dict): return Token( client_id=ClientId(token_payload.get("cli")), user_id=UserId(token_payload.get("sub")) if token_payload.get("sub") else None, token_type=token_payload.get("typ"), )
def from_strings( client_id: str = None, user_id: str = None, correlation_id: str = None, ip: str = None, ): return InfoId( ClientId(client_id) if client_id else None, UserId(user_id) if user_id else None, CorrelationId(correlation_id) if correlation_id else None, ip, )
def from_dict(kdict: dict): client_id = ClientId(kdict.get("client_id")) if kdict.get("client_id") else None user_id = UserId(kdict.get("user_id")) if kdict.get("user_id") else None correlation_id = ( ClientId(kdict.get("correlation_id")) if kdict.get("correlation_id") else None ) ip = kdict.get("ip") return InfoId( client_id=client_id, user_id=user_id, correlation_id=correlation_id, ip=ip )
def update_from_headers(self, headers: Dict[str, str]): if headers: client_id = headers.get("X-Onboarding-Clientid") user_id = headers.get("X-Onboarding-Userid") correlation_id = headers.get("X-Correlation-Id") ip = headers.get("X-Forwarded-For") self.client_id = ClientId(client_id) if client_id else self.client_id self.user_id = UserId(user_id) if user_id else self.user_id self.correlation_id = ( CorrelationId(correlation_id) if correlation_id else self.correlation_id ) self.ip = ip if ip else self.ip return self
def retrieve(self, client_id: ClientId, user_id: UserId) -> Result[User, Error]: with self.collection_context() as collection: user_doc = collection.find_one({ "user_id": user_id.value, "client_id": client_id.value }) if user_doc: return Success( User( name=Name(user_doc.name), client_id=ClientId(user_doc.client_id), user_id=UserId(user_doc.user_id), )) else: return Failure(UserNotFoundError(user_id))
def retrieve(self, client_id: ClientId, user_id: UserId) -> Result[User, Error]: with self.session_scope() as session: user_model = (session.query(self.UserModel).filter( self.UserModel.client_id == client_id.value).filter( self.UserModel.user_id == user_id.value).first()) if not user_model: return Failure(UserNotFoundError(user_id)) return Success( User( name=Name(user_model.name), client_id=ClientId(user_model.client_id), user_id=UserId(user_model.user_id), ))
def test_should_return_200_when_call_a_entry_point_with_required_jwt_type_token_and_user_id( client_app, given_auth_token_headers_creator, given_any_name): headers = given_auth_token_headers_creator(type_token="ADMIN_TOKEN") response = client_app.post("/petisco/user", headers=headers, data=dict(name=given_any_name.value)) sleep(1.0) assert response.status_code == 200 user_id = response.json["user_id"] headers_with_user = given_auth_token_headers_creator( type_token="USER_TOKEN", user_id=UserId(user_id)) response = client_app.get("/petisco/user/name", headers=headers_with_user) assert response.json["name"] == given_any_name.value
def test_should_return_200_when_call_sum_with_valid_values_with_external_headers( client_app ): headers = { "Content-Type": "multipart/form-data", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36", "X-Correlation-Id": CorrelationId.generate().value, "X-Onboarding-Clientid": "petisco", "X-Onboarding-Userid": UserId.generate().value, } multipart_data = dict(value_1=2, value_2=3) response = client_app.post( "/petisco/sum", headers=headers, data=multipart_data, content_type="multipart/form-data", ) assert response.status_code == 200 assert response.json["result"] == 5
def test_should_declare_a_user_id_with_none(): with pytest.raises(InvalidUuidError): UserId(None)
import pytest from petisco.domain.value_objects.client_id import ClientId from petisco.domain.value_objects.user_id import UserId from petisco.security.token_decoder.token_decoder import TokenDecoder @pytest.mark.unit @pytest.mark.parametrize( "type_token,client_id,user_id", [ ("ADMIN_TOKEN", ClientId("client-id"), None), ("ADMIN_TOKEN", ClientId("client-id"), UserId.generate()), ("BACKEND_TOKEN", ClientId("client-id"), UserId.generate()), ], ) def test_should_decode_a_token_successfully(type_token, client_id, user_id, given_auth_token_headers_creator): auth_token = given_auth_token_headers_creator(type_token, client_id, user_id)["Authorization"] token_decoder = TokenDecoder() token = token_decoder.execute(auth_token).unwrap() assert token.token_type == type_token assert token.client_id == client_id assert token.user_id == user_id
def random(): return InfoId( ClientId("petisco-client"), UserId.generate(), CorrelationId.generate() )
def given_any_user_id() -> UserId: return UserId("9f74b5c0-2196-4a27-a03f-cb92d66e2bbd")
from petisco.domain.value_objects.user_id import UserId DEFAULT_ORGANIZATION = "alice" DEFAULT_SERVICE = "petisco" DEFAULT_EXCHANGE_NAME = f"{DEFAULT_ORGANIZATION}.{DEFAULT_SERVICE}" DEFAULT_USER_ID = UserId("9f74b5c0-2196-4a27-a03f-cb92d66e2bbd") DEFAULT_MAX_RETRIES = 5 DEFAULT_VERBOSE = True
def controller(): return Success(UserId.generate())
def test_should_declare_a_user_id_manually(value): user_id = UserId(value) assert isinstance(user_id, UserId) assert user_id.value == value
def test_should_fail_when_declare_a_user_id_that_exceeds_default_length_limits( ): with pytest.raises(InvalidUuidError): UserId("my_user_id_is_too_long_for_default_limit_length")
def test_should_declare_a_legacy_user_id_generated_automatically(): user_id = UserId.generate_legacy() assert isinstance(user_id, UserId) assert len(user_id.value) == 16
def create(name: Name): user = User(name, UserId.generate()) user.record(UserCreated(user.user_id, user.name)) return user
def test_should_declare_a_user_id_with_empty_string(): with pytest.raises(InvalidUuidError): UserId("")
def controller(): user_id = UserId(value) return Success(user_id)