Skip to content

Commit ca1c3d1

Browse files
committed
Added password edit route
1 parent 41966f5 commit ca1c3d1

File tree

2 files changed

+150
-25
lines changed

2 files changed

+150
-25
lines changed

PyMatcha.postman_collection.json

Lines changed: 125 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,12 +1580,13 @@
15801580
}
15811581
},
15821582
"url": {
1583-
"raw": "{{host}}/profile/email",
1583+
"raw": "{{host}}/profile/edit/email",
15841584
"host": [
15851585
"{{host}}"
15861586
],
15871587
"path": [
15881588
"profile",
1589+
"edit",
15891590
"email"
15901591
]
15911592
}
@@ -1768,17 +1769,121 @@
17681769
}
17691770
},
17701771
"url": {
1771-
"raw": "{{host}}/profile/email",
1772+
"raw": "{{host}}/profile/edit/email",
17721773
"host": [
17731774
"{{host}}"
17741775
],
17751776
"path": [
17761777
"profile",
1778+
"edit",
17771779
"email"
17781780
]
17791781
}
17801782
},
17811783
"response": []
1784+
},
1785+
{
1786+
"name": "Edit password",
1787+
"event": [
1788+
{
1789+
"listen": "test",
1790+
"script": {
1791+
"id": "f7b0849b-2da8-4c40-b2b2-fb1adde71c16",
1792+
"exec": [
1793+
"var response = JSON.parse(responseBody);",
1794+
"",
1795+
"",
1796+
"pm.test(\"Status code is 200\", function () {",
1797+
" pm.response.to.have.status(200);",
1798+
"});",
1799+
"",
1800+
"",
1801+
"tests[\"success is true\"] = response.success == true",
1802+
"tests[\"Message is correct\"] = response.message == \"User password successfully updated.\"",
1803+
"pm.collectionVariables.set(\"user_password\", \"dolphin2\")"
1804+
],
1805+
"type": "text/javascript"
1806+
}
1807+
}
1808+
],
1809+
"request": {
1810+
"method": "PUT",
1811+
"header": [],
1812+
"body": {
1813+
"mode": "raw",
1814+
"raw": "{\n\t\"old_password\": \"{{user_password}}\",\n\t\"new_password\": \"dolphin2\"\n}",
1815+
"options": {
1816+
"raw": {
1817+
"language": "json"
1818+
}
1819+
}
1820+
},
1821+
"url": {
1822+
"raw": "{{host}}/profile/edit/password",
1823+
"host": [
1824+
"{{host}}"
1825+
],
1826+
"path": [
1827+
"profile",
1828+
"edit",
1829+
"password"
1830+
]
1831+
}
1832+
},
1833+
"response": []
1834+
},
1835+
{
1836+
"name": "Login user after password edit",
1837+
"event": [
1838+
{
1839+
"listen": "test",
1840+
"script": {
1841+
"id": "6e4ec56d-ad5e-450b-805e-230bfd47f4d7",
1842+
"exec": [
1843+
"var response = JSON.parse(responseBody);",
1844+
"",
1845+
"",
1846+
"pm.test(\"Status code is 200\", function () {",
1847+
" pm.response.to.have.status(200);",
1848+
"});",
1849+
"",
1850+
"tests[\"JSON return code is 200\"] = response.code == 200",
1851+
"",
1852+
"tests[\"success is true\"] = response.success == true",
1853+
"",
1854+
"tests[\"Is profile completed is 1\"] = response.return.is_profile_completed == 1",
1855+
"",
1856+
"pm.collectionVariables.set(\"user_access_token\", response.return.access_token);",
1857+
"pm.collectionVariables.set(\"user_refresh_token\", response.return.refresh_token);"
1858+
],
1859+
"type": "text/javascript"
1860+
}
1861+
}
1862+
],
1863+
"request": {
1864+
"method": "POST",
1865+
"header": [],
1866+
"body": {
1867+
"mode": "raw",
1868+
"raw": "{\n\t\"password\": \"{{user_password}}\",\n\t\"username\": \"{{user_email}}\"\n}",
1869+
"options": {
1870+
"raw": {
1871+
"language": "json"
1872+
}
1873+
}
1874+
},
1875+
"url": {
1876+
"raw": "{{host}}/auth/login",
1877+
"host": [
1878+
"{{host}}"
1879+
],
1880+
"path": [
1881+
"auth",
1882+
"login"
1883+
]
1884+
}
1885+
},
1886+
"response": []
17821887
}
17831888
],
17841889
"protocolProfileBehavior": {}
@@ -1801,6 +1906,7 @@
18011906
"pm.collectionVariables.set(\"user_lastname\", \"Bar\")",
18021907
"pm.collectionVariables.set(\"user_gender\", \"other\")",
18031908
"pm.collectionVariables.set(\"user_orientation\", \"bisexual\")",
1909+
"pm.collectionVariables.set(\"user_password\", \"admin\")",
18041910
""
18051911
],
18061912
"type": "text/javascript"
@@ -1839,15 +1945,10 @@
18391945
"method": "GET",
18401946
"header": [],
18411947
"url": {
1842-
"raw": "http://127.0.0.1:5000/debug/redis",
1843-
"protocol": "http",
1948+
"raw": "{{host}}/debug/redis",
18441949
"host": [
1845-
"127",
1846-
"0",
1847-
"0",
1848-
"1"
1950+
"{{host}}"
18491951
],
1850-
"port": "5000",
18511952
"path": [
18521953
"debug",
18531954
"redis"
@@ -1891,91 +1992,91 @@
18911992
],
18921993
"variable": [
18931994
{
1894-
"id": "094262c7-3966-4dfa-a853-f1c5d51c2b44",
1995+
"id": "0b227a65-f661-47b2-8bc4-cbaadf557d17",
18951996
"key": "host",
18961997
"value": "http://127.0.0.1:5000",
18971998
"type": "string"
18981999
},
18992000
{
1900-
"id": "f20e8057-8051-4c8e-9c4e-26a827f3869a",
2001+
"id": "cda2689f-841d-40cf-83f7-126c84c175e5",
19012002
"key": "user_id",
19022003
"value": "",
19032004
"type": "string"
19042005
},
19052006
{
1906-
"id": "e9df5103-fe2c-43d4-9af7-c75f8c2e807d",
2007+
"id": "901e9e11-29dd-4cff-9f8f-5ef222000d7d",
19072008
"key": "user_email",
19082009
"value": "foo@example.org",
19092010
"type": "string"
19102011
},
19112012
{
1912-
"id": "60e17880-09b8-4d3e-8469-b981889cfc2d",
2013+
"id": "873da2ea-7d43-4e13-a409-2a42f5b3fada",
19132014
"key": "user_username",
19142015
"value": "foo",
19152016
"type": "string"
19162017
},
19172018
{
1918-
"id": "17f0f4d6-706b-4b1a-af55-4dce7951b034",
2019+
"id": "11c9af91-cfc0-4bac-a197-6f3efa0edf42",
19192020
"key": "user_password",
19202021
"value": "admin",
19212022
"type": "string"
19222023
},
19232024
{
1924-
"id": "56bde633-3094-4e67-b371-6988ee53f656",
2025+
"id": "4bb1d9e2-3eee-49b0-9591-8a87e4b28a05",
19252026
"key": "user_firstname",
19262027
"value": "Foo",
19272028
"type": "string"
19282029
},
19292030
{
1930-
"id": "b1e5aef2-ccc1-4dcd-9f30-72de325d356f",
2031+
"id": "69f58f99-73b4-4fc6-bfe6-46ff80240ce2",
19312032
"key": "user_lastname",
19322033
"value": "Bar",
19332034
"type": "string"
19342035
},
19352036
{
1936-
"id": "70de06f1-3986-4d0c-8896-fa1d407c9ab7",
2037+
"id": "e305f6b4-a16d-48e0-b3df-da36fffd800b",
19372038
"key": "debug_token",
19382039
"value": "xX69jules69Xx",
19392040
"type": "string"
19402041
},
19412042
{
1942-
"id": "af19a08b-cac2-407e-aad0-a50701a46b00",
2043+
"id": "89274e60-1321-42ab-9158-6518061bbb5d",
19432044
"key": "user_access_token",
19442045
"value": "",
19452046
"type": "string"
19462047
},
19472048
{
1948-
"id": "aa09044e-e5cf-4f2d-b3e7-e36ae5a69768",
2049+
"id": "998a540c-261c-4202-84ea-fb7734042831",
19492050
"key": "user_refresh_token",
19502051
"value": "",
19512052
"type": "string"
19522053
},
19532054
{
1954-
"id": "491d80fa-5b4a-4b45-a20d-bf78b25956c3",
2055+
"id": "3553c278-be1c-4547-80a8-5f2c221a40f9",
19552056
"key": "expired_token",
19562057
"value": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1OTAyNDIyNjQsIm5iZiI6MTU5MDI0MjI2NCwianRpIjoiNTM0ZGI5NzQtOWE4Ni00MGViLWE1NDEtMDg3N2ZmNzQ3NDVhIiwiZXhwIjoxNTkwMjQyMzI0LCJpZGVudGl0eSI6eyJpZCI6NTEyLCJlbWFpbCI6ImZvb0BleGFtcGxlLm9yZyIsInVzZXJuYW1lIjoiYmFyIiwiaXNfb25saW5lIjp0cnVlLCJkYXRlX2xhc3RzZWVuIjoiU2F0LCAyMyBNYXkgMjAyMCAxMzo1Nzo0NCBHTVQifSwiZnJlc2giOnRydWUsInR5cGUiOiJhY2Nlc3MifQ.NTp2P0WNkXDwzuzrcNIQdeAfizQ57HQgkzBLSvY1yHU",
19572058
"type": "string"
19582059
},
19592060
{
1960-
"id": "a371fdfe-b9d2-4c9f-ba66-70d4c9ff2642",
2061+
"id": "b4792aa7-be9a-45e7-9420-4ddae8a688b7",
19612062
"key": "user_orientation",
19622063
"value": "heterosexual",
19632064
"type": "string"
19642065
},
19652066
{
1966-
"id": "cc1b47ba-e150-4129-8875-95508dc6fa71",
2067+
"id": "b4f62225-257f-40dc-b89a-517c054381bd",
19672068
"key": "user_bio",
19682069
"value": "Lorem Ipsum is the single greatest threat. We are not - we are not keeping up with other websites. Lorem Ipsum best not make any more threats to your website. It will be met with fire and fury like the world has never seen. Does everybody know that pig named Lorem Ipsum? An ‘extremely credible source’ has called my office and told me that Barack Obama’s placeholder text is a fraud.",
19692070
"type": "string"
19702071
},
19712072
{
1972-
"id": "4b28aa5c-618c-48ff-a301-d237ce387e3e",
2073+
"id": "f4264583-0652-4208-9490-f130eec59dfb",
19732074
"key": "user_gender",
19742075
"value": "male",
19752076
"type": "string"
19762077
},
19772078
{
1978-
"id": "c8757bb2-7bb4-4b1a-9ac5-f5e2e9b13e5a",
2079+
"id": "6c69f0b0-9c4a-4566-b37f-27c4f445dc6c",
19792080
"key": "user_birthdate",
19802081
"value": "1590674628",
19812082
"type": "string"

backend/PyMatcha/routes/api/profile.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
from flask import request
2626
from PyMatcha.errors import BadRequestError
2727
from PyMatcha.errors import NotFoundError
28+
from PyMatcha.errors import UnauthorizedError
2829
from PyMatcha.models.tag import Tag
2930
from PyMatcha.success import Success
31+
from PyMatcha.utils import hash_password
3032
from PyMatcha.utils.confirm_token import generate_confirmation_token
3133
from PyMatcha.utils.decorators import validate_params
3234
from PyMatcha.utils.mail import send_mail_text
@@ -119,7 +121,7 @@ def edit_profile():
119121
return Success("User successfully modified !")
120122

121123

122-
@profile_bp.route("/profile/email", methods=["PUT"])
124+
@profile_bp.route("/profile/edit/email", methods=["PUT"])
123125
@fjwt.jwt_required
124126
@validate_params({"email": str})
125127
def edit_email():
@@ -138,3 +140,25 @@ def edit_email():
138140
body=os.getenv("APP_URL") + "/auth/confirm/" + token,
139141
)
140142
return Success("Email sent for new email")
143+
144+
145+
@profile_bp.route("/profile/edit/password", methods=["PUT"])
146+
@fjwt.jwt_required
147+
@validate_params({"old_password": str, "new_password": str})
148+
def edit_password():
149+
data = request.get_json()
150+
old_password = data["old_password"]
151+
new_password = data["new_password"]
152+
current_user = fjwt.current_user
153+
if not current_user.check_password(old_password):
154+
raise UnauthorizedError("Incorrect password", "Try again")
155+
current_user.password = hash_password(new_password)
156+
current_user.save()
157+
# TODO: Send mail
158+
send_mail_text.delay(
159+
dest=current_user.email,
160+
subject="Password change notification",
161+
body=f"Your password was changed at {datetime.datetime.utcnow()}."
162+
f" If you believe it wasn't you, please change it immediatly",
163+
)
164+
return Success("User password successfully updated.")

0 commit comments

Comments
 (0)