def test_registration_clones_project_wiki_pages(self, mock_signal, project, user): project = factories.ProjectFactory(creator=user, is_public=True) wiki = NodeWikiFactory(node=project) current_wiki = NodeWikiFactory(node=project, version=2) registration = project.register_node(get_default_metaschema(), Auth(user), '', None) assert registration.wiki_private_uuids == {} registration_wiki_current = NodeWikiPage.load(registration.wiki_pages_current[current_wiki.page_name]) assert registration_wiki_current.node == registration assert registration_wiki_current._id != current_wiki._id registration_wiki_version = NodeWikiPage.load(registration.wiki_pages_versions[wiki.page_name][0]) assert registration_wiki_version.node == registration assert registration_wiki_version._id != wiki._id
def update_wiki_pages(nodes): for i, node in enumerate(nodes): if node['wiki_pages_versions']: cloned_wiki_pages = {} for key, wiki_versions in node['wiki_pages_versions'].items(): cloned_wiki_pages[key] = [] for wiki_id in wiki_versions: node_wiki = NodeWikiPage.load(wiki_id) if not node_wiki: continue if node_wiki.to_storage()['node'] != node['_id']: if not node_wiki.node: move_to_backup_collection(node_wiki) continue clone = node_wiki.clone_wiki(node['_id']) logger.info('Cloned wiki page {} from node {} to {}'.format(wiki_id, node_wiki.node, node['_id'])) cloned_wiki_pages[key].append(clone._id) # update current wiki page if node_wiki.is_current: wiki_pages_current = node['wiki_pages_current'] wiki_pages_current[key] = clone._id db.node.update({'_id': node['_id']}, {'$set': {'wiki_pages_current': wiki_pages_current}}) else: cloned_wiki_pages[key].append(wiki_id) db.node.update({'_id': node['_id']}, {'$set': {'wiki_pages_versions': cloned_wiki_pages}})
def test_forked_project_wiki_pages_created_pre_fork_get_cloned(self): project = self.set_up_project_with_wiki_page() fork = project.fork_node(auth=Auth(self.user)) # reset wiki pages for test fork.wiki_pages_versions = project.wiki_pages_versions fork.wiki_pages_current = project.wiki_pages_current fork.save() main() # update_wiki_pages(self.find_node_record(fork._id)) fork.reload() wiki_versions = fork.wiki_pages_versions[self.wiki.page_name] current_wiki = NodeWikiPage.load(fork.wiki_pages_current[self.current_wiki.page_name]) assert_equal(current_wiki.node, fork) assert_not_equal(current_wiki._id, self.current_wiki._id) wiki_version = NodeWikiPage.load(wiki_versions[0]) assert_equal(wiki_version.node, fork) assert_not_equal(wiki_version._id, self.current_wiki._id)
def test_registration_wiki_pages_created_pre_registration_get_cloned(self): project = self.set_up_project_with_wiki_page() registration = project.register_node(get_default_metaschema(), Auth(self.user), '', None) # reset wiki pages for test registration.wiki_pages_versions = project.wiki_pages_versions registration.wiki_pages_current = project.wiki_pages_current registration.save() main() registration.reload() wiki_versions = registration.wiki_pages_versions[self.wiki.page_name] current_wiki = NodeWikiPage.load(registration.wiki_pages_current[self.current_wiki.page_name]) assert_equal(current_wiki.node, registration) assert_not_equal(current_wiki._id, self.current_wiki._id) wiki_version = NodeWikiPage.load(wiki_versions[0]) assert_equal(wiki_version.node, registration) assert_not_equal(wiki_version._id, self.current_wiki._id)
def test_old_wiki_versions_not_returned(self): self._set_up_public_project_with_wiki_page() # TODO: Remove mocking when StoredFileNode is implemented with mock.patch('osf.models.AbstractNode.update_search'): current_wiki = NodeWikiFactory(node=self.public_project, user=self.user) old_version_id = self.public_project.wiki_pages_versions[current_wiki.page_name][-2] old_version = NodeWikiPage.load(old_version_id) url = '/{}wikis/{}/'.format(API_BASE, old_version._id) res = self.app.get(url, expect_errors=True) assert_equal(res.status_code, 404)
def test_old_wiki_versions_not_returned(self): self._set_up_public_project_with_wiki_page() # TODO: Remove mocking when StoredFileNode is implemented with mock.patch('osf.models.AbstractNode.update_search'): current_wiki = NodeWikiFactory(node=self.public_project, user=self.user) old_version_id = self.public_project.wiki_pages_versions[ current_wiki.page_name][-2] old_version = NodeWikiPage.load(old_version_id) url = '/{}wikis/{}/'.format(API_BASE, old_version._id) res = self.app.get(url, expect_errors=True) assert_equal(res.status_code, 404)
def get_wiki(self, check_permissions=True): pk = self.kwargs[self.wiki_lookup_url_kwarg] wiki = NodeWikiPage.load(pk) if not wiki: raise NotFound if wiki.is_deleted: raise Gone # only show current wiki versions if not wiki.is_current: raise NotFound if check_permissions: # May raise a permission denied self.check_object_permissions(self.request, wiki) return wiki
def test_wiki_pages_with_invalid_nodes_are_removed_after_cloning(self): project = ProjectFactory(creator=self.user, is_public=True) wiki = NodeWikiFactory(node=project) fork = project.fork_node(auth=Auth(self.user)) fork.wiki_pages_versions = project.wiki_pages_versions fork.wiki_pages_current = project.wiki_pages_current fork.save() # Remove original node - wiki.node no longer points to an existing project Node.remove_one(project._id) # clone wiki page main() fork.reload() cloned_wiki_id = fork.wiki_pages_versions[wiki.page_name][0] cloned_wiki = NodeWikiPage.load(cloned_wiki_id) assert_equal(cloned_wiki.node._id, fork._id) # move original wiki page to unmigratedwikipages collection assert_false(db.nodewikipage.find_one({'_id': wiki._id})) assert_true(db.unmigratedwikipages.find_one({'_id': wiki._id}))
def update_wiki_pages(nodes): for i, node in enumerate(nodes): if node['wiki_pages_versions']: cloned_wiki_pages = {} for key, wiki_versions in node['wiki_pages_versions'].items(): cloned_wiki_pages[key] = [] for wiki_id in wiki_versions: node_wiki = NodeWikiPage.load(wiki_id) if not node_wiki: continue if node_wiki.to_storage()['node'] != node['_id']: if not node_wiki.node: move_to_backup_collection(node_wiki) continue clone = node_wiki.clone_wiki(node['_id']) logger.info( 'Cloned wiki page {} from node {} to {}'.format( wiki_id, node_wiki.node, node['_id'])) cloned_wiki_pages[key].append(clone._id) # update current wiki page if node_wiki.is_current: wiki_pages_current = node['wiki_pages_current'] wiki_pages_current[key] = clone._id db.node.update({'_id': node['_id']}, { '$set': { 'wiki_pages_current': wiki_pages_current } }) else: cloned_wiki_pages[key].append(wiki_id) db.node.update( {'_id': node['_id']}, {'$set': { 'wiki_pages_versions': cloned_wiki_pages }})