_multiprocess_can_split_ = False

from init import setUp
setUp()

from unittest import TestCase
from mock import patch
import mock

SERVICE_KEY = 'test_generated_key.pem'
SERVICE_EMAIL = '<EMAIL>'
USER_EMAIL = '<EMAIL>'
USER2_EMAIL = '<EMAIL>'


class DictObject(object):

    def __init__(self, **properties):
        self.__dict__.update(properties)

def get_gae_mock_fetch():
    fetch = mock.MagicMock()
    fetch.__call__ = mock.MagicMock()
    return fetch


def get_gae_mock_memcache():
    memcache = mock.MagicMock()
    memcache.get, memcache.set = mock.MagicMock(), mock.MagicMock()
    memcache.get.return_value = None
    return memcache


class TestToken(TestCase):

    @patch('google.appengine.api.urlfetch.fetch', get_gae_mock_fetch())
    @patch('google.appengine.api.memcache', get_gae_mock_memcache())
    def test_gets_token(self):
        'Token tests (offline)'
        from gapi import oauth2, exceptions
        from google.appengine.api.urlfetch import fetch
        from google.appengine.api import memcache
        token = oauth2.TokenRequest(SERVICE_EMAIL, SERVICE_KEY, 'https://www.googleapis.com/auth/calendar', USER_EMAIL)
        self.assertFalse(fetch.called, msg="Token fetched before needed (lazy evaluation failed).")

        fetch.return_value = DictObject(**{
            'status_code': 200,
            'content': '{"access_token" : "<KEY>", "token_type" : "Bearer", "expires_in" : 3250 }'
        })
        fetch.return_value.headers = {}
        token._lock, token._unlock = lambda : True, lambda : True
        header = str(token)
        self.assertEquals(header, 'Bearer 1/8xbJqaOZXSUZbHLl5EO<KEY>', msg="The token string representation does not conform values from auth server")
        self.assertEquals(memcache.get.call_count, 1, msg="Token tries to retrieve the key from memcahced first.")
        self.assertEquals(memcache.set.call_args[0][2], 3250-300, msg="The memcache expiration equals expiration of the token got from auth server minus 300s.")

        fetch.return_value.status_code = 403
        fetch.return_value.content = '{"error": "invalid_grant"}'
        token = oauth2.TokenRequest(SERVICE_EMAIL, SERVICE_KEY, 'https://www.googleapis.com/auth/calendar', USER_EMAIL)
        token._lock, token._unlock = lambda : True, lambda : True
        self.assertRaises(exceptions.InvalidGrantException, token.get_token)

_multiprocess_can_split_ = False

from init import setUp
setUp()

from unittest import TestCase
from mock import patch
import mock

SERVICE_KEY = 'test_generated_key.pem'
SERVICE_EMAIL = '<EMAIL>'
USER_EMAIL = '<EMAIL>'
USER2_EMAIL = '<EMAIL>'


class DictObject(object):

    def __init__(self, **properties):
        self.__dict__.update(properties)

def get_gae_mock_fetch():
    fetch = mock.MagicMock()
    fetch.__call__ = mock.MagicMock()
    return fetch


def get_gae_mock_memcache():
    memcache = mock.MagicMock()
    memcache.get, memcache.set = mock.MagicMock(), mock.MagicMock()
    memcache.get.return_value = None
    return memcache


class TestToken(TestCase):

    @patch('google.appengine.api.urlfetch.fetch', get_gae_mock_fetch())
    @patch('google.appengine.api.memcache', get_gae_mock_memcache())
    def test_gets_token(self):
        'Token tests (offline)'
        from gapi import oauth2, exceptions
        from google.appengine.api.urlfetch import fetch
        from google.appengine.api import memcache
        token = oauth2.TokenRequest(SERVICE_EMAIL, SERVICE_KEY, 'https://www.googleapis.com/auth/calendar', USER_EMAIL)
        self.assertFalse(fetch.called, msg="Token fetched before needed (lazy evaluation failed).")

        fetch.return_value = DictObject(**{
            'status_code': 200,
            'content': '{"access_token" : "<KEY>", "token_type" : "Bearer", "expires_in" : 3250 }'
        })
        fetch.return_value.headers = {}
        token._lock, token._unlock = lambda : True, lambda : True
        header = str(token)
        self.assertEquals(header, 'Bearer 1/8xbJqaOZXSUZbHLl5EO<KEY>', msg="The token string representation does not conform values from auth server")
        self.assertEquals(memcache.get.call_count, 1, msg="Token tries to retrieve the key from memcahced first.")
        self.assertEquals(memcache.set.call_args[0][2], 3250-300, msg="The memcache expiration equals expiration of the token got from auth server minus 300s.")

        fetch.return_value.status_code = 403
        fetch.return_value.content = '{"error": "invalid_grant"}'
        token = oauth2.TokenRequest(SERVICE_EMAIL, SERVICE_KEY, 'https://www.googleapis.com/auth/calendar', USER_EMAIL)
        token._lock, token._unlock = lambda : True, lambda : True
        self.assertRaises(exceptions.InvalidGrantException, token.get_token)
