Skip to content

Commit

Permalink
M2-5595: Error message appears when trying to reset password (#1146)
Browse files Browse the repository at this point in the history
### πŸ“ Description

πŸ”— [Jira Ticket M2-5595](https://mindlogger.atlassian.net/browse/M2-5595)

This PR adds support for the mobile value of the `MindLogger-Content-Source` HTTP header. The PR #1100 started making use of this HTTP header, but didn't account for the mobile value so this fixes that. I've also added a test case for it
  • Loading branch information
sultanofcardio authored Mar 4, 2024
1 parent c956bd6 commit 813bf78
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
66 changes: 66 additions & 0 deletions src/apps/users/tests/test_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,72 @@ async def test_password_recovery_admin(self, client: TestClient, user_create: Us
assert len(TestMail.mails) == 4
assert TestMail.mails[0].recipients[0] == password_recovery_request.email

async def test_password_recovery_mobile(self, client: TestClient, user_create: UserCreate):
# Password recovery
password_recovery_request: PasswordRecoveryRequest = PasswordRecoveryRequest(email=user_create.dict()["email"])

response = await client.post(
url=self.password_recovery_url,
data=password_recovery_request.dict(),
headers={"MindLogger-Content-Source": "mobile"},
)

cache = RedisCache()

assert response.status_code == status.HTTP_201_CREATED
keys = await cache.keys(key=f"PasswordRecoveryCache:{user_create.email}*")
assert len(keys) == 1
assert password_recovery_request.email in keys[0]
assert len(TestMail.mails) == 5
assert TestMail.mails[0].recipients[0] == password_recovery_request.email
assert TestMail.mails[0].subject == "MindLogger"

response = await client.post(
url=self.password_recovery_url,
data=password_recovery_request.dict(),
)

assert response.status_code == status.HTTP_201_CREATED

new_keys = await cache.keys(key=f"PasswordRecoveryCache:{user_create.email}*")
assert len(keys) == 1
assert keys[0] != new_keys[0]
assert len(TestMail.mails) == 6
assert TestMail.mails[0].recipients[0] == password_recovery_request.email

async def test_password_recovery_invalid(self, client: TestClient, user_create: UserCreate):
# Password recovery
password_recovery_request: PasswordRecoveryRequest = PasswordRecoveryRequest(email=user_create.dict()["email"])

response = await client.post(
url=self.password_recovery_url,
data=password_recovery_request.dict(),
headers={"MindLogger-Content-Source": "invalid-content-source"},
)

cache = RedisCache()

assert response.status_code == status.HTTP_201_CREATED
keys = await cache.keys(key=f"PasswordRecoveryCache:{user_create.email}*")
assert len(keys) == 1
assert password_recovery_request.email in keys[0]
assert len(TestMail.mails) == 7
assert TestMail.mails[0].recipients[0] == password_recovery_request.email
assert TestMail.mails[0].subject == "MindLogger"

response = await client.post(
url=self.password_recovery_url,
data=password_recovery_request.dict(),
)

assert response.status_code == status.HTTP_201_CREATED

new_keys = await cache.keys(key=f"PasswordRecoveryCache:{user_create.email}*")
assert len(keys) == 1
assert keys[0] != new_keys[0]
assert len(TestMail.mails) == 8
assert TestMail.mails[0].recipients[0] == password_recovery_request.email

async def test_password_recovery_approve(self, client: TestClient, user_create: UserCreate):
cache = RedisCache()

Expand Down
11 changes: 7 additions & 4 deletions src/infrastructure/http/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ async def get_mindlogger_content_source(
) -> MindloggerContentSource:
"""Fetch the Mindlogger-Content-Source HTTP header."""

return getattr(
MindloggerContentSource,
request.headers.get("mindlogger-content-source", MindloggerContentSource.web.name),
)
try:
return getattr(
MindloggerContentSource,
request.headers.get("mindlogger-content-source", MindloggerContentSource.web.name),
)
except AttributeError:
return MindloggerContentSource.web


def get_language(request: Request) -> str:
Expand Down
1 change: 1 addition & 0 deletions src/infrastructure/http/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ class MindloggerContentSource(str, Enum):

web = "web"
admin = "admin"
mobile = "mobile"

0 comments on commit 813bf78

Please sign in to comment.