@@ -85,9 +85,12 @@ def test_basic_login_disables_token_auth(self):
85
85
"""
86
86
s = self .build_session ()
87
87
s .token_auth ('token goes here' )
88
- assert 'Authorization' in s .headers
88
+ req = requests .Request ('GET' , 'https://api.github.com/' )
89
+ pr = s .prepare_request (req )
90
+ assert 'token token goes here' == pr .headers ['Authorization' ]
89
91
s .basic_auth ('username' , 'password' )
90
- assert 'Authorization' not in s .headers
92
+ pr = s .prepare_request (req )
93
+ assert 'token token goes here' != pr .headers ['Authorization' ]
91
94
92
95
@mock .patch .object (requests .Session , 'request' )
93
96
def test_handle_two_factor_auth (self , request_mock ):
@@ -136,7 +139,9 @@ def test_token_auth(self):
136
139
"""Test that token auth will work with a valid token"""
137
140
s = self .build_session ()
138
141
s .token_auth ('token goes here' )
139
- assert s .headers ['Authorization' ] == 'token token goes here'
142
+ req = requests .Request ('GET' , 'https://api.github.com/' )
143
+ pr = s .prepare_request (req )
144
+ assert pr .headers ['Authorization' ] == 'token token goes here'
140
145
141
146
def test_token_auth_disables_basic_auth (self ):
142
147
"""Test that using token auth removes the value of the auth attribute.
@@ -146,15 +151,49 @@ def test_token_auth_disables_basic_auth(self):
146
151
s = self .build_session ()
147
152
s .auth = ('foo' , 'bar' )
148
153
s .token_auth ('token goes here' )
149
- assert s .auth is None
154
+ assert s .auth != ('foo' , 'bar' )
155
+ assert isinstance (s .auth , session .TokenAuth )
150
156
151
157
def test_token_auth_does_not_use_falsey_values (self ):
152
158
"""Test that token auth will not authenticate with falsey values"""
153
159
bad_tokens = [None , '' ]
160
+ req = requests .Request ('GET' , 'https://api.github.com/' )
154
161
for token in bad_tokens :
155
162
s = self .build_session ()
156
163
s .token_auth (token )
157
- assert 'Authorization' not in s .headers
164
+ pr = s .prepare_request (req )
165
+ assert 'Authorization' not in pr .headers
166
+
167
+ def test_token_auth_with_netrc_works (self , tmpdir ):
168
+ """
169
+ Test that token auth will be used instead of netrc.
170
+
171
+ With no auth specified, requests will use any matching auths
172
+ in .netrc/_netrc files
173
+ """
174
+ token = "my-valid-token"
175
+ s = self .build_session ()
176
+ s .token_auth (token )
177
+
178
+ netrc_contents = (
179
+ "machine api.github.com\n "
180
+ "login sigmavirus24\n "
181
+ "password invalid_token_for_test_verification\n "
182
+ )
183
+ # cover testing netrc behaviour on different OSs
184
+ dotnetrc = tmpdir .join (".netrc" )
185
+ dotnetrc .write (netrc_contents )
186
+ dashnetrc = tmpdir .join ("_netrc" )
187
+ dashnetrc .write (netrc_contents )
188
+
189
+ with mock .patch .dict ('os.environ' , {'HOME' : str (tmpdir )}):
190
+ # prepare_request triggers reading of .netrc files
191
+ pr = s .prepare_request (
192
+ requests .Request (
193
+ 'GET' , 'https://api.github.com/users/sigmavirus24' )
194
+ )
195
+ auth_header = pr .headers ['Authorization' ]
196
+ assert auth_header == 'token {0}' .format (token )
158
197
159
198
def test_two_factor_auth_callback_handles_None (self ):
160
199
s = self .build_session ()
@@ -214,13 +253,15 @@ def test_no_auth(self):
214
253
"""Verify that no_auth removes existing authentication."""
215
254
s = self .build_session ()
216
255
s .basic_auth ('user' , 'password' )
217
- s . headers [ 'Authorization' ] = 'token foobarbogus'
256
+ req = requests . Request ( 'GET' , 'https://api.github.com/' )
218
257
219
258
with s .no_auth ():
220
- assert 'Authentication' not in s .headers
259
+ pr = s .prepare_request (req )
260
+ assert 'Authorization' not in pr .headers
221
261
assert s .auth is None
222
262
223
- assert s .headers ['Authorization' ] == 'token foobarbogus'
263
+ pr = s .prepare_request (req )
264
+ assert 'Authorization' in pr .headers
224
265
43AF
assert s .auth == ('user' , 'password' )
225
266
226
267
def test_retrieve_client_credentials_when_set (self ):
0 commit comments