Guest User

apitest

a guest
Feb 18th, 2016
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. from django.test import TestCase
  2. from django.test import Client
  3. from rest_framework.test import APIClient
  4. from django.contrib.auth import authenticate, login
  5. from django.contrib.auth.models import User
  6.  
  7. class APITestCase(TestCase):
  8.     def setUp(self):
  9.         user = User.objects.create_user('TestUser', None, 'TestPass')
  10.         user.save()
  11.  
  12.     def test_API_checks_auth(self):
  13.         """
  14.         Tests if API root throws 403(Forbidden) if not authenticated
  15.         """
  16.         client = APIClient()
  17.         response = client.get('/api/')
  18.         self.assertEqual(response.status_code, 403)
  19.  
  20.     def test_API_works_with_authentication(self):
  21.         """
  22.         Tests whether API endpoints are visible when authenticated
  23.         """
  24.         client = Client()  
  25.         rest_client = APIClient()
  26.         client.login(username='TestUser', password='TestPass')
  27.         rest_client.login(username='TestUser', password='TestPass')
  28.         response = rest_client.get('/api/posts/', format = 'json')
  29.         self.assertEqual(response.status_code, 200)
Add Comment
Please, Sign In to add comment