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

W-15805907: assign_permission_set_licenses accepts the api_names in either developerName or the PermissionSetLicenseKey #3798

Merged
merged 8 commits into from
Jun 14, 2024
4 changes: 2 additions & 2 deletions cumulusci/tasks/preflight/licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def _run_task(self):
class GetAvailablePermissionSetLicenses(BaseSalesforceApiTask):
def _run_task(self):
self.return_values = [
result["DeveloperName"]
result["PermissionSetLicenseKey"]
for result in self.sf.query(
"SELECT DeveloperName FROM PermissionSetLicense"
"SELECT PermissionSetLicenseKey FROM PermissionSetLicense"
)["records"]
]
licenses = "\n".join(self.return_values)
Expand Down
6 changes: 3 additions & 3 deletions cumulusci/tasks/preflight/tests/test_licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ def test_psl_preflight(self):
task._init_api.return_value.query.return_value = {
"totalSize": 2,
"records": [
{"DeveloperName": "TEST1"},
{"DeveloperName": "TEST2"},
{"PermissionSetLicenseKey": "TEST1"},
{"PermissionSetLicenseKey": "TEST2"},
],
}
task()

task._init_api.return_value.query.assert_called_once_with(
"SELECT DeveloperName FROM PermissionSetLicense"
"SELECT PermissionSetLicenseKey FROM PermissionSetLicense"
)
assert task.return_values == ["TEST1", "TEST2"]

Expand Down
24 changes: 23 additions & 1 deletion cumulusci/tasks/salesforce/users/permsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,34 @@ class AssignPermissionSetLicenses(AssignPermissionSets):
}

permission_name = "PermissionSetLicense"
permission_name_field = "DeveloperName"
permission_name_field = ["DeveloperName", "PermissionSetLicenseKey"]
permission_label = "Permission Set License"
assignment_name = "PermissionSetLicenseAssign"
assignment_lookup = "PermissionSetLicenseId"
assignment_child_relationship = "PermissionSetLicenseAssignments"

def _get_perm_ids(self):
perms_by_ids = {}
api_names = "', '".join(self.options["api_names"])
missing_perms = {api_name: True for api_name in self.options["api_names"]}
for permission_field in self.permission_name_field:
perms = self.sf.query(
f"SELECT Id,{permission_field} FROM {self.permission_name} WHERE {permission_field} IN ('{api_names}')"
)
perms_by_ids_subset = {
p["Id"]: p[permission_field] for p in perms["records"]
}
perms_by_ids.update(perms_by_ids_subset)
missing_perms.update(
{api_name: False for api_name in perms_by_ids.values()}
)

if any(missing_perms.values()):
raise CumulusCIException(
f"The following {self.permission_label}s were not found: {', '.join(api_names for api_names in missing_perms if missing_perms[api_names])}."
)
return perms_by_ids


class AssignPermissionSetGroups(AssignPermissionSets):
task_docs = """
Expand Down
64 changes: 59 additions & 5 deletions cumulusci/tasks/salesforce/users/tests/test_permsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,16 @@ def test_create_permsetlicense(self):
],
},
)
responses.add(
method="GET",
url=f"{task.org_config.instance_url}/services/data/v{CURRENT_SF_API_VERSION}/query/?q=SELECT+Id%2CPermissionSetLicenseKey+FROM+PermissionSetLicense+WHERE+PermissionSetLicenseKey+IN+%28%27PermSetLicense1%27%2C+%27PermSetLicense2%27%29",
status=200,
json={
"done": True,
"totalSize": 1,
"records": [],
},
)
responses.add(
method="POST",
url=f"{task.org_config.instance_url}/services/data/v{CURRENT_SF_API_VERSION}/composite/sobjects",
Expand All @@ -487,7 +497,7 @@ def test_create_permsetlicense(self):

task()

assert len(responses.calls) == 3
assert len(responses.calls) == 4

@responses.activate
def test_create_permsetlicense__no_assignments(self):
Expand Down Expand Up @@ -526,9 +536,21 @@ def test_create_permsetlicense__no_assignments(self):
"Id": "0PL000000000000",
"DeveloperName": "PermSetLicense1",
},
],
},
)

responses.add(
method="GET",
url=f"{task.org_config.instance_url}/services/data/v{CURRENT_SF_API_VERSION}/query/?q=SELECT+Id%2CPermissionSetLicenseKey+FROM+PermissionSetLicense+WHERE+PermissionSetLicenseKey+IN+%28%27PermSetLicense1%27%2C+%27PermSetLicense2%27%29",
status=200,
json={
"done": True,
"totalSize": 1,
"records": [
{
"Id": "0PL000000000001",
"DeveloperName": "PermSetLicense2",
"PermissionSetLicenseKey": "PermSetLicense2",
},
],
},
Expand Down Expand Up @@ -564,7 +586,7 @@ def test_create_permsetlicense__no_assignments(self):
)
task()

assert len(responses.calls) == 3
assert len(responses.calls) == 4

@responses.activate
def test_create_permsetlicense__alias(self):
Expand Down Expand Up @@ -607,13 +629,26 @@ def test_create_permsetlicense__alias(self):
"Id": "0PL000000000000",
"DeveloperName": "PermSetLicense1",
},
],
},
)

responses.add(
method="GET",
url=f"{task.org_config.instance_url}/services/data/v{CURRENT_SF_API_VERSION}/query/?q=SELECT+Id%2CPermissionSetLicenseKey+FROM+PermissionSetLicense+WHERE+PermissionSetLicenseKey+IN+%28%27PermSetLicense1%27%2C+%27PermSetLicense2%27%29",
status=200,
json={
"done": True,
"totalSize": 1,
"records": [
{
"Id": "0PL000000000001",
"DeveloperName": "PermSetLicense2",
"PermissionSetLicenseKey": "PermSetLicense2",
},
],
},
)

responses.add(
method="POST",
url=f"{task.org_config.instance_url}/services/data/v{CURRENT_SF_API_VERSION}/sobjects/PermissionSetLicenseAssign/",
Expand Down Expand Up @@ -643,7 +678,7 @@ def test_create_permsetlicense__alias(self):
)
task()

assert len(responses.calls) == 3
assert len(responses.calls) == 4

@responses.activate
def test_create_permsetlicense__alias_raises(self):
Expand Down Expand Up @@ -715,6 +750,25 @@ def test_create_permsetlicense_raises(self):
],
},
)
responses.add(
method="GET",
url=f"{task.org_config.instance_url}/services/data/v{CURRENT_SF_API_VERSION}/query/?q=SELECT+Id%2CPermissionSetLicenseKey+FROM+PermissionSetLicense+WHERE+PermissionSetLicenseKey+IN+%28%27PermSetLicense1%27%2C+%27PermSetLicense2%27%2C+%27PermSetLicense3%27%29",
status=200,
json={
"done": True,
"totalSize": 1,
"records": [
{
"Id": "0PL000000000000",
"PermissionSetLicenseKey": "PermSetLicense1",
},
{
"Id": "0PL000000000001",
"PermissionSetLicenseKey": "PermSetLicense2",
},
],
},
)

with pytest.raises(CumulusCIException):
task()
Expand Down
Loading