# Path: tests/utils.py
# def request_factory_login(factory, user=None, method="request", **kwargs):
#     """Based on this gist: https://gist.github.com/964472"""
#     # engine = import_module(settings.SESSION_ENGINE)
#     request = getattr(factory, method)(**kwargs)
#     # request.session = engine.SessionStore()
#     # request.session[SESSION_KEY] = user.pk
#     request.user = user or AnonymousUser()
#     return request


import uuid
import pytest
from django.contrib.auth.models import User
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.core import mail
from django.http import Http404
from django.http import QueryDict
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
from django.urls import reverse
from organizations.backends.defaults import BaseBackend
from organizations.backends.defaults import InvitationBackend
from organizations.backends.defaults import RegistrationBackend
from organizations.models import Organization
from tests.utils import request_factory_login
        from django.conf import settings
        from organizations.backends import invitation_backend
        from organizations.backends import registration_backend
        InvitationBackend().send_notification(
            self.user, domain="example.com", organization=org, sender=self.pending_user
        )
        self.assertEqual(1, len(mail.outbox))
        self.assertEqual(mail.outbox[0].subject, "You've been added to an organization")


@override_settings(USE_TZ=True)
class TestRegistration(TestCase):

    fixtures = ["users.json", "orgs.json"]

    def setUp(self):
        mail.outbox = []
        self.factory = RequestFactory()
        self.tokenizer = PasswordResetTokenGenerator()
        self.user = User.objects.get(username="krist")
        self.pending_user = User.objects.create_user(
            username="theresa", email="t@example.com", password="test"
        )
        self.pending_user.is_active = False
        self.pending_user.save()

    def test_backend_definition(self):

        self.assertTrue(isinstance(registration_backend(), RegistrationBackend))

    def test_register_authenticated(self):
        """Ensure an already authenticated user is redirected"""
        backend = RegistrationBackend()

# Path: tests/utils.py
# def request_factory_login(factory, user=None, method="request", **kwargs):
#     """Based on this gist: https://gist.github.com/964472"""
#     # engine = import_module(settings.SESSION_ENGINE)
#     request = getattr(factory, method)(**kwargs)
#     # request.session = engine.SessionStore()
#     # request.session[SESSION_KEY] = user.pk
#     request.user = user or AnonymousUser()
#     return request


# Path: tests/backends/test_default_backend.py
import uuid
import pytest
from django.contrib.auth.models import User
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.core import mail
from django.http import Http404
from django.http import QueryDict
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
from django.urls import reverse
from organizations.backends.defaults import BaseBackend
from organizations.backends.defaults import InvitationBackend
from organizations.backends.defaults import RegistrationBackend
from organizations.models import Organization
from tests.utils import request_factory_login
        from django.conf import settings
        from organizations.backends import invitation_backend
        from organizations.backends import registration_backend

        InvitationBackend().send_notification(
            self.user, domain="example.com", organization=org, sender=self.pending_user
        )
        self.assertEqual(1, len(mail.outbox))
        self.assertEqual(mail.outbox[0].subject, "You've been added to an organization")


@override_settings(USE_TZ=True)
class TestRegistration(TestCase):

    fixtures = ["users.json", "orgs.json"]

    def setUp(self):
        mail.outbox = []
        self.factory = RequestFactory()
        self.tokenizer = PasswordResetTokenGenerator()
        self.user = User.objects.get(username="krist")
        self.pending_user = User.objects.create_user(
            username="theresa", email="t@example.com", password="test"
        )
        self.pending_user.is_active = False
        self.pending_user.save()

    def test_backend_definition(self):

        self.assertTrue(isinstance(registration_backend(), RegistrationBackend))

    def test_register_authenticated(self):
        """Ensure an already authenticated user is redirected"""
        backend = RegistrationBackend()

        request = request_factory_login(self.factory, self.user)