Example #1
0
def test_search_components_auth(mock_gwc, mock_auth, mock_requests,
                                components_search_results, hoster):
    hoster_credential = "hoster"
    local_credential = "local"
    mock_gwc.return_value.cachito_nexus_hoster_username = hoster_credential
    mock_gwc.return_value.cachito_nexus_hoster_password = hoster_credential
    mock_gwc.return_value.cachito_nexus_username = local_credential
    mock_gwc.return_value.cachito_nexus_password = local_credential

    mock_rv = mock.Mock()
    mock_rv.ok = True
    mock_rv.json.return_value = components_search_results
    mock_requests.get.return_value = mock_rv
    results = nexus.search_components(in_nexus_hoster=hoster,
                                      repository="cachito-js-hosted",
                                      type="npm")

    assert results == components_search_results["items"]
    assert mock_requests.get.call_count == 1
    if hoster:
        mock_auth.assert_called_once_with(hoster_credential, hoster_credential)
    else:
        mock_auth.assert_called_once_with(local_credential, local_credential)
Example #2
0
def test_search_components(mock_requests, components_search_results):
    # Split up the components_search_results fixture into two pages to test pagination
    first_page = copy.deepcopy(components_search_results)
    first_page["items"].pop(1)
    first_page["continuationToken"] = "someToken"
    mock_rv_first = mock.Mock()
    mock_rv_first.ok = True
    mock_rv_first.json.return_value = first_page

    second_page = copy.deepcopy(components_search_results)
    second_page["items"].pop(0)
    mock_rv_second = mock.Mock()
    mock_rv_second.ok = True
    mock_rv_second.json.return_value = second_page

    mock_requests.get.side_effect = [mock_rv_first, mock_rv_second]

    results = nexus.search_components(repository="cachito-js-hosted",
                                      type="npm")

    assert results == components_search_results["items"]

    assert mock_requests.get.call_count == 2
Example #3
0
def test_search_components_failed(mock_requests):
    mock_requests.get.return_value.ok = False

    expected = "Failed to search for components in Nexus"
    with pytest.raises(CachitoError, match=expected):
        nexus.search_components(repository="cachito-js-hosted", type="npm")
Example #4
0
def test_search_components_connection_error(mock_requests):
    mock_requests.get.side_effect = requests.ConnectionError()

    expected = "Could not connect to the Nexus instance to search for components"
    with pytest.raises(CachitoError, match=expected):
        nexus.search_components(repository="cachito-js-hosted", type="npm")