File: test_cli_login.py

package info (click to toggle)
python-bugzilla 3.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,116 kB
  • sloc: python: 6,160; makefile: 7
file content (125 lines) | stat: -rw-r--r-- 4,354 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.

import tempfile

import pytest

import bugzilla

import tests
import tests.mockbackend
import tests.utils


#################################
# 'bugzilla login' mock testing #
#################################

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"
    tmp = tempfile.NamedTemporaryFile()
    fakebz = tests.mockbackend.make_bz(
        bz_kwargs={"use_creds": True, "tokenfile": tmp.name},
        user_login_args="data/mockargs/test_login.txt",
        user_login_return={'id': 1234, 'token': 'my-fake-token'},
        user_get_args=None,
        user_get_return={})
    fakebz.connect("https://example.com")
    out = run_cli(cmd, fakebz)
    assert "Token cache saved" in out
    assert fakebz.tokenfile in out
    assert "Consider using bugzilla API" in out
    tests.utils.diff_compare(open(tmp.name).read(),
            "data/clioutput/tokenfile.txt")

    # Returns success for logged_in check and hits another tokenfile line
    cmd = "bugzilla --ensure-logged-in "
    cmd += "login FOO BAR"
    fakebz = tests.mockbackend.make_bz(
        bz_kwargs={"use_creds": True, "tokenfile": None},
        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 not saved" in out


def test_interactive_login(monkeypatch, run_cli):
    bz = tests.mockbackend.make_bz(
        user_login_args="data/mockargs/test_interactive_login.txt",
        user_login_return={},
        user_logout_args=None,
        user_logout_return={},
        user_get_args=None,
        user_get_return={})

    tests.utils.monkeypatch_getpass(monkeypatch)
    cmd = "bugzilla login"
    fakestdin = "fakeuser\nfakepass\n"
    out = run_cli(cmd, bz, stdin=fakestdin)
    assert "Bugzilla Username:" in out
    assert "Bugzilla Password:" in out

    # API key prompting and saving
    tmp = tempfile.NamedTemporaryFile()
    bz.configpath = [tmp.name]
    bz.url = "https://example.com"

    cmd = "bugzilla login --api-key"
    fakestdin = "MY-FAKE-KEY\n"
    out = run_cli(cmd, bz, stdin=fakestdin)
    assert "API Key:" in out
    assert tmp.name in out
    tests.utils.diff_compare(open(tmp.name).read(),
            "data/clioutput/test_interactive_login_apikey_rcfile.txt")

    # Check that we don't attempt to log in if API key is configured
    assert bz.api_key
    cmd = "bugzilla login"
    out = run_cli(cmd, bz)
    assert "already have an API" in out