-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathtest_searching.py
More file actions
204 lines (159 loc) · 7.03 KB
/
test_searching.py
File metadata and controls
204 lines (159 loc) · 7.03 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from os import environ
from app.utils import random_string
from .helpers import get_api_key
def test_search(
module_client, module_db, fake_auth_from_oc, fake_algolia_save,
fake_algolia_search):
client = module_client
first_term = random_string()
apikey = get_api_key(client)
# Create resource and find it in the search results.
resource = client.post(
"/api/v1/resources",
json=[dict(
name=f"{first_term}",
category="Website",
url=f"{first_term}",
paid=False,
)],
headers={'x-apikey': apikey}
)
result = client.get(f"/api/v1/search?q={first_term}")
assert (resource.status_code == 200)
assert (result.status_code == 200)
assert (
result.json['resources'][0]['url'] == resource.json['resources'][0].get('url'))
# Update the resource and test that search results reflect changes
updated_term = random_string()
resource_id = resource.json['resources'][0].get('id')
resource = client.put(f"/api/v1/resources/{resource_id}",
json=dict(url=f"{updated_term}",),
headers={'x-apikey': apikey})
result = client.get(f"/api/v1/search?q={updated_term}")
assert (resource.status_code == 200)
assert (result.status_code == 200)
assert (result.json['resources'][0]['url'] == resource.json['resource'].get('url'))
def test_search_paid_filter(module_client,
fake_algolia_search):
client = module_client
# Test on the unpaid resources
result = client.get("/api/v1/search?paid=false")
assert (result.status_code == 200)
# Test on the paid resources
result = client.get("/api/v1/search?paid=true")
assert (result.status_code == 200)
# Test with invalid paid attribute given ( defaults to all )
result = client.get("/api/v1/search?paid=something")
assert (result.status_code == 200)
def test_search_category_filter(module_client,
fake_algolia_search):
client = module_client
# Test on book resources
result = client.get("/api/v1/search?category=books")
assert (result.status_code == 200)
result = client.get("/api/v1/search?category=Books")
assert (result.status_code == 200)
# Test with invalid category attribute given ( defaults to all )
result = client.get("/api/v1/search?category=")
assert (result.status_code == 200)
def test_search_language_filter(module_client,
fake_algolia_search):
client = module_client
# Test on Python resources
result = client.get("/api/v1/search?languages=python")
assert (result.status_code == 200)
result = client.get("/api/v1/search?languages=Python")
assert (result.status_code == 200)
# Test on multiple languages
result = client.get("/api/v1/search?languages=python&languages=javascript")
assert (result.status_code == 200)
# Tests with invalid languages attribute given ( defaults to all )
result = client.get("/api/v1/search?languages=")
assert (result.status_code == 200)
result = client.get("/api/v1/search?languages=test&languages=")
assert (result.status_code == 200)
def test_algolia_exception_error(module_client,
module_db,
fake_auth_from_oc,
fake_algolia_exception):
# Reset the FLASK_ENV to run Algolia error handling code
environ["FLASK_ENV"] = "not_development"
client = module_client
first_term = random_string()
apikey = get_api_key(client)
response = client.get("/api/v1/search?q=python")
assert (response.status_code == 500)
assert (
"Algolia" in response.get_json().get("errors")[0].get("algolia-failed").get(
"message")
)
response = client.post("/api/v1/resources",
json=[dict(
name=f"{first_term}",
category="Website",
url=f"{first_term}",
paid=False,
)],
headers={'x-apikey': apikey}
)
assert (response.status_code == 500)
assert ("Algolia" in response.get_json().get("errors")[0].get("algolia-failed").get(
"message"))
updated_term = random_string()
response = client.put("/api/v1/resources/1",
json=dict(
name="New name",
languages=["New language"],
category="New Category",
url=f"https://{updated_term}.url",
paid=False,
notes="New notes"
),
headers={'x-apikey': apikey}
)
assert (response.status_code == 500)
assert ("Algolia" in response.get_json().get("errors")[0].get("algolia-failed").get(
"message"))
# Set it back to development for other tests
environ["FLASK_ENV"] = "development"
def test_algolia_unreachable_host_error(module_client,
module_db,
fake_auth_from_oc,
fake_algolia_unreachable_host,):
# Reset the FLASK_ENV to run Algolia error handling code
environ["FLASK_ENV"] = "not_development"
client = module_client
first_term = random_string()
apikey = get_api_key(client)
response = client.get("/api/v1/search?q=python")
assert (response.status_code == 500)
assert ("Algolia" in response.get_json().get("errors")[0].get("algolia-failed").get(
"message"))
response = client.post("/api/v1/resources",
json=[dict(
name=f"{first_term}",
category="Website",
url=f"{first_term}",
paid=False,
)],
headers={'x-apikey': apikey})
assert (response.status_code == 500)
assert ("Algolia" in response.get_json().get("errors")[0].get("algolia-failed").get(
"message"))
updated_term = random_string()
response = client.put("/api/v1/resources/1",
json=dict(
name="New name",
languages=["New language"],
category="New Category",
url=f"https://{updated_term}.url",
paid=False,
notes="New notes"
),
headers={'x-apikey': apikey}
)
assert (response.status_code == 500)
assert ("Algolia" in response.get_json().get("errors")[0].get("algolia-failed").get(
"message"))
# Set it back to development for other tests
environ["FLASK_ENV"] = "development"