class AnonymousCohortMiddlewareTest(TestCase):

    def setUp(self):
        super(AnonymousCohortMiddlewareTest, self).setUp()
        self.acm = AnonymousCohortMiddleware()
        self.request = Mock()

    def test_anonymous_no_cohort(self):
        self.request.user = AnonymousUser()
        self.assertFalse(hasattr(self.request.user, 'cohort'))
        self.assertIsNone(self.acm.process_request(self.request))
        self.assertTrue(hasattr(self.request.user, 'cohort'))
        self.assertIsNone(self.request.user.cohort)

    @override_settings(ADELAIDEX_LTI={'LINK_TEXT': 'Hi there'})
    def test_anonymous_default_cohort(self):
        self.request.user = AnonymousUser()
        self.assertFalse(hasattr(self.request.user, 'cohort'))
        self.assertIsNone(self.acm.process_request(self.request))
        self.assertTrue(hasattr(self.request.user, 'cohort'))
        self.assertIsNotNone(self.request.user.cohort)
        self.assertEquals(self.request.user.cohort.title, 'Hi there')

    def test_authenticated_no_cohort(self):
        self.request.user = AnonymousUser()
        self.request.user.is_authenticated = lambda : True
        setattr(self.request.user, 'cohort', None)
        self.assertTrue(hasattr(self.request.user, 'cohort'))
        self.assertIsNone(self.acm.process_request(self.request))
        self.assertIsNone(self.request.user.cohort)

    def test_authenticated_own_cohort(self):
        self.request.user = AnonymousUser()
        self.request.user.is_authenticated = lambda : True
        own_cohort = Mock()
        setattr(self.request.user, 'cohort', own_cohort)
        self.assertTrue(hasattr(self.request.user, 'cohort'))
        self.assertIsNone(self.acm.process_request(self.request))
        self.assertIsNotNone(self.request.user.cohort)
        self.assertEquals(self.request.user.cohort, own_cohort)

    @override_settings(ADELAIDEX_LTI={'LINK_TEXT': 'Hi there'})
    def test_authenticated_default_cohort(self):
        self.request.user = AnonymousUser()
        self.request.user.is_authenticated = lambda : True
        setattr(self.request.user, 'cohort', None)
        self.assertTrue(hasattr(self.request.user, 'cohort'))
        self.assertIsNone(self.acm.process_request(self.request))
        self.assertIsNone(self.request.user.cohort)

    @override_settings(ADELAIDEX_LTI={'LINK_TEXT': 'Hi there'})
    def test_authenticated_default_own_cohort(self):
        self.request.user = AnonymousUser()
        self.request.user.is_authenticated = lambda : True
        own_cohort = Mock()
        setattr(self.request.user, 'cohort', own_cohort)
        self.assertTrue(hasattr(self.request.user, 'cohort'))
        self.assertIsNone(self.acm.process_request(self.request))
        self.assertIsNotNone(self.request.user.cohort)
        self.assertEquals(self.request.user.cohort, own_cohort)
 def setUp(self):
     super(AnonymousCohortMiddlewareTest, self).setUp()
     self.acm = AnonymousCohortMiddleware()
     self.request = Mock()