class BearerAuthenticationTests(TestCase): """ Tests for the BearerAuthentication class. """ def setUp(self): super(BearerAuthenticationTests, self).setUp() self.auth = BearerAuthentication() self.factory = APIRequestFactory() def _create_request(self, token='12345', token_name='Bearer'): """Create request with authorization header. """ auth_header = '{} {}'.format(token_name, token) request = self.factory.get('/', HTTP_AUTHORIZATION=auth_header) return request def test_authenticate_header(self): """The method should return the string Bearer.""" self.assertEqual(self.auth.authenticate_header(self._create_request()), 'Bearer') def test_authenticate_invalid_token(self): """If no token is supplied, or if the token contains spaces, the method should raise an exception.""" # Missing token request = self._create_request('') self.assertRaises(AuthenticationFailed, self.auth.authenticate, request) # Token with spaces request = self._create_request('abc 123 456') self.assertRaises(AuthenticationFailed, self.auth.authenticate, request)
def setUp(self): super(BearerAuthenticationTests, self).setUp() self.auth = BearerAuthentication() self.factory = APIRequestFactory()