示例#1
0
 def get(self, name: str) -> UserResponse:
     """
     Read user from artifactory. Fill object if exist
     :param name: Name of the user to retrieve
     :return: UserModel
     """
     try:
         response = self._get(f"api/{self._uri}/{name}")
         logger.debug("User %s found", name)
         return UserResponse(**response.json())
     except requests.exceptions.HTTPError as error:
         if error.response.status_code == 404 or error.response.status_code == 400:
             logger.error("User %s does not exist", name)
             raise UserNotFoundException(f"{name} does not exist")
         raise ArtifactoryException from error
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)