def test_get_object_returns_emergency_contact_if_logged_in(self): ''' Tests the get_object() method which is overridden in EmergencyContactDeleteView. Function returns emergency contact with pk specified in url if logged in user matches emergency contact user ''' ec = EmergencyContact.objects.create(full_name='Don Gately', relationship='Buddy', user=self.user) request = self.factory.get( reverse('account_info:emerg_contact_delete', args=[ec.id])) request.user = self.user view = EmergencyContactDeleteView() view = setup_view(view, request, pk=ec.id) emcon = view.get_object() self.assertEqual(ec, emcon)
def test_view_uses_correct_template_with_get_request(self): ec = EmergencyContact.objects.create(full_name='Don Gately', relationship='Buddy', user=self.user) request = self.factory.get('/fake/') request.user = self.user response = EmergencyContactDeleteView.as_view()(request, pk=ec.id) self.assertTrue('emerg_contact/delete.html' in response.template_name)
def test_post_request_redirects_to_login_if_user_not_logged_in(self): '''POST''' request = self.factory.post('/fake/') request.user = '' response = EmergencyContactDeleteView.as_view()(request) self.assertEqual(response.status_code, 302) redirect_url = reverse('authentication:login') + '?next=' + '/fake/' self.assertEqual(response.url, redirect_url)
def test_200_response_from_get_request(self): ec = EmergencyContact.objects.create(full_name='Don Gately', relationship='Buddy', user=self.user) request = self.factory.get('/fake/') request.user = self.user response = EmergencyContactDeleteView.as_view()(request, pk=ec.id) self.assertEqual(response.status_code, 200)
def test_get_object_returns_404_response_if_not_logged_in(self): ''' Tests the get_object() method which is overridden in EmergencyContactDeleteView. Function returns 404 response if the emergency_contact.user.id doesn't match the logged in user. ''' logged_out_user = User.objects.create_user(email='*****@*****.**', password='******') # Assign one emergency contact to the logged out user ec = EmergencyContact.objects.create(full_name='Don Gately', relationship='Buddy', user=logged_out_user) request = self.factory.get( reverse('account_info:emerg_contact_delete', args=[ec.id])) request.user = self.user view = EmergencyContactDeleteView() view = setup_view(view, request, pk=ec.id) with self.assertRaises(Http404): response = view.get_object()
def test_post_redirects_to_list_view_if_user_is_logged_in(self): ec = EmergencyContact.objects.create(full_name='Don Gately', relationship='Buddy', user=self.user) request = self.factory.post('/fake/') request.user = self.user response = EmergencyContactDeleteView.as_view()(request, pk=ec.id) self.assertEqual(response.status_code, 302) self.assertEqual(response.url, reverse('account_info:emerg_contact_list'))