def test_notification_settings_view_uses_notification_settings_form(self): user = UserFactory.build(id=1) organization = create_fake_organization(id=2, slug="test") request = RequestFactory().get("/") request.user = user context = UserNotificationSettings.as_view()(request, org_slug=organization.slug).context_data self.assertIsInstance(context["form"], NotificationSettingsForm)
def test_notification_settings_view_uses_notification_settings_form(self): user = UserFactory.build(id=1) organization = create_fake_organization(id=2, slug='test') request = RequestFactory().get('/') request.user = user context = UserNotificationSettings.as_view()( request, org_slug=organization.slug).context_data self.assertIsInstance(context['form'], NotificationSettingsForm)
def test_posting_valid_data_saves_settings(self, settings_obj): organization = create_fake_organization(id=2, slug='test') request = RequestFactory().post( reverse('notification_settings', args=[organization.slug]), {'notification_level': unicode(NO_NOTIFICATIONS)}) user = UserFactory.build(id=1) request.user = user # This patch depends on the UsertNotificationSettings.model patch # It needs to return the object created by that patch, which is passed # in as a parameter. # The only way I've found to handle the dependency is to do this patch # here with patch('publicweb.views.UserNotificationSettings.model.objects', get=lambda organization, user: settings_obj): UserNotificationSettings.as_view()(request, org_slug=organization.slug) self.assertTrue(settings_obj.save.called)
def test_notification_settings_view_requires_login(self): request = RequestFactory().get("/") user = AnonymousUser() organization = create_fake_organization(id=2) request.user = user response = UserNotificationSettings.as_view()(request, organization=organization.id) self.assertIsInstance(response, HttpResponseRedirect)
def test_notification_settings_view_requires_login(self): request = RequestFactory().get('/') user = AnonymousUser() organization = create_fake_organization(id=2) request.user = user response = UserNotificationSettings.as_view()( request, organization=organization.id) self.assertIsInstance(response, HttpResponseRedirect)
def test_cancel_doesnt_save_settings(self, settings_obj): user = UserFactory.build(id=1) organization = create_fake_organization(id=2, slug="test") request = RequestFactory().post( reverse("notification_settings", args=[organization.id]), {"notification_level": unicode(NO_NOTIFICATIONS), "submit": "Cancel"}, ) request.user = user # This patch depends on the UsertNotificationSettings.model patch # It needs to return the object created by that patch, which is passed # in as a parameter. # The only way I've found to handle the dependency is to do this patch # here with patch( "publicweb.views.UserNotificationSettings.model.objects", get=lambda organization, user: settings_obj ): UserNotificationSettings.as_view()(request, org_slug=organization.slug) self.assertFalse(settings_obj.save.called)
def test_posting_invalid_data_returns_form_with_errors(self, settings_obj): user = UserFactory.build(id=1) organization = create_fake_organization(id=2, slug="test") request = RequestFactory().post(reverse("notification_settings", args=[organization.id])) request.user = user # This patch depends on the UsertNotificationSettings.model patch # It needs to return the object created by that patch, which is passed # in as a parameter. # The only way I've found to handle the dependency is to do this patch # here with patch( "publicweb.views.UserNotificationSettings.model.objects", get=lambda organization, user: settings_obj ): response = UserNotificationSettings.as_view()(request, org_slug=organization.slug) self.assertIn("form", response.context_data) self.assertTrue(response.context_data["form"].errors)
def test_posting_invalid_data_returns_form_with_errors(self, settings_obj): user = UserFactory.build(id=1) organization = create_fake_organization(id=2, slug='test') request = RequestFactory().post( reverse('notification_settings', args=[organization.id])) request.user = user # This patch depends on the UsertNotificationSettings.model patch # It needs to return the object created by that patch, which is passed # in as a parameter. # The only way I've found to handle the dependency is to do this patch # here with patch('publicweb.views.UserNotificationSettings.model.objects', get=lambda organization, user: settings_obj): response = UserNotificationSettings.as_view()( request, org_slug=organization.slug) self.assertIn('form', response.context_data) self.assertTrue(response.context_data['form'].errors)