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

feat: adds create support for users #142

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
90 changes: 87 additions & 3 deletions src/posit/connect/users.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from typing import List, overload
from typing import List, Optional, overload


import requests
Expand Down Expand Up @@ -210,8 +210,92 @@ def get(self, id: str) -> User:
**response.json(),
)

def create(self) -> User:
raise NotImplementedError()
@overload
def create(
self,
username: str,
email: str = ...,
first_name: str = ...,
last_name: str = ...,
user_role: str = ...,
user_must_set_password: bool = ...,
password: str = ...,
unique_id: str = ...,
) -> User:
"""Creates a user.

Creates a user from provided information (saml, pam, password, proxy, oauth2).

Parameters
----------
username : str
email : str, optional
first_name : str, optional
last_name : str, optional
user_role : str, optional
user_must_set_password : bool, optional
password : str, optional
unique_id : str, optional

Returns
-------
User
The created user.
"""
...

@overload
def create(self, prefix: str) -> User:
"""Creates a user.

Creates a user via a remote authentication provider (ldap, google).

Parameters
----------
prefix : str, optional

Returns
-------
User
The created user.
"""
...

@overload
def create(self, *args, **kwargs) -> User:
"""Creates a user.

Returns
-------
User
The created user.
"""
...

def create(self, *args, **kwargs) -> User:
"""Creates a user.

Returns
-------
User
The created user.
"""
body = dict(*args, **kwargs)
if "prefix" in body:
# Assume the remote flow if a 'prefix' is supplied
url = urls.append_path(self.config.url, "v1/users/remote")
paginator = Paginator(self.session, url, params=body)
results = paginator.fetch_results()
# TODO - Add assertion to verify result set includes a single user.
result = results[0]
url = urls.append_path(self.config.url, "v1/users")
ticket = result["temp_ticket"]
response = self.session.put(url, json={"temp_ticket": ticket})
return User(config=self.config, session=self.session, **response.json())

url = urls.append_path(self.config.url, "v1/users")
response = self.session.post(url, json=body)
return User(config=self.config, session=self.session, **response.json())

def update(self) -> User:
raise NotImplementedError()
Expand Down
20 changes: 20 additions & 0 deletions tests/posit/connect/__api__/v1/users/remote.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"results": [
{
"email": "carlos@connect.example",
"username": "carlos12",
"first_name": "Carlos",
"last_name": "User",
"user_role": "publisher",
"created_time": "2019-09-09T15:24:32Z",
"updated_time": "2022-03-02T20:25:06Z",
"active_time": "2020-05-11T16:58:45Z",
"confirmed": true,
"locked": true,
"guid": "20a79ce3-6e87-4522-9faf-be24228800a4",
"temp_ticket": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
}
],
"current_page": 1,
"total": 1
}
52 changes: 52 additions & 0 deletions tests/posit/connect/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,58 @@ def test_count(self):
assert count == 3


class TestUsersCreate:
@responses.activate
def test_with_prefix(self):
responses.get(
"https://connect.example/__api__/v1/users/remote",
json=load_mock("v1/users/remote.json"),
match=[
responses.matchers.query_param_matcher(
{
"page_number": 1,
"page_size": 500,
"prefix": "carlos@connect.example",
}
)
],
)
responses.put(
"https://connect.example/__api__/v1/users",
json=load_mock("v1/user.json"),
match=[
responses.matchers.json_params_matcher(
{
"temp_ticket": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
}
)
],
)

con = Client(api_key="12345", url="https://connect.example/")
user = con.users.create(prefix="carlos@connect.example")
assert user.email == "carlos@connect.example"

@responses.activate
def test(self):
params = {
"email": "carlos@connect.example",
"username": "carlos12",
"first_name": "Carlos",
"last_name": "User",
}

responses.post(
"https://connect.example/__api__/v1/users",
json=load_mock("v1/user.json"),
match=[responses.matchers.json_params_matcher(params)],
)

con = Client(api_key="12345", url="https://connect.example/")
user = con.users.create(**params)
assert user.email == "carlos@connect.example"


class TestUsersFindOne:
@responses.activate
def test_default(self):
Expand Down