File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change 4
4
from collections import Callable
5
5
from github3 import __version__
6
6
from logging import getLogger
7
+ from contextlib import contextmanager
7
8
8
9
__url_cache__ = {}
9
10
__logs__ = getLogger (__package__ )
@@ -109,3 +110,15 @@ def token_auth(self, token):
109
110
})
110
111
# Unset username/password so we stop sending them
111
112
self .auth = None
113
+
114
+ @contextmanager
115
+ def temporary_basic_auth (self , * auth ):
116
+ old_basic_auth = self .auth
117
+ old_token_auth = self .headers .get ('Authorization' )
118
+
119
+ self .basic_auth (* auth )
120
+ yield
121
+
122
+ self .auth = old_basic_auth
123
+ if old_token_auth :
124
+ self .headers ['Authorization' ] = old_token_auth
Original file line number Diff line number Diff line change @@ -184,3 +184,19 @@ def test_issubclass_of_requests_Session(self):
184
184
"""Test that GitHubSession is a subclass of requests.Session"""
185
185
assert issubclass (session .GitHubSession ,
186
186
requests .Session )
187
+
188
+ def test_can_use_temporary_basic_auth (self ):
189
+ """Test that temporary_basic_auth resets old auth."""
190
+ s = self .build_session ()
191
+ s .basic_auth ('foo' , 'bar' )
192
+ with s .temporary_basic_auth ('temp' , 'pass' ):
193
+ assert s .auth != ('foo' , 'bar' )
194
+
195
+ assert s .auth == ('foo' , 'bar' )
196
+
197
+ def test_temporary_basic_auth_replaces_auth (self ):
198
+ """Test that temporary_basic_auth sets the proper credentials."""
199
+ s = self .build_session ()
200
+ s .basic_auth ('foo' , 'bar' )
201
+ with s .temporary_basic_auth ('temp' , 'pass' ):
202
+ assert s .auth == ('temp' , 'pass' )
You can’t perform that action at this time.
0 commit comments