def test_get_success_url_redirects_to_trip_detail(self): request = self.factory.get('/fake/') request.user = self.user view = LocationEditView() kwargs = { 'trip_id': self.trip.id, 'location_type': 'camp', 'pk': self.triplocation.id } view = setup_view(view, request, **kwargs) success_url = view.get_success_url() intended_url = reverse('trips:trip_detail', args=(self.trip.id, )) self.assertEqual(success_url, intended_url)
def test_get_request_raises_exception_with_invalid_location_type(self): """ When a get request is sent with an invalid location_type, a ValueError will be raised """ request = self.factory.get('/fake/') request.user = self.user view = LocationEditView() kwargs = { 'trip_id': self.trip.id, 'location_type': 'invalid', 'pk': self.triplocation.id } view = setup_view(view, request, **kwargs) test = lambda: view.get(request, **kwargs) self.assertRaises(Http404, test)
def test_get_context_data_includes_key_trip_id(self): ''' The get_context_data includes key 'trip_id' ''' request = self.factory.get('/fake/') request.user = self.user view = LocationEditView() kwargs = { 'trip_id': self.trip.id, 'location_type': 'camp', 'pk': self.triplocation.id } view = setup_view(view, request, **kwargs) view.get(request, **kwargs) context = view.get_context_data() self.assertIn('trip_id', context)
def test_post_request_redirects_to_login_if_user_not_logged_in(self): request = self.factory.post('/fake/') request.user = '' response = LocationEditView.as_view()(request) self.assertEqual(response.status_code, 302) redirect_url = reverse('authentication:login') + '?next=' + '/fake/' self.assertEqual(response.url, redirect_url)
def test_value_of_instance_variables_set_through_get_camp(self): """ When a get request is sent with location_type=camp in the url, the view will set instance variables """ request = self.factory.get('/fake/') request.user = self.user view = LocationEditView() kwargs = { 'trip_id': self.trip.id, 'location_type': 'camp', 'pk': self.triplocation.id } view = setup_view(view, request, **kwargs) view.get(request, **kwargs) self.assertEqual(view.page_title, 'Edit camp details') self.assertEqual(view.submit_button_title, 'Save Camp')
def test_view_sets_instance_variables_through_post_request(self): """ When a post request is sent, the view will set instance variables to be used later in the context """ request = self.factory.post('/fake/') request.user = self.user view = LocationEditView() kwargs = { 'trip_id': self.trip.id, 'location_type': 'trailhead', 'pk': self.triplocation.id } view = setup_view(view, request, **kwargs) view.get(request, **kwargs) self.assertIsNotNone(view.page_title) self.assertIsNotNone(view.submit_button_title)
def test_view_uses_correct_template(self): request = self.factory.get('/fake/') request.user = self.user response = LocationEditView.as_view()(request, trip_id=self.trip.id, location_type='trailhead', pk=self.triplocation.id) self.assertTrue('trips/location.html' in response.template_name)
def test_200_response_from_get_request(self): request = self.factory.get('/fake/') request.user = self.user response = LocationEditView.as_view()(request, trip_id=self.trip.id, location_type='camp', pk=self.triplocation.id) self.assertEqual(response.status_code, 200)
def test_get_form_kwargs_returns_tuple_of_choices(self): ''' The get_form_kwargs method will add key 'choices' to the form kwargs with value set to a tuple of tuples ''' request = self.factory.get('/fake/') request.user = self.user view = LocationEditView() kwargs = { 'trip_id': self.trip.id, 'location_type': 'camp', 'pk': self.triplocation.id } view = setup_view(view, request, **kwargs) view.get(request, **kwargs) #pass date_type='night' to simulate logic in view for location_type='camp' date_list = self.trip.get_date_choices(date_type='night') choices = [] for item in date_list: choices.append((item, item)) choices_tuple = tuple(choices) form_kwargs = view.get_form_kwargs() self.assertIn('choices', form_kwargs) self.assertEqual(form_kwargs['choices'], choices_tuple)