def _get_content(self, content_path_fetcher): path = content_path_fetcher() content_path = self.reduce_path(path) splited_local_path = content_path.strip('/').split('/') workspace_name = webdav_convert_file_name_to_bdd(splited_local_path[0]) wapi = WorkspaceApi( current_user=self.current_user, session=self.dbsession, config=self.app_config, ) workspace = wapi.get_one_by_label(workspace_name) parents = [] if len(splited_local_path) > 2: parent_string = splited_local_path[1:-1] parents = [webdav_convert_file_name_to_bdd(x) for x in parent_string] content_api = ContentApi( config=self.app_config, current_user=self.current_user, session=self.dbsession ) return content_api.get_one_by_filename_and_parent_labels( content_label=webdav_convert_file_name_to_bdd(basename(path)), content_parent_labels=parents, workspace=workspace, )
def _get_content(self, content_path_fetcher): path = content_path_fetcher() content_path = self.reduce_path(path) splited_local_path = content_path.strip('/').split('/') workspace_name = webdav_convert_file_name_to_bdd(splited_local_path[0]) wapi = WorkspaceApi( current_user=self.current_user, session=self.dbsession, config=self.app_config, ) workspace = wapi.get_one_by_label(workspace_name) parents = [] if len(splited_local_path) > 2: parent_string = splited_local_path[1:-1] parents = [ webdav_convert_file_name_to_bdd(x) for x in parent_string ] content_api = ContentApi(config=self.app_config, current_user=self.current_user, session=self.dbsession) return content_api.get_one_by_filename_and_parent_labels( content_label=webdav_convert_file_name_to_bdd(basename(path)), content_parent_labels=parents, workspace=workspace, )
def test_func__create_new_content_with_notification__ok__nominal_case(self): uapi = UserApi(current_user=None, session=self.session, config=self.app_config) current_user = uapi.get_one_by_email("*****@*****.**") # Create new user with notification enabled on w1 workspace wapi = WorkspaceApi(current_user=current_user, session=self.session, config=self.app_config) workspace = wapi.get_one_by_label("Recipes") user = uapi.get_one_by_email("*****@*****.**") wapi.enable_notifications(user, workspace) api = ContentApi(current_user=user, session=self.session, config=self.app_config) item = api.create( content_type_list.Folder.slug, workspace, None, "parent", do_save=True, do_notify=False ) api.create( content_type_list.File.slug, workspace, item, "file1", do_save=True, do_notify=True ) # Send mail async from redis queue with daemon daemon = MailSenderDaemon(self.app_config, burst=True) daemon.run() # check mail received response = self.get_mailhog_mails() headers = response[0]["Content"]["Headers"] assert headers["From"][0] == '"Bob i. via Tracim" <test_user_from+3@localhost>' assert headers["To"][0] == "Global manager <*****@*****.**>" assert headers["Subject"][0] == "[TRACIM] [Recipes] file1 (Open)" assert headers["References"][0] == "test_user_refs+22@localhost" assert ( headers["Reply-to"][0] == '"Bob i. & all members of Recipes" <test_user_reply+22@localhost>' )
def test_func__create_new_content_with_notification__ok__nominal_case( self): uapi = UserApi( current_user=None, session=self.session, config=self.app_config, ) current_user = uapi.get_one_by_email('*****@*****.**') # Create new user with notification enabled on w1 workspace wapi = WorkspaceApi( current_user=current_user, session=self.session, config=self.app_config, ) workspace = wapi.get_one_by_label('Recipes') user = uapi.get_one_by_email('*****@*****.**') wapi.enable_notifications(user, workspace) api = ContentApi( current_user=user, session=self.session, config=self.app_config, ) item = api.create( content_type_list.Folder.slug, workspace, None, 'parent', do_save=True, do_notify=False, ) item2 = api.create( content_type_list.File.slug, workspace, item, 'file1', do_save=True, do_notify=True, ) # Send mail async from redis queue redis = get_redis_connection(self.app_config) queue = get_rq_queue( redis, 'mail_sender', ) worker = SimpleWorker([queue], connection=queue.connection) worker.work(burst=True) # check mail received response = requests.get('http://127.0.0.1:8025/api/v1/messages') response = response.json() headers = response[0]['Content']['Headers'] assert headers['From'][ 0] == '"Bob i. via Tracim" <test_user_from+3@localhost>' # nopep8 assert headers['To'][0] == 'Global manager <*****@*****.**>' assert headers['Subject'][0] == '[TRACIM] [Recipes] file1 (Open)' assert headers['References'][0] == 'test_user_refs+22@localhost' assert headers['Reply-to'][ 0] == '"Bob i. & all members of Recipes" <test_user_reply+22@localhost>' # nopep8
def _get_workspace(self, workspace_id_fetcher): workspace_id = workspace_id_fetcher() wapi = WorkspaceApi( current_user=self.current_user, session=self.dbsession, config=self.app_config, show_deleted=True, ) return wapi.get_one_by_label(workspace_id)
def test_func__create_comment_with_notification__ok__nominal_case(self): uapi = UserApi( current_user=None, session=self.session, config=self.app_config, ) current_user = uapi.get_one_by_email('*****@*****.**') # set admin as french, useful to verify if i18n work properly current_user.lang = 'fr' # Create new user with notification enabled on w1 workspace wapi = WorkspaceApi( current_user=current_user, session=self.session, config=self.app_config, ) workspace = wapi.get_one_by_label('Recipes') user = uapi.get_one_by_email('*****@*****.**') wapi.enable_notifications(user, workspace) api = ContentApi( current_user=user, session=self.session, config=self.app_config, ) item = api.create( content_type_list.Folder.slug, workspace, None, 'parent', do_save=True, do_notify=False, ) item2 = api.create( content_type_list.File.slug, workspace, item, 'file1', do_save=True, do_notify=False, ) api.create_comment(parent=item2, content='My super comment', do_save=True, do_notify=True) transaction.commit() # check mail received response = requests.get('http://127.0.0.1:8025/api/v1/messages') response = response.json() headers = response[0]['Content']['Headers'] assert headers['From'][ 0] == '"Bob i. via Tracim" <test_user_from+3@localhost>' # nopep8 assert headers['To'][0] == 'Global manager <*****@*****.**>' assert headers['Subject'][0] == '[TRACIM] [Recipes] file1 (Open)' assert headers['References'][0] == 'test_user_refs+22@localhost' assert headers['Reply-to'][ 0] == '"Bob i. & all members of Recipes" <test_user_reply+22@localhost>' # nopep8
def test_func__create_new_content_with_notification__ok__nominal_case(self): uapi = UserApi( current_user=None, session=self.session, config=self.app_config, ) current_user = uapi.get_one_by_email('*****@*****.**') # Create new user with notification enabled on w1 workspace wapi = WorkspaceApi( current_user=current_user, session=self.session, config=self.app_config, ) workspace = wapi.get_one_by_label('Recipes') user = uapi.get_one_by_email('*****@*****.**') wapi.enable_notifications(user, workspace) api = ContentApi( current_user=user, session=self.session, config=self.app_config, ) item = api.create( content_type_list.Folder.slug, workspace, None, 'parent', do_save=True, do_notify=False, ) item2 = api.create( content_type_list.File.slug, workspace, item, 'file1', do_save=True, do_notify=True, ) # Send mail async from redis queue redis = get_redis_connection( self.app_config ) queue = get_rq_queue( redis, 'mail_sender', ) worker = SimpleWorker([queue], connection=queue.connection) worker.work(burst=True) # check mail received response = self.get_mailhog_mails() headers = response[0]['Content']['Headers'] assert headers['From'][0] == '"Bob i. via Tracim" <test_user_from+3@localhost>' # nopep8 assert headers['To'][0] == 'Global manager <*****@*****.**>' assert headers['Subject'][0] == '[TRACIM] [Recipes] file1 (Open)' assert headers['References'][0] == 'test_user_refs+22@localhost' assert headers['Reply-to'][0] == '"Bob i. & all members of Recipes" <test_user_reply+22@localhost>' # nopep8
def test_func__create_new_content_with_notification__ok__nominal_case( self): uapi = UserApi( current_user=None, session=self.session, config=self.app_config, ) current_user = uapi.get_one_by_email('*****@*****.**') # Create new user with notification enabled on w1 workspace wapi = WorkspaceApi( current_user=current_user, session=self.session, config=self.app_config, ) workspace = wapi.get_one_by_label('Recipes') user = uapi.get_one_by_email('*****@*****.**') wapi.enable_notifications(user, workspace) api = ContentApi( current_user=user, session=self.session, config=self.app_config, ) item = api.create( content_type_list.Folder.slug, workspace, None, 'parent', do_save=True, do_notify=False, ) item2 = api.create( content_type_list.File.slug, workspace, item, 'file1', do_save=True, do_notify=True, ) # Send mail async from redis queue with daemon daemon = MailSenderDaemon(self.app_config, burst=True) daemon.run() # check mail received response = self.get_mailhog_mails() headers = response[0]['Content']['Headers'] assert headers['From'][ 0] == '"Bob i. via Tracim" <test_user_from+3@localhost>' # nopep8 assert headers['To'][0] == 'Global manager <*****@*****.**>' assert headers['Subject'][0] == '[TRACIM] [Recipes] file1 (Open)' assert headers['References'][0] == 'test_user_refs+22@localhost' assert headers['Reply-to'][ 0] == '"Bob i. & all members of Recipes" <test_user_reply+22@localhost>' # nopep8
def test_func__create_comment_with_notification__ok__nominal_case(self): uapi = UserApi( current_user=None, session=self.session, config=self.app_config, ) current_user = uapi.get_one_by_email('*****@*****.**') # set admin as french, useful to verify if i18n work properly current_user.lang = 'fr' # Create new user with notification enabled on w1 workspace wapi = WorkspaceApi( current_user=current_user, session=self.session, config=self.app_config, ) workspace = wapi.get_one_by_label('Recipes') user = uapi.get_one_by_email('*****@*****.**') wapi.enable_notifications(user, workspace) api = ContentApi( current_user=user, session=self.session, config=self.app_config, ) item = api.create( content_type_list.Folder.slug, workspace, None, 'parent', do_save=True, do_notify=False, ) item2 = api.create( content_type_list.File.slug, workspace, item, 'file1', do_save=True, do_notify=False, ) api.create_comment(parent=item2, content='My super comment', do_save=True, do_notify=True) transaction.commit() # check mail received response = self.get_mailhog_mails() headers = response[0]['Content']['Headers'] assert headers['From'][0] == '"Bob i. via Tracim" <test_user_from+3@localhost>' # nopep8 assert headers['To'][0] == 'Global manager <*****@*****.**>' assert headers['Subject'][0] == '[TRACIM] [Recipes] file1 (Open)' assert headers['References'][0] == 'test_user_refs+22@localhost' assert headers['Reply-to'][0] == '"Bob i. & all members of Recipes" <test_user_reply+22@localhost>' # nopep8
class RootResource(DAVCollection): """ RootResource ressource that represents tracim's home, which contains all workspaces """ def __init__(self, path: str, environ: dict, tracim_context: "WebdavTracimContext"): super(RootResource, self).__init__(path, environ) self.tracim_context = tracim_context self.user = tracim_context.current_user self.session = tracim_context.dbsession # TODO BS 20170221: Web interface should list all workspace to. We # disable it here for moment. When web interface will be updated to # list all workspace, change this here to. self.workspace_api = WorkspaceApi( current_user=self.user, session=self.session, force_role=True, config=tracim_context.app_config, ) def __repr__(self) -> str: return "<DAVCollection: RootResource>" @webdav_check_right(is_user) def getMemberNames(self) -> [str]: """ This method returns the names (here workspace's labels) of all its children Though for perfomance issue, we're not using this function anymore """ members_names = [] for workspace in self.workspace_api.get_all(): if webdav_convert_file_name_to_display( workspace.label) in members_names: label = "{workspace_label}~~{workspace_id}".format( workspace_label=workspace.label, workspace_id=workspace.workspace_id) else: label = workspace.label members_names.append(webdav_convert_file_name_to_display(label)) @webdav_check_right(is_user) def getMember(self, label: str) -> DAVCollection: """ This method returns the child Workspace that corresponds to a given name Though for perfomance issue, we're not using this function anymore """ try: workspace = self.workspace_api.get_one_by_label(label) # fix path workspace_path = "%s%s%s" % ( self.path, "" if self.path == "/" else "/", webdav_convert_file_name_to_display(workspace.label), ) # return item return WorkspaceResource( path=workspace_path, environ=self.environ, workspace=workspace, tracim_context=self.tracim_context, label=workspace.label, ) except AttributeError: return None @webdav_check_right(is_trusted_user) def createEmptyResource(self, name: str): """ This method is called whenever the user wants to create a DAVNonCollection resource (files in our case). There we don't allow to create files at the root; only workspaces (thus collection) can be created. """ raise DAVError(HTTP_FORBIDDEN, contextinfo="Not allowed to create new root") @webdav_check_right(is_trusted_user) def createCollection(self, name: str): """ This method is called whenever the user wants to create a DAVCollection resource as a child (in our case, we create workspaces as this is the root). [For now] we don't allow to create new workspaces through webdav client. Though if we come to allow it, deleting the error's raise will make it possible. """ # TODO : remove comment here # raise DAVError(HTTP_FORBIDDEN) workspace_name = webdav_convert_file_name_to_bdd(name) try: new_workspace = self.workspace_api.create_workspace(workspace_name) except (UserNotAllowedToCreateMoreWorkspace, EmptyLabelNotAllowed) as exc: raise DAVError(HTTP_FORBIDDEN, contextinfo=str(exc)) self.workspace_api.save(new_workspace) self.workspace_api.execute_created_workspace_actions(new_workspace) transaction.commit() # fix path workspace_path = "%s%s%s" % ( self.path, "" if self.path == "/" else "/", webdav_convert_file_name_to_display(new_workspace.label), ) # create item return WorkspaceResource( path=workspace_path, environ=self.environ, workspace=new_workspace, tracim_context=self.tracim_context, label=new_workspace.label, ) @webdav_check_right(is_user) def getMemberList(self): """ This method is called by wsgidav when requesting with a depth > 0, it will return a list of _DAVResource of all its direct children """ members = [] members_names = [] for workspace in self.workspace_api.get_all(): if webdav_convert_file_name_to_display( workspace.label) in members_names: label = "{workspace_label}~~{workspace_id}".format( workspace_label=workspace.label, workspace_id=workspace.workspace_id) else: label = workspace.label # fix path workspace_label = webdav_convert_file_name_to_display(label) path = add_trailing_slash(self.path) # return item workspace_path = "{}{}".format(path, workspace_label) members.append( WorkspaceResource( path=workspace_path, environ=self.environ, workspace=workspace, tracim_context=self.tracim_context, label=label, )) members_names.append(workspace_label) return members
class RootResource(DAVCollection): """ RootResource ressource that represents tracim's home, which contains all workspaces """ def __init__(self, path: str, environ: dict, tracim_context: 'WebdavTracimContext'): super(RootResource, self).__init__(path, environ) self.tracim_context = tracim_context self.user = tracim_context.current_user self.session = tracim_context.dbsession # TODO BS 20170221: Web interface should list all workspace to. We # disable it here for moment. When web interface will be updated to # list all workspace, change this here to. self.workspace_api = WorkspaceApi( current_user=self.user, session=self.session, force_role=True, config=tracim_context.app_config ) def __repr__(self) -> str: return '<DAVCollection: RootResource>' @webdav_check_right(is_user) def getMemberNames(self) -> [str]: """ This method returns the names (here workspace's labels) of all its children Though for perfomance issue, we're not using this function anymore """ return [ webdav_convert_file_name_to_display(workspace.label) for workspace in self.workspace_api.get_all() ] @webdav_check_right(is_user) def getMember(self, label: str) -> DAVCollection: """ This method returns the child Workspace that corresponds to a given name Though for perfomance issue, we're not using this function anymore """ try: workspace = self.workspace_api.get_one_by_label(label) # fix path workspace_path = '%s%s%s' % (self.path, '' if self.path == '/' else '/', webdav_convert_file_name_to_display(workspace.label)) # return item return WorkspaceResource( workspace_path, self.environ, workspace, tracim_context=self.tracim_context ) except AttributeError: return None @webdav_check_right(is_trusted_user) def createEmptyResource(self, name: str): """ This method is called whenever the user wants to create a DAVNonCollection resource (files in our case). There we don't allow to create files at the root; only workspaces (thus collection) can be created. """ raise DAVError(HTTP_FORBIDDEN) @webdav_check_right(is_trusted_user) def createCollection(self, name: str): """ This method is called whenever the user wants to create a DAVCollection resource as a child (in our case, we create workspaces as this is the root). [For now] we don't allow to create new workspaces through webdav client. Though if we come to allow it, deleting the error's raise will make it possible. """ # TODO : remove comment here # raise DAVError(HTTP_FORBIDDEN) workspace_name = webdav_convert_file_name_to_bdd(name) new_workspace = self.workspace_api.create_workspace(workspace_name) self.workspace_api.save(new_workspace) transaction.commit() # fix path workspace_path = '%s%s%s' % ( self.path, '' if self.path == '/' else '/', webdav_convert_file_name_to_display(new_workspace.label) ) # create item return WorkspaceResource( workspace_path, self.environ, new_workspace, tracim_context=self.tracim_context ) @webdav_check_right(is_user) def getMemberList(self): """ This method is called by wsgidav when requesting with a depth > 0, it will return a list of _DAVResource of all its direct children """ members = [] for workspace in self.workspace_api.get_all(): # fix path workspace_label = webdav_convert_file_name_to_display(workspace.label) path = add_trailing_slash(self.path) # return item workspace_path = '{}{}'.format(path, workspace_label) members.append( WorkspaceResource( path=workspace_path, environ=self.environ, workspace=workspace, tracim_context=self.tracim_context ) ) return members
def get_workspace_from_path(self, path: str, api: WorkspaceApi) -> Workspace: try: return api.get_one_by_label(transform_to_bdd(path.split('/')[1])) except NoResultFound: return None
class RootResource(DAVCollection): """ RootResource ressource that represents tracim's home, which contains all workspaces """ def __init__(self, path: str, environ: dict, tracim_context: 'WebdavTracimContext'): super(RootResource, self).__init__(path, environ) self.tracim_context = tracim_context self.user = tracim_context.current_user self.session = tracim_context.dbsession # TODO BS 20170221: Web interface should list all workspace to. We # disable it here for moment. When web interface will be updated to # list all workspace, change this here to. self.workspace_api = WorkspaceApi(current_user=self.user, session=self.session, force_role=True, config=tracim_context.app_config) def __repr__(self) -> str: return '<DAVCollection: RootResource>' @webdav_check_right(is_user) def getMemberNames(self) -> [str]: """ This method returns the names (here workspace's labels) of all its children Though for perfomance issue, we're not using this function anymore """ return [workspace.label for workspace in self.workspace_api.get_all()] @webdav_check_right(is_user) def getMember(self, label: str) -> DAVCollection: """ This method returns the child Workspace that corresponds to a given name Though for perfomance issue, we're not using this function anymore """ try: workspace = self.workspace_api.get_one_by_label(label) workspace_path = '%s%s%s' % ( self.path, '' if self.path == '/' else '/', webdav_convert_file_name_to_display(workspace.label)) return WorkspaceResource(workspace_path, self.environ, workspace, tracim_context=self.tracim_context) except AttributeError: return None @webdav_check_right(is_trusted_user) def createEmptyResource(self, name: str): """ This method is called whenever the user wants to create a DAVNonCollection resource (files in our case). There we don't allow to create files at the root; only workspaces (thus collection) can be created. """ raise DAVError(HTTP_FORBIDDEN) @webdav_check_right(is_trusted_user) def createCollection(self, name: str): """ This method is called whenever the user wants to create a DAVCollection resource as a child (in our case, we create workspaces as this is the root). [For now] we don't allow to create new workspaces through webdav client. Though if we come to allow it, deleting the error's raise will make it possible. """ # TODO : remove comment here # raise DAVError(HTTP_FORBIDDEN) new_workspace = self.workspace_api.create_workspace(name) self.workspace_api.save(new_workspace) workspace_path = '%s%s%s' % ( self.path, '' if self.path == '/' else '/', webdav_convert_file_name_to_display(new_workspace.label)) transaction.commit() return WorkspaceResource(workspace_path, self.environ, new_workspace, tracim_context=self.tracim_context) @webdav_check_right(is_user) def getMemberList(self): """ This method is called by wsgidav when requesting with a depth > 0, it will return a list of _DAVResource of all its direct children """ members = [] for workspace in self.workspace_api.get_all(): workspace_path = '%s%s%s' % (self.path, '' if self.path == '/' else '/', workspace.label) members.append( WorkspaceResource(path=workspace_path, environ=self.environ, workspace=workspace, tracim_context=self.tracim_context)) return members