Created
December 4, 2017 14:05
-
-
Save mrchrisadams/0e8e039058228e744d42d6278a63b906 to your computer and use it in GitHub Desktop.
how should I mock and patch this?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import environ as env | |
import logging | |
from auth0.v3.management import Auth0 | |
logger = logging.getLogger() | |
class SomeAuthWrapper(): | |
""" | |
Wraps the API calls we need to make to administer users | |
""" | |
def __init__(self, domain=None, api_token=None): | |
if domain is None: | |
self.domain = env.get('AUTH0_DOMAIN') | |
else: | |
self.domain = domain | |
if api_token is None: | |
self.mgmt_api_token = env.get('AUTH0_TOKEN') | |
else: | |
self.mgmt_api_token = api_token | |
self.auth0 = Auth0(self.domain, self.mgmt_api_token) | |
def post_list_to_auth0(self, email_list): | |
""" | |
Take a list of emails, then post to an auth0 endpoint with the correct | |
connection | |
""" | |
results = [] | |
for email in email_list: | |
logger.info("Creating account for {}".format(email)) | |
new_user = { | |
"connection": "email", | |
"email": email, | |
"email_verified": True, | |
"verify_email": False, | |
} | |
try: | |
results.append(self.auth0.users.create(new_user)) | |
except: | |
logger.exception("Import issue for {}".format(email)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from some_auth0 import SomeAuthWrapper | |
import pytest | |
def auth_creates_user_with_auth0(monkeypatch): | |
# define mock response within this function, we're not using anywhere else | |
def mock_response(): | |
# this is the response I get back normallly | |
return { | |
'email': 'chris+testing_a_thing@productscience.co.uk', | |
'email_verified': True, | |
'user_id': 'email|longstring', | |
'picture': 'https://s.gravatar.com/avatar/26eb58f29fe8babb18914b743331ff3b?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Fch.png', | |
'identities': [{'connection': 'email', | |
'user_id': 'long_ass_hash', | |
'provider': 'email', | |
'isSocial': False} | |
], | |
'updated_at': '2017-12-04T13:29:20.907Z', | |
'created_at': '2017-12-04T13:20:39.270Z' | |
} | |
# I don't want to call the API on each test - just check that it is called, with the right args | |
monkeypatch.setattr(cl8_auth.auth0.users,'new_user', mock_response) | |
cl8_auth = ConstellateAuth() | |
cl8.post_list_to_auth0(['chris+testing_a_thing@productscience.co.uk']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment