1
+ import logging
2
+
1
3
import pytest
2
4
3
5
from twine import auth
4
6
from twine import exceptions
5
7
from twine import utils
6
8
7
- cred = auth .CredentialInput
8
-
9
9
10
10
@pytest .fixture
11
11
def config () -> utils .RepositoryConfig :
@@ -20,7 +20,7 @@ def get_password(system, user):
20
20
21
21
monkeypatch .setattr (auth , "keyring" , MockKeyring )
22
22
23
- pw = auth .Resolver (config , cred ("user" )).password
23
+ pw = auth .Resolver (config , auth . CredentialInput ("user" )).password
24
24
assert pw == "user@system sekure pa55word"
25
25
26
26
@@ -32,37 +32,41 @@ def get_password(system, user):
32
32
33
33
monkeypatch .setattr (auth , "keyring" , MockKeyring )
34
34
35
- pw = auth .Resolver (config , cred ("user" )).password
35
+ pw = auth .Resolver (config , auth . CredentialInput ("user" )).password
36
36
assert pw == "entered pw"
37
37
38
38
39
39
def test_no_password_defers_to_prompt (monkeypatch , entered_password , config ):
40
40
config .update (password = None )
41
- pw = auth .Resolver (config , cred ("user" )).password
41
+ pw = auth .Resolver (config , auth . CredentialInput ("user" )).password
42
42
assert pw == "entered pw"
43
43
44
44
45
45
def test_empty_password_bypasses_prompt (monkeypatch , entered_password , config ):
46
46
config .update (password = "" )
47
- pw = auth .Resolver (config , cred ("user" )).password
47
+ pw = auth .Resolver (config , auth . CredentialInput ("user" )).password
48
48
assert pw == ""
49
49
50
50
51
51
def test_no_username_non_interactive_aborts (config ):
52
52
with pytest .raises (exceptions .NonInteractive ):
53
- auth .Private (config , cred ("user" )).password
53
+ auth .Private (config , auth . CredentialInput ("user" )).password
54
54
55
55
56
56
def test_no_password_non_interactive_aborts (config ):
57
57
with pytest .raises (exceptions .NonInteractive ):
58
- auth .Private (config , cred ("user" )).password
58
+ auth .Private (config , auth . CredentialInput ("user" )).password
59
59
60
60
61
- def test_get_username_and_password_keyring_overrides_prompt (monkeypatch , config ):
61
+ def test_get_username_and_password_keyring_overrides_prompt (
62
+ monkeypatch , config , caplog
63
+ ):
64
+ caplog .set_level (logging .INFO , "twine" )
65
+
62
66
class MockKeyring :
63
67
@staticmethod
64
68
def get_credential (system , user ):
65
- return cred (
69
+ return auth . CredentialInput (
66
70
"real_user" , "real_user@{system} sekure pa55word" .format (** locals ())
67
71
)
68
72
@@ -75,10 +79,16 @@ def get_password(system, user):
75
79
76
80
monkeypatch .setattr (auth , "keyring" , MockKeyring )
77
81
78
- res = auth .Resolver (config , cred ())
82
+ res = auth .Resolver (config , auth .CredentialInput ())
83
+
79
84
assert res .username == "real_user"
80
85
assert res .password == "real_user@system sekure pa55word"
81
86
87
+ assert caplog .messages == [
88
+ "username set from keyring" ,
89
+ "password set from keyring" ,
90
+ ]
91
+
82
92
83
93
@pytest .fixture
84
94
def keyring_missing_get_credentials (monkeypatch ):
@@ -94,21 +104,21 @@ def entered_username(monkeypatch):
94
104
def test_get_username_keyring_missing_get_credentials_prompts (
95
105
entered_username , keyring_missing_get_credentials , config
96
106
):
97
- assert auth .Resolver (config , cred ()).username == "entered user"
107
+ assert auth .Resolver (config , auth . CredentialInput ()).username == "entered user"
98
108
99
109
100
110
def test_get_username_keyring_missing_non_interactive_aborts (
101
111
entered_username , keyring_missing_get_credentials , config
102
112
):
103
113
with pytest .raises (exceptions .NonInteractive ):
104
- auth .Private (config , cred ()).username
114
+ auth .Private (config , auth . CredentialInput ()).username
105
115
106
116
107
117
def test_get_password_keyring_missing_non_interactive_aborts (
108
118
entered_username , keyring_missing_get_credentials , config
109
119
):
110
120
with pytest .raises (exceptions .NonInteractive ):
111
- auth .Private (config , cred ("user" )).password
121
+ auth .Private (config , auth . CredentialInput ("user" )).password
112
122
113
123
114
124
@pytest .fixture
@@ -138,7 +148,7 @@ def get_credential(system, username):
138
148
def test_get_username_runtime_error_suppressed (
139
149
entered_username , keyring_no_backends_get_credential , recwarn , config
140
150
):
141
- assert auth .Resolver (config , cred ()).username == "entered user"
151
+ assert auth .Resolver (config , auth . CredentialInput ()).username == "entered user"
142
152
assert len (recwarn ) == 1
143
153
warning = recwarn .pop (UserWarning )
144
154
assert "fail!" in str (warning )
@@ -147,7 +157,7 @@ def test_get_username_runtime_error_suppressed(
147
157
def test_get_password_runtime_error_suppressed (
148
158
entered_password , keyring_no_backends , recwarn , config
149
159
):
150
- assert auth .Resolver (config , cred ("user" )).password == "entered pw"
160
+ assert auth .Resolver (config , auth . CredentialInput ("user" )).password == "entered pw"
151
161
assert len (recwarn ) == 1
152
162
warning = recwarn .pop (UserWarning )
153
163
assert "fail!" in str (warning )
@@ -162,4 +172,33 @@ def get_credential(system, username):
162
172
return None
163
173
164
174
monkeypatch .setattr (auth , "keyring" , FailKeyring ())
165
- assert auth .Resolver (config , cred ()).username == "entered user"
175
+ assert auth .Resolver (config , auth .CredentialInput ()).username == "entered user"
176
+
177
+
178
+ def test_logs_cli_values (caplog ):
179
+ caplog .set_level (logging .INFO , "twine" )
180
+
181
+ res = auth .Resolver (config , auth .CredentialInput ("username" , "password" ))
182
+
183
+ assert res .username == "username"
184
+ assert res .password == "password"
185
+
186
+ assert caplog .messages == [
187
+ "username set by command options" ,
188
+ "password set by command options" ,
189
+ ]
190
+
191
+
192
+ def test_logs_config_values (config , caplog ):
193
+ caplog .set_level (logging .INFO , "twine" )
194
+
195
+ config .update (username = "username" , password = "password" )
196
+ res = auth .Resolver (config , auth .CredentialInput ())
197
+
198
+ assert res .username == "username"
199
+ assert res .password == "password"
200
+
201
+ assert caplog .messages == [
202
+ "username set from config file" ,
203
+ "password set from config file" ,
204
+ ]
0 commit comments