def test_get_queryset_returns_trips_for_logged_in_user(self): ''' Tests the get_queryset() method which is overridden in TripListView. Function returns only trips for which the currently logged in user is a member (and has accepted membership). ''' request = self.factory.get('/fake/') request.user = self.user view = TripListView() kwargs={} view = setup_view(view, request, **kwargs) trip = Trip.objects.create( title='title', start_date=timezone.now().date() ) trip_nonmember = Trip.objects.create( title='title', start_date=timezone.now().date() ) TripMember.objects.create( member=self.user, trip=trip, email="*****@*****.**" ) queryset = view.get_queryset() self.assertEqual(list(self.user.trip_set.all()), list(queryset))
def test_get_context_data_returns_upcoming_trips(self): ''' The get_context_data returns an 'upcoming_trip_list', including only trips for which the logged in user is a member ''' request = self.factory.get('/fake/') request.user = self.user view = TripListView() kwargs = {} view = setup_view(view, request, **kwargs) trip_past = Trip.objects.create(title='title1', start_date=timezone.now().date() + datetime.timedelta(days=-1)) trip_future = Trip.objects.create(title='title2', start_date=timezone.now().date() + datetime.timedelta(days=1)) TripMember.objects.create( member=self.user, trip=trip_past, ) TripMember.objects.create( member=self.user, trip=trip_future, ) view.object_list = self.user.trip_set.all() context = view.get_context_data() for e in list(view.object_list.filter(start_date__gte=timezone.now())): self.assertIn(e, list(context['upcoming_trip_list'])) self.assertEqual( len(list(view.object_list.filter(start_date__gte=timezone.now()))), len(list(context['upcoming_trip_list'])))
def test_get_request_redirects_to_login_if_user_not_logged_in(self): request = self.factory.get('/fake/') request.user = '' response = TripListView.as_view()(request) self.assertEqual(response.status_code, 302) redirect_url = reverse('authentication:login') + '?next=' + '/fake/' self.assertEqual(response.url, redirect_url)
def test_no_trips(self): """ If no trips exist, an appropriate message is displayed. """ request = self.factory.get('/fake/') request.user = self.user response = TripListView.as_view()(request) self.assertContains(response, "You don't have any upcoming trips planned") self.assertContains(response, "You haven't completed any trips yet")
def test_view_uses_correct_template(self): request = self.factory.get('/fake/') request.user = self.user response = TripListView.as_view()(request) self.assertTrue('trips/index.html' in response.template_name)
def test_200_response_from_get_request(self): request = self.factory.get('/fake/') request.user = self.user response = TripListView.as_view()(request) self.assertEqual(response.status_code, 200)
https://docs.djangoproject.com/en/3.0/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.conf import settings from django.conf.urls.static import static from django.contrib import admin from django.urls import path, include from trips.views import TripListView urlpatterns = [ path('admin/', admin.site.urls), path('', TripListView.as_view()), path('home/', TripListView.as_view(), name='home'), path('auth/', include('user_auth.urls')), path('trips/', include('trips.urls')), ] + [ *static(settings.STATIC_URL, document_root=settings.STATIC_ROOT), ] + [ *static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT), ]