Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.24 KB | None | 0 0
  1. def standard_hooks_mock(user_auth=False, client_auth=None, client_user_auth=False, session_auth=None, session_command=None):
  2.     """
  3.    Mock out standard hooks for the scope of the test
  4.  
  5.    :param user_auth: A bool defining whether to automatically pass the user auth tests
  6.    :param client_auth: A list of dictionaries. Each dictionary should contain the `official` and `origin` keys,
  7.                        defining respectively whether the client should be denoted as official, and whether the client
  8.                        is the origin of the request.
  9.    :param session_auth: A bool defining whether to automatically pass the session based authentication tests
  10.    :return (connector_instance, linked_auth):
  11.            Connector instance is an instance of the StepConnector class defining the appropriate session mocks for the
  12.            hooks. Linked auth is the authentication data that needs to be appended to the fake request
  13.    """
  14.     if client_auth:
  15.         assert type(client_auth) == list
  16.         for client_mock_data in client_auth:
  17.             assert type(client_mock_data) == dict
  18.             client_auth_keys = client_mock_data.keys()
  19.             check_keys = {
  20.                 "official": bool,
  21.                 "origin": bool,
  22.                 "client_id": str
  23.             }
  24.             for k,v in check_keys.items():
  25.                 assert k in client_auth_keys
  26.                 assert type(client_mock_data[k]) == v
  27.     else:
  28.         client_auth = []
  29.     # The mocking steps for the StepConnector instance
  30.     hook_steps = {}
  31.     linked_auth = {}
  32.     new_key = lambda: max(hook_steps.keys())+1 if hook_steps else 1
  33.     # Mock out hooks.user_auth
  34.     if client_auth:
  35.         for client in client_auth:
  36.             if client["origin"]:
  37.                 client_id_field = "origin_client_id"
  38.                 client_secret_field = "origin_client_secret"
  39.             else:
  40.                 client_id_field = "client_id"
  41.                 client_secret_field = "client_secret"
  42.             client_secret = "client-secret".encode('utf-8')
  43.             signed_client_secret = signer.sign(client_secret).decode('utf-8')
  44.             # Generate a hash of the client secret
  45.             client_secret_hash = bcrypt.hashpw(client_secret, bcrypt.gensalt()).decode('utf-8')
  46.             linked_auth.update({
  47.                 client_id_field: client["client_id"],
  48.                 client_secret_field: signed_client_secret
  49.             })
  50.             raw_client_attrs = {
  51.                 "official": client["official"],
  52.                 "client_secret": client_secret_hash,
  53.                 "client_id": client["client_id"]
  54.             }
  55.             for k,v in client.items():
  56.                 if k not in raw_client_attrs.keys():
  57.                     raw_client_attrs.update({k:v})
  58.             def client_attrs(x): return [raw_client_attrs]
  59.             # If the client is official, optionally add it twice, so the official hook will also pass
  60.             hook_steps.update({new_key(): client_attrs})
  61.             if client["official"]:
  62.                 if client["mock_official"]:
  63.                     hook_steps.update({new_key(): client_attrs})
  64.     if user_auth:
  65.         # Password is hash of 'tachi'
  66.         def user_exists(x): return [{"username": "holden",
  67.                                 "password": "$2b$12$ICD1Tzv2oFBWXLphBEhIO.PKm3VxxJxSPTCkMHMAQUPwei.IOMLJS"}]
  68.         linked_auth.update({
  69.             "username": "holden",
  70.             "password": "tachi"
  71.         })
  72.         hook_steps.update({new_key(): user_exists})
  73.     if client_user_auth:
  74.         # Generate an access token
  75.         # The encrypted value of token
  76.         def access_token_exists(x):
  77.             return [{"access_token": "$2b$12$g59RaPDZRoIMV/cpnaElOOqi37vPCeeyxe5GQX7gDpLPhRZZ3vsYG"}]
  78.         signed_token = signer.sign("token".encode('utf-8'))
  79.         linked_auth.update({"access_token": signed_token})
  80.         hook_steps.update({new_key(): access_token_exists})
  81.  
  82.     if session_auth:
  83.         # Generate a random session id for this mock, so I don't have to worry about removing mocks from
  84.         # the sessions.sessions dict after the scope of the test is finished
  85.         session_id = str(uuid.uuid4())
  86.         # Sign the session id
  87.         signed_session_id = signer.sign(session_id.encode('utf-8'))
  88.         linked_auth.update({"session_id": signed_session_id})
  89.         # Create a mock session instance that provides the same keys as it
  90.         session_instance_mock = MagicMock()
  91.         session_instance_mock.username = "holden"
  92.         session_instance_mock.client = "rocinate"
  93.         if not callable(session_command):
  94.             s = session_command
  95.  
  96.             def session_command(x):
  97.                 return s
  98.         session_instance_mock.command = MagicMock(side_effect=session_command)
  99.         # Check whether the client and user also need to be explicitly mocked in the auth
  100.         if not client_auth:
  101.             linked_auth.update({"client_id": "rocinate"})
  102.         if not user_auth:
  103.             linked_auth.update({"username": "holden"})
  104.         # Insert the session into the sessions.sessions dict
  105.         sessions.sessions.update({session_id: session_instance_mock})
  106.     connector_instance = StepConnector(hook_steps)
  107.     return (connector_instance, linked_auth)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement