view test_twitter_client.py @ 7:c6aef586ab82

added success_delegate
author Atul Varma <avarma@mozilla.com>
date Sat, 12 Jun 2010 19:47:49 -0700
parents 02ae64fff24d
children 51dfea268026
line wrap: on
line source

import minimock

from twitter_client import TwitterOauthClientApp

class Mock(minimock.Mock):
    def __repr__(self):
        return "<Mock %s>" % self.mock_name

def app(request_tokens=None):
    if request_tokens is None:
        request_tokens = {}
    consumer = 'mock consumer'
    oauth = Mock('oauth')
    success_delegate = Mock('success_delegate')
    toc = TwitterOauthClientApp(consumer, oauth, 'http://foo.com/oauth_callback',
                                request_tokens, success_delegate)
    return (consumer, oauth, success_delegate, toc)

def test_404():
    """
    >>> _, _, _, toc = app()
    >>> environ = dict(PATH_INFO='/', QUERY_STRING='')
    >>> toc(environ, Mock('start_response'))
    Called start_response('404 Not Found', [('Content-Type', 'text/plain')])
    ['path not found: /']
    """

    pass

def test_request_redirect(self):
    """
    >>> consumer, oauth, _, toc = app()
    >>> client = Mock('client')
    >>> client.request.mock_returns = (
    ...   {'status': '200'},
    ...   'oauth_token=token&oauth_token_secret=secret&'
    ...   'oauth_callback_confirmed=true'
    ... )
    >>> oauth.Client.mock_returns = client
    >>> environ = dict(PATH_INFO='/request', QUERY_STRING='')
    >>> toc(environ, Mock('start_response'))
    Called oauth.Client('mock consumer')
    Called client.request(
        'https://api.twitter.com/oauth/request_token?oauth_callback=http%3A%2F%2Ffoo.com%2Foauth_callback',
        'GET')
    Called start_response(
        '302 Found',
        [('Location', 'https://api.twitter.com/oauth/authorize?oauth_token=token')])
    []
    """

    pass

def test_callback(self):
    """
    >>> storage = {'token': {'oauth_token': 'token', 'oauth_token_secret': 'secret'}}
    >>> consumer, oauth, success_delegate, toc = app(storage)
    >>> client = Mock('client')
    >>> client.request.mock_returns = (
    ...   {'status': '200'},
    ...   'oauth_token=token&oauth_token_secret=secret&'
    ...   'user_id=userid&screen_name=bob'
    ... )
    >>> oauth.Client.mock_returns = client
    >>> token = Mock('token')
    >>> oauth.Token.mock_returns = token
    >>> environ = dict(
    ...   PATH_INFO='/callback',
    ...   QUERY_STRING='oauth_token=token&oauth_verifier=verifier'
    ... )
    >>> success_delegate.mock_returns = ['success']
    >>> toc(environ, Mock('start_response'))
    Called oauth.Token('token', 'secret')
    Called token.set_verifier('verifier')
    Called oauth.Client('mock consumer', <Mock token>)
    Called client.request('https://api.twitter.com/oauth/access_token', 'POST')
    Called success_delegate(
        {'QUERY_STRING': 'oauth_token=token&oauth_verifier=verifier', 'PATH_INFO': '/callback', 'oauth.access_token': {'oauth_token_secret': 'secret', 'user_id': 'userid', 'oauth_token': 'token', 'screen_name': 'bob'}},
        <Mock start_response>)
    ['success']
    """

    pass

if __name__ == '__main__':
    import doctest
    doctest.testmod()
    print "done running tests."