def list(self) -> List[Group]: """ Lists all the groups :return: GroupList """ response = self._get(f"api/{self._uri}") logger.debug("List all groups successful") return [Group(**group) for group in response.json()]
def update(self, group: Group) -> Group: """ Updates an exiting group in Artifactory with the provided group details. :param group: Group to be updated :return: Updated group """ group_name = group.name self.get(group_name) self._post(f"api/{self._uri}/{group_name}", json=group.dict()) logger.debug("Group %s successfully updated", group_name) return self.get(group_name)
def get(self, name: str) -> Group: """ Get the details of an Artifactory Group :param name: Name of the group to retrieve :return: Found artifactory group """ try: r = self._get(f"api/{self._uri}/{name}") return Group(**r.json()) except requests.exceptions.HTTPError as e: if e.response.status_code == 404 or e.response.status_code == 400: logging.error(f"Group {name} does not exist") raise GroupNotFoundException(f"Group {name} does not exist") raise ArtifactoryException from e
def create(self, group: Group) -> Group: """ Creates a new group in Artifactory or replaces an existing group :param group: Group to create :return: Created group """ group_name = group.name try: self.get(group_name) logger.error("Group %s already exists", group_name) raise GroupAlreadyExistsException(f"Group {group_name} already exists") except GroupNotFoundException: self._put(f"api/{self._uri}/{group_name}", json=group.dict()) logger.debug("Group %s successfully created", group_name) return self.get(group.name)
def get(self, name: str) -> Group: """ Get the details of an Artifactory Group :param name: Name of the group to retrieve :return: Found artifactory group """ try: response = self._get(f"api/{self._uri}/{name}", params={"includeUsers": True}) logger.debug("Group %s found", name) return Group(**response.json()) except requests.exceptions.HTTPError as error: if error.response.status_code in (404, 400): logger.error("Group %s does not exist", name) raise GroupNotFoundException(f"Group {name} does not exist") raise ArtifactoryException from error
import pytest import responses from pyartifactory import ArtifactoryGroup from pyartifactory.exception import GroupNotFoundException, GroupAlreadyExistsException from pyartifactory.models import AuthModel, PasswordModel, Group URL = "http://localhost:8080/artifactory" AUTH = ("user", "password_or_apiKey") NEW_GROUP = Group(name="test_group", description="test_group") PASSWORD = PasswordModel(password="******") class TestGroup: @staticmethod @responses.activate def test_create_group_fail_if_group_already_exists(mocker): responses.add( responses.GET, f"{URL}/api/security/groups/{NEW_GROUP.name}", json=NEW_GROUP.dict(), status=200, ) artifactory_group = ArtifactoryGroup(AuthModel(url=URL, auth=AUTH)) mocker.spy(artifactory_group, "get") with pytest.raises(GroupAlreadyExistsException): artifactory_group.create(NEW_GROUP) artifactory_group.get.assert_called_once_with(NEW_GROUP.name)
import pytest import responses from pyartifactory import ArtifactoryGroup from pyartifactory.exception import GroupNotFoundException, GroupAlreadyExistsException from pyartifactory.models import AuthModel, PasswordModel, Group URL = "http://localhost:8080/artifactory" AUTH = ("user", "password_or_apiKey") NEW_GROUP = Group(name="test_group", description="test_group") GROUP_WITH_USERS = Group(name="test_group", users=["user1", "user2"]) PASSWORD = PasswordModel(password="******") @responses.activate def test_create_group_fail_if_group_already_exists(mocker): responses.add( responses.GET, f"{URL}/api/security/groups/{NEW_GROUP.name}", json=NEW_GROUP.dict(), status=200, ) artifactory_group = ArtifactoryGroup(AuthModel(url=URL, auth=AUTH)) mocker.spy(artifactory_group, "get") with pytest.raises(GroupAlreadyExistsException): artifactory_group.create(NEW_GROUP) artifactory_group.get.assert_called_once_with(NEW_GROUP.name)