# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from json import loads
from mock import MagicMock

from oauthlib.common import urlencode
from oauthlib.oauth2 import RequestValidator, RevocationEndpoint

from ....unittest import TestCase


class RevocationEndpointTest(TestCase):

    def setUp(self):
        self.validator = MagicMock(wraps=RequestValidator())
        self.validator.authenticate_client.return_value = True
        self.validator.revoke_token.return_value = True
        self.endpoint = RevocationEndpoint(self.validator)

        self.uri = 'https://example.com/revoke_token'
        self.headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
        }

    def test_revoke_token(self):
        for token_type in ('access_token', 'refresh_token', 'invalid'):
            body = urlencode([('token', 'foo'),
                              ('token_type_hint', token_type)])
            h, b, s = self.endpoint.create_revocation_response(self.uri,
                    headers=self.headers, body=body)
            self.assertEqual(h, {})
            self.assertEqual(b, '')
            self.assertEqual(s, 200)

        # don't specify token_type_hint
        body = urlencode([('token', 'foo')])
        h, b, s = self.endpoint.create_revocation_response(self.uri,
                headers=self.headers, body=body)
        self.assertEqual(h, {})
        self.assertEqual(b, '')
        self.assertEqual(s, 200)
    
    def test_revoke_token_without_client_authentication(self):
        self.validator.client_authentication_required.return_value = False
        self.validator.authenticate_client.return_value = False        
        for token_type in ('access_token', 'refresh_token', 'invalid'):
            body = urlencode([('token', 'foo'),
                              ('token_type_hint', token_type)])
            h, b, s = self.endpoint.create_revocation_response(self.uri,
                    headers=self.headers, body=body)
            self.assertEqual(h, {})
            self.assertEqual(b, None)
            self.assertEqual(s, 200)

    def test_revoke_token_without_client_authentication(self):
        self.validator.client_authentication_required.return_value = False
        self.validator.authenticate_client.return_value = False
        for token_type in ('access_token', 'refresh_token', 'invalid'):
            body = urlencode([('token', 'foo'),
                              ('token_type_hint', token_type)])
            h, b, s = self.endpoint.create_revocation_response(self.uri,
                    headers=self.headers, body=body)
            self.assertEqual(h, {})
            self.assertEqual(b, '')
            self.assertEqual(s, 200)

    def test_revoke_with_callback(self):
        endpoint = RevocationEndpoint(self.validator, enable_jsonp=True)
        callback = 'package.hello_world'
        for token_type in ('access_token', 'refresh_token', 'invalid'):
            body = urlencode([('token', 'foo'),
                              ('token_type_hint', token_type),
                              ('callback', callback)])
            h, b, s = endpoint.create_revocation_response(self.uri,
                    headers=self.headers, body=body)
            self.assertEqual(h, {})
            self.assertEqual(b, callback + '();')
            self.assertEqual(s, 200)

    def test_revoke_unsupported_token(self):
        endpoint = RevocationEndpoint(self.validator,
                                      supported_token_types=['access_token'])
        body = urlencode([('token', 'foo'),
                          ('token_type_hint', 'refresh_token')])
        h, b, s = endpoint.create_revocation_response(self.uri,
                headers=self.headers, body=body)
        self.assertEqual(h, {})
        self.assertEqual(loads(b)['error'], 'unsupported_token_type')
        self.assertEqual(s, 400)

        h, b, s = endpoint.create_revocation_response(self.uri,
                headers=self.headers, body='')
        self.assertEqual(h, {})
        self.assertEqual(loads(b)['error'], 'invalid_request')
        self.assertEqual(s, 400)

@@ -389,16 +389,90 @@
 ator())%0A
+        self.validator.client_authentication_required.return_value = True%0A
         
@@ -518,32 +518,32 @@
 rn_value = True%0A
-
         self.val
@@ -1554,46 +1554,521 @@
 00)%0A
+%0A
     
-%0A    def test_revoke_token_without
+def test_revoke_token_client_authentication_failed(self):%0A        self.validator.authenticate_client.return_value = False%0A        body = urlencode(%5B('token', 'foo'),%0A                          ('token_type_hint', 'access_token')%5D)%0A        h, b, s = self.endpoint.create_revocation_response(self.uri,%0A                headers=self.headers, body=body)%0A        self.assertEqual(h, %7B%7D)%0A        self.assertEqual(loads(b)%5B'error'%5D, 'invalid_client')%0A        self.assertEqual(s, 401)%0A%0A    def test_revoke_token_public
 _cli
@@ -2202,32 +2202,35 @@
 henticate_client
+_id
 .return_value = 
@@ -2233,21 +2233,12 @@
 e = 
-False        
+True
 %0A   
@@ -2686,15 +2686,14 @@
 ken_
-without
+public
 _cli
@@ -2702,32 +2702,39 @@
 t_authentication
+_failed
 (self):%0A        
@@ -2834,32 +2834,35 @@
 henticate_client
+_id
 .return_value = 
@@ -2871,84 +2871,8 @@
 lse%0A
-        for token_type in ('access_token', 'refresh_token', 'invalid'):%0A    
     
@@ -2929,36 +2929,32 @@
                 
-    
 ('token_type_hin
@@ -2957,34 +2957,34 @@
 _hint', 
-token_type)%5D)%0A    
+'access_token')%5D)%0A
         
@@ -3036,36 +3036,32 @@
 ponse(self.uri,%0A
-    
                 
@@ -3093,36 +3093,32 @@
 y=body)%0A        
-    
 self.assertEqual
@@ -3125,36 +3125,32 @@
 (h, %7B%7D)%0A        
-    
 self.assertEqual
@@ -3146,35 +3146,61 @@
 assertEqual(
-b, '')%0A    
+loads(b)%5B'error'%5D, 'invalid_client')%0A
         self
@@ -3207,35 +3207,35 @@
 .assertEqual(s, 
-200
+401
 )%0A%0A    def test_

Fix tests