File: utils.py

package info (click to toggle)
django-oauth-toolkit 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,852 kB
  • sloc: python: 12,414; makefile: 159; javascript: 9
file content (32 lines) | stat: -rw-r--r-- 779 bytes parent folder | download | duplicates (3)
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
import base64
from unittest import mock


def get_basic_auth_header(user, password):
    """
    Return a dict containing the correct headers to set to make HTTP Basic
    Auth request
    """
    user_pass = "{0}:{1}".format(user, password)
    auth_string = base64.b64encode(user_pass.encode("utf-8"))
    auth_headers = {
        "HTTP_AUTHORIZATION": "Basic " + auth_string.decode("utf-8"),
    }

    return auth_headers


def spy_on(meth):
    """
    Util function to add a spy onto a method of a class.
    """
    spy = mock.MagicMock()

    def wrapper(self, *args, **kwargs):
        spy(self, *args, **kwargs)
        return_value = meth(self, *args, **kwargs)
        spy.returned = return_value
        return return_value

    wrapper.spy = spy
    return wrapper