Commit 19cd915b authored by Rob Carleski's avatar Rob Carleski 🇮🇸
Browse files

Fail on single-word shared account names

* collab-create-shared now requires first and last names be specified
* when account name is one long word
parent 9c462039
......@@ -25,8 +25,8 @@ class SharedAccount:
def __init__(self, args, config):
self.__dict__.update(args)
self.config = config
self.create_extra_attr()
self.logger = logging.getLogger(__name__)
self.create_extra_attr()
def create_box(self):
try:
......@@ -271,7 +271,7 @@ https://documentation.its.umich.edu/node/339/
'Adding Mcommunity group alias',
extra={'entity': self.account}
)
client.update_group_aliases(self.full_name)
client.update_group_aliases(self.group_alias)
except Exception as e:
self.logger.warning(e, extra={'entity': self.account})
......@@ -393,6 +393,13 @@ The password for your requested shared account is:
)
else:
split = re.split(r'[\-\_\.\s]+', self.account)
if len(split) == 1:
self.logger.error(
'Cannot create first and last name from given account.'
' Rerun script specifying first and last names.',
extra={'entity': self.account}
)
exit(2)
self.first_name = ' '.join(split[:len(split)//2]).title()
self.last_name = ' '.join(split[len(split)//2:]).title()
self.full_name = '{} {}'.format(
......@@ -401,7 +408,7 @@ The password for your requested shared account is:
)
self.account = re.sub(r'[^\w_-]', '.', self.account).lower().strip()
self.full_name = re.sub(r'[^\w_-]', '.', self.account).strip()
self.group_alias = re.sub(r'[^\w_-]', '.', self.full_name).strip()
self.email = '{}@{}'.format(
self.account,
self.config['google']['domain']
......
......@@ -3,6 +3,7 @@ from . import mock_kadmin
from . import mock_mcomm
from collab_admin_kit import create_shared, reset_shared
import os
import pytest
import yaml
from unittest import mock
......@@ -87,3 +88,35 @@ def test_upload_and_share_password():
]
}
sa.upload_and_share_password()
def test_valid_account_to_name_translation():
accounts = ['test-account', 'test.account', 'test_account', 'test account']
for account in accounts:
sa = create_shared.SharedAccount(
{
'account': account,
'password': 'pass12word',
'owners': ['testuser1', 'testuser2'],
'first_name': None,
'last_name': None
},
yaml.load(open(config), Loader=yaml.BaseLoader)
)
assert sa.first_name == "Test"
assert sa.last_name == "Account"
assert sa.full_name == "Test Account"
def test_invalid_account_to_name_translation(capsys):
with pytest.raises(SystemExit):
create_shared.SharedAccount(
{
'account': 'longaccountname',
'password': 'pass12word',
'owners': ['testuser1', 'testuser2'],
'first_name': None,
'last_name': None
},
yaml.load(open(config), Loader=yaml.BaseLoader)
)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment