Skip to content

Commit ac1b9e3

Browse files
authored
Explicitly raise 405 on unimpleneted method calls (#514)
* Raise 405 by default * Fix test * Fix test
1 parent 9a51c41 commit ac1b9e3

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

gramps_webapi/api/resources/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
"""API resource endpoints."""
2121

22+
from flask import abort
2223
from flask.views import MethodView
2324

2425
from ..auth import (
@@ -32,6 +33,26 @@
3233
class Resource(MethodView):
3334
"""Base class for API resources."""
3435

36+
def get(self, *args, **kwargs):
37+
"""Default GET endpoint."""
38+
abort(405)
39+
40+
def put(self, *args, **kwargs):
41+
"""Default PUT endpoint."""
42+
abort(405)
43+
44+
def post(self, *args, **kwargs):
45+
"""Default POST endpoint."""
46+
abort(405)
47+
48+
def delete(self, *args, **kwargs):
49+
"""Default DELETE endpoint."""
50+
abort(405)
51+
52+
def patch(self, *args, **kwargs):
53+
"""Default PATCH endpoint."""
54+
abort(405)
55+
3556

3657
class ProtectedResource(Resource):
3758
"""Resource requiring JWT authentication."""

tests/test_endpoints/test_user.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,16 @@ def tearDown(self):
112112
self.dbman.remove_database(self.name)
113113

114114
def test_change_password_wrong_method(self):
115-
rv = self.client.get(BASE_URL + "/users/-/password/change")
116-
assert rv.status_code == 404
115+
rv = self.client.post(
116+
BASE_URL + "/token/", json={"username": "user", "password": "123"}
117+
)
118+
assert rv.status_code == 200
119+
token = rv.json["access_token"]
120+
rv = self.client.get(
121+
BASE_URL + "/users/-/password/change",
122+
headers={"Authorization": f"Bearer {token}"},
123+
)
124+
assert rv.status_code == 405
117125

118126
def test_change_password_no_token(self):
119127
rv = self.client.post(

0 commit comments

Comments
 (0)