import re
from importlib import import_module

from django.contrib.admindocs.views import extract_views_from_urlpatterns, simplify_regex
from django.conf import settings


def get_service_endpoint_urls(urlconfig=None, default_value='1'):
    """
    This function finds all endpoint urls in a service.

    Args:
        urlconfig: A django url config module to use. Defaults to settings.ROOT_URLCONF
        default_value: A string value to replace all url parameters with

    Returns:
        A list of urls with path parameters (i.e. <pk>) replaced with default_value
    """
    if not urlconfig:
        urlconfig = getattr(settings, 'ROOT_URLCONF')

    try:
        urlconfig_mod = import_module(urlconfig)
    except Exception as ex:
        raise Exception(
            "Unable to import url config module. Url Config: {0}. Message: {1}".format(urlconfig, ex.message))

    extracted_views = extract_views_from_urlpatterns(urlconfig_mod.urlpatterns)
    views_regex_url_patterns = [item[1] for item in extracted_views]
    simplified_regex_url_patterns = [simplify_regex(pattern) for pattern in views_regex_url_patterns]

    # Strip out urls we don't need to test.
    result_urls = []
    pattern = re.compile(r'<\w+>')

    for url in simplified_regex_url_patterns:
        if url.find('<format>') != -1:
            continue

        if url == u'/':
            continue

        if url == u'/health':
            continue

        parameters = pattern.findall(url)
        for param in parameters:
            url = url.replace(param, default_value)

        result_urls.append(url)

    return result_urls

@@ -164,16 +164,138 @@
 ttings%0A%0A
+from .authentication import User%0Afrom .utils import jwt_payload_handler, service_jwt_payload_handler, jwt_encode_handler%0A%0A
 %0Adef get
@@ -1737,8 +1737,1224 @@
 lt_urls%0A
+%0A%0Aclass AuthenticationMixin:%0A    def create_user(self, roles, user_id):%0A        return User(roles=roles, pk=user_id)%0A%0A    def create_user_token(self, user):%0A        payload = jwt_payload_handler(user)%0A        token = %22JWT %7B%7D%22.format(jwt_encode_handler(payload))%0A        return token%0A%0A    def create_service_token(self, service_name):%0A        payload = service_jwt_payload_handler(service_name)%0A        token = %22JWT %7B%7D%22.format(jwt_encode_handler(payload))%0A        return token%0A%0A    def get_staff_token(self, user_id=1):%0A        staff_user = self.create_user(%5B'user', 'staff'%5D, user_id)%0A        return self.create_user_token(staff_user)%0A%0A    def get_user_token(self, user_id=1):%0A        user = self.create_user(%5B'user'%5D, user_id)%0A        return self.create_user_token(user)%0A%0A    def get_guest_token(self, user_id=1):%0A        user = self.create_user(%5B'user'%5D, user_id)%0A        setattr(user, 'isGuest', True)%0A        return self.create_user_token(user)%0A%0A    def get_service_token(self, service_name='Test'):%0A        return self.create_service_token(service_name)%0A%0A    def authorize_as(self, user_type):%0A        token = getattr(self, 'get_%25s_token' %25 user_type)()%0A        self.client.credentials(HTTP_AUTHORIZATION=token)

Add AuthenticationMixin to help authentication in other services.