Skip to content

Commit

Permalink
Add Users.send_verification_email() method (#201)
Browse files Browse the repository at this point in the history
* Add `Users.send_verification_email()` method

* Fix Magic Auth challenge

* Fix test
  • Loading branch information
jonatascastro12 authored Aug 24, 2023
1 parent 9b619a4 commit 7b70e0c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
22 changes: 22 additions & 0 deletions tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ def mock_password_challenge_response(self):
"token": "token_123",
}

@pytest.fixture
def mock_magic_auth_challenge_response(self):
return {
"id": "auth_challenge_01E4ZCR3C56J083X43JQXF3JK5",
}

def test_create_user(self, mock_user, mock_request_method):
mock_request_method("post", mock_user, 201)

Expand Down Expand Up @@ -324,3 +330,19 @@ def test_complete_password_reset(self, capture_and_mock_request, mock_user):
assert response["id"] == "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"
assert request["json"]["token"] == token
assert request["json"]["new_password"] == new_password

def test_send_verification_email(
self, capture_and_mock_request, mock_magic_auth_challenge_response
):
user = "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"

url, _ = capture_and_mock_request(
"post", mock_magic_auth_challenge_response, 200
)

response = self.users.send_verification_email(user=user)

assert url[0].endswith(
"users/user_01H7ZGXFP5C6BBQY6Z7277ZCT0/send_verification_email"
)
assert response["id"] == "auth_challenge_01E4ZCR3C56J083X43JQXF3JK5"
13 changes: 13 additions & 0 deletions workos/resources/magic_auth_challenge_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from workos.resources.base import WorkOSBaseResource


class WorkOSMagicAuthChallenge(WorkOSBaseResource):
"""Representation of a MagicAuthChallenge identifier as returned by WorkOS through User Management features.
Attributes:
OBJECT_FIELDS (list): List of fields a WorkOSMagicAuthChallenge comprises.
"""

OBJECT_FIELDS = [
"id",
]
28 changes: 27 additions & 1 deletion workos/users.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import workos
from workos.resources.authentication_response import WorkOSAuthenticationResponse
from workos.resources.magic_auth_challenge_response import WorkOSMagicAuthChallenge
from workos.resources.password_challenge_response import WorkOSPasswordChallengeResponse
from workos.resources.list import WorkOSListResource
from workos.resources.users import (
Expand All @@ -20,6 +21,7 @@
USER_SESSION_TOKEN_PATH = "users/session/token"
USER_PASSWORD_RESET_CHALLENGE_PATH = "users/password_reset_challenge"
USER_PASSWORD_RESET_PATH = "users/password_reset"
USER_SEND_VERIFICATION_EMAIL_PATH = "users/{0}/send_verification_email"

RESPONSE_LIMIT = 10

Expand Down Expand Up @@ -390,7 +392,7 @@ def complete_password_reset(
new_password (str): The new password to be set for the user.
Returns:
dict: User response from WorkOS.
dict: User response from WorkOS.
"""

headers = {}
Expand All @@ -409,3 +411,27 @@ def complete_password_reset(
)

return WorkOSUser.construct_from_response(response).to_dict()

def send_verification_email(
self,
user,
):
"""Sends a verification email to the provided user.
Kwargs:
user (str): The unique ID of the User whose email address will be verified.
Returns:
dict: User response from WorkOS.
"""

headers = {}

response = self.request_helper.request(
USER_SEND_VERIFICATION_EMAIL_PATH.format(user),
method=REQUEST_METHOD_POST,
headers=headers,
token=workos.api_key,
)

return WorkOSMagicAuthChallenge.construct_from_response(response).to_dict()

0 comments on commit 7b70e0c

Please sign in to comment.