def test_hub_order_by_score(self): hub = create_hub('High Score Hub') hub2 = create_hub('Low Score Hub') actions = create_actions(10, hub=hub) actions = create_actions(5, hub=hub2) url = self.base_url + '?ordering=-score' response = get_get_response(url) response_data = response.data['results'] h1_first = False h2_second = False for h in response_data: if h['id'] == hub.id: h1_first = True elif h1_first and h['id'] == hub2.id: h2_second = True self.assertTrue(h1_first and h2_second) cache.clear() url = self.base_url + '?ordering=score' response = get_get_response(url) response_data = response.data['results'] h2_first = False h1_second = False for h in response_data: if h['id'] == hub2.id: h2_first = True elif h2_first and h['id'] == hub.id: h1_second = True self.assertTrue(h2_first and h1_second)
def test_authors_field_is_optional(self): hub_1 = create_hub(name='Hub 1') hub_2 = create_hub(name='Hub 2') paper = helpers.create_paper(title='Serialized Paper Title') paper.hubs.add(hub_1) paper.hubs.add(hub_2) paper_dict = model_to_dict(paper) serialized = PaperSerializer(data=paper_dict) self.assertTrue(serialized.is_valid())
def test_non_author_cannot_edit_hypothesis(self): author = create_random_default_user('author') non_author = create_random_default_user('non_author') hub = create_hub() self.client.force_authenticate(author) doc_response = self.client.post( "/api/hypothesis/", { "document_type": "HYPOTHESIS", "created_by": author.id, "full_src": "body", "is_public": True, "renderable_text": "body", "title": "title", "hubs": [hub.id], }) self.client.force_authenticate(non_author) updated_response = self.client.post( f"/api/hypothesis/{doc_response.data['id']}/upsert/", { "hypothesis_id": doc_response.data["id"], "title": "updated title", "document_type": "HYPOTHESIS", "full_src": "updated body", "renderable_text": "body", }) self.assertEqual(updated_response.status_code, 403)
def test_moderator_can_restore_doc(self): author = create_random_default_user('author') mod = create_random_default_user('mod', moderator=True) hub = create_hub() self.client.force_authenticate(author) doc_response = self.client.post( "/api/researchhub_posts/", { "document_type": "DISCUSSION", "created_by": author.id, "full_src": "body", "is_public": True, "renderable_text": "body", "title": "title", "hubs": [hub.id], }) delete_response = self.client.delete( f"/api/researchhub_unified_documents/{doc_response.data['unified_document_id']}/censor/" ) self.client.force_authenticate(mod) restore_response = self.client.patch( f"/api/researchhub_unified_documents/{doc_response.data['unified_document_id']}/restore/" ) self.assertEqual(restore_response.data['is_removed'], False)
def test_non_author_cannot_delete_doc(self): author = create_random_default_user('author') non_author = create_random_default_user('non_author') hub = create_hub() self.client.force_authenticate(author) doc_response = self.client.post( f"/api/researchhub_posts/", { "document_type": "DISCUSSION", "created_by": author.id, "full_src": "body", "is_public": True, "renderable_text": "body", "title": "title", "hubs": [hub.id], }) self.client.force_authenticate(non_author) response = self.client.delete( f"/api/researchhub_unified_documents/{doc_response.data['unified_document_id']}/censor/" ) doc = ResearchhubUnifiedDocument.objects.get( id=doc_response.data['unified_document_id']) self.assertEqual(doc.is_removed, False)
def non_author_cannot_update_post(self): author = create_random_default_user('author') nonauthor = create_random_default_user('nonauthor') hub = create_hub() self.client.force_authenticate(author) doc_response = self.client.post( "/api/researchhub_posts/", { "document_type": "DISCUSSION", "created_by": author.id, "full_src": "body", "is_public": True, "renderable_text": "body", "title": "title", "hubs": [hub.id], }) self.client.force_authenticate(nonauthor) updated_response = self.client.post( "/api/researchhub_posts/", { "post_id": doc_response.data["id"], "title": "updated title", "document_type": "DISCUSSION", "created_by": author.id, "full_src": "body", "is_public": True, "renderable_text": "body", "hubs": [hub.id], }) self.assertEqual(updated_response.status_code, 403)
def test_basic_user_cannot_delete_hub(self): basic_user = create_random_authenticated_user('basic_user') self.client.force_authenticate(basic_user) hub = create_hub(name="some hub") response = self.client.delete(f"/api/hub/{hub.id}/censor/") self.assertTrue(response.status_code, 401)
def test_moderator_can_delete_hub(self): mod = create_random_authenticated_user('mod', moderator=True) self.client.force_authenticate(mod) hub = create_hub(name="some hub") response = self.client.delete(f"/api/hub/{hub.id}/censor/") self.assertTrue(response.status_code, 200)
def test_create_thread_creates_action_with_paper_hubs(self): user = create_random_default_user('nacho') hub = create_hub(name='Nacho Libre') paper = create_paper() paper.hubs.add(hub) create_thread(paper=paper, created_by=user) action = user.actions.all()[0] self.assertIn(hub, action.hubs.all())
def test_invite_to_hub_does_not_email_subscribers(self): subscriber = create_random_default_user('subscriber') subscriber.email = '*****@*****.**' # Must use whitelisted email subscriber.save() hub = create_hub('Invite to Hub No Email') hub.subscribers.add(subscriber) response = self.get_invite_to_hub_response(self.user, hub, [subscriber.email]) self.assertNotContains(response, subscriber.email, status_code=200)
def test_basic_user_cannot_edit_hub(self): basic_user = create_random_authenticated_user('basic_user') self.client.force_authenticate(basic_user) hub = create_hub(name="some hub") response = self.client.put(f"/api/hub/{hub.id}/", { "name": "updated name", "id": hub.id, "description": "description", }) h = Hub.objects.get(id=hub.id) self.assertNotEqual(h.name, "updated name")
def setUp(self): self.author = create_random_default_user('author') self.non_author = create_random_default_user('non_author') self.moderator = create_moderator(first_name='moderator', last_name='moderator') self.hub = create_hub() self.client.force_authenticate(self.author) # Create org response = self.client.post('/api/organization/', {'name': 'test org'}) self.org = response.data # Create Note note_response = self.client.post( '/api/note/', { 'grouping': 'WORKSPACE', 'organization_slug': self.org['slug'], 'title': 'TEST' } ) self.note = note_response.data # Create Note version note_version_response = self.client.post( '/api/note_content/', { 'full_src': 'test content', 'note': self.note['id'], 'plain_text': 'test content' } ) self.note_version = note_version_response.data # Author Publish doc_response = self.client.post("/api/researchhub_posts/", { "document_type": "DISCUSSION", "created_by": self.author.id, "full_src": "body", "renderable_text": "body", "title": "title", "note_id": self.note['id'], "hubs": [self.hub.id], }) self.post = doc_response.data
def author_can_create_hypothesis(self): author = create_random_default_user('author') hub = create_hub() self.client.force_authenticate(author) doc_response = self.client.post( "/api/hypothesis/", { "document_type": "HYPOTHESIS", "created_by": author.id, "full_src": "body", "is_public": True, "renderable_text": "body", "title": "hypothesis", "hubs": [hub.id], }) self.assertEqual(doc_response.status_code, 200)
def test_hub_actions_is_paginated(self): hub = create_hub(name='Calpurnia') paper = create_paper() hub.papers.add(paper) for x in range(11): create_thread(paper=paper, created_by=self.user) page = 1 url = self.base_url + f'{hub.id}/latest_actions/?page={page}' response = get_get_response(url) self.assertContains(response, 'count":11', status_code=200) result_count = len(response.data['results']) self.assertEqual(result_count, 10) page = 2 url = self.base_url + f'{hub.id}/latest_actions/?page={page}' response = get_get_response(url) self.assertContains(response, 'count":11', status_code=200) result_count = len(response.data['results']) self.assertEqual(result_count, 1)
def test_hub_editors_can_censor_papers(self): hub = create_hub() user_editor = create_random_default_user('user_editor') Permission.objects.create( access_type=EDITOR, content_type=ContentType.objects.get_for_model(Hub), object_id=hub.id, user=user_editor, ) user_uploader = create_random_default_user('user_uploader') test_paper = create_paper(uploaded_by=user_uploader) test_paper.hubs.add(hub) test_paper.save() self.client.force_authenticate(user_editor) response = self.client.put(f"/api/paper/{test_paper.id}/censor_paper/", {"id": test_paper.id}) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['is_removed'], True)
def test_no_doi_with_sufficient_funds(self): author = create_random_default_user('author') hub = create_hub() self.client.force_authenticate(author) distributor = Distributor(Distribution('TEST_REWARD', 5, False), author, None, time.time()) distributor.distribute() doc_response = self.client.post( "/api/researchhub_posts/", { "assign_doi": False, "document_type": "DISCUSSION", "created_by": author.id, "full_src": "body", "is_public": True, "renderable_text": "body", "title": "title", "hubs": [hub.id], }) self.assertEqual(doc_response.status_code, 200) self.assertEqual(int(author.get_balance()), 5)
def test_invite_to_hub(self): hub = create_hub('Invite to Hub') email = '*****@*****.**' response = self.get_invite_to_hub_response(self.user, hub, [email]) self.assertContains(response, email, status_code=200)
def setUp(self): self.base_url = '/api/hub/' self.hub = create_hub(name='View Test Hub') self.hub2 = create_hub(name='View Test Hub 2') self.user = create_random_authenticated_user('hub_user')