def test_revoke_user_api_key(): responses.add(responses.DELETE, f"{URL}/api/security/apiKey/test_user", status=200) artifactory_security = ArtifactorySecurity(AuthModel(url=URL, auth=AUTH)) artifactory_security.revoke_user_api_key("test_user")
def test_create_virtual_repository_success(mocker): responses.add(responses.GET, f"{URL}/api/repositories/{VIRTUAL_REPOSITORY.key}", status=404) responses.add( responses.PUT, f"{URL}/api/repositories/{VIRTUAL_REPOSITORY.key}", json=VIRTUAL_REPOSITORY_RESPONSE.dict(), status=201, ) responses.add( responses.GET, f"{URL}/api/repositories/{VIRTUAL_REPOSITORY.key}", json=VIRTUAL_REPOSITORY_RESPONSE.dict(), status=200, ) artifactory_repo = ArtifactoryRepository(AuthModel(url=URL, auth=AUTH)) mocker.spy(artifactory_repo, "get_virtual_repo") virtual_repo = artifactory_repo.create_virtual_repo(VIRTUAL_REPOSITORY) artifactory_repo.get_virtual_repo.assert_called_with( VIRTUAL_REPOSITORY.key) assert artifactory_repo.get_virtual_repo.call_count == 2 assert virtual_repo == VIRTUAL_REPOSITORY_RESPONSE.dict()
def test_update_local_repository_using_update_repo_success(mocker): responses.add( responses.GET, f"{URL}/api/repositories/{UPDATED_LOCAL_REPOSITORY.key}", json=UPDATED_LOCAL_REPOSITORY_RESPONSE.dict(), status=200, ) responses.add( responses.POST, f"{URL}/api/repositories/{UPDATED_LOCAL_REPOSITORY.key}", status=200, ) responses.add( responses.GET, f"{URL}/api/repositories/{UPDATED_LOCAL_REPOSITORY.key}", json=UPDATED_LOCAL_REPOSITORY_RESPONSE.dict(), status=200, ) artifactory_repo = ArtifactoryRepository(AuthModel(url=URL, auth=AUTH)) mocker.spy(artifactory_repo, "get_repo") updated_repo = artifactory_repo.update_repo(UPDATED_LOCAL_REPOSITORY) assert isinstance(updated_repo, LocalRepositoryResponse) assert updated_repo == UPDATED_LOCAL_REPOSITORY_RESPONSE
def test_delete_repo_success(): responses.add( responses.DELETE, f"{URL}/api/repositories/{VIRTUAL_REPOSITORY.key}", status=204, ) artifactory_repo = ArtifactoryRepository(AuthModel(url=URL, auth=AUTH)) artifactory_repo.delete(VIRTUAL_REPOSITORY.key)
def test_get_user_error_not_found(): responses.add(responses.GET, f"{URL}/api/security/users/{USER.name}", status=404) artifactory_user = ArtifactoryUser(AuthModel(url=URL, auth=AUTH)) with pytest.raises(UserNotFoundException): artifactory_user.get(NEW_USER.name)
def test_get_group_error_not_found(): responses.add(responses.GET, f"{URL}/api/security/groups/{NEW_GROUP.name}", status=404) artifactory_group = ArtifactoryGroup(AuthModel(url=URL, auth=AUTH)) with pytest.raises(GroupNotFoundException): artifactory_group.get(NEW_GROUP.name)
def test_revoke_access_token_success(): responses.add(responses.POST, f"{URL}/api/security/token/revoke", status=200) artifactory_security = ArtifactorySecurity(AuthModel(url=URL, auth=AUTH)) result = artifactory_security.revoke_access_token(token="my-token") assert result is True
def test_get_remote_repository_error_not_found(): responses.add(responses.GET, f"{URL}/api/repositories/{REMOTE_REPOSITORY.key}", status=404) artifactory_repo = ArtifactoryRepository(AuthModel(url=URL, auth=AUTH)) with pytest.raises(RepositoryNotFoundException): artifactory_repo.get_remote_repo(REMOTE_REPOSITORY.key)
def test_revoke_access_token_fail_no_token_provided(): responses.add(responses.POST, f"{URL}/api/security/token/revoke", status=400) artifactory_security = ArtifactorySecurity(AuthModel(url=URL, auth=AUTH)) with pytest.raises(InvalidTokenDataException): artifactory_security.revoke_access_token()
def test_get_local_repository_using_get_repo_error_not_found(mocker): responses.add(responses.GET, f"{URL}/api/repositories/{LOCAL_REPOSITORY.key}", status=404) artifactory_repo = ArtifactoryRepository(AuthModel(url=URL, auth=AUTH)) mocker.spy(artifactory_repo, "get_repo") with pytest.raises(RepositoryNotFoundException): artifactory_repo.get_repo(LOCAL_REPOSITORY.key)
def test_delete_repo_fail_if_repo_not_found(): responses.add(responses.DELETE, f"{URL}/api/repositories/{REMOTE_REPOSITORY.key}", status=404) artifactory_repo = ArtifactoryRepository(AuthModel(url=URL, auth=AUTH)) with pytest.raises(requests.exceptions.HTTPError): artifactory_repo.delete(REMOTE_REPOSITORY.key)
def test_list_group_success(mocker): responses.add( responses.GET, f"{URL}/api/security/groups", json=[NEW_GROUP.dict()], status=200 ) artifactory_group = ArtifactoryGroup(AuthModel(url=URL, auth=AUTH)) mocker.spy(artifactory_group, "list") artifactory_group.list() artifactory_group.list.assert_called_once()
def __init__( self, url: str, auth: Tuple[str, str], verify: bool = True, cert: str = None, ): self.artifactory = AuthModel(url=url, auth=auth, verify=verify, cert=cert) self.users = ArtifactoryUser(self.artifactory) self.groups = ArtifactoryGroup(self.artifactory) self.security = ArtifactorySecurity(self.artifactory) self.repositories = ArtifactoryRepository(self.artifactory) self.artifacts = ArtifactoryArtifact(self.artifactory) self.permissions = ArtifactoryPermission(self.artifactory)
def test_get_artifact_file_info_success(): responses.add( responses.GET, f"{URL}/api/storage/{ARTIFACT_PATH}", status=200, json=FILE_INFO_RESPONSE, ) artifactory = ArtifactoryArtifact(AuthModel(url=URL, auth=AUTH)) artifact = artifactory.info(ARTIFACT_PATH) assert artifact.dict() == FILE_INFO.dict()
def test_update_group_fail_if_group_not_found(mocker): responses.add(responses.GET, f"{URL}/api/security/groups/{NEW_GROUP.name}", status=404) artifactory_group = ArtifactoryGroup(AuthModel(url=URL, auth=AUTH)) mocker.spy(artifactory_group, "get") with pytest.raises(GroupNotFoundException): artifactory_group.update(NEW_GROUP) artifactory_group.get.assert_called_once_with(NEW_GROUP.name)
def test_get_artifact_property_not_found_error(): responses.add( responses.GET, f"{URL}/api/storage/{ARTIFACT_PATH}?properties=a_property_not_found", json={"errors": [{"status": 404, "message": "No properties could be found."}]}, status=404, ) artifactory = ArtifactoryArtifact(AuthModel(url=URL, auth=AUTH)) with pytest.raises(PropertyNotFoundException): artifactory.properties(ARTIFACT_PATH, properties=["a_property_not_found"])
def test_get_artifact_folder_info_success(): responses.add( responses.GET, f"{URL}/api/storage/{ARTIFACT_REPO}", status=200, json=FOLDER_INFO_RESPONSE, ) artifactory = ArtifactoryArtifact(AuthModel(url=URL, auth=AUTH)) artifact = artifactory.info(ARTIFACT_REPO) assert isinstance(artifact, ArtifactFolderInfoResponse) assert artifact.dict() == FOLDER_INFO.dict()
def test_update_user_fail_if_user_not_found(mocker): responses.add(responses.GET, f"{URL}/api/security/users/{NEW_USER.name}", status=404) artifactory_user = ArtifactoryUser(AuthModel(url=URL, auth=AUTH)) mocker.spy(artifactory_user, "get") with pytest.raises(UserNotFoundException): artifactory_user.update(NEW_USER) artifactory_user.get.assert_called_once_with(NEW_USER.name)
def test_update_local_repository_using_update_repo_fail_if_repo_not_found( mocker): responses.add(responses.GET, f"{URL}/api/repositories/{LOCAL_REPOSITORY.key}", status=404) artifactory_repo = ArtifactoryRepository(AuthModel(url=URL, auth=AUTH)) mocker.spy(artifactory_repo, "get_repo") with pytest.raises(RepositoryNotFoundException): artifactory_repo.update_repo(LOCAL_REPOSITORY) artifactory_repo.get_repo.assert_called_once_with(LOCAL_REPOSITORY.key)
def test_get_artifact_stats_success(): responses.add( responses.GET, f"{URL}/api/storage/{ARTIFACT_PATH}?stats", json=ARTIFACT_STATS.dict(), status=200, ) artifactory = ArtifactoryArtifact(AuthModel(url=URL, auth=AUTH)) artifact_stats = artifactory.stats(ARTIFACT_PATH) assert artifact_stats.dict() == ARTIFACT_STATS.dict()
def test_get_artifact_all_properties_success(): responses.add( responses.GET, f"{URL}/api/storage/{ARTIFACT_PATH}?properties", json=ARTIFACT_MULTIPLE_PROPERTIES.dict(), status=200, ) artifactory = ArtifactoryArtifact(AuthModel(url=URL, auth=AUTH)) artifact_properties = artifactory.properties(ARTIFACT_PATH) assert artifact_properties.dict() == ARTIFACT_MULTIPLE_PROPERTIES.dict()
def test_get_artifact_single_property_success(): responses.add( responses.GET, f"{URL}/api/storage/{ARTIFACT_PATH}?properties=prop1", json=ARTIFACT_ONE_PROPERTY.dict(), status=200, ) artifactory = ArtifactoryArtifact(AuthModel(url=URL, auth=AUTH)) artifact_properties = artifactory.properties(ARTIFACT_PATH, ["prop1"]) assert artifact_properties.dict() == ARTIFACT_ONE_PROPERTY.dict()
def test_get_encrypted_password(): data = PASSWORD.dict() data["password"] = PASSWORD.password.get_secret_value() responses.add(responses.GET, f"{URL}/api/security/encryptedPassword", json=data, status=200) artifactory_security = ArtifactorySecurity(AuthModel(url=URL, auth=AUTH)) enc_pass = artifactory_security.get_encrypted_password() assert enc_pass.password.get_secret_value( ) == PASSWORD.password.get_secret_value()
def test_get_virtual_repository_using_get_repo_success(mocker): responses.add( responses.GET, f"{URL}/api/repositories/{VIRTUAL_REPOSITORY.key}", json=VIRTUAL_REPOSITORY_RESPONSE.dict(), status=200, ) artifactory_repo = ArtifactoryRepository(AuthModel(url=URL, auth=AUTH)) virtual_repo = artifactory_repo.get_repo(VIRTUAL_REPOSITORY.key) assert virtual_repo == VIRTUAL_REPOSITORY_RESPONSE
def test_get_api_key(): data = API_KEY.dict() data["apiKey"] = API_KEY.apiKey.get_secret_value() responses.add(responses.GET, f"{URL}/api/security/apiKey", json=data, status=200) artifactory_security = ArtifactorySecurity(AuthModel(url=URL, auth=AUTH)) api_key = artifactory_security.get_api_key() assert api_key.apiKey.get_secret_value( ) == API_KEY.apiKey.get_secret_value()
def test_get_remote_repository_using_get_repo_success(): responses.add( responses.GET, f"{URL}/api/repositories/{REMOTE_REPOSITORY.key}", json=REMOTE_REPOSITORY_RESPONSE.dict(), status=200, ) artifactory_repo = ArtifactoryRepository(AuthModel(url=URL, auth=AUTH)) remote_repo = artifactory_repo.get_repo(REMOTE_REPOSITORY.key) assert remote_repo == REMOTE_REPOSITORY_RESPONSE
def test_download_artifact_success(tmp_path): artifact_name = ARTIFACT_PATH.split("/")[1] responses.add(responses.GET, f"{URL}/{ARTIFACT_PATH}", json=artifact_name, status=200) artifactory = ArtifactoryArtifact(AuthModel(url=URL, auth=AUTH)) artifact = artifactory.download(ARTIFACT_PATH, str(tmp_path.resolve())) assert artifact == f"{tmp_path.resolve()}/{artifact_name}" assert (tmp_path / artifact_name).is_file()
def test_list_repositories_success(mocker): responses.add( responses.GET, f"{URL}/api/repositories", json=[SIMPLE_REPOSITORY.dict()], status=200, ) artifactory_repo = ArtifactoryRepository(AuthModel(url=URL, auth=AUTH)) mocker.spy(artifactory_repo, "list") artifactory_repo.list() artifactory_repo.list.assert_called_once()
def test_get_remote_repository_success(mocker): responses.add( responses.GET, f"{URL}/api/repositories/{REMOTE_REPOSITORY.key}", json=REMOTE_REPOSITORY_RESPONSE.dict(), status=200, ) artifactory_repo = ArtifactoryRepository(AuthModel(url=URL, auth=AUTH)) mocker.spy(artifactory_repo, "get_remote_repo") artifactory_repo.get_remote_repo(REMOTE_REPOSITORY.key) artifactory_repo.get_remote_repo.assert_called_once()
def test_list_user_success(mocker): responses.add( responses.GET, f"{URL}/api/security/users", json=[SIMPLE_USER.dict()], status=200, ) artifactory_user = ArtifactoryUser(AuthModel(url=URL, auth=AUTH)) mocker.spy(artifactory_user, "list") artifactory_user.list() artifactory_user.list.assert_called_once()