# 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


from django.contrib.auth import get_user_model
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
from organizations.forms import OrganizationAddForm
from organizations.forms import OrganizationForm
from organizations.forms import OrganizationUserAddForm
from organizations.forms import OrganizationUserForm
from organizations.models import Organization
from tests.utils import request_factory_login

    def setUp(self):
        self.factory = RequestFactory()

    def test_expected_valid_data_validates(self):
        """Test our happy path"""
        request = self.factory.request()
        form = OrganizationAddForm(
            request,
            data={"slug": "new_org", "name": "New Org", "email": "cthulu@oldgods.org"},
        )
        self.assertTrue(form.is_valid())

    def test_add_organization_for_existing_user(self):
        user = User.objects.create_user(
            "timmy", password="ajsdkfa3", email="timmy@whoa.com"
        )
        request = self.factory.request()
        form = OrganizationAddForm(
            request, data={"slug": "new_org", "name": "New Org", "email": user.email}
        )
        self.assertTrue(form.is_valid())
        new_org = form.save()
        self.assertTrue(new_org.is_active)
        self.assertEqual(new_org.name, "New Org")

    def test_add_organization_for_new_user(self):
        user = User.objects.create_user(
            "timmy", password="ajsdkfa3", email="timmy@whoa.com"
        )

# 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/test_forms.py
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
from organizations.forms import OrganizationAddForm
from organizations.forms import OrganizationForm
from organizations.forms import OrganizationUserAddForm
from organizations.forms import OrganizationUserForm
from organizations.models import Organization
from tests.utils import request_factory_login


    def setUp(self):
        self.factory = RequestFactory()

    def test_expected_valid_data_validates(self):
        """Test our happy path"""
        request = self.factory.request()
        form = OrganizationAddForm(
            request,
            data={"slug": "new_org", "name": "New Org", "email": "cthulu@oldgods.org"},
        )
        self.assertTrue(form.is_valid())

    def test_add_organization_for_existing_user(self):
        user = User.objects.create_user(
            "timmy", password="ajsdkfa3", email="timmy@whoa.com"
        )
        request = self.factory.request()
        form = OrganizationAddForm(
            request, data={"slug": "new_org", "name": "New Org", "email": user.email}
        )
        self.assertTrue(form.is_valid())
        new_org = form.save()
        self.assertTrue(new_org.is_active)
        self.assertEqual(new_org.name, "New Org")

    def test_add_organization_for_new_user(self):
        user = User.objects.create_user(
            "timmy", password="ajsdkfa3", email="timmy@whoa.com"
        )

        request = request_factory_login(self.factory, user)