from django.contrib.auth.models import AnonymousUser

from organizations.tests.base import OrganizationTestCase
from organizations.utils import active_organization
from organizations.models import (
    ORGANIZATION_SESSION_KEY, OrganizationUserRelation)


class TestUtils(OrganizationTestCase):

    def test_active_organization(self):
        request = self.mk_request('get', '/')
        request.user = AnonymousUser()
        self.assertIs(active_organization(request), None)

        user = self.mk_user()
        request.user = user
        self.assertIs(active_organization(request), None)

        organization = self.mk_organization()
        request.session[ORGANIZATION_SESSION_KEY] = organization.pk
        self.assertIs(active_organization(request), None)

        OrganizationUserRelation.objects.create(
            user=user, organization=organization)
        self.assertEqual(active_organization(request), organization)

@@ -47,16 +47,85 @@
 usUser%0A%0A
+from mock import patch%0A%0Afrom organizations import context_processors%0A
 from org
@@ -989,20 +989,1603 @@
 est), organization)%0A
+%0A    def test_org_context_processor(self):%0A        user = self.mk_user()%0A        organization = self.mk_organization(users=%5Buser%5D)%0A        request = self.mk_request('get', '/')%0A        request.user = user%0A        request.session%5BORGANIZATION_SESSION_KEY%5D = organization.pk%0A        context = context_processors.org(request)%0A        self.assertEqual(context%5B'active_organization'%5D, organization)%0A        self.assertEqual(set(context%5B'organizations'%5D), set(%5Borganization%5D))%0A        self.assertTrue(context%5B'is_active_organization_admin'%5D)%0A%0A        with patch('organizations.context_processors.active_organization',%0A                   return_value=None):%0A            context = context_processors.org(request)%0A            self.assertEqual(context%5B'active_organization'%5D, None)%0A            self.assertEqual(%0A                set(context%5B'organizations'%5D), set(%5Borganization%5D))%0A            self.assertFalse(context%5B'is_active_organization_admin'%5D)%0A%0A        organization.organizationuserrelation_set.filter(%0A            user=user).update(is_admin=False)%0A        context = context_processors.org(request)%0A        self.assertEqual(context%5B'active_organization'%5D, organization)%0A        self.assertEqual(set(context%5B'organizations'%5D), set(%5Borganization%5D))%0A        self.assertFalse(context%5B'is_active_organization_admin'%5D)%0A%0A        request.user = AnonymousUser()%0A        context = context_processors.org(request)%0A        self.assertEqual(context%5B'active_organization'%5D, None)%0A        self.assertEqual(context%5B'organizations'%5D, %5B%5D)%0A        self.assertFalse(context%5B'is_active_organization_admin'%5D)%0A

test org context processor