-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(comment edit history): Get edit history
- The user should be able to see the edit history of a comment which is saved when a user changes the comment [Delivers #161966628]
- Loading branch information
1 parent
43eafe7
commit 6af6bba
Showing
10 changed files
with
304 additions
and
95 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
authors/apps/articles/migrations/0006_commentedithistory.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Generated by Django 2.1 on 2018-12-21 07:07 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('articles', '0005_auto_20181219_1816'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='CommentEditHistory', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('body', models.CharField(max_length=1000)), | ||
('updated_at', models.DateTimeField(auto_now_add=True)), | ||
('comment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='articles.Comment')), | ||
], | ||
options={ | ||
'db_table': 'comment_edits', | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
authors/apps/articles/tests/test_comment_edit_history.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import json | ||
|
||
from django.test import TestCase, Client | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase, APIClient | ||
|
||
|
||
class TestCommentHistory(APITestCase): | ||
def setUp(self): | ||
self.client = APIClient() | ||
self.token = self.login().get('token', 'badtoken') | ||
self.COMMENT_URL = reverse("articles:update-delete-comment", kwargs={ | ||
"article_slug": "good-article", | ||
"comment_pk": 1 | ||
}) | ||
self.comment = dict( | ||
comment=dict( | ||
body="I love playing nothing around" | ||
) | ||
) | ||
self.modified_comment = dict( | ||
comment=dict( | ||
body="I love playing guitar around" | ||
) | ||
) | ||
self.create_article() | ||
self.create_comment() | ||
|
||
def test_cannot_save_no_change(self): | ||
url = self.COMMENT_URL.replace("/1/", | ||
"/"+str(self.create_comment().get("comment", | ||
{}).get('id', 1))+"/") | ||
self.COMMENT_URL = url | ||
response = self.client.put(url, | ||
data=json.dumps(self.comment), | ||
content_type="application/json", | ||
HTTP_AUTHORIZATION="Token " + self.token) | ||
url = self.COMMENT_URL.replace("/1/", | ||
"/"+str(self.create_comment().get("comment", | ||
{}).get('id', 1))+"/") | ||
self.COMMENT_URL = url | ||
response3 = self.client.get(url) | ||
self.assertEqual(response3.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response3.data.get("previous_versions", [])), 0) | ||
|
||
def test_can_save_edits(self): | ||
url = self.COMMENT_URL.replace("/1/", | ||
"/"+str(self.create_comment().get("comment", | ||
{}).get('id', 1))+"/") | ||
self.COMMENT_URL = url | ||
response = self.client.put(url, | ||
data=json.dumps(self.modified_comment), | ||
content_type="application/json", | ||
HTTP_AUTHORIZATION="Token " + self.token) | ||
response2 = self.client.put(self.COMMENT_URL, | ||
data=json.dumps(self.comment), | ||
content_type="application/json", | ||
HTTP_AUTHORIZATION="Token " + self.token) | ||
response3 = self.client.get(self.COMMENT_URL) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response2.status_code, status.HTTP_200_OK) | ||
self.assertEqual(2, len(response3.data.get("previous_versions", []))) | ||
|
||
def test_can_get_edits(self): | ||
url = self.COMMENT_URL.replace("/1/", | ||
"/"+str(self.create_comment().get("comment", | ||
{}).get('id', 1))+"/") | ||
self.COMMENT_URL = url | ||
self.update_comment() | ||
response = self.client.get(url) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertNotEqual(response.data.get('previous_versions', ''), '') | ||
|
||
def test_cannot_get_no_edits(self): | ||
url = self.COMMENT_URL.replace("/1/", | ||
"/"+str(self.create_comment().get("comment", | ||
{}).get('id', 1))+"/") | ||
response = self.client.get(url) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data.get('previous_versions', ''), '') | ||
|
||
def create_comment(self): | ||
data = self.client.post(reverse("articles:list-create-comment", kwargs={ | ||
"article_slug": "good-article" | ||
}), | ||
data=json.dumps(self.comment), | ||
content_type="application/json", | ||
HTTP_AUTHORIZATION="Token " + self.token) | ||
return data.data | ||
|
||
def login(self): | ||
self.register() | ||
user_test = dict(user=dict( | ||
email='silaskenn@gmail.com', | ||
username='silaskenn', | ||
password='Password@2019' | ||
)) | ||
logged = self.client.post(reverse("authentication:user-login"), data=json.dumps(user_test), | ||
content_type="application/json") | ||
return logged.data | ||
|
||
def register(self): | ||
user_test = dict(user=dict( | ||
email='silaskenn@gmail.com', | ||
username='silaskenn', | ||
password='Password@2019' | ||
)) | ||
self.client.post(reverse("authentication:user-signup"), data=json.dumps(user_test), | ||
content_type="application/json") | ||
|
||
def create_article(self): | ||
article = dict(article=dict( | ||
title="Good article", | ||
description='This article is too good for authorshaven', | ||
body='Sometimes you just have to write code that only you understands' | ||
)) | ||
self.client.post(reverse("articles:articles"), data=json.dumps(article), | ||
content_type="application/json", HTTP_AUTHORIZATION='Token ' + self.token) | ||
|
||
def update_comment(self): | ||
res = self.client.put(self.COMMENT_URL, | ||
data=json.dumps(self.modified_comment), | ||
content_type="application/json", | ||
HTTP_AUTHORIZATION="Token " + self.token) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.