Skip to content

Commit

Permalink
Add Users client and Users.create_user() method (#191)
Browse files Browse the repository at this point in the history
* Add Users client and Users.create_user() method

* Fix comment and format.
  • Loading branch information
jonatascastro12 committed Aug 17, 2023
1 parent 2667837 commit b83034c
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def setup(self):
client._passwordless = None
client._portal = None
client._sso = None
client._users = None

def test_initialize_sso(self, set_api_key_and_client_id):
assert bool(client.sso)
Expand All @@ -36,6 +37,9 @@ def test_initialize_passwordless(self, set_api_key):
def test_initialize_portal(self, set_api_key):
assert bool(client.portal)

def test_initialize_users(self, set_api_key, set_client_id):
assert bool(client.users)

def test_initialize_sso_missing_api_key(self, set_client_id):
with pytest.raises(ConfigurationException) as ex:
client.sso
Expand Down Expand Up @@ -107,3 +111,28 @@ def test_initialize_portal_missing_api_key(self):
message = str(ex)

assert "api_key" in message

def test_initialize_users_missing_client_id(self, set_api_key):
with pytest.raises(ConfigurationException) as ex:
client.users

message = str(ex)

assert "client_id" in message

def test_initialize_users_missing_api_key(self, set_client_id):
with pytest.raises(ConfigurationException) as ex:
client.users

message = str(ex)

assert "api_key" in message

def test_initialize_users_missing_api_key_and_client_id(self):
with pytest.raises(ConfigurationException) as ex:
client.users

message = str(ex)

assert "api_key" in message
assert "client_id" in message
28 changes: 28 additions & 0 deletions tests/test_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest

from tests.utils.fixtures.mock_user import MockUser
from workos.users import Users


class TestUsers(object):
@pytest.fixture(autouse=True)
def setup(self, set_api_key, set_client_id):
self.users = Users()

@pytest.fixture
def mock_user(self):
return MockUser("user_01H7ZGXFP5C6BBQY6Z7277ZCT0").to_dict()

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

payload = {
"email": "[email protected]",
"first_name": "Marcelina",
"last_name": "Hoeger",
"password": "password",
"email_verified": False,
}
user = self.users.create_user(payload)

assert user["id"] == "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"
31 changes: 31 additions & 0 deletions tests/utils/fixtures/mock_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import datetime
from workos.resources.base import WorkOSBaseResource


class MockUser(WorkOSBaseResource):
def __init__(self, id):
self.id = id
self.email = "[email protected]"
self.first_name = "Marcelina"
self.last_name = "Hoeger"
self.user_type = "unmanaged"
self.sso_profile_id = None
self.email_verified_at = ""
self.google_oauth_profile_id = None
self.microsoft_oauth_profile_id = None
self.created_at = datetime.datetime.now()
self.updated_at = datetime.datetime.now()

OBJECT_FIELDS = [
"id",
"email",
"first_name",
"last_name",
"user_type",
"sso_profile_id",
"email_verified_at",
"google_oauth_profile_id",
"microsoft_oauth_profile_id",
"created_at",
"updated_at",
]
7 changes: 7 additions & 0 deletions workos/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from workos.webhooks import Webhooks
from workos.mfa import Mfa
from workos.events import Events
from workos.users import Users


class Client(object):
Expand Down Expand Up @@ -73,5 +74,11 @@ def mfa(self):
self._mfa = Mfa()
return self._mfa

@property
def users(self):
if not getattr(self, "_users", None):
self._users = Users()
return self._users


client = Client()
23 changes: 23 additions & 0 deletions workos/resources/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from workos.resources.base import WorkOSBaseResource


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

OBJECT_FIELDS = [
"id",
"email",
"first_name",
"last_name",
"user_type",
"sso_profile_id",
"email_verified_at",
"google_oauth_profile_id",
"microsoft_oauth_profile_id",
"created_at",
"updated_at",
]
54 changes: 54 additions & 0 deletions workos/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import workos
from workos.resources.list import WorkOSListResource
from workos.resources.users import (
WorkOSUser,
)
from workos.utils.request import (
RequestHelper,
REQUEST_METHOD_POST,
)
from workos.utils.validation import validate_settings, USERS_MODULE

USER_PATH = "users"

RESPONSE_LIMIT = 10


class Users(WorkOSListResource):
"""Offers methods for using the WorkOS User Management API."""

@validate_settings(USERS_MODULE)
def __init__(self):
pass

@property
def request_helper(self):
if not getattr(self, "_request_helper", None):
self._request_helper = RequestHelper()
return self._request_helper

def create_user(self, user):
"""Create a new unmanaged user with email password authentication.
Args:
user (dict) - An user object
user[email] (string) - The email address of the user.
user[password] (string) - The password to set for the user.
user[first_name] (string) - The user's first name.
user[last_name] (string) - The user's last name.
user[email_verified] (bool) - Whether the user's email address was previously verified.
Returns:
dict: Created User response from WorkOS.
"""
headers = {}

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

return WorkOSUser.construct_from_response(response).to_dict()
2 changes: 2 additions & 0 deletions workos/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
SSO_MODULE = "SSO"
WEBHOOKS_MODULE = "Webhooks"
MFA_MODULE = "MFA"
USERS_MODULE = "UserManagement"

REQUIRED_SETTINGS_FOR_MODULE = {
AUDIT_LOGS_MODULE: [
Expand Down Expand Up @@ -42,6 +43,7 @@
],
WEBHOOKS_MODULE: ["api_key"],
MFA_MODULE: ["api_key"],
USERS_MODULE: ["client_id", "api_key"],
}


Expand Down

0 comments on commit b83034c

Please sign in to comment.