/
test_credentials.py
82 lines (63 loc) · 2.54 KB
/
test_credentials.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import os
import shutil
import sys
import unittest
import solvebio
import solvebio.cli.credentials as creds
import contextlib
@contextlib.contextmanager
def nostdout():
savestderr = sys.stdout
class Devnull(object):
def write(self, _):
pass
sys.stdout = Devnull()
try:
yield
finally:
sys.stdout = savestderr
class TestCredentials(unittest.TestCase):
def setUp(self):
self.solvebiodir = os.path.join(os.path.dirname(__file__),
'data', '.solvebio')
self.api_host = solvebio.api_host
solvebio.api_host = 'https://api.solvebio.com'
def tearDown(self):
solvebio.api_host = self.api_host
if os.path.isdir(self.solvebiodir):
shutil.rmtree(self.solvebiodir)
def test_credentials(self):
datadir = os.path.join(os.path.dirname(__file__), 'data')
os.environ['HOME'] = datadir
# Make sure we don't have have the test solvebio directory
if os.path.isdir(self.solvebiodir):
shutil.rmtree(self.solvebiodir)
cred_file = creds.netrc.path()
self.assertTrue(os.path.exists(cred_file),
"cred file created when it doesn't exist first")
self.assertEqual(creds.get_credentials(), None,
'Should not find credentials')
test_credentials_file = os.path.join(datadir, 'test_creds')
shutil.copy(test_credentials_file, cred_file)
auths = creds.get_credentials()
self.assertTrue(auths is not None, 'Should find credentials')
solvebio.api_host = 'https://example.com'
auths = creds.get_credentials()
self.assertEqual(auths, None,
'Should not find credentials for host {0}'
.format(solvebio.api_host))
solvebio.api_host = 'https://api.solvebio.com'
creds.delete_credentials()
auths = creds.get_credentials()
self.assertEqual(auths, None,
'Should not find removed credentials for '
'host {0}'.format(solvebio.api_host))
pair = ('testagain@solvebio.com', 'b00b00',)
creds.save_credentials(*pair)
auths = creds.get_credentials()
self.assertTrue(auths is not None,
'Should get newly set credentials for '
'host {0}'.format(solvebio.api_host))
self.assertEqual(auths, pair, 'Should get back creds we saved')