def test_non_empty_user(self): user = User() data = { 'name': { 'fullName': 'Joe Doe', 'firstName': 'Joe', 'lastName': 'Doe' }, 'uid': 'joe.doe', 'roles': [], 'stateCode': [None], 'tenant': None, 'displayHome': False, 'guid': '123' } user.set_name(data['name']) user.set_uid(data['uid']) user.set_guid(data['guid']) name = user.get_name() self.assertEqual(name, {'name': data['name']}) uid = user.get_uid() self.assertEqual(uid, data['uid']) context = user.get_user_context() self.assertEqual(context['displayHome'], data['displayHome']) self.assertEqual(context['guid'], data['guid']) guid = user.get_guid() self.assertEqual(guid, data['guid'])
def test_get_user_context(self): user = User() data = {'name': {'fullName': 'Joe Doe', 'firstName': 'Joe', 'lastName': 'Doe'}, 'uid': 'joe.doe', 'roles': ['TEACHER'], 'tenant': 'dog', 'guid': '123'} user.set_name(data['name']) user.set_uid(data['uid']) user.set_guid(data['guid']) context = user.get_user_context() self.assertEqual(context['uid'], data['uid']) self.assertEqual(context['name'], data['name']) self.assertEqual(context['guid'], data['guid'])
def test_non_empty_user(self): user = User() data = {'name': {'fullName': 'Joe Doe', 'firstName': 'Joe', 'lastName': 'Doe'}, 'uid': 'joe.doe', 'roles': [], 'stateCode': [None], 'tenant': None, 'displayHome': False, 'guid': '123'} user.set_name(data['name']) user.set_uid(data['uid']) user.set_guid(data['guid']) name = user.get_name() self.assertEqual(name, {'name': data['name']}) uid = user.get_uid() self.assertEqual(uid, data['uid']) context = user.get_user_context() self.assertEqual(context['displayHome'], data['displayHome']) self.assertEqual(context['guid'], data['guid']) guid = user.get_guid() self.assertEqual(guid, data['guid'])
def test_get_user_context(self): user = User() data = { 'name': { 'fullName': 'Joe Doe', 'firstName': 'Joe', 'lastName': 'Doe' }, 'uid': 'joe.doe', 'roles': ['TEACHER'], 'tenant': 'dog', 'guid': '123' } user.set_name(data['name']) user.set_uid(data['uid']) user.set_guid(data['guid']) context = user.get_user_context() self.assertEqual(context['uid'], data['uid']) self.assertEqual(context['name'], data['name']) self.assertEqual(context['guid'], data['guid'])
class RegistrationTest(unittest.TestCase): def setUp(self): self.dummy_uid = 'bbunny' self.dummy_user = User() self.dummy_user.set_uid(self.dummy_uid) self.dummy_file_path = 'tmp/filename.zip' self.dummy_file_name = 'ActualName.zip' self.__request = DummyRequest() def tearDown(self): self.__request = None @patch('hpz.swi.download_service.authenticated_userid') @patch('hpz.frs.registration_service.FileRegistry.get_registration_info') @patch('os.path.isfile') @patch('hpz.swi.download_service.logger.info') def test_download_file(self, logger_patch, is_file_patch, get_reg_info_patch, auth_userid_patch): auth_userid_patch.return_value = self.dummy_user get_reg_info_patch.return_value = { "user_id": self.dummy_uid, "file_path": self.dummy_file_path, "file_name": self.dummy_file_name } is_file_patch.return_value = True logger_patch.return_value = None self.__request.method = 'GET' self.__request.matchdict['reg_id'] = '1234' self.__request.registry.settings['mode'] = 'prod' response = download_file(None, self.__request) self.assertEqual(response.status_code, 200) headers = response.headers self.assertEqual(len(headers), 3) self.assertEqual(headers['X-Sendfile'], self.dummy_file_path) self.assertEqual(headers['Content-Type'], '') self.assertEqual(headers['Content-Disposition'], 'attachment; filename=ActualName.zip') logger_patch.assert_called_once_with( 'File %s was successfully downloaded', 'tmp/filename.zip') @patch('hpz.swi.download_service.authenticated_userid') @patch('hpz.frs.registration_service.FileRegistry.get_registration_info') @patch('os.path.isfile') @patch('hpz.swi.download_service.logger.error') def test_download_file_not_registered(self, logger_patch, is_file_patch, get_reg_info_patch, auth_userid_patch): auth_userid_patch.return_value = self.dummy_user get_reg_info_patch.return_value = None is_file_patch.return_value = True logger_patch.return_value = None self.__request.method = 'GET' self.__request.matchdict['reg_id'] = '12345' response = download_file(None, self.__request) self.assertEqual(response.status_code, 404) logger_patch.assert_called_once_with( 'No file record is registered with requested id %s', '12345') @patch('hpz.swi.download_service.authenticated_userid') @patch('hpz.frs.registration_service.FileRegistry.get_registration_info') @patch('os.path.isfile') @patch('hpz.swi.download_service.logger.error') def test_download_file_not_owner(self, logger_patch, is_file_patch, get_reg_info_patch, auth_userid_patch): auth_userid_patch.return_value = self.dummy_user reg_user_id = 'dduck' get_reg_info_patch.return_value = { "user_id": reg_user_id, "file_path": self.dummy_file_path, "file_name": self.dummy_file_name } is_file_patch.return_value = True logger_patch.return_value = None self.__request.method = 'GET' self.__request.matchdict['reg_id'] = '12345' response = download_file(None, self.__request) self.assertEqual(response.status_code, 404) logger_patch.assert_called_once_with( 'User %s is not owner of the file with registration id %s', 'bbunny', '12345') @patch('hpz.swi.download_service.authenticated_userid') @patch('hpz.frs.registration_service.FileRegistry.get_registration_info') @patch('os.path.isfile') @patch('hpz.swi.download_service.logger.error') def test_download_file_still_processing(self, logger_patch, is_file_patch, get_reg_info_patch, auth_userid_patch): auth_userid_patch.return_value = self.dummy_user get_reg_info_patch.return_value = { "user_id": self.dummy_uid, "file_path": None, "file_name": None } is_file_patch.return_value = True logger_patch.return_value = None self.__request.method = 'GET' self.__request.matchdict['reg_id'] = '12345' response = download_file(None, self.__request) self.assertEqual(response.status_code, 404) logger_patch.assert_called_once_with( 'File with registration id %s is not yet available', '12345') @patch('hpz.swi.download_service.authenticated_userid') @patch('hpz.frs.registration_service.FileRegistry.get_registration_info') @patch('os.path.isfile') @patch('hpz.swi.download_service.logger.error') def test_download_file_not_on_disk(self, logger_patch, is_file_patch, get_reg_info_patch, auth_userid_patch): auth_userid_patch.return_value = self.dummy_user get_reg_info_patch.return_value = { "user_id": self.dummy_uid, "file_path": self.dummy_file_path, "file_name": self.dummy_file_name } is_file_patch.return_value = False logger_patch.return_value = None self.__request.method = 'GET' self.__request.matchdict['reg_id'] = '12345' response = download_file(None, self.__request) self.assertEqual(response.status_code, 404) logger_patch.assert_called_once_with( 'File %s is registered, but does not exist on disk', 'tmp/filename.zip') @patch('hpz.swi.download_service.authenticated_userid') @patch('hpz.frs.registration_service.FileRegistry.get_registration_info') def test_file_exists_file_not_exist(self, get_reg_info_patch, auth_userid_patch): auth_userid_patch.return_value = self.dummy_user get_reg_info_patch.return_value = { "user_id": self.dummy_uid, "file_path": self.dummy_file_path, "file_name": self.dummy_file_name } self.__request.method = 'GET' self.__request.matchdict['reg_id'] = '12345' response = is_file_exist(None, self.__request) self.assertEqual(response.status_code, 404) @patch('hpz.swi.download_service.authenticated_userid') @patch('hpz.frs.registration_service.FileRegistry.get_registration_info') @patch('os.path.isfile') @patch('hpz.swi.download_service.logger.info') def test_file_exists_for_valid_file(self, logger_patch, is_file_patch, get_reg_info_patch, auth_userid_patch): auth_userid_patch.return_value = self.dummy_user get_reg_info_patch.return_value = { "user_id": self.dummy_uid, "file_path": self.dummy_file_path, "file_name": self.dummy_file_name } is_file_patch.return_value = True logger_patch.return_value = None self.__request.method = 'GET' self.__request.matchdict['reg_id'] = '1234' response = is_file_exist(None, self.__request) self.assertEqual(response.status_code, 200)
class RegistrationTest(unittest.TestCase): def setUp(self): self.dummy_uid = 'bbunny' self.dummy_user = User() self.dummy_user.set_uid(self.dummy_uid) self.dummy_file_path = 'tmp/filename.zip' self.dummy_file_name = 'ActualName.zip' self.__request = DummyRequest() def tearDown(self): self.__request = None @patch('hpz.swi.download_service.authenticated_userid') @patch('hpz.frs.registration_service.FileRegistry.get_registration_info') @patch('os.path.isfile') @patch('hpz.swi.download_service.logger.info') def test_download_file(self, logger_patch, is_file_patch, get_reg_info_patch, auth_userid_patch): auth_userid_patch.return_value = self.dummy_user get_reg_info_patch.return_value = {"user_id": self.dummy_uid, "file_path": self.dummy_file_path, "file_name": self.dummy_file_name} is_file_patch.return_value = True logger_patch.return_value = None self.__request.method = 'GET' self.__request.matchdict['reg_id'] = '1234' self.__request.registry.settings['mode'] = 'prod' response = download_file(None, self.__request) self.assertEqual(response.status_code, 200) headers = response.headers self.assertEqual(len(headers), 3) self.assertEqual(headers['X-Sendfile'], self.dummy_file_path) self.assertEqual(headers['Content-Type'], '') self.assertEqual(headers['Content-Disposition'], 'attachment; filename=ActualName.zip') logger_patch.assert_called_once_with('File %s was successfully downloaded', 'tmp/filename.zip') @patch('hpz.swi.download_service.authenticated_userid') @patch('hpz.frs.registration_service.FileRegistry.get_registration_info') @patch('os.path.isfile') @patch('hpz.swi.download_service.logger.error') def test_download_file_not_registered(self, logger_patch, is_file_patch, get_reg_info_patch, auth_userid_patch): auth_userid_patch.return_value = self.dummy_user get_reg_info_patch.return_value = None is_file_patch.return_value = True logger_patch.return_value = None self.__request.method = 'GET' self.__request.matchdict['reg_id'] = '12345' response = download_file(None, self.__request) self.assertEqual(response.status_code, 404) logger_patch.assert_called_once_with('No file record is registered with requested id %s', '12345') @patch('hpz.swi.download_service.authenticated_userid') @patch('hpz.frs.registration_service.FileRegistry.get_registration_info') @patch('os.path.isfile') @patch('hpz.swi.download_service.logger.error') def test_download_file_not_owner(self, logger_patch, is_file_patch, get_reg_info_patch, auth_userid_patch): auth_userid_patch.return_value = self.dummy_user reg_user_id = 'dduck' get_reg_info_patch.return_value = {"user_id": reg_user_id, "file_path": self.dummy_file_path, "file_name": self.dummy_file_name} is_file_patch.return_value = True logger_patch.return_value = None self.__request.method = 'GET' self.__request.matchdict['reg_id'] = '12345' response = download_file(None, self.__request) self.assertEqual(response.status_code, 404) logger_patch.assert_called_once_with('User %s is not owner of the file with registration id %s', 'bbunny', '12345') @patch('hpz.swi.download_service.authenticated_userid') @patch('hpz.frs.registration_service.FileRegistry.get_registration_info') @patch('os.path.isfile') @patch('hpz.swi.download_service.logger.error') def test_download_file_still_processing(self, logger_patch, is_file_patch, get_reg_info_patch, auth_userid_patch): auth_userid_patch.return_value = self.dummy_user get_reg_info_patch.return_value = {"user_id": self.dummy_uid, "file_path": None, "file_name": None} is_file_patch.return_value = True logger_patch.return_value = None self.__request.method = 'GET' self.__request.matchdict['reg_id'] = '12345' response = download_file(None, self.__request) self.assertEqual(response.status_code, 404) logger_patch.assert_called_once_with('File with registration id %s is not yet available', '12345') @patch('hpz.swi.download_service.authenticated_userid') @patch('hpz.frs.registration_service.FileRegistry.get_registration_info') @patch('os.path.isfile') @patch('hpz.swi.download_service.logger.error') def test_download_file_not_on_disk(self, logger_patch, is_file_patch, get_reg_info_patch, auth_userid_patch): auth_userid_patch.return_value = self.dummy_user get_reg_info_patch.return_value = {"user_id": self.dummy_uid, "file_path": self.dummy_file_path, "file_name": self.dummy_file_name} is_file_patch.return_value = False logger_patch.return_value = None self.__request.method = 'GET' self.__request.matchdict['reg_id'] = '12345' response = download_file(None, self.__request) self.assertEqual(response.status_code, 404) logger_patch.assert_called_once_with('File %s is registered, but does not exist on disk', 'tmp/filename.zip') @patch('hpz.swi.download_service.authenticated_userid') @patch('hpz.frs.registration_service.FileRegistry.get_registration_info') def test_file_exists_file_not_exist(self, get_reg_info_patch, auth_userid_patch): auth_userid_patch.return_value = self.dummy_user get_reg_info_patch.return_value = {"user_id": self.dummy_uid, "file_path": self.dummy_file_path, "file_name": self.dummy_file_name} self.__request.method = 'GET' self.__request.matchdict['reg_id'] = '12345' response = is_file_exist(None, self.__request) self.assertEqual(response.status_code, 404) @patch('hpz.swi.download_service.authenticated_userid') @patch('hpz.frs.registration_service.FileRegistry.get_registration_info') @patch('os.path.isfile') @patch('hpz.swi.download_service.logger.info') def test_file_exists_for_valid_file(self, logger_patch, is_file_patch, get_reg_info_patch, auth_userid_patch): auth_userid_patch.return_value = self.dummy_user get_reg_info_patch.return_value = {"user_id": self.dummy_uid, "file_path": self.dummy_file_path, "file_name": self.dummy_file_name} is_file_patch.return_value = True logger_patch.return_value = None self.__request.method = 'GET' self.__request.matchdict['reg_id'] = '1234' response = is_file_exist(None, self.__request) self.assertEqual(response.status_code, 200)
class Session: ''' Simple class that holds user session information, such as guid, user id, name, roles, and tenant ''' def __init__(self): self.__initialize_session() # leave datetime only this class, not save in session context self.__expiration = None self.__last_access = None # initialize all session values def __initialize_session(self): self.__session = {} self.__user = User() self.__session_id = None self.__session['idpSessionIndex'] = None self.__session['nameId'] = None # serialize to text def get_session_json_context(self): # Get User Info and combined the dictionary combined_context = self.__user.get_user_context() combined_context.update_session(self.__session) return json.dumps(combined_context) def get_session_id(self): return self.__session_id def get_uid(self): return self.__user.get_uid() def get_email(self): return self.__user.get_email() def get_roles(self): return self.__user.get_roles() def get_tenants(self): return self.__user.get_tenants() def get_guid(self): return self.__user.get_guid() def get_name(self): return self.__user.get_name() def get_idp_session_index(self): return self.__session['idpSessionIndex'] def get_name_id(self): return self.__session['nameId'] def get_last_access(self): return self.__last_access def get_expiration(self): return self.__expiration def get_user(self): return self.__user def set_session_id(self, session_id): ''' @param session_id: the session id ''' self.__session_id = session_id def set_uid(self, uid): ''' @param uid: the uid ''' self.__user.set_uid(uid) def set_email(self, email): ''' @param uid: the uid ''' self.__user.set_email(email) def set_user_context(self, context): self.__user.set_context(context) def set_guid(self, guid): ''' @param guid: the user guid to set ''' self.__user.set_guid(guid) def set_fullName(self, fullName): ''' @param fullName: the full name ''' self.__user.set_full_name(fullName) def set_lastName(self, lastName): ''' @param lastName: the last name ''' self.__user.set_last_name(lastName) def set_firstName(self, firstName): ''' @param firstName: the first name ''' self.__user.set_first_name(firstName) def set_idp_session_index(self, index): ''' @param index: the idp session index ''' self.__session['idpSessionIndex'] = index def set_name_id(self, name_id): ''' @param name_id: the name id ''' self.__session['nameId'] = name_id def set_session(self, session): self.__session = session self.__set_user(session) def set_expiration(self, datetime): self.__expiration = datetime def set_last_access(self, datetime): self.__last_access = datetime def __set_user(self, info): self.__user.set_user_info(info)