diff --git a/sdcclient/_common.py b/sdcclient/_common.py index 50bce9fe..03dfdc87 100644 --- a/sdcclient/_common.py +++ b/sdcclient/_common.py @@ -547,6 +547,35 @@ def download_sysdig_capture(self, capture_id): return True, res.content + def create_user(self, user_email, first_name=None, last_name=None, password=None): + ''' + Provisions a new user to use Sysdig without sending an email notification. + If password is not set through this request a random one is generated for the user + which requires them to reset password on first login. + + Args: + user_email (str): Email of the user to provision. + first_name (str): First name of the user to provision. Can be null. + last_name (str): Last name of the user to provision. Can be null. + password (str): Default password for the user to provision. If this is not set, a random one is generated. + + Returns: + The provisioned user information. + + ''' + + user_info = { + "username": user_email, + "firstName": first_name, + "lastName": last_name, + "password": password, + } + user_info = {k: v for k, v in user_info.items() if v} + + res = self.http.post(self.url + '/api/user/provisioning/', headers=self.hdrs, data=json.dumps(user_info), + verify=self.ssl_verify) + return self._request_result(res) + def create_user_invite(self, user_email, first_name=None, last_name=None, system_role=None): '''**Description** Invites a new user to use Sysdig Monitor. This should result in an email notification to the specified address. diff --git a/specs/_common/user_provisioning_spec.py b/specs/_common/user_provisioning_spec.py new file mode 100644 index 00000000..066ccab7 --- /dev/null +++ b/specs/_common/user_provisioning_spec.py @@ -0,0 +1,45 @@ +import os +from random import choice +from string import ascii_letters + +from expects import expect, contain +from mamba import before, description, after, it, context + +from sdcclient import SdcClient +from specs import be_successful_api_call + +with description("User Provisioning", "integration") as self: + with before.each: + self.client = SdcClient(sdc_url=os.getenv("SDC_MONITOR_URL", "https://app.sysdigcloud.com"), + token=os.getenv("SDC_MONITOR_TOKEN")) + self.user_name = "terraform-test+user@sysdig.com" + + with after.each: + self.client.delete_user(self.user_name) + + with it("is able to provision the user with only the username"): + ok, res = self.client.create_user(self.user_name) + expect((ok, res)).to(be_successful_api_call) + + with it("is able to provision the user with name and lastname"): + ok, res = self.client.create_user(self.user_name, "Name", "LastName") + expect((ok, res)).to(be_successful_api_call) + + with it("is able to provision the user with password"): + random_password = "".join(choice(ascii_letters) for _ in range(20)) + ok, res = self.client.create_user(self.user_name, password=random_password) + expect((ok, res)).to(be_successful_api_call) + + with it("is able to provision the user with name, lastname and password"): + random_password = "".join(choice(ascii_letters) for _ in range(20)) + ok, res = self.client.create_user(self.user_name, "Name", "LastName", random_password) + expect((ok, res)).to(be_successful_api_call) + + with context("when the customer already exists"): + with it("returns an error saying it already exists"): + ok, res = self.client.create_user(self.user_name) + expect((ok, res)).to(be_successful_api_call) + + ok, res = self.client.create_user(self.user_name) + expect((ok, res)).not_to(be_successful_api_call) + expect(res).to(contain(f"User {self.user_name} already exists"))