-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Users client and Users.create_user() method * Fix comment and format.
- Loading branch information
1 parent
2667837
commit b83034c
Showing
7 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters