@@ -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,43 @@ 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' )
150
155
151
156
def test_token_auth_does_not_use_falsey_values (self ):
152
157
"""Test that token auth will not authenticate with falsey values"""
153
158
bad_tokens = [None , '' ]
159
+ req = requests .Request ('GET' , 'https://api.github.com/' )
154
160
for token in bad_tokens :
155
161
s = self .build_session ()
156
162
s .token_auth (token )
157
- assert 'Authorization' not in s .headers
163
+ pr = s .prepare_request (req )
164
+ assert 'Authorization' not in pr .headers
165
+
166
+ def test_token_auth_with_netrc_works (self , tmpdir ):
167
+
168
+ token = "my-valid-token"
169
+ s = self .build_session ()
170
+ s .token_auth (token )
171
+
172
+ netrc_contents = (
173
+ "machine api.github.com\n "
174
+ "login sigmavirus24\n "
175
+ "password invalid_token_for_test_verification\n "
176
+ )
177
+ # cover testing netrc behaviour on different OSs
178
+ dotnetrc = tmpdir .join (".netrc" )
179
+ dotnetrc .write (netrc_contents )
180
+ dashnetrc = tmpdir .join ("_netrc" )
181
+ dashnetrc .write (netrc_contents )
182
+
183
+ with mock .patch .dict ('os.environ' , {'HOME' : str (tmpdir )}):
184
+ # prepare_request triggers reading of .netrc files
185
+ pr = s .prepare_request (
186
+ requests .Request (
187
+ 'GET' , 'https://api.github.com/users/sigmavirus24' )
188
+ )
189
+ auth_header = pr .headers ['Authorization' ]
190
+ assert auth_header == 'token {0}' .format (token )
158
191
159
192
def test_two_factor_auth_callback_handles_None (self ):
160
193
s = self .build_session ()
@@ -214,13 +247,15 @@ def test_no_auth(self):
214
247
"""Verify that no_auth removes existing authentication."""
215
248
s = self .build_session ()
216
249
s .basic_auth ('user' , 'password' )
217
- s . headers [ 'Authorization' ] = 'token foobarbogus'
250
+ req = requests . Request ( 'GET' , 'https://api.github.com/' )
218
251
219
252
with s .no_auth ():
220
- assert 'Authentication' not in s .headers
253
+ pr = s .prepare_request (req )
254
+ assert 'Authorization' not in pr .headers
221
255
assert s .auth is None
222
256
223
- assert s .headers ['Authorization' ] == 'token foobarbogus'
257
+ pr = s .prepare_request (req )
258
+ assert 'Authorization' in pr .headers
224
259
assert s .auth == ('user' , 'password' )
225
260
226
261
def test_retrieve_client_credentials_when_set (self ):
0 commit comments