|
1 | 1 | from __future__ import unicode_literals |
2 | 2 | import json |
3 | 3 | import time |
| 4 | +import tempfile |
| 5 | +import shutil |
| 6 | +import os |
4 | 7 | from base64 import b64encode |
5 | 8 | from copy import deepcopy |
6 | 9 | from unittest import TestCase |
@@ -242,3 +245,36 @@ def fake_send(r, **kwargs): |
242 | 245 | self.assertIs(sess.authorized, False) |
243 | 246 | sess.fetch_token(url) |
244 | 247 | self.assertIs(sess.authorized, True) |
| 248 | + |
| 249 | + |
| 250 | +class OAuth2SessionNetrcTest(OAuth2SessionTest): |
| 251 | + """Ensure that there is no magic auth handling. |
| 252 | +
|
| 253 | + By default, requests sessions have magic handling of netrc files, |
| 254 | + which is undesirable for this library because it will take |
| 255 | + precedence over manually set authentication headers. |
| 256 | + """ |
| 257 | + |
| 258 | + def setUp(self): |
| 259 | + # Set up a temporary home directory |
| 260 | + self.homedir = tempfile.mkdtemp() |
| 261 | + self.prehome = os.environ.get('HOME', None) |
| 262 | + os.environ['HOME'] = self.homedir |
| 263 | + |
| 264 | + # Write a .netrc file that will cause problems |
| 265 | + netrc_loc = os.path.expanduser('~/.netrc') |
| 266 | + with open(netrc_loc, 'w') as f: |
| 267 | + f.write( |
| 268 | + 'machine i.b\n' |
| 269 | + ' password abc123\n' |
| 270 | + |
| 271 | + ) |
| 272 | + |
| 273 | + super(OAuth2SessionNetrcTest, self).setUp() |
| 274 | + |
| 275 | + def tearDown(self): |
| 276 | + super(OAuth2SessionNetrcTest, self).tearDown() |
| 277 | + |
| 278 | + if self.prehome is not None: |
| 279 | + os.environ['HOME'] = self.prehome |
| 280 | + shutil.rmtree(self.homedir) |
0 commit comments