Skip to content

Commit 9235d17

Browse files
authored
chore: update test suite (#5)
* chore: add debugging options for flask app * test: rename fixtures * test: add pytest configuration file * refactor: fix tests pylint issues * chore: update pytest debugger configuration * fix: change aunthentication decorator * fix: update tests * test: rename fixtures database models * test: add missing test assertions
1 parent 4ca7105 commit 9235d17

File tree

19 files changed

+308
-161
lines changed

19 files changed

+308
-161
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ README.rst
99
README.md
1010
*.pyc
1111
__pycache__
12+
logs/*

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ MANIFEST
3434
*.spec
3535

3636
# Installer logs
37+
logs/*
3738
pip-log.txt
3839
pip-delete-this-directory.txt
3940

cli/test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55

66

77
@click.command()
8-
@click.argument("path", default="tests")
9-
def test(path):
8+
def test():
109
"""
1110
Run tests with Pytest (pytest-sugar).
1211
1312
:param path: Test path
1413
:return: Subprocess call result
1514
"""
16-
command = f"py.test -s {path}"
15+
command = "py.test --capture=no"
1716
return subprocess.call(command, shell=True)

docker/local/application/start

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ set -o errexit
44
set -o pipefail
55
set -o nounset
66

7-
flask run -h 0.0.0.0 -p 8000
7+
python -m debugpy --listen 0.0.0.0:5678 wsgi.py --wait-for-client --multiprocess -m flask run -h 0.0.0.0 -p 5000

local.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ services:
1515
- ./.envs/.local/.application
1616
- ./.envs/.local/.mongo
1717
ports:
18+
- "5678:5678"
1819
- "8000:8000"
1920
command: /start
2021

pytest.ini

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[pytest]
2+
addopts = -ra -q --pdbcls=IPython.terminal.debugger:TerminalPdb
3+
minversion = 7.0
4+
python_files =
5+
test_*.py
6+
python_functions =
7+
test_*
8+
required_plugins =
9+
pytest-sugar>=0.9.4
10+
pytest-cov>=3.0.0
11+
pytest-flask>=1.2.0
12+
pytest-factoryboy>=2.4.0
13+
testpaths =
14+
tests

quotes_api/api/resources/quote.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def get(self, quote_id):
127127
"""Get quote by id."""
128128
try:
129129
quote = Quote.objects.get_or_404(id=quote_id)
130+
130131
except Exception:
131132
return (
132133
{"error": "Quote does not exist."},
@@ -140,8 +141,13 @@ def put(self, quote_id):
140141
"""Replace entire quote."""
141142
try:
142143
quote = Quote.objects.get_or_404(id=quote_id)
144+
143145
except Exception:
144-
return {"error": "Quote does not exist."}
146+
return (
147+
{"error": "Quote does not exist."},
148+
HttpStatus.NOT_FOUND_404.value,
149+
)
150+
145151
try:
146152
# Create quote schema instance
147153
quote_schema = QuoteSchema()
@@ -151,6 +157,7 @@ def put(self, quote_id):
151157
quote.save()
152158

153159
return "", HttpStatus.NO_CONTENT_204.value
160+
154161
except Exception:
155162
return (
156163
{"error": "Missing data."},
@@ -162,11 +169,13 @@ def patch(self, quote_id):
162169
"""Update quote fields."""
163170
try:
164171
quote = Quote.objects.get_or_404(id=quote_id)
172+
165173
except Exception:
166174
return (
167175
{"error": "Quote does not exist."},
168176
HttpStatus.NOT_FOUND_404.value,
169177
)
178+
170179
try:
171180
# Check quote schema instance
172181
quote_schema = QuoteSchema(partial=True)
@@ -176,6 +185,7 @@ def patch(self, quote_id):
176185
quote.save()
177186

178187
return "", HttpStatus.NO_CONTENT_204.value
188+
179189
except Exception:
180190
return {"error": "Missing data."}, HttpStatus.BAD_REQUEST_400.value
181191

@@ -184,6 +194,7 @@ def delete(self, quote_id):
184194
"""Delete quote."""
185195
try:
186196
quote = Quote.objects.get_or_404(id=quote_id)
197+
187198
except Exception:
188199
return (
189200
{"error": "Quote does not exist."},
@@ -193,6 +204,7 @@ def delete(self, quote_id):
193204
try:
194205
quote.delete()
195206
return "", HttpStatus.NO_CONTENT_204.value
207+
196208
except Exception:
197209
return (
198210
{"error": "Could not delete quote."},
@@ -334,8 +346,10 @@ def post(self):
334346
# Create quote schema instace
335347
quote_schema = QuoteSchema()
336348
data = quote_schema.load(request.json)
349+
337350
except Exception:
338351
return {"error": "Missing data."}, HttpStatus.BAD_REQUEST_400.value
352+
339353
try:
340354
# Create new database entry
341355
quote = Quote(**data)
@@ -453,8 +467,8 @@ def get(self):
453467

454468
# Converting CommandCursor class iterator into a list and
455469
# then getting the only item in it
456-
random_quote = list(quote_collection.aggregate(pipeline))
457-
random_quote["id"] = random_quote["_id"]
470+
random_quote = list(quote_collection.aggregate(pipeline))[0]
471+
random_quote["id"] = random_quote.pop("_id")
458472

459473
# Create quote schema instance
460474
quote_schema = QuoteSchema()

quotes_api/api/schemas/quote.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class QuoteSchema(ma.Schema):
77
"""Marshmallow quote schema."""
88

99
id = ma.String(dump_only=True)
10-
quote_text = ma.String()
11-
author_name = ma.String()
10+
quote_text = ma.String(required=True)
11+
author_name = ma.String(required=True)
1212
author_image = ma.URL()
13-
tags = ma.List(ma.String())
13+
tags = ma.List(ma.String(required=True))

quotes_api/auth/decorators.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ def wrapper(*args, **kwargs):
3838
for role in roles:
3939
if Role(role) not in role_list:
4040
denied = True
41+
42+
if Role(role) in role_list:
43+
denied = False
4144
break
4245

4346
if denied:

quotes_api/auth/resources/token.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ class RefreshTokenRevoke(Resource):
234234

235235
@jwt_required(refresh=True)
236236
def delete(self):
237-
"""Revokes a refresh token from the database.
237+
"""
238+
Revokes a refresh token from the database.
238239
239240
Used mainly for logout.
240241
"""

quotes_api/auth/resources/user.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ def get(self, user_id):
310310
"""Get user by id."""
311311
try:
312312
user = User.objects.get_or_404(id=user_id)
313+
313314
except Exception:
314315
return (
315316
{"error": "User does not exist."},
@@ -325,8 +326,13 @@ def put(self, user_id):
325326
"""Replace entire user."""
326327
try:
327328
user = User.objects.get_or_404(id=user_id)
329+
328330
except Exception:
329-
return {"error": "User does not exist."}
331+
return (
332+
{"error": "User does not exist."},
333+
HttpStatus.NOT_FOUND_404.value,
334+
)
335+
330336
try:
331337
# Create user schema instance
332338
user_schema = UserSchema()
@@ -345,11 +351,13 @@ def patch(self, user_id):
345351
"""Update user fields."""
346352
try:
347353
user = User.objects.get_or_404(id=user_id)
354+
348355
except Exception:
349356
return (
350357
{"error": "User does not exist."},
351358
HttpStatus.NOT_FOUND_404.value,
352359
)
360+
353361
try:
354362
# Create user schema instance and ignore any missing fields
355363
user_schema = UserSchema(partial=True)
@@ -369,14 +377,17 @@ def delete(self, user_id):
369377

370378
try:
371379
user = User.objects.get_or_404(id=user_id)
380+
372381
except Exception:
373382
return (
374383
{"error": "User does not exist."},
375384
HttpStatus.NOT_FOUND_404.value,
376385
)
386+
377387
try:
378388
user.delete()
379389
return "", HttpStatus.NO_CONTENT_204.value
390+
380391
except Exception:
381392
return (
382393
{"error": "Could not delete user"},

quotes_api/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class TestingConfig(Config):
5454
SECRET_KEY = "testing"
5555

5656
# Mongoengine Configuration
57-
MONGODB_DB = "quotes_test_database"
57+
MONGODB_DB = "test_quotes_database"
5858
MONGODB_HOST = "mongo"
5959

6060

0 commit comments

Comments
 (0)