_Client   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 10
c 0
b 0
f 0
wmc 4
1
# -*- coding: utf-8 -*-
2
# Standard Library
3
import functools
4
5
# Third Party Stuff
6
from unittest import mock
7
import pytest
8
9
10
class PartialMethodCaller:
11
    def __init__(self, obj, **partial_params):
12
        self.obj = obj
13
        self.partial_params = partial_params
14
15
    def __getattr__(self, name):
16
        return functools.partial(
17
            getattr(self.obj, name), **self.partial_params)
18
19
20
@pytest.fixture
21
def client():
22
    '''Overrides default client fixture adding a mocked
23
    up login method and a json() helper
24
    '''
25
    from django.test.client import Client
26
27
    class _Client(Client):
28
        def login(
29
            self, user=None,
30
                backend="django.contrib.auth.backends.ModelBackend",
31
                **credentials):
32
            if user is None:
33
                return super(_Client, self).login(**credentials)
34
35
            with mock.patch('django.contrib.auth.authenticate') as authenticate:
36
                user.backend = backend
37
                authenticate.return_value = user
38
                return super(_Client, self).login(**credentials)
39
40
        @property
41
        def json(self):
42
            return PartialMethodCaller(
43
                obj=self, content_type='application/json;charset="utf-8"')
44
45
    return _Client()
46
47
48
@pytest.fixture
49
def base_url(live_server):
50
    return live_server.url
51
52
53
@pytest.fixture
54
def outbox():
55
    from django.core import mail
56
57
    return mail.outbox
58