Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def standard_hooks_mock(user_auth=False, client_auth=None, client_user_auth=False, session_auth=None, session_command=None):
- """
- Mock out standard hooks for the scope of the test
- :param user_auth: A bool defining whether to automatically pass the user auth tests
- :param client_auth: A list of dictionaries. Each dictionary should contain the `official` and `origin` keys,
- defining respectively whether the client should be denoted as official, and whether the client
- is the origin of the request.
- :param session_auth: A bool defining whether to automatically pass the session based authentication tests
- :return (connector_instance, linked_auth):
- Connector instance is an instance of the StepConnector class defining the appropriate session mocks for the
- hooks. Linked auth is the authentication data that needs to be appended to the fake request
- """
- if client_auth:
- assert type(client_auth) == list
- for client_mock_data in client_auth:
- assert type(client_mock_data) == dict
- client_auth_keys = client_mock_data.keys()
- check_keys = {
- "official": bool,
- "origin": bool,
- "client_id": str
- }
- for k,v in check_keys.items():
- assert k in client_auth_keys
- assert type(client_mock_data[k]) == v
- else:
- client_auth = []
- # The mocking steps for the StepConnector instance
- hook_steps = {}
- linked_auth = {}
- new_key = lambda: max(hook_steps.keys())+1 if hook_steps else 1
- # Mock out hooks.user_auth
- if client_auth:
- for client in client_auth:
- if client["origin"]:
- client_id_field = "origin_client_id"
- client_secret_field = "origin_client_secret"
- else:
- client_id_field = "client_id"
- client_secret_field = "client_secret"
- client_secret = "client-secret".encode('utf-8')
- signed_client_secret = signer.sign(client_secret).decode('utf-8')
- # Generate a hash of the client secret
- client_secret_hash = bcrypt.hashpw(client_secret, bcrypt.gensalt()).decode('utf-8')
- linked_auth.update({
- client_id_field: client["client_id"],
- client_secret_field: signed_client_secret
- })
- raw_client_attrs = {
- "official": client["official"],
- "client_secret": client_secret_hash,
- "client_id": client["client_id"]
- }
- for k,v in client.items():
- if k not in raw_client_attrs.keys():
- raw_client_attrs.update({k:v})
- def client_attrs(x): return [raw_client_attrs]
- # If the client is official, optionally add it twice, so the official hook will also pass
- hook_steps.update({new_key(): client_attrs})
- if client["official"]:
- if client["mock_official"]:
- hook_steps.update({new_key(): client_attrs})
- if user_auth:
- # Password is hash of 'tachi'
- def user_exists(x): return [{"username": "holden",
- "password": "$2b$12$ICD1Tzv2oFBWXLphBEhIO.PKm3VxxJxSPTCkMHMAQUPwei.IOMLJS"}]
- linked_auth.update({
- "username": "holden",
- "password": "tachi"
- })
- hook_steps.update({new_key(): user_exists})
- if client_user_auth:
- # Generate an access token
- # The encrypted value of token
- def access_token_exists(x):
- return [{"access_token": "$2b$12$g59RaPDZRoIMV/cpnaElOOqi37vPCeeyxe5GQX7gDpLPhRZZ3vsYG"}]
- signed_token = signer.sign("token".encode('utf-8'))
- linked_auth.update({"access_token": signed_token})
- hook_steps.update({new_key(): access_token_exists})
- if session_auth:
- # Generate a random session id for this mock, so I don't have to worry about removing mocks from
- # the sessions.sessions dict after the scope of the test is finished
- session_id = str(uuid.uuid4())
- # Sign the session id
- signed_session_id = signer.sign(session_id.encode('utf-8'))
- linked_auth.update({"session_id": signed_session_id})
- # Create a mock session instance that provides the same keys as it
- session_instance_mock = MagicMock()
- session_instance_mock.username = "holden"
- session_instance_mock.client = "rocinate"
- if not callable(session_command):
- s = session_command
- def session_command(x):
- return s
- session_instance_mock.command = MagicMock(side_effect=session_command)
- # Check whether the client and user also need to be explicitly mocked in the auth
- if not client_auth:
- linked_auth.update({"client_id": "rocinate"})
- if not user_auth:
- linked_auth.update({"username": "holden"})
- # Insert the session into the sessions.sessions dict
- sessions.sessions.update({session_id: session_instance_mock})
- connector_instance = StepConnector(hook_steps)
- return (connector_instance, linked_auth)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement