class SetOrgMiddlewareTest(DashTest): def setUp(self): super(SetOrgMiddlewareTest, self).setUp() self.middleware = SetOrgMiddleware() self.request = Mock(spec=HttpRequest) self.request.user = User.objects.get(pk=-1) self.request.path = '/' self.request.get_host.return_value="ureport.io" self.request.META = dict(HTTP_HOST=None) def test_process_request_without_org(self): response = self.middleware.process_request(self.request) self.assertEqual(response.template_name, 'public/org_chooser.haml') self.assertFalse(response.context_data['orgs']) def test_process_request_with_org(self): ug_org = self.create_org('uganda', self.admin) ug_dash_url = ug_org.subdomain + ".ureport.io" self.request.get_host.return_value=ug_dash_url response = self.middleware.process_request(self.request) self.assertEqual(response, None) self.assertEqual(self.request.org, ug_org) self.request.user = self.admin response = self.middleware.process_request(self.request) self.assertEqual(response, None) self.assertEqual(self.request.org, ug_org) self.assertEquals(self.request.user.get_org(), ug_org) # test invalid subdomain wrong_subdomain_url = "blabla.ureport.io" self.request.get_host.return_value=wrong_subdomain_url response = self.middleware.process_request(self.request) self.assertEqual(response.template_name, 'public/org_chooser.haml') self.assertEquals(len(response.context_data['orgs']), 1) self.assertEquals(response.context_data['orgs'][0], ug_org) self.assertEqual(self.request.org, None) self.assertEquals(self.request.user.get_org(), None) rw_org = self.create_org('rwanda', self.admin) wrong_subdomain_url = "blabla.ureport.io" self.request.get_host.return_value=wrong_subdomain_url response = self.middleware.process_request(self.request) self.assertEqual(response.template_name, 'public/org_chooser.haml') self.assertEquals(len(response.context_data['orgs']), 2) self.assertTrue(rw_org in response.context_data['orgs']) self.assertTrue(ug_org in response.context_data['orgs'])
class SetOrgMiddlewareTest(DashTest): def setUp(self): super(SetOrgMiddlewareTest, self).setUp() self.middleware = SetOrgMiddleware() self.request = Mock(spec=HttpRequest) self.request.user = User.objects.get(pk=-1) self.request.path = '/' self.request.get_host.return_value="ureport.io" self.request.META = dict(HTTP_HOST=None) def test_process_request_without_org(self): response = self.middleware.process_request(self.request) self.assertEqual(response, None) self.assertEqual(self.request.org, None) def test_process_request_with_org(self): ug_org = self.create_org('uganda', self.admin) ug_dash_url = ug_org.subdomain + ".ureport.io" self.request.get_host.return_value = ug_dash_url response = self.middleware.process_request(self.request) self.assertEqual(response, None) self.assertEqual(self.request.org, ug_org) self.request.user = self.admin response = self.middleware.process_request(self.request) self.assertEqual(response, None) self.assertEqual(self.request.org, ug_org) self.assertEquals(self.request.user.get_org(), ug_org) # test invalid subdomain wrong_subdomain_url = "blabla.ureport.io" self.request.get_host.return_value=wrong_subdomain_url response = self.middleware.process_request(self.request) self.assertEqual(response, None) self.assertEqual(self.request.org, None) def test_process_view(self): with patch('django.core.urlresolvers.ResolverMatch') as resolver_mock: resolver_mock.url_name.return_value = "public.index" self.request.resolver_match = resolver_mock ug_org = self.create_org('uganda', self.admin) ug_dash_url = ug_org.subdomain + ".ureport.io" self.request.get_host.return_value=ug_dash_url # test invalid subdomain wrong_subdomain_url = "blabla.ureport.io" self.request.get_host.return_value=wrong_subdomain_url self.request.org = None response = self.middleware.process_view(self.request, IndexView.as_view(), [], dict()) self.assertEquals(response.status_code, 302) self.assertEquals(response.url, reverse(settings.SITE_CHOOSER_URL_NAME)) self.assertEqual(self.request.org, None) self.assertEquals(self.request.user.get_org(), None) rw_org = self.create_org('rwanda', self.admin) wrong_subdomain_url = "blabla.ureport.io" self.request.get_host.return_value=wrong_subdomain_url response = self.middleware.process_view(self.request, IndexView.as_view(), [], dict()) self.assertEquals(response.status_code, 302) self.assertEquals(response.url, reverse(settings.SITE_CHOOSER_URL_NAME))
class SetOrgMiddlewareTest(UreportTest): def setUp(self): super(SetOrgMiddlewareTest, self).setUp() self.middleware = SetOrgMiddleware() self.request = Mock(spec=HttpRequest) self.request.user = self.anon self.request.path = "/" self.request.get_host.return_value = "ureport.io" self.request.META = dict(HTTP_HOST=None) def test_process_request_without_org(self): response = self.middleware.process_request(self.request) self.assertEqual(response, None) self.assertEqual(self.request.org, None) def test_process_request_with_org(self): ug_org = self.create_org("uganda", pytz.timezone("Africa/Kampala"), self.admin) ug_dash_url = ug_org.subdomain + ".ureport.io" self.request.get_host.return_value = ug_dash_url response = self.middleware.process_request(self.request) self.assertEqual(response, None) self.assertEqual(self.request.org, ug_org) self.request.user = self.admin response = self.middleware.process_request(self.request) self.assertEqual(response, None) self.assertEqual(self.request.org, ug_org) self.assertEqual(self.request.user.get_org(), ug_org) # test invalid subdomain wrong_subdomain_url = "blabla.ureport.io" self.request.get_host.return_value = wrong_subdomain_url response = self.middleware.process_request(self.request) self.assertEqual(response, None) self.assertEqual(self.request.org, None) def test_process_view(self): with patch("django.urls.ResolverMatch") as resolver_mock: resolver_mock.url_name.return_value = "public.index" self.request.resolver_match = resolver_mock ug_org = self.uganda ug_dash_url = ug_org.subdomain + ".ureport.io" self.request.get_host.return_value = ug_dash_url # test invalid subdomain wrong_subdomain_url = "blabla.ureport.io" self.request.get_host.return_value = wrong_subdomain_url self.request.org = None response = self.middleware.process_view(self.request, IndexView.as_view(), [], dict()) self.assertEqual(response.status_code, 302) self.assertEqual(response.url, reverse(settings.SITE_CHOOSER_URL_NAME)) self.assertEqual(self.request.org, None) self.assertEqual(self.request.user.get_org(), None) self.create_org("rwanda", pytz.timezone("Africa/Kigali"), self.admin) wrong_subdomain_url = "blabla.ureport.io" self.request.get_host.return_value = wrong_subdomain_url response = self.middleware.process_view(self.request, IndexView.as_view(), [], dict()) self.assertEqual(response.status_code, 302) self.assertEqual(response.url, reverse(settings.SITE_CHOOSER_URL_NAME))