def test_news_comments_counter_must_be_decremented(self): news = self.blend_news() news_comment = NewsCommentPayload(news, comment="Some comment")\ .post(user=self.admin) self.assertEqual(news.comments_count, 1) NewsCommentPayload().delete(news_comment.id, user=self.admin) self.assertEqual(news.comments_count, 0)
def test_author_can_edit_his_comments(self, username, expected): user = getattr(self, username) if username else None news_comment = self.blend_news_comment(author=self.user_1) response = NewsCommentPayload(comment="New text comment")\ .patch(news_comment.id, user=user, code=expected) if expected == 200: response.assertHasComment("New text comment")
def test_field_author_cannot_be_edited_by_users(self): news_comment = self.blend_news_comment(author=self.user_1) NewsCommentPayload()\ .set_author(self.user_2)\ .patch(news_comment.id, user=self.user_1, code=403)\ .assertRaiseJsonApiError('/data/relationships/author/data')
def test_field_author_can_be_edited_by_admin(self): news_comment = self.blend_news_comment(author=self.user_1) NewsCommentPayload()\ .set_author(self.user_2)\ .patch(news_comment.id, user=self.admin)\ .assertHasRelationshipAuthorData(self.user_2)
def test_news_comments_counter_must_be_incremented(self): news = self.blend_news() self.assertEqual(news.comments_count, 0) NewsCommentPayload(news, comment="Some comment")\ .post(user=self.user_1) self.assertEqual(news.comments_count, 1)
def test_news_comment_via_news(self): news_comment = self.blend_news_comment() response = NewsCommentPayload(_url_collection="/v1/news/{}/comments" .format(news_comment.news.id))\ .get_collection(user=self.user_2)\ .assertCount(1) response.data[0].assertHasPublicAttributes(news_comment)
def test_field_creation_date_time_cannot_be_changed(self): news_comment = self.blend_news_comment() payload = NewsCommentPayload()\ ._set_attribute('created_on_datetime', "2018-09-27T23:14:19")\ .patch(news_comment.id, user=self.admin) self.assertEqual(payload.created_on_datetime, news_comment.created_on_datetime)
def test_news_comment_details_can_be_accessed_as(self, username, expected): user = getattr(self, username) if username else None news_comment = self.blend_news_comment() NewsCommentPayload().get(news_comment.id, user=user, code=expected, args={ 'include': 'news' }).assertHasPublicAttributes(news_comment)
def test_user_did_not_opted_in_to_subscribe_to_the_news(self): news = self.blend_news() NewsCommentPayload(news, comment="Some comment")\ .set_subscribe(False)\ .post(user=self.user_1)\ .assertAttributeNotPresent('subscribe') news_subscription = db.session \ .query(NewsSubscription) \ .filter(NewsSubscription.user_id == self.user_1.id, NewsSubscription.news_id == news.id) self.assertEqual(news_subscription.count(), 0)
def test_news_comment_collection_can_be_accessed_as(self, username, expected): news_comments = self.blend_news_comment(user=self.user_1, count=3) user = getattr(self, username) if username else None response = NewsCommentPayload()\ .get_collection(user=user, code=expected)\ .assertCount(3) response.data[0].assertHasPublicAttributes(news_comments[0]) response.data[1].assertHasPublicAttributes(news_comments[1]) response.data[2].assertHasPublicAttributes(news_comments[2])
def test_user_opted_in_to_subscribe_to_the_news(self): news_comment = self.blend_news_comment(author=self.user_1, subscribe=False) news_subscription = db.session \ .query(NewsSubscription) \ .filter(NewsSubscription.user_id == self.user_1.id, NewsSubscription.news_id == news_comment.news.id) self.assertEqual(news_subscription.count(), 0) NewsCommentPayload()\ .set_subscribe(True)\ .patch(news_comment.id, user=self.user_1) self.assertEqual(news_subscription.count(), 1) payload = NewsCommentPayload().set_subscribe(False) payload.patch(news_comment.id, user=self.user_1) self.assertEqual(news_subscription.count(), 0) # delete again payload.patch(news_comment.id, user=self.user_1) self.assertEqual(news_subscription.count(), 0)
def test_news_subscription_must_be_cleaned(self): news = self.blend_news() news_comment_payload = NewsCommentPayload(news, comment="Some comment")\ .set_subscribe(True) news_comment = news_comment_payload.post(user=self.admin) news_comment_payload.post(user=self.user_1) self.assertEqual(news.comments_count, 2) news_subscription = db.session \ .query(NewsSubscription) \ .filter( NewsSubscription.user_id == self.admin.id, NewsSubscription.news_id == news.id ) self.assertEqual(news_subscription.count(), 1) self.assertTrue(news_subscription.first().subscribed) news_comment_payload.delete(news_comment.id, user=self.admin) self.assertEqual(news.comments_count, 1) self.assertEqual(news_subscription.count(), 0)
def test_can_be_posted_as_anonymous(self): NewsCommentPayload().post(user=None, code=401)
def test_field_comment_must_accept_html_subset(self, comment, expected): news = self.blend_news() NewsCommentPayload(news)\ .set_comment(comment)\ .post(user=self.admin)\ .assertHasComment(expected)
def test_field_comment_cannot_be_blank(self, comment): news = self.blend_news() NewsCommentPayload(news)\ .set_comment(comment)\ .post(user=self.admin, code=422)\ .assertRaiseJsonApiError('/data/attributes/comment')
def test_field_comment_must_be_present(self): news = self.blend_news() NewsCommentPayload(news)\ .post(user=self.admin, code=422)\ .assertRaiseJsonApiError('/data/attributes/comment')
def test_field_creation_datetime_set_automatically(self): news = self.blend_news() NewsCommentPayload(news, comment="Some comment")\ .set_author(self.user_1)\ .post(user=self.admin)\ .assertCreationDateTime()
def test_field_author_can_be_overrided_by_admin(self): news = self.blend_news() NewsCommentPayload(news, comment="Some comment")\ .set_author(self.user_2)\ .post(user=self.admin)\ .assertHasRelationshipAuthorData(self.user_2)
def test_field_author_must_be_current_user(self): news = self.blend_news() NewsCommentPayload(news, comment="Some comment")\ .set_author(self.user_2)\ .post(user=self.user_1, code=403)\ .assertRaiseJsonApiError('/data/relationships/author/data')
def test_field_author_defaults_to_current_user_even_for_admin(self): news = self.blend_news() NewsCommentPayload(news, comment="Some comment")\ .post(user=self.admin)\ .assertHasRelationshipAuthorData(self.admin)
def test_field_comment_cannot_be_blank(self, comment, expected): news_comment = self.blend_news_comment() NewsCommentPayload(comment=comment)\ .patch(news_comment.id, user=self.admin)\ .assertHasComment(expected)
def test_news_comment_via_author_unexistent(self): self.blend_news_comment() NewsCommentPayload(_url_collection="/v1/users/{}/news-comments" .format(666))\ .get_collection(user=self.user_2, code=404)\ .assertRaiseJsonApiError('author_id')
def test_author_relationship(self): news_comment = self.blend_news_comment(author=self.user_1) NewsCommentPayload(_url="/v1/news-comments/{}/relationships/author")\ .get(news_comment.id, user=self.user_2)\ .assertHasData('user', self.user_1.id)
def test_field_comment_must_accept_unicode(self, comment, expected): news_comment = self.blend_news_comment() NewsCommentPayload(comment=comment)\ .patch(news_comment.id, user=self.admin)\ .assertHasComment(expected)
def test_move_relationship(self): news_comment = self.blend_news_comment(author=self.user_1) NewsCommentPayload(_url="/v1/news-comments/{}/relationships/news")\ .get(news_comment.id, user=self.user_2)\ .assertHasData('news', news_comment.news.id)
def test_as(self, username, expected): user = getattr(self, username) if username else None news = self.blend_news() news_comment = NewsCommentPayload(news, comment="Some comment")\ .post(user=self.user_1) NewsCommentPayload().delete(news_comment.id, user=user, code=expected)
def test_authentication_required(self): news_comment = self.blend_news_comment() NewsCommentPayload().patch(news_comment.id, user=None, code=401)
def test_can_be_posted_as(self, username, expected): user = getattr(self, username) if username else None news = self.blend_news() NewsCommentPayload(news, comment="Some comment")\ .post(user=user, code=expected)
def test_admin_can_edit_any_comments(self): news_comment = self.blend_news_comment(author=self.user_1) NewsCommentPayload(comment="New text comment")\ .patch(news_comment.id, user=self.admin)\ .assertHasComment("New text comment")