fixtures.py
1.53 KB
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
53
54
55
56
57
58
import json
import pytest
from django.contrib.auth import get_user_model
from oauth2_provider.models import get_application_model
from rest_framework.test import APIClient
User = get_user_model()
CONTENT_TYPE_JSON = 'application/json'
@pytest.fixture
def create_user(username, first_name='Admin', last_name='Root', email=None):
user, created = User.objects.get_or_create(
username=username,
email='{}@root.com'.format(username) if email is None else email,
defaults=dict(
first_name=first_name,
last_name=last_name,
password='password'
)
)
return user
@pytest.fixture
def get_default_test_user():
test_user = create_user(username='test_user', first_name='Test', last_name='User', email='tests@user')
return test_user
def get_client_application():
Application = get_application_model()
application, _ = Application.objects.get_or_create(
name='TestApp',
client_type=Application.CLIENT_CONFIDENTIAL,
authorization_grant_type=Application.GRANT_CLIENT_CREDENTIALS,
skip_authorization=True
)
return application
def client_authorized():
app = get_client_application()
client = APIClient()
r = client.post('/oauth2/token/', {
'grant_type': 'client_credentials',
'client_id': app.client_id,
'client_secret': app.client_secret
})
response = json.loads(r.content)
client = APIClient()
client.credentials(HTTP_AUTHORIZATION='Bearer ' + response['access_token'])
return client