def test_owner_required(self):
        request = RequestFactory().get('/fake-path')
        view = self.OwnerReqView(
            template_name='test_views.html',
            model=HikerDiaryEntry)
        view = setup_view(view, request, pk=self.diary.pk)

        # valid object, user not superuser
        self.assertTrue(view.check_owner(self.hiker1.hiker))
        self.assertFalse(view.check_owner(self.hiker2.hiker))

        # user is superuser and superuser_allowed
        self.user_no_profile.is_superuser = True
        self.assertTrue(view.check_owner(self.user_no_profile))

        # user is superuser but not owner, and not superuser_allowed
        view.superuser_allowed = False
        self.assertFalse(view.check_owner(self.user_no_profile))

        # non-existent object
        view = setup_view(view, request, pk=9999)
        self.assertFalse(view.check_owner(self.hiker1.hiker))
        self.assertFalse(view.check_owner(self.hiker2.hiker))

        # mixin used on view for model with no ownership field
        view = self.OwnerReqView(
            template_name='test_views.html',
            model=Region)
        view = setup_view(view, request, pk=self.region.pk)
        with self.assertRaises(AttributeError):
            view.check_owner(self.hiker1.hiker)
Beispiel #2
0
 def test_trailhead_create_view_get_form(self):
     view = TrailheadCreateView(template_name='test_views.html')
     view = setup_view(view, self.request,
                       co_region_slug=self.co_region.slug)
     form = view.get_form()
     self.assertTrue(form.fields['new_region'].required)
     self.assertFalse(form.fields['region'].required)
     view = setup_view(view, self.request, region_slug=self.region.slug,
                       co_region_slug=self.co_region.slug)
     form = view.get_form()
     self.assertEquals(self.region, form.initial['region'])
Beispiel #3
0
    def test_trailheads_map_queryset(self, mock_serializer):
        mock_serializer.return_value = {'serialized': True}
        region = RegionFactory()
        trailhead = TrailheadFactory(region=region)
        HikeFactory(trailhead=trailhead)
        request = RequestFactory().get('/fake-path')
        view = TrailheadMapListView(template_name='test_views.html')
        view = setup_view(view, request, region_name=region.name)

        qs = view.get_queryset()
        self.assertEquals(qs, mock_serializer.return_value)

        view1 = TrailheadMapListView(template_name='test_views.html')
        view1 = setup_view(view1, request, region_name='Not a Region')
Beispiel #4
0
    def test_search_by_hike_name_queryset(self, mock_serializer):
        mock_serializer.return_value = {'serialized': True}
        region = RegionFactory()
        trailhead = TrailheadFactory(region=region)
        HikeFactory(trailhead=trailhead, name='Test Hike')
        request = RequestFactory().get('/fake-path/?search_text=hike')
        view = SearchByHikeName(template_name='test_views.html')
        view = setup_view(view, request, region_name=region.name)

        qs = view.get_queryset()
        self.assertEquals(qs, mock_serializer.return_value)

        view1 = TrailheadMapListView(template_name='test_views.html')
        view1 = setup_view(view1, request, region_name='Not a Region')
Beispiel #5
0
 def test_hiker_delete_view_success_url(self):
     hiker = HikerFactory()
     diary = HikerDiaryEntryFactory(hiker=hiker)
     request = RequestFactory().get('/fake-path')
     view = HikerDeleteView(template_name='test_views.html',
                            model=HikerDiaryEntry)
     view = setup_view(view, request, user_slug=hiker.slug, slug=diary.slug)
     request.user = UserFactory()
     request.user.hiker = HikerFactory()
     with self.assertRaises(ImproperlyConfigured):
         view.get_success_url()
     view = HikerDeleteView(template_name='test_views.html',
                            model=HikerDiaryEntry,
                            success_url_name='hikers:profile')
     view = setup_view(view, request, user_slug=hiker.slug, slug=diary.slug)
     self.assertIn(hiker.slug, view.get_success_url())
    def test_hiker_access_methods(self, mock_redirect, mock_no_permission):
        mock_redirect.return_value = 'inactive redirect'
        mock_no_permission.return_value = 'no permission'
        request = RequestFactory().get('/fake-path')
        user = None
        view = self.HikerAccessView(template_name='test_views.html',
                                    model=Hiker,
                                    group_failure_path='group fail')
        view = setup_view(view, request)

        self.assertEquals(mock_redirect.return_value, view.handle_inactive())
        view.raise_exception = True
        with self.assertRaises(PermissionDenied):
            view.handle_inactive()

        self.assertTrue(view.check_owner(user))
        self.assertTrue(view.check_group(user))

        self.assertEquals(mock_no_permission.return_value,
                          view.access_failed(view.ownership_failure_path,
                                             request))

        mock_redirect.return_value = 'access failed redirect'
        self.assertEquals(mock_redirect.return_value,
                          view.access_failed(view.group_failure_path,
                                             request))
Beispiel #7
0
 def test_hike_home_redirect_url(self):
     CountryRegion.objects.create(region_name='pacific northwest',
                                  country_abbrev='us',
                                  slug='pacific-northwest-us')
     view = HikeHomeRedirectView()
     view = setup_view(view, self.request)
     self.assertIn('pacific-northwest', view.get_redirect_url())
Beispiel #8
0
 def test_upload_view_success_url_returns_image_model_url(self):
     image = Image.objects.create(title='title', image='image.jpg')
     upload_request = RequestFactory().get('/foo')
     upload_view = UploadView()
     upload_view.object = image
     view = setup_view(upload_view, upload_request)
     self.assertEqual(view.get_success_url(), image.get_absolute_url())
 def test_root_url_returns_dashboard_template(self):
     dashboard_request = RequestFactory().get('/')
     dashboard_view = DashboardView()
     view = setup_view(dashboard_view, dashboard_request)
     dashboard_view.object_list = []
     template_names = view.get_template_names()
     self.assertEqual(template_names, ['dashboard.html'])
Beispiel #10
0
 def test_diary_entry_view(self):
     view = HikerDiaryEntriesView(template_name='test_views.html')
     view = setup_view(view, self.request, user_slug=self.hiker.slug)
     HikerDiaryEntryFactory(hiker=self.hiker)
     HikerDiaryEntryFactory()
     self.assertQuerysetEqual(
         view.get_queryset(),
         map(repr, HikerDiaryEntry.objects.filter(hiker=self.hiker)))
Beispiel #11
0
 def test_photos_view(self):
     view = HikerPhotosView(template_name='test_views.html')
     view = setup_view(view, self.request, user_slug=self.hiker.slug)
     HikerPhotoFactory(hiker=self.hiker)
     HikerPhotoFactory()
     self.assertQuerysetEqual(
         view.get_queryset(),
         map(repr, HikerPhoto.objects.filter(hiker=self.hiker)))
Beispiel #12
0
 def test_hikers_hikes_view(self):
     view = HikerHikesView(template_name='test_views.html')
     view = setup_view(view, self.request, user_slug=self.hiker.slug)
     MyHikeFactory(hiker=self.hiker)
     MyHikeFactory()
     self.assertQuerysetEqual(
         view.get_queryset(),
         map(repr, MyHike.objects.filter(hiker=self.hiker)))
Beispiel #13
0
 def test_hike_create_view_get_form(self):
     view = HikeCreateView(template_name='test_views.html')
     view = setup_view(view, self.request, region_slug=self.region.slug,
                       trailhead_slug=self.trailhead.slug,
                       co_region_slug=self.co_region.slug)
     form = view.get_form()
     self.assertEquals(self.region.name, form.initial['region_name'])
     self.assertEquals(self.trailhead, form.initial['trailhead'])
Beispiel #14
0
    def test_profile_access(self):
        request = RequestFactory().get('/fake-path')
        request.user = self.user_no_profile
        view = self.ProfileAccessView(
            template_name='test_views.html',
            model=Hiker)
        view = setup_view(view, request, user_slug=self.hiker1.slug)

        # returns false since profile attempted does not belong to user
        self.assertFalse(view.check_owner(request.user))

        # test method with correct user
        self.assertTrue(view.check_owner(self.hiker1.hiker))

        # check method with non-existent profile
        view = setup_view(view, request, user_slug='not-a-user')
        self.assertFalse(view.check_owner(self.hiker1.hiker))
Beispiel #15
0
 def test_hiker_photo_delete_get_form(self):
     diary1 = HikerDiaryEntryFactory(hiker=self.hiker)
     hiker2 = HikerFactory()
     diary2 = HikerDiaryEntryFactory(hiker=hiker2)
     view = HikerPhotosUpdateView(template_name='test_views.html')
     view = setup_view(view, self.request, user_slug=self.hiker.slug)
     form = view.get_form()
     self.assertIn(diary1, form.fields['diary_entry'].queryset)
     self.assertNotIn(diary2, form.fields['diary_entry'].queryset)
Beispiel #16
0
    def test_trailhead_update_queryset(self, mock_queryset):
        mock_queryset.return_value = 'queryset'
        view = TrailheadUpdateView(template_name='test_views.html')
        view = setup_view(view, self.request, region_slug=self.region.slug,
                          trailhead_slug=self.trailhead.slug,
                          co_region_slug=self.co_region.slug)

        qs = view.get_queryset()
        self.assertEquals(mock_queryset.return_value, qs)
Beispiel #17
0
 def test_get(self, mock_get_object):
     request = RequestFactory().get('/fake-path')
     view = self.FormsetView(template_name='test_views.html',
                             model=TestParentModel,
                             form_class=TestParentForm,
                             formset_classes=(TestChildFormset,))
     view = setup_view(view, request, pk=1)
     view.get(request)
     mock_get_object.assert_called()
Beispiel #18
0
    def test_get_form_formset(self):
        request = RequestFactory().get('/fake-path')
        view = self.FormsetView(template_name='test_views.html',
                                model=TestParentModel,
                                form_class=TestParentForm,
                                formset_classes=(TestChildFormset,))
        view = setup_view(view, request)
        forms_dict = view.get_form_formsets()
        self.assertIn('form', forms_dict.keys())

        formsets = forms_dict['formsets']
        self.assertIsInstance(formsets[0], TestChildFormset)

        view1 = self.FormsetView(template_name='test_views.html',
                                 model=TestParentModel,
                                 form_class=TestParentForm)
        view1 = setup_view(view1, request)
        forms_dict1 = view1.get_form_formsets()
        self.assertIn('form', forms_dict1.keys())
        self.assertEquals(0, len(forms_dict1['formsets']))

        view2 = self.FormsetView(template_name='test_views.html',
                                 model=TestParentModel,
                                 form_class=TestParentForm,
                                 formset_classes=['string'])
        view2 = setup_view(view2, request)
        with self.assertRaises(ValueError):
            view2.get_form_formsets()

        view2 = self.FormsetView(template_name='test_views.html',
                                 model=TestParentModel,
                                 form_class=TestParentForm,
                                 formset_classes=[TestChildModel])
        view2 = setup_view(view2, request)
        with self.assertRaises(ValueError):
            view2.get_form_formsets()

        view3 = self.FormsetView(template_name='test_views.html',
                                 model=TestParentModel,
                                 form_class=TestParentForm,
                                 formset_classes=TestChildFormset)
        view3 = setup_view(view3, request)
        with self.assertRaises(TypeError):
            view3.get_form_formsets()
Beispiel #19
0
 def test_form_invalid(self, mock_render_func, mock_context_func):
     request = RequestFactory().get('/fake-path')
     view = self.FormsetView(template_name='test_views.html',
                             model=TestParentModel,
                             form_class=TestParentForm,
                             formset_classes=(TestChildFormset,))
     view = setup_view(view, request)
     view.form_invalid({'form': 'form', 'formset': 'formset'})
     mock_render_func.assert_called()
     mock_context_func.assert_called()
Beispiel #20
0
    def test_hike_detail_queryset(self, mock_queryset):
        mock_queryset.return_value = 'queryset'
        hike = HikeFactory(trailhead=self.trailhead)
        view = HikeDetailView(template_name='test_views.html')
        view = setup_view(view, self.request, region_slug=self.region.slug,
                          trailhead_slug=self.trailhead.slug,
                          hike_slug=hike.slug,
                          co_region_slug=self.co_region.slug)

        qs = view.get_queryset()
        self.assertEquals(mock_queryset.return_value, qs)
Beispiel #21
0
    def test_search_by_distance_queryset(self, mock_serializer, mock_crow):
        mock_serializer.return_value = {'serialized': True}
        region = RegionFactory()
        trailhead = TrailheadFactory(region=region)
        HikeFactory(trailhead=trailhead, name='Test Hike')
        mock_crow.return_value = [trailhead]
        request = RequestFactory().get('/fake-path/?search_text=9_97219')
        view = SearchByDistance(template_name='test_views.html')
        view = setup_view(view, request, region_name=region.name)

        qs = view.get_queryset()
        self.assertEquals(qs, mock_serializer.return_value)
Beispiel #22
0
 def test_hiker_create_view_valid(self):
     request = RequestFactory().get('/fake-path')
     view = HikerCreateView(template_name='test_views.html',
                            model=HikerDiaryEntry)
     view = setup_view(view, request)
     request.user = UserFactory()
     request.user.hiker = HikerFactory()
     form = MagicMock()
     form.is_valid = MagicMock()
     form.is_valid.return_value = True
     self.assertIsInstance(view.form_valid(form),
                           HttpResponseRedirect)
Beispiel #23
0
    def setUp(self):  # noqa
        self.co_region = CountryRegionFactory()
        self.region = RegionFactory(country_region=self.co_region)
        self.trailhead = TrailheadFactory(region=self.region)
        self.hike = HikeFactory(trailhead=self.trailhead)

        self.request = RequestFactory().get('/fake-path')
        view = TemplateView(template_name='test_views.html')
        self.view = setup_view(view, self.request,
                               co_region_slug=self.co_region.slug,
                               region_slug=self.region.slug,
                               trailhead_slug=self.trailhead.slug,
                               hike_slug=self.hike.slug)
Beispiel #24
0
 def test_hiker_delete_view_queryset(self):
     hiker = HikerFactory()
     diary = HikerDiaryEntryFactory(hiker=hiker)
     diary2 = HikerDiaryEntryFactory()
     request = RequestFactory().get('/fake-path')
     view = HikerDeleteView(template_name='test_views.html',
                            model=HikerDiaryEntry)
     view = setup_view(view, request, user_slug=hiker.slug)
     request.user = UserFactory()
     request.user.hiker = HikerFactory()
     qs = view.get_queryset()
     self.assertIn(diary, qs)
     self.assertNotIn(diary2, qs)
Beispiel #25
0
 def test_form_valid(self):
     request = RequestFactory().get('/fake-path')
     view = self.FormsetView(template_name='test_views.html',
                             model=TestParentModel,
                             form_class=TestParentForm,
                             formset_classes=(TestChildFormset,))
     view = setup_view(view, request)
     form = MagicMock()
     form.is_valid = MagicMock()
     form.is_valid.return_value = False
     formset = MagicMock()
     formset.is_valid = MagicMock()
     formset.is_valid.return_value = True
     self.assertIsInstance(view.form_valid(form, [formset]),
                           HttpResponseRedirect)
Beispiel #26
0
 def test_post(self, mock_form_invalid, mock_get_formsets, mock_get_object):
     form = MagicMock()
     form.is_valid = MagicMock()
     form.is_valid.return_value = False
     formset = MagicMock()
     formset.is_valid = MagicMock()
     formset.is_valid.return_value = True
     mock_get_formsets.return_value = {'form': form, 'formsets': [formset]}
     mock_form_invalid.return_value = 'invalid'
     request = RequestFactory().get('/fake-path')
     view = self.FormsetView(template_name='test_views.html',
                             model=TestParentModel,
                             form_class=TestParentForm,
                             formset_classes=(TestChildFormset,))
     view = setup_view(view, request, pk=1)
     view.post(request)
     mock_get_object.assert_called()
Beispiel #27
0
    def test_hiker_access_dispatch(self, mock_no_permission, mock_inactive,
                                   mock_access_failed, mock_group, mock_owner):
        mock_owner.return_value = True
        mock_group.return_value = True
        mock_inactive.return_value = 'inactive'
        mock_no_permission.return_value = 'no permission'
        request = RequestFactory().get('/fake-path')
        view = self.HikerAccessView(template_name='test_views.html',
                                    model=Hiker,
                                    ownership_failure_path='owner fail',
                                    group_failure_path='group fail')
        view = setup_view(view, request)

        # unauthenticated user
        request.user = AnonymousUser()
        self.assertEquals(mock_no_permission.return_value,
                          view.dispatch(request))

        # user with no permissions flags
        request.user = self.user_no_profile
        self.assertEquals(200, view.dispatch(request).status_code)

        # inactive user
        request.user.is_active = False
        self.assertEquals(mock_inactive.return_value, view.dispatch(request))

        # check methods return false
        request.user.is_active = True
        mock_owner.return_value = False
        mock_group.return_value = False
        view.dispatch(request)
        mock_access_failed.assert_called_with(view.ownership_failure_path,
                                              request)

        mock_owner.return_value = True
        view.dispatch(request)

        mock_access_failed.assert_called_with(view.group_failure_path, request)
Beispiel #28
0
    def test_group_required(self):
        request = RequestFactory().get('/fake-path')
        view = self.GroupReqView(
            template_name='test_views.html',
            model=HikerDiaryEntry)
        view = setup_view(view, request)
        group_name = 'test_group'
        group = Group.objects.create(name=group_name)

        # user not superuser, group None
        self.assertTrue(view.check_group(self.hiker1.hiker))

        # user is superuser and superuser_allowed
        view.group_required = group_name
        self.user_no_profile.is_superuser = True
        self.assertTrue(view.check_group(self.user_no_profile))

        # user is superuser but not group, and not superuser_allowed
        view.superuser_allowed = False
        self.assertFalse(view.check_group(self.user_no_profile))

        group_user = self.hiker1.hiker
        group_user.groups.add(group)
        self.assertTrue(view.check_group(group_user))
Beispiel #29
0
 def test_settings_view_uses_settings_template(self):
     settings_request = RequestFactory().get('/settings/1/')
     settings_view = SettingsView()
     view = setup_view(settings_view, settings_request)
     template_names = view.get_template_names()
     self.assertEqual(template_names, ['settings.html'])
Beispiel #30
0
 def test_profile_index_redirect(self):
     view = ProfileIndexRedirect(template_name='test_views.html')
     view = setup_view(view, self.request, user_slug=self.hiker.slug)
     self.assertIn(self.hiker.slug, view.get_redirect_url())