Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 3760cf7cb7
Fetching contributors…

Cannot retrieve contributors at this time

file 536 lines (462 sloc) 22.424 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
import json
from urllib import urlencode
from .base import BaseHTTPTestCase
from handlers import TwitterAuthHandler, FollowsHandler, FollowingHandler

class HandlersTestCase(BaseHTTPTestCase):

    def setUp(self):
        super(HandlersTestCase, self).setUp()

        TwitterAuthHandler.authenticate_redirect = \
          twitter_authenticate_redirect

    def test_homepage(self):
        response = self.client.get('/')
        self.assertEqual(response.code, 200)
        self.assertTrue('Login with' in response.body)

    def test_twitter_login(self):
        assert not self.db.User.find_one({'username': 'peterbe'})
        TwitterAuthHandler.get_authenticated_user = \
          twitter_get_authenticated_user
        url = self.reverse_url('auth_twitter')
        response = self.client.get(url)
        self.assertEqual(response.code, 302)
        self.assertTrue('twitter.com' in response.headers['location'])

        response = self.client.get(url, {'oauth_token':'xxx'})
        self.assertEqual(response.code, 302)

        user = self.db.User.find_one({'username': 'peterbe'})
        self.assertEqual(user['access_token'],
                         {'key': '0123456789',
                          'secret': 'xxx'})

        url = self.reverse_url('logout')
        response = self.client.get(url)
        self.assertEqual(response.code, 302)
        response = self.client.get('/')
        self.assertEqual(response.code, 200)
        self.assertTrue('Login with Twitter' in response.body)

        self.assertEqual(int(self.redis.get('auths:total')), 1)
        self.assertEqual(int(self.redis.get('auths:username:peterbe')), 1)

    def test_twitter_login_twitter_failing(self):
        TwitterAuthHandler.get_authenticated_user = \
          make_twitter_get_authenticated_user_callback(None)
        url = self.reverse_url('auth_twitter')
        response = self.client.get(url)
        self.assertEqual(response.code, 302)
        self.assertTrue('twitter.com' in response.headers['location'])

        response = self.client.get(url, {'oauth_token':'xxx'})
        self.assertEqual(response.code, 200)
        self.assertTrue("Sorry" in response.body)
        self.assertTrue("Try again" in response.body)

    def _login(self, username=u'peterbe', name=u'Peter Bengtsson',
                     email=None):
        struct = {
          'name': name,
          'username': username,
          'email': email,
          'access_token': {'key': '0123456789',
                           'secret': 'xxx'}
        }
        TwitterAuthHandler.get_authenticated_user = \
          make_twitter_get_authenticated_user_callback(struct)
        TwitterAuthHandler.authenticate_redirect = \
          twitter_authenticate_redirect
        url = self.reverse_url('auth_twitter')
        response = self.client.get(url)
        self.assertEqual(response.code, 302)
        self.assertTrue('twitter.com' in response.headers['location'])

        response = self.client.get(url, {'oauth_token':'xxx'})
        assert response.code == 302

    def test_home(self):
        url = self.reverse_url('test')
        response = self.client.get('/')
        self.assertEqual(response.code, 200)
        self.assertTrue('Login with Twitter to start' in response.body)

        self._login(username='peppe')
        response = self.client.get('/')
        self.assertEqual(response.code, 200)
        self.assertTrue('Login with Twitter to start' not in response.body)

    def test_test_service(self):
        url = self.reverse_url('test')
        response = self.client.get(url)
        self.assertEqual(response.code, 302)
        self.assertTrue(self.reverse_url('auth_twitter') in
                        response.headers['location'])
        self._login()

        response = self.client.get(url)
        self.assertEqual(response.code, 200)


    def test_json(self):
        self.assertEqual(self.redis.get('lookups:json'), None)
        FollowsHandler.twitter_request = \
          make_mock_twitter_request({u'relationship': {
                   u'target': {u'followed_by': False,
                               u'following': False,
                               u'screen_name': u'obama'}}})

        url = self.reverse_url('json')
        response = self.client.get(url)
        self.assertEqual(response.code, 200)
        struct = json.loads(response.body)
        self.assertTrue('usernames' in struct['ERROR'])

        response = self.client.get(url, {'username': 'obama'})
        self.assertEqual(response.code, 200)
        struct = json.loads(response.body)
        self.assertTrue('Not authorized' in struct['ERROR'])

        self._login()
        response = self.client.get(url, {'username': 'obama'})
        self.assertEqual(response.code, 200)
        struct = json.loads(response.body)
        self.assertEqual(struct['obama'], False)

        # do it again and it should be picked up by the cache
        FollowsHandler.twitter_request = \
          make_mock_twitter_request({u'relationship': {
                   u'target': {u'followed_by': True,
                               u'following': True,
                               u'screen_name': u'obama'}}})
        response = self.client.get(url, {'username': 'obama'})
        self.assertEqual(response.code, 200)
        struct = json.loads(response.body)
        self.assertEqual(struct['obama'], False)

        self.assertEqual(int(self.redis.get('lookups:json')), 2)
        self.assertEqual(int(self.redis.get('lookups:username:peterbe')), 2)
        self.assertEqual(int(self.redis.get('lookups:usernames')), 2)

    def test_json_with_overriding_you(self):
        FollowsHandler.twitter_request = \
          make_mock_twitter_request({u'relationship': {
                   u'target': {u'followed_by': False,
                               u'following': False,
                               u'screen_name': u'obama'}}})

        self._login()
        url = self.reverse_url('json')
        response = self.client.get(url, {'username': 'obama', 'you': 'bob'})
        self.assertEqual(response.code, 200)
        struct = json.loads(response.body)
        self.assertTrue(struct['ERROR'])

        url = self.reverse_url('jsonp')
        response = self.client.get(url, {'username': 'obama', 'you': 'bob'})
        self.assertEqual(response.code, 200)
        self.assertTrue('ERROR' in response.body)

        url = self.reverse_url('json')
        response = self.client.get(url, {'username': 'obama', 'you': 'peterbe'})
        self.assertEqual(response.code, 200)
        struct = json.loads(response.body)
        self.assertEqual(struct['obama'], False)

        url = self.reverse_url('jsonp')
        response = self.client.get(url, {'username': 'obama', 'you': 'peterbe'})
        self.assertEqual(response.code, 200)
        self.assertTrue('"obama":' in response.body)
        self.assertTrue('false' in response.body)

        url = self.reverse_url('json')
        # do it again and it should be picked up by the cache
        FollowsHandler.twitter_request = \
          make_mock_twitter_request({u'relationship': {
                   u'target': {u'followed_by': True,
                               u'following': True,
                               u'screen_name': u'obama'}}})
        response = self.client.get(url, {'username': 'obama'})
        self.assertEqual(response.code, 200)
        struct = json.loads(response.body)
        self.assertEqual(struct['obama'], False)


    def test_jsonp(self):
        FollowsHandler.twitter_request = \
          make_mock_twitter_request({u'relationship': {
                   u'target': {u'followed_by': False,
                               u'following': False,
                               u'screen_name': u'obama'}}})

        url = self.reverse_url('jsonp')
        self._login()
        response = self.client.get(url, {'username': 'obama'})
        self.assertEqual(response.code, 200)
        self.assertEqual(response.body, 'callback({"obama": false})')
        response = self.client.get(url, {'username': 'obama', 'callback': 'FOO'})
        self.assertEqual(response.code, 200)
        self.assertEqual(response.body, 'FOO({"obama": false})')

        self.assertEqual(int(self.redis.get('lookups:json')), 0)
        self.assertEqual(int(self.redis.get('lookups:jsonp')), 2)
        self.assertEqual(int(self.redis.get('lookups:username:peterbe')), 2)
        self.assertEqual(int(self.redis.get('lookups:usernames')), 2)

    def test_jsonp(self):
        url = self.reverse_url('jsonp')
        response = self.client.get(url, {'username': 'obama'})
        self.assertEqual(response.code, 200)
        self.assertTrue(response.body.startswith('callback({"ERROR":'))
        response = self.client.get(url, {'username': 'obama', 'callback': 'FOO'})
        self.assertEqual(response.code, 200)
        self.assertTrue(response.body.startswith('FOO({"ERROR":'))

    def test_following_none_cached(self):
        FollowsHandler.twitter_request = \
          make_mock_twitter_request([
           {u'connections': [u'none'],
            u'name': u'Stephen Fry',
            u'screen_name': u'stephenfry'},
           {u'connections': [u'following', u'followed_by'],
            u'name': u'fox2mike',
            u'screen_name': u'fox2mike'}])

        url = self.reverse_url('json')
        self._login()
        response = self.client.get(url, {'usernames': 'stephenfry,fox2mike'})
        self.assertEqual(response.code, 200)
        struct = json.loads(response.body)
        self.assertEqual(struct['stephenfry'], False)
        self.assertEqual(struct['fox2mike'], True)

        # do it again and both should be cached
        FollowsHandler.twitter_request = \
          make_mock_twitter_request([
           {u'connections': [u'following', u'followed_by'],
            u'name': u'Stephen Fry',
            u'screen_name': u'stephenfry'},
           {u'connections': [u'none'],
            u'name': u'fox2mike',
            u'screen_name': u'fox2mike'}])

        response = self.client.get(url, {'usernames': 'stephenfry,fox2mike'})
        self.assertEqual(response.code, 200)
        struct = json.loads(response.body)
        self.assertEqual(struct['stephenfry'], False)
        self.assertEqual(struct['fox2mike'], True)

        FollowsHandler.twitter_request = \
          make_mock_twitter_request({u'relationship': {
                   u'target': {u'followed_by': True,
                               u'following': True,
                               u'screen_name': u'ashley'}}})

        response = self.client.get(url,
          {'usernames': 'stephenfry,fox2mike,ashley'})
        self.assertEqual(response.code, 200)
        struct = json.loads(response.body)
        self.assertEqual(struct['stephenfry'], False)
        self.assertEqual(struct['fox2mike'], True)
        self.assertEqual(struct['ashley'], True)

    def test_following(self):
        url = self.reverse_url('following', 'obama')
        response = self.client.get(url)
        self.assertEqual(response.code, 302)
        self.assertTrue(self.reverse_url('auth_twitter') in
                        response.headers['location'])

        self._login()
        FollowingHandler.twitter_request = \
          make_mock_twitter_request({
            "/friendships/show": {u'relationship': {
                                    u'target': {u'followed_by': False,
                                    u'following': False,
                                    u'screen_name': u'obama'}}},
            "/users/show?screen_name=obama": {u'followers_count': 41700,
                            u'following': False,
                            u'friends_count': 1300,
                            u'name': u'Barak',
                            u'screen_name': u'obama',
                            },
            "/users/show?screen_name=peterbe": {
                            u'followers_count': 417,
                            u'following': False,
                            u'friends_count': 330,
                            u'name': u'Peter Bengtsson',
                            u'screen_name': u'peterbe',
                            }
            })

        response = self.client.get(url)
        self.assertEqual(response.code, 200)
        self.assertTrue('<title>obama is too cool for me' in response.body)
        self.assertTrue('%.1f' % (41700.0/1300) in response.body)
        self.assertTrue('%.1f' % (417.0/330) in response.body)

        # same URL but different remote data should yield the same result
        # because of temporary caching
        FollowingHandler.twitter_request = \
          make_mock_twitter_request({
            "/friendships/show": {u'relationship': {
                                    u'target': {u'followed_by': True,
                                      u'following': True,
                                      u'screen_name': u'obama'}}},
            "/users/show?screen_name=obama": {u'followers_count': 41700,
                            u'following': False,
                            u'friends_count': 1301,
                            u'name': u'Barak',
                            u'screen_name': u'obama',
                            },
            "/users/show?screen_name=peterbe": {
                            u'followers_count': 417,
                            u'following': False,
                            u'friends_count': 331,
                            u'name': u'Peter Bengtsson',
                            u'screen_name': u'peterbe',
                            }
            })

        response = self.client.get(url)
        self.assertEqual(response.code, 200)
        self.assertTrue('<title>obama is too cool for me' in response.body)
        self.assertTrue('%.1f' % (41700.0/1300) in response.body)
        self.assertTrue('%.1f' % (417.0/330) in response.body)

        url = self.reverse_url('following', 'chris')
        FollowingHandler.twitter_request = \
          make_mock_twitter_request({
            "/friendships/show": {u'relationship': {
                                    u'target': {u'followed_by': True,
                                      u'following': True,
                                      u'screen_name': u'chris'}}},
            "/users/show?screen_name=chris": {u'followers_count': 400,
                            u'following': True,
                            u'friends_count': 1000,
                            u'name': u'West',
                            u'screen_name': u'chris',
                            },
            })
        response = self.client.get(url)
        self.assertEqual(response.code, 200)
        self.assertTrue('<title>chris follows me' in response.body)
        self.assertTrue('%.1f' % (400.0/1000) in response.body)
        self.assertTrue('%.1f' % (417.0/330) in response.body)

    def test_following_temporary_glitch_on_info(self):
        url = self.reverse_url('following', 'obama')
        response = self.client.get(url)
        self.assertEqual(response.code, 302)
        self.assertTrue(self.reverse_url('auth_twitter') in
                        response.headers['location'])

        self._login()
        FollowingHandler.twitter_request = \
          make_mock_twitter_request({
            "/friendships/show": {u'relationship': {
                                    u'target': {u'followed_by': False,
                                    u'following': False,
                                    u'screen_name': u'obama'}}},
            "/users/show?screen_name=obama": {u'followers_count': 41700,
                            u'following': False,
                            u'friends_count': 1300,
                            u'name': u'Barak',
                            u'screen_name': u'obama',
                            },
            "/users/show?screen_name=peterbe": None
            })

        response = self.client.get(url)
        self.assertEqual(response.code, 200)
        self.assertTrue("Sorry" in response.body)
        self.assertTrue("Unable to look up info for peterbe"
                        in response.body)

    def test_following_temporary_glitch_on_friendship(self):
        url = self.reverse_url('following', 'obama')
        response = self.client.get(url)
        self.assertEqual(response.code, 302)
        self.assertTrue(self.reverse_url('auth_twitter') in
                        response.headers['location'])

        self._login()
        FollowingHandler.twitter_request = \
          make_mock_twitter_request({
            "/friendships/show": None,
            "/users/show?screen_name=obama": {u'followers_count': 41700,
                            u'following': False,
                            u'friends_count': 1300,
                            u'name': u'Barak',
                            u'screen_name': u'obama',
                            },
            "/users/show?screen_name=peterbe": {
                            u'followers_count': 417,
                            u'following': False,
                            u'friends_count': 330,
                            u'name': u'Peter Bengtsson',
                            u'screen_name': u'peterbe',
                            }
            })

        response = self.client.get(url)
        self.assertEqual(response.code, 200)
        self.assertTrue("Sorry" in response.body)
        self.assertTrue("Unable to look up friendship for obama"
                        in response.body)

    def test_following_someone_following_0(self):
        url = self.reverse_url('following', 'obama')
        response = self.client.get(url)
        self.assertEqual(response.code, 302)
        self.assertTrue(self.reverse_url('auth_twitter') in
                        response.headers['location'])

        self._login()
        FollowingHandler.twitter_request = \
          make_mock_twitter_request({
            "/friendships/show": {u'relationship': {
                                    u'target': {u'followed_by': False,
                                    u'following': False,
                                    u'screen_name': u'obama'}}},
            "/users/show?screen_name=obama": {u'followers_count': 41700,
                            u'following': False,
                            u'friends_count': 0,
                            u'name': u'Barak',
                            u'screen_name': u'obama',
                            },
            "/users/show?screen_name=peterbe": {
                            u'followers_count': 417,
                            u'following': False,
                            u'friends_count': 330,
                            u'name': u'Peter Bengtsson',
                            u'screen_name': u'peterbe',
                            }
            })

        response = self.client.get(url)
        self.assertEqual(response.code, 200)
        self.assertTrue('<title>obama is too cool for me' in response.body)
        self.assertTrue('%.1f' % (417.0/330) in response.body)
        self.assertTrue('%.1f' % (41700.0/1) in response.body)


def make_twitter_get_authenticated_user_callback(struct):
    def twitter_get_authenticated_user(self, callback, **kw):
        callback(struct)
    return twitter_get_authenticated_user

def twitter_get_authenticated_user(self, callback, **kw):
    callback({
      'name': u'Peter Bengtsson',
      'username': u'peterbe',
      'email': None,
      'access_token': {'key': '0123456789', 'secret': 'xxx'}
    })

def twitter_authenticate_redirect(self):
    self.redirect(self._OAUTH_AUTHENTICATE_URL)


def make_mock_twitter_request(result, path=None):
    def twitter_request(self, path, callback, **kw):
        """
SINGLE FRIENDSHIP SHOW

Sample response if it's someone really cool:

{u'relationship': {u'source': {...
u'followed_by': False,
u'following': False,
...
u'screen_name': u'peterbe',
},
u'target': {u'followed_by': False,
u'following': False,
...
u'screen_name': u'ConanOBrien'}}}

Sample response if you are being followed by someone:

{u'relationship': {u'source': {...
u'followed_by': True,
u'following': False,
...
u'screen_name': u'peterbe'},
u'target': {u'followed_by': False,
u'following': True,
...
u'screen_name': u'Dal'}}}

Sample response if you follow each other:

{u'relationship': {u'source': {...
u'followed_by': True,
u'following': True,
u'screen_name': u'peterbe'},
u'target': {u'followed_by': True,
u'following': True,
...
u'screen_name': u'jlongster'}}}

MULTIPLE FRIENDSHIP LOOKUP

Both people follow:

[{u'connections': [u'followed_by'],
...
u'name': u'Toshiaki Toyama',
u'screen_name': u'manboubird'},
{u'connections': [u'following', u'followed_by'],
...
u'name': u'Chris AtLee',
u'screen_name': u'chrisatlee'}]

One follows one doesn't:

[{u'connections': [u'none'],
...
u'name': u'Stephen Fry',
u'screen_name': u'stephenfry'},
{u'connections': [u'following', u'followed_by'],
...
u'name': u'fox2mike',
u'screen_name': u'fox2mike'}]

"""
        kw.pop('access_token')
        long_path = path + '?' + urlencode(kw)
        if isinstance(result, dict):
            send_back = result.get(long_path, result.get(path, result))
        else:
            send_back = result
        callback(send_back)
    return twitter_request
Something went wrong with that request. Please try again.