Skip to content

Commit

Permalink
Verify body, title length for posts
Browse files Browse the repository at this point in the history
  • Loading branch information
yattias committed Jun 18, 2024
1 parent 60b7244 commit 23605ae
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 131 deletions.
207 changes: 81 additions & 126 deletions src/researchhub_document/tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def setUp(self):
object_id=self.organization.id,
user=self.member_user,
)

self.hub = create_hub("hub")

def test_author_can_delete_doc(self):
Expand All @@ -51,8 +51,8 @@ def test_author_can_delete_doc(self):
"created_by": author.id,
"full_src": "body",
"is_public": True,
"renderable_text": "body",
"title": "title",
"renderable_text": "sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body",
"title": "sufficiently long title. sufficiently long title.",
"hubs": [hub.id],
},
)
Expand Down Expand Up @@ -80,8 +80,8 @@ def test_author_can_restore_doc(self):
"created_by": author.id,
"full_src": "body",
"is_public": True,
"renderable_text": "body",
"title": "title",
"renderable_text": "sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body",
"title": "sufficiently long title. sufficiently long title.",
"hubs": [hub.id],
},
)
Expand Down Expand Up @@ -110,8 +110,8 @@ def test_moderator_can_restore_doc(self):
"created_by": author.id,
"full_src": "body",
"is_public": True,
"renderable_text": "body",
"title": "title",
"renderable_text": "sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body",
"title": "sufficiently long title. sufficiently long title.",
"hubs": [hub.id],
},
)
Expand Down Expand Up @@ -141,8 +141,8 @@ def test_non_author_cannot_delete_doc(self):
"created_by": author.id,
"full_src": "body",
"is_public": True,
"renderable_text": "body",
"title": "title",
"renderable_text": "sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body",
"title": "sufficiently long title. sufficiently long title.",
"hubs": [hub.id],
},
)
Expand Down Expand Up @@ -173,8 +173,8 @@ def test_moderator_can_delete_doc(self):
"created_by": author.id,
"full_src": "body",
"is_public": True,
"renderable_text": "body",
"title": "title",
"renderable_text": "sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body",
"title": "sufficiently long title. sufficiently long title.",
"hubs": [hub.id],
},
)
Expand Down Expand Up @@ -204,14 +204,56 @@ def test_author_can_create_post(self):
"created_by": author.id,
"full_src": "body",
"is_public": True,
"renderable_text": "body",
"title": "title",
"renderable_text": "sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body",
"title": "sufficiently long title. sufficiently long title.",
"hubs": [hub.id],
},
)

self.assertEqual(doc_response.status_code, 200)

def test_min_post_title_length(self):
author = create_random_default_user("author")
hub = create_hub()

self.client.force_authenticate(author)

doc_response = self.client.post(
"/api/researchhubpost/",
{
"document_type": "DISCUSSION",
"created_by": author.id,
"full_src": "body",
"is_public": True,
"renderable_text": "body",
"title": "short title",
"hubs": [hub.id],
},
)

self.assertEqual(doc_response.status_code, 400)

def test_min_post_body_length(self):
author = create_random_default_user("author")
hub = create_hub()

self.client.force_authenticate(author)

doc_response = self.client.post(
"/api/researchhubpost/",
{
"document_type": "DISCUSSION",
"created_by": author.id,
"full_src": "body",
"is_public": True,
"renderable_text": "short body",
"title": "long title long title long title",
"hubs": [hub.id],
},
)

self.assertEqual(doc_response.status_code, 400)

def test_user_can_create_post_with_multiple_authors(self):
note = create_note(self.admin_user, self.organization)

Expand All @@ -227,8 +269,8 @@ def test_user_can_create_post_with_multiple_authors(self):
"hubs": [self.hub.id],
"is_public": True,
"note_id": note[0].id,
"renderable_text": "body",
"title": "title",
"renderable_text": "sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body",
"title": "sufficiently long title. sufficiently long title.",
},
)

Expand All @@ -249,8 +291,8 @@ def test_user_cannot_create_post_with_non_members(self):
"hubs": [self.hub.id],
"is_public": True,
"note_id": note[0].id,
"renderable_text": "body",
"title": "title",
"renderable_text": "sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body",
"title": "sufficiently long title. sufficiently long title.",
},
)

Expand All @@ -269,8 +311,8 @@ def test_author_can_update_post(self):
"full_src": "body",
"is_public": True,
"note_id": note[0].id,
"renderable_text": "body",
"title": "title",
"renderable_text": "sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body",
"title": "sufficiently long title. sufficiently long title.",
"hubs": [self.hub.id],
},
)
Expand All @@ -281,17 +323,20 @@ def test_author_can_update_post(self):
"/api/researchhubpost/",
{
"post_id": doc_response.data["id"],
"title": "updated title",
"document_type": "DISCUSSION",
"created_by": self.admin_user.id,
"full_src": "body",
"is_public": True,
"renderable_text": "body",
"title": "updated title. updated title. updated title.",
"renderable_text": "sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body",
"hubs": [self.hub.id],
},
)

self.assertEqual(updated_response.data["title"], "updated title")
self.assertEqual(
updated_response.data["title"],
"updated title. updated title. updated title.",
)

def test_author_cannot_update_post_with_non_members(self):
note = create_note(self.admin_user, self.organization)
Expand All @@ -306,8 +351,8 @@ def test_author_cannot_update_post_with_non_members(self):
"full_src": "body",
"is_public": True,
"note_id": note[0].id,
"renderable_text": "body",
"title": "title",
"renderable_text": "sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body",
"title": "sufficiently long title. sufficiently long title.",
"hubs": [self.hub.id],
},
)
Expand All @@ -319,12 +364,12 @@ def test_author_cannot_update_post_with_non_members(self):
{
"authors": [self.admin_author.id, self.non_member_author.id],
"post_id": doc_response.data["id"],
"title": "updated title",
"document_type": "DISCUSSION",
"created_by": self.admin_user.id,
"full_src": "body",
"is_public": True,
"renderable_text": "body",
"renderable_text": "sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body",
"title": "sufficiently long title. sufficiently long title.",
"hubs": [self.hub.id],
},
)
Expand All @@ -347,8 +392,8 @@ def test_non_author_cannot_update_post(self):
"created_by": author.id,
"full_src": "body",
"is_public": True,
"renderable_text": "body",
"title": "title",
"renderable_text": "sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body",
"title": "sufficiently long title. sufficiently long title.",
"hubs": [hub.id],
"note_id": note[0].id,
},
Expand All @@ -361,106 +406,16 @@ def test_non_author_cannot_update_post(self):
"/api/researchhubpost/",
{
"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_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_author_can_update_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": "title",
"hubs": [hub.id],
},
)
self.assertEqual(doc_response.status_code, 200)

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, 200)
self.assertEqual(updated_response.data["full_markdown"], "updated body")

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",
"renderable_text": "sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body",
"title": "sufficiently long title. sufficiently long title.",
"hubs": [hub.id],
},
)
self.assertEqual(doc_response.status_code, 200)

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)
self.assertEqual(updated_response.status_code, 403)

def test_hub_editors_can_censor_papers(self):
Expand Down Expand Up @@ -529,8 +484,8 @@ def test_register_doi_with_sufficient_funds(self):
"created_by": author.id,
"full_src": "body",
"is_public": True,
"renderable_text": "body",
"title": "title",
"renderable_text": "sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body",
"title": "sufficiently long title. sufficiently long title.",
"hubs": [hub.id],
},
)
Expand All @@ -557,8 +512,8 @@ def test_register_doi_with_insufficient_funds(self):
"created_by": author.id,
"full_src": "body",
"is_public": True,
"renderable_text": "body",
"title": "title",
"renderable_text": "sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body",
"title": "sufficiently long title. sufficiently long title.",
"hubs": [hub.id],
},
)
Expand All @@ -585,8 +540,8 @@ def test_no_doi_with_sufficient_funds(self):
"created_by": author.id,
"full_src": "body",
"is_public": True,
"renderable_text": "body",
"title": "title",
"renderable_text": "sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body. sufficiently long body",
"title": "sufficiently long title. sufficiently long title.",
"hubs": [hub.id],
},
)
Expand Down

0 comments on commit 23605ae

Please sign in to comment.