Coverage for jutil/testing.py : 42%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import json
2from typing import Tuple
3from django.contrib.auth.models import User
4from rest_framework.test import APIClient
5from rest_framework.authtoken.models import Token
8class DefaultTestSetupMixin:
9 user = None
10 token = None
11 api_client = None
12 verbose = True
14 def debug_print(self, *args, **kw):
15 """
16 print() only on verbose=True or self.verbose
17 :param args:
18 :param kw:
19 :return:
20 """
21 if kw.pop('verbose', self.verbose):
22 print(*args)
24 def request(self, method: str, path: str, data: dict, **kw) -> Tuple[dict, int]:
25 """
26 API client request.
27 :param method:
28 :param path:
29 :param data:
30 :param kw:
31 :return:
32 """
33 verbose = kw.pop('verbose', self.verbose)
34 self.debug_print('HTTP {} {} {}'.format(method.upper(), path, data), verbose=verbose)
35 # print('HTTP {method} {data}'.format(method=method.upper(), data=data))
36 # res = getattr(self.api_client, method.lower())(path, data=data)
37 res = getattr(self.api_client, method.lower())(path, data=json.dumps(data), content_type='application/json')
38 reply = res.json()
39 self.debug_print('HTTP {} {} {}: {}'.format(method.upper(), res.status_code, path, reply), verbose=verbose)
40 return reply, res.status_code
42 def post(self, path: str, data: dict, **kw) -> dict:
43 """
44 API client POST
45 :param path:
46 :param data:
47 :param kw:
48 :return:
49 """
50 reply, status_code = self.request('post', path, data, **kw)
51 if status_code >= 300:
52 raise Exception('HTTP {} {} {}: {} {}'.format('POST', status_code, path, data, reply))
53 return reply
55 def get(self, path: str, data: dict, **kw) -> dict:
56 """
57 API client GET
58 :param path:
59 :param data:
60 :param kw:
61 :return:
62 """
63 reply, status_code = self.request('get', path, data, **kw)
64 if status_code >= 300:
65 raise Exception('HTTP {} {} {}: {} {}'.format('GET', status_code, path, data, reply))
66 return reply
68 def put(self, path: str, data: dict, **kw) -> dict:
69 """
70 API client PUT
71 :param path:
72 :param data:
73 :param kw:
74 :return:
75 """
76 reply, status_code = self.request('put', path, data, **kw)
77 if status_code >= 300:
78 raise Exception('HTTP {} {} {}: {} {}'.format('PUT', status_code, path, data, reply))
79 return reply
81 def patch(self, path: str, data: dict, **kw) -> dict:
82 """
83 API client PATCH
84 :param path:
85 :param data:
86 :param kw:
87 :return:
88 """
89 reply, status_code = self.request('patch', path, data, **kw)
90 if status_code >= 300:
91 raise Exception('HTTP {} {} {}: {} {}'.format('PATCH', status_code, path, data, reply))
92 return reply
94 def delete(self, path: str, data: dict, **kw) -> dict:
95 """
96 API client DELETE
97 :param path:
98 :param data:
99 :param kw:
100 :return:
101 """
102 reply, status_code = self.request('delete', path, data, **kw)
103 if status_code >= 300:
104 raise Exception('HTTP {} {} {}: {} {}'.format('DELETE', status_code, path, data, reply))
105 return reply
107 def add_test_user(self, email: str = 'test@example.com', password: str = 'test1234'): # nosec
108 """
109 Add and login test user.
110 :param email:
111 :param password:
112 :return:
113 """
114 self.user = User.objects.filter(username=email).first()
115 if self.user is None: 115 ↛ 117line 115 didn't jump to line 117, because the condition on line 115 was never false
116 self.user = User.objects.create_user(email, email, password) # type: ignore
117 user = self.user
118 assert isinstance(user, User)
119 user.set_password(password)
120 user.save()
121 self.token = Token.objects.get_or_create(user=self.user)[0]
122 self.api_client = APIClient()
123 self.api_client.credentials(HTTP_AUTHORIZATION='Token {}'.format(self.token.key))
124 return self.user