Skip to content

Commit

Permalink
add admin change password route
Browse files Browse the repository at this point in the history
  • Loading branch information
wuttinanhi committed Oct 22, 2022
1 parent b7d4ee1 commit ec722f5
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
17 changes: 15 additions & 2 deletions auth/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from util.validate_request import ValidateRequest
from werkzeug.exceptions import Unauthorized

from auth.decorator import login_required
from auth.decorator import admin_only, login_required
from auth.function import GetUser

blueprint = Blueprint("auth", __name__, url_prefix="/auth")
Expand All @@ -36,6 +36,10 @@ class ChangePasswordDto(Schema):
new_password = fields.Str(required=True, validate=validate.Length(min=8, max=50))


class AdminChangePasswordDto(ChangePasswordDto):
user_id = fields.Int(required=True)


@blueprint.route("/login", methods=["POST"])
def login():
data = ValidateRequest(LoginDto, request)
Expand Down Expand Up @@ -79,5 +83,14 @@ def change_password():
raise Unauthorized("Invalid password!")

UserService.change_password(user, data.new_password)


return {"message": "Successfully change password."}, 200


@blueprint.route("/admin/changepassword", methods=["PATCH"])
@admin_only
def admin_change_password():
data = ValidateRequest(AdminChangePasswordDto, request)
user = UserService.find_by_id(data.user_id)
UserService.change_password(user, data.new_password)
return {"message": "Successfully change password."}, 200
2 changes: 1 addition & 1 deletion database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


def get_engine():
return create_engine(os.getenv("DATABASE_URI"), echo=True)
return create_engine(os.getenv("DATABASE_URI"), echo=False)


engine = get_engine()
Expand Down
7 changes: 6 additions & 1 deletion thunder-tests/thunderCollection.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
"sortNum": 10000,
"folders": [],
"settings": {
"headers": [],
"headers": [
{
"name": "X-API-KEY",
"value": "{{ADMIN_KEY}}"
}
],
"auth": {
"type": "bearer",
"bearer": "{{JWT_TOKEN}}"
Expand Down
59 changes: 58 additions & 1 deletion thunder-tests/thunderclient.json
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@
"method": "POST",
"sortNum": 22500,
"created": "2022-10-01T14:54:35.260Z",
"modified": "2022-10-02T03:41:38.346Z",
"modified": "2022-10-22T11:50:04.670Z",
"headers": [],
"params": [],
"body": {
Expand Down Expand Up @@ -731,5 +731,62 @@
"form": []
},
"tests": []
},
{
"_id": "217a3951-af42-430d-a2ae-8eab62eb46c6",
"colId": "55d84e90-6849-46a6-b752-48f30ef9a83c",
"containerId": "",
"name": "changepassword",
"url": "{{BASE_API_URL}}/auth/changepassword",
"method": "PATCH",
"sortNum": 10000,
"created": "2022-10-22T11:47:51.829Z",
"modified": "2022-10-22T11:49:59.653Z",
"headers": [],
"params": [],
"body": {
"type": "json",
"raw": "{\n \"password\": \"annie-password\",\n \"new_password\": \"annie-password\"\n}\n",
"form": []
},
"tests": []
},
{
"_id": "7e96d1fe-0310-4675-a071-5e8ffc38a65b",
"colId": "55d84e90-6849-46a6-b752-48f30ef9a83c",
"containerId": "",
"name": "admin/changepassword",
"url": "{{BASE_API_URL}}/auth/admin/changepassword",
"method": "PATCH",
"sortNum": 5000,
"created": "2022-10-22T11:53:14.849Z",
"modified": "2022-10-22T11:55:04.408Z",
"headers": [],
"params": [],
"body": {
"type": "json",
"raw": "{\n \"user_id\": 2,\n \"password\": \"annie-password\",\n \"new_password\": \"annie-password-new\"\n}\n",
"form": []
},
"tests": []
},
{
"_id": "76616e54-1c52-4c0b-bb41-8fca8db8ba23",
"colId": "55d84e90-6849-46a6-b752-48f30ef9a83c",
"containerId": "",
"name": "login annie (with new password)",
"url": "{{BASE_API_URL}}/auth/login",
"method": "POST",
"sortNum": 23750,
"created": "2022-10-22T11:55:10.172Z",
"modified": "2022-10-22T11:55:22.028Z",
"headers": [],
"params": [],
"body": {
"type": "json",
"raw": "{\n \"email\": \"[email protected]\",\n \"password\": \"annie-password-new\"\n}",
"form": []
},
"tests": []
}
]

0 comments on commit ec722f5

Please sign in to comment.