File: testutils.py

package info (click to toggle)
drf-extensions 0.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,308 kB
  • sloc: python: 7,421; makefile: 11
file content (52 lines) | stat: -rw-r--r-- 1,346 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
45
46
47
48
49
50
51
52
import base64
try:
    from unittest.mock import patch
except ImportError:
    from mock import patch

from rest_framework import HTTP_HEADER_ENCODING

from rest_framework_extensions.key_constructor import bits
from rest_framework_extensions.key_constructor.constructors import KeyConstructor


def get_url_pattern_by_regex_pattern(patterns, pattern_string):
    # todo: test me
    for pattern in patterns:
        if pattern.pattern.regex.pattern == pattern_string:
            return pattern


def override_extensions_api_settings(**kwargs):
    return patch.multiple(
        'rest_framework_extensions.settings.extensions_api_settings',
        **kwargs
    )


def basic_auth_header(username, password):
    credentials = ('%s:%s' % (username, password))
    base64_credentials = base64.b64encode(
        credentials.encode(HTTP_HEADER_ENCODING)
    ).decode(HTTP_HEADER_ENCODING)
    return 'Basic %s' % base64_credentials


class TestFormatKeyBit(bits.KeyBitBase):
    def get_data(self, **kwargs):
        return u'json'


class TestLanguageKeyBit(bits.KeyBitBase):
    def get_data(self, **kwargs):
        return u'ru'


class TestUsedKwargsKeyBit(bits.KeyBitBase):
    def get_data(self, **kwargs):
        return kwargs


class TestKeyConstructor(KeyConstructor):
    format = TestFormatKeyBit()
    language = TestLanguageKeyBit()