Skip to content

Commit 2a2d862

Browse files
committed
Merge branch 'develop'
2 parents ed19834 + 5b7b171 commit 2a2d862

File tree

9 files changed

+91
-34
lines changed

9 files changed

+91
-34
lines changed

arcsecond/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
"ArcsecondConnectionError",
88
"ArcsecondInvalidEndpointError"]
99

10-
__version__ = '0.7.2'
10+
__version__ = '0.7.3'

arcsecond/api/endpoints/_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def _check_and_set_api_key(self, headers, url):
7070
if self.state.verbose:
7171
click.echo('Checking local API key... ', nl=False)
7272

73-
api_key = config_file_read_api_key(self.state.debug)
73+
api_key = config_file_read_api_key(self.state.config_section())
7474
if not api_key:
7575
raise ArcsecondError('Missing API key. You must login first: $ arcsecond login')
7676

@@ -81,7 +81,7 @@ def _check_and_set_api_key(self, headers, url):
8181
return headers
8282

8383
def _check_organisation_membership_and_permission(self, method_name, organisation):
84-
memberships = config_file_read_organisation_memberships(self.state.debug)
84+
memberships = config_file_read_organisation_memberships(self.state.config_section())
8585
if self.state.organisation not in memberships.keys():
8686
raise ArcsecondError('No membership found for organisation {}'.format(organisation))
8787
membership = memberships[self.state.organisation]

arcsecond/api/main.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from arcsecond.config import (config_file_path,
1515
config_file_read_api_key,
1616
config_file_save_api_key,
17+
config_file_read_username,
18+
config_file_read_organisation_memberships,
1719
config_file_save_organisation_membership)
1820

1921
from arcsecond.options import State
@@ -81,8 +83,16 @@ def factory(endpoint_class, state, **kwargs):
8183
@set_api_factory
8284
class Arcsecond(object):
8385
@classmethod
84-
def is_logged_in(cls, state=None):
85-
return ArcsecondAPI.is_logged_in(state)
86+
def is_logged_in(cls, state=None, **kwargs):
87+
return ArcsecondAPI.is_logged_in(state, **kwargs)
88+
89+
@classmethod
90+
def username(cls, state=None, **kwargs):
91+
return ArcsecondAPI.username(state, **kwargs)
92+
93+
@classmethod
94+
def memberships(cls, state=None, **kwargs):
95+
return ArcsecondAPI.memberships(state, **kwargs)
8696

8797
@classmethod
8898
def login(cls, username, password, subdomain, state=None):
@@ -200,7 +210,7 @@ def delete(self, id_name_uuid, **headers):
200210
def _check_organisation_membership(cls, state, username, subdomain):
201211
if state.verbose:
202212
click.echo('Checking Membership of Organisation with subdomain "{}"...'.format(subdomain))
203-
profile, error = PersonalProfileAPIEndPoint(State(verbose=False, debug=state.debug)).read(username)
213+
profile, error = PersonalProfileAPIEndPoint(state.make_new_silent()).read(username)
204214
if error:
205215
ArcsecondAPI._echo_error(state, error)
206216
else:
@@ -209,7 +219,7 @@ def _check_organisation_membership(cls, state, username, subdomain):
209219
if state.verbose:
210220
click.echo('Membership confirmed. Role is "{}", stored in {}.'
211221
.format(memberships[subdomain], config_file_path()))
212-
config_file_save_organisation_membership(subdomain, memberships[subdomain], state.debug)
222+
config_file_save_organisation_membership(subdomain, memberships[subdomain], state.config_section())
213223
else:
214224
if state.verbose:
215225
click.echo('Membership denied.')
@@ -223,15 +233,25 @@ def _get_and_save_api_key(cls, state, username, auth_token):
223233
return ArcsecondAPI._echo_error(state, error)
224234
if result:
225235
api_key = result['api_key']
226-
config_file_save_api_key(api_key, username, state.debug)
236+
config_file_save_api_key(api_key, username, state.config_section())
227237
if state.verbose:
228238
click.echo('Successful API key retrieval and storage in {}. Enjoy.'.format(config_file_path()))
229239
return result
230240

231241
@classmethod
232-
def is_logged_in(cls, state=None):
233-
state = get_api_state(state)
234-
return config_file_read_api_key(debug=state.debug) is not None
242+
def is_logged_in(cls, state=None, **kwargs):
243+
state = get_api_state(state, **kwargs)
244+
return config_file_read_api_key(section=state.config_section()) is not None
245+
246+
@classmethod
247+
def username(cls, state=None, **kwargs):
248+
state = get_api_state(state, **kwargs)
249+
return config_file_read_username(section=state.config_section()) or ''
250+
251+
@classmethod
252+
def memberships(cls, state=None, **kwargs):
253+
state = get_api_state(state, **kwargs)
254+
return config_file_read_organisation_memberships(section=state.config_section())
235255

236256
@classmethod
237257
def login(cls, username, password, subdomain, state=None):

arcsecond/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def login(state, username, password, organisation=None):
5656
@pass_state
5757
def me(state):
5858
"""Fetch your complete user profile."""
59-
username = config_file_read_username(state.debug)
59+
username = config_file_read_username(state.config_section())
6060
if not username:
6161
msg = 'Invalid/missing username: {}. Make sure to login first: $ arcsecond login'.format(username)
6262
raise ArcsecondError(msg)

arcsecond/config.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,17 @@ def config_file_exists():
1111
return os.path.exists(path) and os.path.isfile(path)
1212

1313

14-
def config_file_is_valid(debug=False):
14+
def config_file_is_valid(section='main'):
1515
if not config_file_exists():
1616
return False
1717
config = ConfigParser()
1818
config.read(config_file_path())
19-
section = 'debug' if debug else 'main'
2019
return config[section].get('api_key')
2120

2221

23-
def config_file_save_api_key(api_key, username, debug=False):
22+
def config_file_save_api_key(api_key, username, section='main'):
2423
config = ConfigParser()
2524
config.read(config_file_path())
26-
section = 'debug' if debug else 'main'
2725
if section not in config.keys():
2826
config.add_section(section)
2927
config.set(section, 'username', username)
@@ -32,10 +30,9 @@ def config_file_save_api_key(api_key, username, debug=False):
3230
config.write(f)
3331

3432

35-
def config_file_save_organisation_membership(subdomain, role, debug=False):
33+
def config_file_save_organisation_membership(subdomain, role, section='main'):
3634
config = ConfigParser()
3735
config.read(config_file_path())
38-
section = 'debug' if debug else 'main'
3936
section += ':organisations'
4037
if section not in config.keys():
4138
config.add_section(section)
@@ -44,39 +41,37 @@ def config_file_save_organisation_membership(subdomain, role, debug=False):
4441
config.write(f)
4542

4643

47-
def config_file_read_organisation_memberships(debug=False):
44+
def config_file_read_organisation_memberships(section='main'):
4845
config = ConfigParser()
4946
config.read(config_file_path())
50-
section = 'debug' if debug else 'main'
5147
section += ':organisations'
5248
if section not in config.sections():
5349
return {}
5450
return config[section]
5551

5652

57-
def config_file_read_key(key, debug=False):
53+
def config_file_read_key(key, section='main'):
5854
config = ConfigParser()
5955
config.read(config_file_path())
60-
section = 'debug' if debug else 'main'
6156
if section not in config.sections():
6257
return None
6358
return config[section].get(key, None)
6459

6560

66-
def config_file_read_api_key(debug=False):
67-
return config_file_read_key('api_key', debug=debug)
61+
def config_file_read_api_key(section='main'):
62+
return config_file_read_key('api_key', section=section)
6863

6964

70-
def config_file_read_username(debug=False):
71-
return config_file_read_key('username', debug=debug)
65+
def config_file_read_username(section='main'):
66+
return config_file_read_key('username', section=section)
7267

7368

74-
def config_file_clear_debug_session():
69+
def config_file_clear_section(section):
7570
config = ConfigParser()
7671
config.read(config_file_path())
77-
if 'debug' in config.sections():
78-
del config['debug']
79-
if 'debug:organisations' in config.sections():
80-
del config['debug:organisations']
72+
if section in config.sections():
73+
del config[section]
74+
if section + ':organisations' in config.sections():
75+
del config[section + ':organisations']
8176
with open(config_file_path(), 'w') as f:
8277
config.write(f)

arcsecond/options.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ def __init__(self, verbose=0, debug=False, open=None, organisation=None, is_usin
99
self.organisation = organisation
1010
self.is_using_cli = is_using_cli
1111

12+
def config_section(self):
13+
return 'debug' if self.debug else 'main'
14+
1215
def make_new_silent(self):
1316
return State(verbose=0,
1417
debug=self.debug,

tests/test_arcsecond_root.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from arcsecond import Arcsecond
2+
from .utils import save_test_credentials, clear_test_credentials
3+
4+
5+
def test_default_empty_state():
6+
clear_test_credentials()
7+
assert Arcsecond.is_logged_in(debug=True) is False
8+
assert Arcsecond.username(debug=True) == ''
9+
assert Arcsecond.memberships(debug=True) == {}
10+
11+
12+
def test_default_logged_in_state():
13+
save_test_credentials('cedric')
14+
assert Arcsecond.is_logged_in(debug=True) is True
15+
assert Arcsecond.username(debug=True) == 'cedric'
16+
assert Arcsecond.memberships(debug=True) == {}
17+
18+
19+
def test_default_logged_in_with_membership_state():
20+
save_test_credentials('cedric', {'saao': 'superadmin'})
21+
assert Arcsecond.is_logged_in(debug=True) is True
22+
assert Arcsecond.username(debug=True) == 'cedric'
23+
assert Arcsecond.memberships(debug=True) == {'saao': 'superadmin'}

tests/test_datasets_organisations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from arcsecond import cli
66
from arcsecond.api.error import ArcsecondError
7-
from arcsecond.config import config_file_clear_debug_session
7+
from arcsecond.config import config_file_clear_section
88

99
from .utils import (register_successful_personal_login,
1010
register_successful_organisation_login,
@@ -14,7 +14,7 @@
1414

1515
class DatasetsInOrganisationsTestCase(TestCase):
1616
def setUp(self):
17-
config_file_clear_debug_session()
17+
config_file_clear_section('debug')
1818
httpretty.enable()
1919

2020
def tearDown(self):

tests/utils.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import json
22

33
import httpretty
4-
from arcsecond.api.constants import API_AUTH_PATH_LOGIN, ARCSECOND_API_URL_DEV
4+
55
from arcsecond import cli
6+
from arcsecond.api.constants import API_AUTH_PATH_LOGIN, ARCSECOND_API_URL_DEV
7+
from arcsecond.config import (config_file_clear_section,
8+
config_file_save_api_key,
9+
config_file_save_organisation_membership)
610

711
TEST_LOGIN_USERNAME = 'robot1'
812
TEST_LOGIN_PASSWORD = 'robotpass'
@@ -75,6 +79,18 @@ def register_successful_organisation_login(runner, subdomain, role):
7579
input=TEST_LOGIN_USERNAME + '\n' + TEST_LOGIN_PASSWORD)
7680

7781

82+
def save_test_credentials(username, memberships=None):
83+
if memberships is None:
84+
memberships = dict()
85+
config_file_save_api_key(TEST_API_KEY, username, section='debug')
86+
for k, v in memberships.items():
87+
config_file_save_organisation_membership(k, v, 'debug')
88+
89+
90+
def clear_test_credentials():
91+
config_file_clear_section('debug')
92+
93+
7894
def mock_url_path(method, path, body='', query='', status=200):
7995
path = path + '/' if path[-1] != '/' else path
8096
httpretty.register_uri(method, ARCSECOND_API_URL_DEV + path + query, status=status, body=body)

0 commit comments

Comments
 (0)