def test_is_current_deleted_page(self): node = NodeFactory() ver = NodeWikiPage(page_name='foo', node=node) ver.save() # Simulate a deleted page by not adding ver to # node.wiki_pages_current and node.wiki_pages_versions assert ver.is_current is False
def test_is_current_with_single_version(self): node = NodeFactory() page = NodeWikiPage(page_name='foo', node=node) page.save() node.wiki_pages_current['foo'] = page._id node.wiki_pages_versions['foo'] = [page._id] node.save() assert page.is_current is True
def test_wiki_pages_that_do_not_exist_do_not_get_cloned(self): project = ProjectFactory(creator=self.user, is_public=True) wiki = NodeWikiFactory(node=project) NodeWikiPage.remove_one(wiki._id) # deleted wiki record in node.wiki_pages_versions assert_in(wiki._id, project.wiki_pages_versions[wiki.page_name]) main() project.reload() # wiki_id gets removed from node.wiki_pages_versions assert_not_in(wiki._id, project.wiki_pages_versions[wiki.page_name])
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_is_current_with_multiple_versions(self): node = NodeFactory() ver1 = NodeWikiPage(page_name='foo', node=node) ver2 = NodeWikiPage(page_name='foo', node=node) ver1.save() ver2.save() node.wiki_pages_current['foo'] = ver2._id node.wiki_pages_versions['foo'] = [ver1._id, ver2._id] node.save() assert ver1.is_current is False assert ver2.is_current is True
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_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_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_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 }})
def test_page_name_cannot_be_greater_than_100_characters(self): bad_name = 'a' * 101 page = NodeWikiPage(page_name=bad_name) with pytest.raises(ValidationValueError): page.save()
def test_page_name_cannot_be_greater_than_100_characters(self): bad_name = 'a' * 101 page = NodeWikiPage(page_name=bad_name) with pytest.raises(NameMaximumLengthError): page.save()
def move_to_backup_collection(node_wiki_page): db[BACKUP_COLLECTION].insert(node_wiki_page.to_storage()) NodeWikiPage.remove_one(Q('_id', 'eq', node_wiki_page._id))