def update(self, user: NewUser) -> UserResponse: """ Updates an artifactory user :param user: NewUser object :return: UserModel """ username = user.name self.get(username) data = user.dict() data["password"] = user.password.get_secret_value() self._post(f"api/{self._uri}/{username}", json=data) logging.debug(f"User {username} successfully updated") return self.get(username)
def create(self, user: NewUser) -> UserResponse: """ Create user :param user: NewUser object :return: User """ username = user.name try: self.get(username) logger.error("User %s already exists", username) raise UserAlreadyExistsException(f"User {username} already exists") except UserNotFoundException: data = user.dict() data["password"] = user.password.get_secret_value() self._put(f"api/{self._uri}/{username}", json=data) logger.debug("User %s successfully created", username) return self.get(user.name)
import pytest import responses from pyartifactory import ArtifactoryUser from pyartifactory.exception import UserAlreadyExistsException, UserNotFoundException from pyartifactory.models import AuthModel, NewUser, UserResponse, SimpleUser URL = "http://*****:*****@test.com") NEW_USER = NewUser(name="test_user", password="******", email="*****@*****.**") class TestUser: @staticmethod @responses.activate def test_create_user_fail_if_user_already_exists(mocker): responses.add( responses.GET, f"{URL}/api/security/users/{USER.name}", json=USER.dict(), status=200, ) artifactory_user = ArtifactoryUser(AuthModel(url=URL, auth=AUTH)) mocker.spy(artifactory_user, "get") with pytest.raises(UserAlreadyExistsException): artifactory_user.create(NEW_USER)