diff --git a/practice-app/comment/tests.py b/practice-app/comment/tests.py deleted file mode 100644 index 7ce503c2..00000000 --- a/practice-app/comment/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/practice-app/comment/tests/__init__.py b/practice-app/comment/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/practice-app/comment/tests/test_forms.py b/practice-app/comment/tests/test_forms.py new file mode 100644 index 00000000..8bea571d --- /dev/null +++ b/practice-app/comment/tests/test_forms.py @@ -0,0 +1,66 @@ +from django.test import TestCase +from comment.models import Comment +from category.models import Category +from post.models import Post +from django.contrib.auth.models import User +from comment.forms import * + +class TestForms(TestCase): + + def setUp(self): + category = Category.objects.create( + name = 'name', + definition = 'definition' + ) + + user = User.objects.create_user(username='halil',email = "email", password="12345") + user.save() + + post = Post.objects.create( + title = 'title_post_data', + body = 'body', + category = category, + user = user, + country = 'turkey', + covid19cases = { + 'death': 500, + 'case' : 1000, + }, + ) + + comment1 = Comment.objects.create( + body = 'comment1body', + user = user, + post = post, + ) + + comment2 = Comment.objects.create( + body = 'comment2body', + user = user, + post = post, + ) + + def test_get_form_valid_data(self): + getForm = commentGetForm(data={ + 'post_id' : 1 + }) + self.assertTrue(getForm.is_valid()) + + def test_get_form_no_data(self): + getForm = commentGetForm(data={}) + self.assertFalse(getForm.is_valid()) + self.assertEquals(len(getForm.errors), 1) + + def test_post_form_valid_data(self): + postForm = commentPostForm(data={ + 'body' : 'new comment', + 'post_id' : 1, + 'city_name' : 'istanbul' + }) + + self.assertTrue(postForm.is_valid()) + + def test_post_form_no_data(self): + postForm = commentPostForm(data={}) + self.assertFalse(postForm.is_valid()) + self.assertEquals(len(postForm.errors), 3) diff --git a/practice-app/comment/tests/test_models.py b/practice-app/comment/tests/test_models.py new file mode 100644 index 00000000..d76ad2ac --- /dev/null +++ b/practice-app/comment/tests/test_models.py @@ -0,0 +1,39 @@ +from django.test import TestCase +from comment.models import Comment +from category.models import Category +from post.models import Post +from django.contrib.auth.models import User + +class TestModel(TestCase): + def setUp(self): + category = Category.objects.create( + name = 'name', + definition = 'definition' + ) + + user = User.objects.create_user(username='halil',email = "email", password="12345") + user.save() + + post = Post.objects.create( + title = 'title_post_data', + body = 'body', + category = category, + user = user, + country = 'turkey', + covid19cases = { + 'death': 500, + 'case' : 1000, + }, + ) + + comment_data = { + 'body' : 'body_of_the_comment', + 'user' : user, + 'post' : post, + 'city_name' : 'istanbul' + } + + self._comment = Comment.objects.create(**comment_data) + + def test_str(self): + self.assertEquals(self._comment.__str__(), 'body_of_the_comment') diff --git a/practice-app/comment/tests/test_urls.py b/practice-app/comment/tests/test_urls.py new file mode 100644 index 00000000..43dbced2 --- /dev/null +++ b/practice-app/comment/tests/test_urls.py @@ -0,0 +1,23 @@ +from django.test import SimpleTestCase +from django.urls import resolve,reverse +from comment.views import CommentApiView, getComments, postComment, index + +class TestUrls(SimpleTestCase): + + def test_api_url_resolves(self): + url = reverse('commentApi', args=[1]) + self.assertEquals(resolve(url).func.view_class, CommentApiView) + + def test_get_url_resolves(self): + url = reverse('getComments') + self.assertEquals(resolve(url).func, getComments) + + def test_post_url_resolves(self): + url = reverse('postComment') + self.assertEquals(resolve(url).func, postComment) + + def test_index_url_resolves(self): + url = reverse('commentIndex') + self.assertEquals(resolve(url).func, index) + + \ No newline at end of file diff --git a/practice-app/comment/tests/test_views.py b/practice-app/comment/tests/test_views.py new file mode 100644 index 00000000..1213cb0c --- /dev/null +++ b/practice-app/comment/tests/test_views.py @@ -0,0 +1,134 @@ +from django.test import TestCase, Client +from django.urls import reverse +from comment.models import Comment +from category.models import Category +from post.models import Post +from django.contrib.auth.models import User + +class TestViews(TestCase): + def setUp(self): + self.client = Client() + self.api_url = reverse('commentApi', args=[1]) # api// + self.get_url = reverse('getComments') # 'list/' + self.post_url = reverse('postComment') # 'create/' + self.index_url = reverse('commentIndex') # '' + + category = Category.objects.create( + name = 'name', + definition = 'definition' + ) + + user1 = User.objects.create_user(username='halil',email = "email", password="12345") + user2 = User.objects.create_user(username='burak',email = "email", password="12345") + + user1.save() + user2.save() + + post = Post.objects.create( + title = 'title_post_data', + body = 'body', + category = category, + user = user1, + country = 'turkey', + covid19cases = { + 'death': 500, + 'case' : 1000, + }, + ) + + comment_data = { + 'body' : 'body_of_the_1st_comment', + 'user' : user2, + 'post' : post, + 'city_name' : 'istanbul' + } + + self.create_comment_data = { + 'body': 'comment create body', + 'post' : post, + 'city_name' : 'istanbul' + } + + self.comment_with_wrong_post = { + 'body': 'comment create body', + 'post' : post, + 'city_name' : 'istanbul' + } + + self._comment = Comment.objects.create(**comment_data) + + # API tests: + def test_api_GET(self): + # Performing a proper GET: + response = self.client.get(self.api_url, {'post_id':1}) + self.assertEquals(response.status_code, 200) + self.assertEquals(response.data[0]['body'],'body_of_the_1st_comment') + + def test_api_POST_create_comment(self): + # Performing a proper POST to create a new comment: + self.client.login(username='burak', password='12345') + response = self.client.post(self.api_url, self.create_comment_data) + self.assertEquals(response.status_code, 201) + self.assertEquals(response.data['body'],'comment create body') + + def test_api_POST_not_authenticated(self): + # Non authenticated users cannot perform POST: + response = self.client.post(self.api_url, self.create_comment_data) + self.assertEquals(response.status_code, 403) + + def test_api_POST_missing_input(self): + # There should be input: + self.client.login(username='burak', password='12345') + response = self.client.post(self.api_url) + self.assertEquals(response.status_code, 400) + self.assertEquals(response.data['error'],'Missing input') + + def test_api_POST_no_post(self): + # There cannot be a comment for a nonexisting post: + self.client.login(username='burak', password='12345') + response = self.client.post(reverse('commentApi', args=[2]), { + 'body' : 'comment for a nonexisting post', + 'city_name' : 'ankara' + }) + self.assertEquals(response.status_code, 404) + self.assertEquals(response.data['error'],'There is no post with id 2') + + def test_api_POST_no_body(self): + # There cannot be a comment without a body: + self.client.login(username='burak', password='12345') + response = self.client.post(self.api_url, { + 'city_name' : 'ankara' + }) + self.assertEquals(response.status_code, 400) + self.assertEquals(response.data['error'],'Missing body of the comment') + + def test_api_POST_empty_body(self): + # There cannot be a comment with an empty body: + self.client.login(username='burak', password='12345') + response = self.client.post(self.api_url, { + 'body' : '', + 'city_name' : 'ankara' + }) + self.assertEquals(response.status_code, 400) + self.assertEquals(response.data['error'],'Body of the comment cannot be empty') + + # Frontend tests: + def test_index_GET(self): + response= self.client.get(self.index_url) + self.assertEquals(response.status_code, 200) + self.assertTemplateUsed(response, 'comment.html') + + def test_get_comments_GET(self): + response = self.client.get(self.get_url, {'post_id' : 1}) + self.assertEquals(response.status_code, 200) + self.assertTemplateUsed(response, 'listComments.html') + + def test_post_coment_POST(self): + self.client.login(username='burak', password='12345') + response = self.client.post(self.post_url, { + 'body' : 'a comment for post', + 'post_id' : 1, + 'city_name' : 'ankara' + }) + self.assertEquals(response.status_code, 200) + self.assertTemplateUsed(response, 'comment.html') diff --git a/practice-app/comment/views.py b/practice-app/comment/views.py index a9c81086..f9fddda0 100644 --- a/practice-app/comment/views.py +++ b/practice-app/comment/views.py @@ -67,10 +67,18 @@ def post(self, request, post_id, *args, **kwargs): return Response( {'error':'Missing input'}, status=status.HTTP_400_BAD_REQUEST) - if request.data['body'] == '': + + try: + request.data['body'] + except: return Response( {'error':'Missing body of the comment'}, status=status.HTTP_400_BAD_REQUEST) + + if request.data['body'] == '': + return Response( + {'error':'Body of the comment cannot be empty'}, + status=status.HTTP_400_BAD_REQUEST) city_name = request.data.get('city_name') url = "https://weatherapi-com.p.rapidapi.com/current.json"