Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auth factor methods #217

Merged
merged 5 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 83 additions & 8 deletions tests/test_user_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from tests.utils.fixtures.mock_session import MockSession
from tests.utils.fixtures.mock_user import MockUser
from tests.utils.fixtures.mock_auth_factor_totp import MockAuthFactorTotp
from workos.user_management import UserManagement


Expand Down Expand Up @@ -124,6 +125,47 @@ def mock_magic_auth_challenge_response(self):
"id": "auth_challenge_01E4ZCR3C56J083X43JQXF3JK5",
}

@pytest.fixture
def mock_enroll_auth_factor_response(self):
return {
"authentication_factor": {
"object": "authentication_factor",
"id": "auth_factor_01FVYZ5QM8N98T9ME5BCB2BBMJ",
"created_at": "2022-02-15T15:14:19.392Z",
"updated_at": "2022-02-15T15:14:19.392Z",
"type": "totp",
"totp": {
"qr_code": "data:image/png;base64,{base64EncodedPng}",
"secret": "NAGCCFS3EYRB422HNAKAKY3XDUORMSRF",
"uri": "otpauth://totp/FooCorp:[email protected]?secret=NAGCCFS3EYRB422HNAKAKY3XDUORMSRF&issuer=FooCorp",
},
},
"authentication_challenge": {
"object": "authentication_challenge",
"id": "auth_challenge_01FVYZWQTZQ5VB6BC5MPG2EYC5",
"created_at": "2022-02-15T15:26:53.274Z",
"updated_at": "2022-02-15T15:26:53.274Z",
"expires_at": "2022-02-15T15:36:53.279Z",
"authentication_factor_id": "auth_factor_01FVYZ5QM8N98T9ME5BCB2BBMJ",
},
}

@pytest.fixture
def mock_auth_factors(self):
auth_factors_list = [MockAuthFactorTotp(id=str(i)).to_dict() for i in range(2)]

dict_response = {
"data": auth_factors_list,
"list_metadata": {"before": None, "after": None},
"metadata": {
"params": {
"user_id": "user_12345",
},
"method": UserManagement.list_auth_factors,
},
}
return dict_response

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

Expand Down Expand Up @@ -243,15 +285,15 @@ def test_authenticate_with_magic_auth(
self, capture_and_mock_request, mock_auth_response
):
code = "test_auth"
user = "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"
user_id = "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
ip_address = "192.0.0.1"

url, request = capture_and_mock_request("post", mock_auth_response, 200)

response = self.user_management.authenticate_with_magic_auth(
code=code,
user=user,
user_id=user_id,
user_agent=user_agent,
ip_address=ip_address,
)
Expand All @@ -260,7 +302,7 @@ def test_authenticate_with_magic_auth(
assert response["user"]["id"] == "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"
assert request["json"]["code"] == code
assert request["json"]["user_agent"] == user_agent
assert request["json"]["user_id"] == user
assert request["json"]["user_id"] == user_id
assert request["json"]["ip_address"] == ip_address
assert request["json"]["client_id"] == "client_b27needthisforssotemxo"
assert request["json"]["client_secret"] == "sk_abdsomecharactersm284"
Expand Down Expand Up @@ -356,27 +398,27 @@ def test_complete_password_reset(self, capture_and_mock_request, mock_user):
assert request["json"]["new_password"] == new_password

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

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

response = self.user_management.send_verification_email(user=user)
response = self.user_management.send_verification_email(user_id=user_id)

assert url[0].endswith(
"users/user_01H7ZGXFP5C6BBQY6Z7277ZCT0/send_verification_email"
)
assert response["id"] == "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"

def test_verify_email_code(self, capture_and_mock_request, mock_auth_response):
user = "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"
user_id = "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"
code = "code_123"

url, request = capture_and_mock_request("post", mock_auth_response, 200)

response = self.user_management.verify_email_code(user=user, code=code)
response = self.user_management.verify_email_code(user_id=user_id, code=code)

assert url[0].endswith("users/verify_email_code")
assert request["json"]["user_id"] == user
assert request["json"]["user_id"] == user_id
assert request["json"]["code"] == code
assert response["id"] == "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"

Expand All @@ -390,3 +432,36 @@ def test_send_magic_auth_code(self, capture_and_mock_request, mock_user):
assert url[0].endswith("user_management/magic_auth/send")
assert request["json"]["email"] == email
assert response["id"] == "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"

def test_enroll_auth_factor(
self, mock_enroll_auth_factor_response, mock_request_method
):
user_id = "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"
type = "totp"
totp_issuer = "WorkOS"
totp_user = "[email protected]"

mock_request_method("post", mock_enroll_auth_factor_response, 200)

enroll_auth_factor = self.user_management.enroll_auth_factor(
user_id=user_id,
type=type,
totp_issuer=totp_issuer,
totp_user=totp_user,
)

assert enroll_auth_factor == mock_enroll_auth_factor_response

def test_auth_factors_returns_metadata(
self,
mock_auth_factors,
mock_request_method,
):
mock_request_method("get", mock_auth_factors, 200)

auth_factors = self.user_management.list_auth_factors(
user_id="user_12345",
)

dict_auth_factors = auth_factors.to_dict()
assert dict_auth_factors["metadata"]["params"]["user_id"] == "user_12345"
25 changes: 25 additions & 0 deletions tests/utils/fixtures/mock_auth_factor_totp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import datetime
from workos.resources.base import WorkOSBaseResource


class MockAuthFactorTotp(WorkOSBaseResource):
def __init__(self, id):
self.object = "authentication_factor"
self.id = id
self.created_at = datetime.datetime.now()
self.updated_at = datetime.datetime.now()
self.type = "totp"
self.totp = {
"qr_code": "data:image/png;base64,{base64EncodedPng}",
"secret": "NAGCCFS3EYRB422HNAKAKY3XDUORMSRF",
"uri": "otpauth://totp/FooCorp:[email protected]?secret=NAGCCFS3EYRB422HNAKAKY3XDUORMSRF&issuer=FooCorp",
}

OBJECT_FIELDS = [
"object",
"id",
"created_at",
"updated_at",
"type",
"totp",
]
Loading