|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +from __future__ import absolute_import |
| 4 | +from __future__ import division |
| 5 | +from __future__ import print_function |
| 6 | + |
| 7 | +import os |
| 8 | +import time |
| 9 | + |
| 10 | +import requests |
| 11 | +from wsgi_intercept import add_wsgi_intercept |
| 12 | +from wsgi_intercept import remove_wsgi_intercept |
| 13 | +from wsgi_intercept import requests_intercept |
| 14 | + |
| 15 | +from leancloud import user as user_module |
| 16 | +from leancloud.engine.cookie_session import CookieSessionMiddleware |
| 17 | + |
| 18 | + |
| 19 | +HOST, PORT = 'localhost', 80 |
| 20 | +URL = 'http://{}:{}/'.format(HOST, PORT) |
| 21 | + |
| 22 | +FAKE_USER_DATA = { |
| 23 | + 'sessionToken': 'qmdj8pdidnmyzp0c7yqil91oc', |
| 24 | + 'updatedAt': '2015-07-14T02:31:50.100Z', |
| 25 | + 'phone': '18612340000', |
| 26 | + 'objectId': '55a47496e4b05001a7732c5f', |
| 27 | + 'username': 'fool', |
| 28 | + 'createdAt': '2015-07-14T02:31:50.100Z', |
| 29 | + 'emailVerified': False, |
| 30 | + 'mobilePhoneVerified': False, |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +def application(environ, start_response): |
| 35 | + start_response('200 OK', [('Content-Type', 'text/plain')]) |
| 36 | + return [b'hello!'] |
| 37 | + |
| 38 | + |
| 39 | +def setup(): |
| 40 | + requests_intercept.install() |
| 41 | + |
| 42 | + |
| 43 | +def teardown(): |
| 44 | + requests_intercept.uninstall() |
| 45 | + |
| 46 | + |
| 47 | +def test_cookie_session_middleware(): |
| 48 | + user = user_module.User() |
| 49 | + user._update_data(FAKE_USER_DATA) |
| 50 | + user_module.thread_locals.current_user = user |
| 51 | + |
| 52 | + app = CookieSessionMiddleware(application, b'wtf!') |
| 53 | + add_wsgi_intercept(HOST, PORT, lambda: app) |
| 54 | + |
| 55 | + response = requests.get(URL) |
| 56 | + assert response.cookies['leancloud:session'] |
| 57 | + |
| 58 | + del user_module.thread_locals.current_user |
| 59 | + |
| 60 | + requests.get(URL, cookies=response.cookies) |
| 61 | + current = user_module.User.get_current() |
| 62 | + assert current.id == user.id |
| 63 | + assert current.get_session_token() == user.get_session_token() |
| 64 | + assert not current._attributes |
| 65 | + remove_wsgi_intercept() |
0 commit comments