Skip to content

Instantly share code, notes, and snippets.

@geeky-sh
Last active December 30, 2019 06:18
Show Gist options
  • Save geeky-sh/5ef31386e9d0d69a938f9bc635c0bfb9 to your computer and use it in GitHub Desktop.
Save geeky-sh/5ef31386e9d0d69a938f9bc635c0bfb9 to your computer and use it in GitHub Desktop.
python function goodies
# below functions are used to create authenticated requests that is
# helpful for testing rest api endpoints, the example usage
# is provided in the description of the function
def get_auth_header(access):
"""
This is primarily used in tests to create authenticated request
using the access token provided
"""
return {'HTTP_AUTHORIZATION': 'Bearer {}'.format(access)}
def get_authenticated_client(meth, access, url, data):
"""
This is primarily used in tests to create authenticated client
using the access token provided.
Example usage:
get_authenticated_client(
client.post, 'fdfkljdkl43444', '/api/backoffice/employee/', {'email': 'xyr@gmail.com'})
"""
return meth(url, data=data, **get_auth_header(access))
#
# below function is used to login in the rest api using the django test client
def get_login_response(client, url, email, password):
return client.post(url, data={'email': email, 'password': password})
def get_access_token(client, url, email, password):
response = get_login_response(client, url, email, password)
return response.json()['access']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment