def test_login(run_cli):
    cmd = "bugzilla login FOO BAR"

    fakebz = tests.mockbackend.make_bz(
        user_login_args="data/mockargs/test_login.txt",
        user_login_return=RuntimeError("TEST ERROR"))
    out = run_cli(cmd, fakebz, expectfail=True)
    assert "Login failed: TEST ERROR" in out

    fakebz = tests.mockbackend.make_bz(
        user_login_args="data/mockargs/test_login.txt",
        user_login_return={})
    out = run_cli(cmd, fakebz)
    assert "Login successful" in out

    cmd = "bugzilla --restrict-login --user FOO --password BAR login"
    fakebz = tests.mockbackend.make_bz(
        user_login_args="data/mockargs/test_login-restrict.txt",
        user_login_return={})
    out = run_cli(cmd, fakebz)
    assert "Login successful" in out

    cmd = "bugzilla --ensure-logged-in --user FOO --password BAR login"
    # Raises raw error trying to see if we aren't logged in
    with pytest.raises(NotImplementedError):
        fakebz = tests.mockbackend.make_bz(
            user_login_args="data/mockargs/test_login.txt",
            user_login_return={},
            user_get_args=None,
            user_get_return=NotImplementedError())
        out = run_cli(cmd, fakebz)

    # Errors with expected code
    cmd = "bugzilla --ensure-logged-in --user FOO --password BAR login"
    fakebz = tests.mockbackend.make_bz(
        user_login_args="data/mockargs/test_login.txt",
        user_login_return={},
        user_get_args=None,
        user_get_return=bugzilla.BugzillaError("TESTMESSAGE", code=505))
    out = run_cli(cmd, fakebz, expectfail=True)
    assert "--ensure-logged-in passed but you" in out

    # Returns success for logged_in check and hits a tokenfile line
    cmd = "bugzilla --ensure-logged-in "
    cmd += "login FOO BAR"
    fakebz = tests.mockbackend.make_bz(
        bz_kwargs={"use_creds": True},
        user_login_args="data/mockargs/test_login.txt",
        user_login_return={'id': 1234, 'token': 'my-fake-token'},
        user_get_args=None,
        user_get_return={})
    out = run_cli(cmd, fakebz)
    assert "Token cache saved" in out
    assert fakebz.tokenfile in out
    assert "Consider using bugzilla API" in out
def test_query_url_fail():
    # test some handling of query from_url errors
    query = {"query_format": "advanced", "product": "FOO"}
    checkstr = "does not appear to support"

    exc = bugzilla.BugzillaError("FAKEERROR query_format", code=123)
    bz = tests.mockbackend.make_bz(version="4.0.0",
                                   bug_search_args=None,
                                   bug_search_return=exc)
    try:
        bz.query(query)
    except Exception as e:
        assert checkstr in str(e)

    bz = tests.mockbackend.make_bz(version="5.1.0",
                                   bug_search_args=None,
                                   bug_search_return=exc)
    try:
        bz.query(query)
    except Exception as e:
        assert checkstr not in str(e)
Example #3
0
def testExceptions(run_cli):
    """
    Test exception handling around main()
    """
    fakebz = tests.mockbackend.make_bz(bug_search_args=None,
                                       bug_search_return=KeyboardInterrupt())
    out = run_cli("bugzilla query --bug_id 1", fakebz, expectfail=True)
    assert "user request" in out

    fakebz = tests.mockbackend.make_bz(
        bug_search_args=None, bug_search_return=bugzilla.BugzillaError("foo"))
    out = run_cli("bugzilla query --bug_id 1", fakebz, expectfail=True)
    assert "Server error:" in out

    fakebz = tests.mockbackend.make_bz(
        bug_search_args=None, bug_search_return=requests.exceptions.SSLError())
    out = run_cli("bugzilla query --bug_id 1", fakebz, expectfail=True)
    assert "trust the remote server" in out

    fakebz = tests.mockbackend.make_bz(
        bug_search_args=None,
        bug_search_return=requests.exceptions.ConnectionError())
    out = run_cli("bugzilla query --bug_id 1", fakebz, expectfail=True)
    assert "Connection lost" in out