-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_urls.py
181 lines (142 loc) · 6.07 KB
/
test_urls.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import json
from urllib.error import HTTPError
import pytest
import requests
from django.contrib.auth import get_user_model
from django.contrib.auth.models import UserManager
from gql import Client
from gql import gql
from gql.transport.requests import RequestsHTTPTransport
from graphqlclient import GraphQLClient
from requests.auth import HTTPBasicAuth
from rest_framework.authtoken.models import Token
from rest_framework.test import APIClient
from api.models import Category
from api.models import Ingredient
@pytest.fixture
def prepare_db():
fake_category_name = "fake-category-name"
fake_category = Category.objects.create(name=fake_category_name)
fake_ingredient_notes = "fake_ingredient_notes"
fake_ingredient_name = "fake_ingredient_name"
Ingredient.objects.create(name=fake_ingredient_name, notes=fake_ingredient_notes, category=fake_category)
@pytest.fixture
def get_user_from_admin():
User: UserManager = get_user_model().objects
some_user = User.all().first()
return some_user if some_user else User.create_superuser("random-user", None, "random-password")
@pytest.mark.django_db
def test_should_get_401_as_categories_api_is_protected():
response = APIClient().get("/api/v1/categories/")
assert response.status_code == 401
@pytest.mark.django_db
def test_should_get_401_as_ingredients_api_is_protected():
response = APIClient().get("/api/v1/ingredients/")
assert response.status_code == 401
@pytest.mark.django_db
def test_should_generate_token_from_registered_user():
get_user_model().objects.create_superuser("fake-api-user", None, "fake-api-password")
data = {"username": "fake-api-user", "password": "fake-api-password"}
response = APIClient().post("/api/auth-token/", json.dumps(data), content_type="application/json")
assert response.status_code == 200
assert type(response.data["token"]) == str
assert len(response.data["token"]) == 40
def test_should_acess_categories_api_with_authenticated_user(live_server):
salt_api_user = get_user_model().objects.create_superuser("salt-api-user", None, "salt-api-password")
created_token = Token.objects.create(key="22216a91b7ddbd7331f0b0ed3af085412f2729de", user=salt_api_user)
headers = {"Authorization": f"Token {created_token.key}", "Content-Type": "application/json"}
response = requests.get(f"{live_server.url}/api/v1/categories/", headers=headers)
assert response.status_code == 200
headers = {"Content-Type": "application/json"}
response = requests.get(f"{live_server.url}/api/v1/categories/", headers=headers)
assert response.status_code == 401
def test_should_authenticate_with_basic_authentication(live_server):
get_user_model().objects.create_superuser("ant-api-user", None, "ant-api-password")
session = requests.Session()
session.auth = ("ant-api-user", "ant-api-password")
response = session.get(f"{live_server.url}/api/v1/categories/")
assert response.status_code == 200
assert requests.get(f"{live_server.url}/api/v1/categories/").status_code == 401
def test_prisma_python_graphql_client(live_server, get_user_from_admin):
"""
Know more at: https://github.com/prisma/python-graphql-client
"""
some_user = get_user_from_admin
created_token = Token.objects.create(key="6c5d69c150b32dcb9c746672c0185d6d8454ca21", user=some_user)
client = GraphQLClient(f"{live_server.url}/api/graphql/")
query = """
query {
allCategories{
id
name
ingredients {
id
name
notes
}
}
}
"""
try:
client.execute(query)
except Exception as e:
assert type(e) == HTTPError
assert e.code == 401
client.inject_token(f"Token {created_token}")
result = client.execute(query)
assert result == '{"data":{"allCategories":[]}}'
@pytest.mark.skip(reason="Apparently no way of currently testing this")
def test_gql_client(live_server, prepare_db, get_user_from_admin):
"""
Know more at: https://github.com/graphql-python/gql
"""
url = f"{live_server.url}/api/graphql/"
transport = RequestsHTTPTransport(url=url, use_json=True)
try:
Client(retries=3, transport=transport, fetch_schema_from_transport=True)
except Exception as e:
assert type(e) == requests.exceptions.HTTPError
assert e.response.status_code == 401
some_user = get_user_from_admin
created_token = Token.objects.create(key="7c5d69c150b32dcb9c746672c0185d6d8454ca21", user=some_user)
# One way to authenticate
headers = {"Authorization": f"Token {created_token}"}
transport = RequestsHTTPTransport(url=url, use_json=True, headers=headers)
Client(retries=3, transport=transport, fetch_schema_from_transport=True)
# Another way
transport = RequestsHTTPTransport(url=url, use_json=True, auth=HTTPBasicAuth("fake", "situation"))
try:
Client(retries=3, transport=transport, fetch_schema_from_transport=True)
except Exception as e:
assert type(e) == requests.exceptions.HTTPError
assert e.response.status_code == 401
password = "MyHonestSaltPassword"
some_user.set_password(password)
some_user.save()
transport = RequestsHTTPTransport(url=url, use_json=True, auth=HTTPBasicAuth(some_user.username, password))
client = Client(retries=3, transport=transport, fetch_schema_from_transport=True)
query = gql(
"""
query {
allCategories{
id
name
ingredients {
id
name
notes
}
}
}
"""
)
result = client.execute(query)
assert len(result["allCategories"]) == 1
for category in result["allCategories"]:
assert category["id"] == "1"
assert category["name"] == "fake-category-name"
assert len(category["ingredients"]) == 1
for ingredient in category["ingredients"]:
assert ingredient["id"] == "1"
assert ingredient["name"] == "fake_ingredient_name"
assert ingredient["notes"] == "fake_ingredient_notes"