comparison 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
comparison
equal deleted inserted replaced
6:f0273e301ee4 7:c6aef586ab82
1 from minimock import Mock 1 import minimock
2 2
3 from twitter_client import TwitterOauthClientApp 3 from twitter_client import TwitterOauthClientApp
4
5 class Mock(minimock.Mock):
6 def __repr__(self):
7 return "<Mock %s>" % self.mock_name
4 8
5 def app(request_tokens=None): 9 def app(request_tokens=None):
6 if request_tokens is None: 10 if request_tokens is None:
7 request_tokens = {} 11 request_tokens = {}
8 consumer = 'mock consumer' 12 consumer = 'mock consumer'
9 oauth = Mock('oauth') 13 oauth = Mock('oauth')
14 success_delegate = Mock('success_delegate')
10 toc = TwitterOauthClientApp(consumer, oauth, 'http://foo.com/oauth_callback', 15 toc = TwitterOauthClientApp(consumer, oauth, 'http://foo.com/oauth_callback',
11 request_tokens) 16 request_tokens, success_delegate)
12 return (consumer, oauth, toc) 17 return (consumer, oauth, success_delegate, toc)
13 18
14 def test_404(): 19 def test_404():
15 """ 20 """
16 >>> _, _, toc = app() 21 >>> _, _, _, toc = app()
17 >>> environ = dict(PATH_INFO='/', QUERY_STRING='') 22 >>> environ = dict(PATH_INFO='/', QUERY_STRING='')
18 >>> toc(environ, Mock('start_response')) 23 >>> toc(environ, Mock('start_response'))
19 Called start_response('404 Not Found', [('Content-Type', 'text/plain')]) 24 Called start_response('404 Not Found', [('Content-Type', 'text/plain')])
20 ['path not found: /'] 25 ['path not found: /']
21 """ 26 """
22 27
23 pass 28 pass
24 29
25 def test_request_redirect(self): 30 def test_request_redirect(self):
26 """ 31 """
27 >>> consumer, oauth, toc = app() 32 >>> consumer, oauth, _, toc = app()
28 >>> client = Mock('client') 33 >>> client = Mock('client')
29 >>> client.request.mock_returns = ( 34 >>> client.request.mock_returns = (
30 ... {'status': '200'}, 35 ... {'status': '200'},
31 ... 'oauth_token=token&oauth_token_secret=secret&' 36 ... 'oauth_token=token&oauth_token_secret=secret&'
32 ... 'oauth_callback_confirmed=true' 37 ... 'oauth_callback_confirmed=true'
44 [] 49 []
45 """ 50 """
46 51
47 pass 52 pass
48 53
49 class MockToken(Mock):
50 def __repr__(self):
51 return "<Mock token>"
52
53 def test_callback(self): 54 def test_callback(self):
54 """ 55 """
55 >>> storage = {'token': {'oauth_token': 'token', 'oauth_token_secret': 'secret'}} 56 >>> storage = {'token': {'oauth_token': 'token', 'oauth_token_secret': 'secret'}}
56 >>> consumer, oauth, toc = app(storage) 57 >>> consumer, oauth, success_delegate, toc = app(storage)
57 >>> client = Mock('client') 58 >>> client = Mock('client')
58 >>> client.request.mock_returns = ( 59 >>> client.request.mock_returns = (
59 ... {'status': '200'}, 60 ... {'status': '200'},
60 ... 'oauth_token=token&oauth_token_secret=secret&' 61 ... 'oauth_token=token&oauth_token_secret=secret&'
61 ... 'user_id=userid&screen_name=bob' 62 ... 'user_id=userid&screen_name=bob'
62 ... ) 63 ... )
63 >>> oauth.Client.mock_returns = client 64 >>> oauth.Client.mock_returns = client
64 >>> token = MockToken('token') 65 >>> token = Mock('token')
65 >>> oauth.Token.mock_returns = token 66 >>> oauth.Token.mock_returns = token
66 >>> environ = dict( 67 >>> environ = dict(
67 ... PATH_INFO='/callback', 68 ... PATH_INFO='/callback',
68 ... QUERY_STRING='oauth_token=token&oauth_verifier=verifier' 69 ... QUERY_STRING='oauth_token=token&oauth_verifier=verifier'
69 ... ) 70 ... )
71 >>> success_delegate.mock_returns = ['success']
70 >>> toc(environ, Mock('start_response')) 72 >>> toc(environ, Mock('start_response'))
71 Called oauth.Token('token', 'secret') 73 Called oauth.Token('token', 'secret')
72 Called token.set_verifier('verifier') 74 Called token.set_verifier('verifier')
73 Called oauth.Client('mock consumer', <Mock token>) 75 Called oauth.Client('mock consumer', <Mock token>)
74 Called client.request('https://api.twitter.com/oauth/access_token', 'POST') 76 Called client.request('https://api.twitter.com/oauth/access_token', 'POST')
75 Called start_response('200 OK', [('Content-Type', 'text/plain')]) 77 Called success_delegate(
78 {'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'}},
79 <Mock start_response>)
76 ['success'] 80 ['success']
77 """ 81 """
78 82
79 pass 83 pass
80 84
81 if __name__ == '__main__': 85 if __name__ == '__main__':
82 import doctest 86 import doctest
83 doctest.testmod() 87 doctest.testmod()
84 print "all tests passed." 88 print "done running tests."