File: test_utils.py

package info (click to toggle)
flask-oauthlib 0.9.5-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 664 kB
  • sloc: python: 4,870; makefile: 155
file content (44 lines) | stat: -rw-r--r-- 1,590 bytes parent folder | download
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
import unittest
import wsgiref.util
from contextlib import contextmanager
import mock
import werkzeug.wrappers
from flask_oauthlib.utils import extract_params
from oauthlib.common import Request


@contextmanager
def set_flask_request(wsgi_environ):
    """
    Test helper context manager that mocks the flask request global I didn't
    need the whole request context just to test the functions in helpers and I
    wanted to be able to set the raw WSGI environment
    """
    environ = {}
    environ.update(wsgi_environ)
    wsgiref.util.setup_testing_defaults(environ)
    r = werkzeug.wrappers.Request(environ)

    with mock.patch.dict(extract_params.__globals__, {'request': r}):
        yield


class UtilsTestSuite(unittest.TestCase):

    def test_extract_params(self):
        with set_flask_request({'QUERY_STRING': 'test=foo&foo=bar'}):
            uri, http_method, body, headers = extract_params()
            self.assertEquals(uri, 'http://127.0.0.1/?test=foo&foo=bar')
            self.assertEquals(http_method, 'GET')
            self.assertEquals(body, {})
            self.assertEquals(headers, {'Host': '127.0.0.1'})

    def test_extract_params_with_urlencoded_json(self):
        wsgi_environ = {
            'QUERY_STRING': 'state=%7B%22t%22%3A%22a%22%2C%22i%22%3A%22l%22%7D'
        }
        with set_flask_request(wsgi_environ):
            uri, http_method, body, headers = extract_params()
            # Request constructor will try to urldecode the querystring, make
            # sure this doesn't fail.
            Request(uri, http_method, body, headers)