-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
N - Added test get/delete/update for comment
- Loading branch information
1 parent
9944aa7
commit a971786
Showing
6 changed files
with
329 additions
and
10 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import json | ||
|
||
import pytest | ||
from rest_framework.test import APITransactionTestCase | ||
|
||
from api.models import Product, Comment | ||
from authorize.helpers import generate_jwt_token | ||
from authorize.models import User | ||
|
||
|
||
class TestComment(APITransactionTestCase): | ||
|
||
@classmethod | ||
@pytest.mark.django_db | ||
def setUp(cls): | ||
cls.admin = User.objects.create_superuser( | ||
title='admin', | ||
email='[email protected]', | ||
password='Mypassword', | ||
) | ||
cls.user1 = User.objects.create_user( | ||
title='user1', | ||
email='[email protected]', | ||
password='Mypassword', | ||
) | ||
cls.user2 = User.objects.create_user( | ||
title='user2', | ||
email='[email protected]', | ||
password='Mypassword', | ||
) | ||
|
||
cls.product1 = Product.objects.create( | ||
title='product 1', | ||
description='description 1', | ||
created_by=cls.user1, | ||
) | ||
cls.comment1 = Comment.objects.create( | ||
body='comment1', | ||
is_approved=False, | ||
created_by=cls.user1, | ||
product=cls.product1, | ||
) | ||
cls.comment2 = Comment.objects.create( | ||
body='comment2', | ||
is_approved=True, | ||
created_by=cls.user1, | ||
product=cls.product1, | ||
) | ||
cls.comment3 = Comment.objects.create( | ||
body='comment3', | ||
is_approved=True, | ||
created_by=cls.user2, | ||
product=cls.product1, | ||
) | ||
|
||
def test_delete(self): | ||
"""Delete Comment""" | ||
|
||
self.jwt_token = generate_jwt_token(self.user1.id) | ||
self.client.force_authenticate(user=self.user1, token=self.jwt_token) | ||
|
||
response = self.client.delete( | ||
path=f'/api/products/{self.product1.id}/comments/{self.comment1.id}', | ||
content_type='application/json' | ||
) | ||
assert response.status_code == 204 | ||
|
||
# when user is not admin, user can not delete others comment | ||
response = self.client.delete( | ||
path=f'/api/products/{self.product1.id}/comments/{self.comment3.id}', | ||
content_type='application/json' | ||
) | ||
assert response.status_code == 403 | ||
|
||
self.jwt_token = generate_jwt_token(self.admin.id) | ||
self.client.force_authenticate(user=self.admin, token=self.jwt_token) | ||
# when user is admin, admin can delete others comment | ||
response = self.client.delete( | ||
path=f'/api/products/{self.product1.id}/comments/{self.comment3.id}', | ||
content_type='application/json' | ||
) | ||
assert response.status_code == 204 | ||
|
||
response = self.client.delete( | ||
path=f'/api/products/{self.product1.id}/comments/{0}', | ||
content_type='application/json' | ||
) | ||
assert response.status_code == 404 | ||
|
||
response = self.client.delete( | ||
path=f'/api/products/{self.product1.id}/comments/{"0"}', | ||
content_type='application/json' | ||
) | ||
assert response.status_code == 404 | ||
|
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,89 @@ | ||
import json | ||
|
||
import pytest | ||
from rest_framework.test import APITransactionTestCase | ||
|
||
from api.models import Product, Comment | ||
from authorize.helpers import generate_jwt_token | ||
from authorize.models import User | ||
|
||
|
||
class TestComment(APITransactionTestCase): | ||
|
||
@classmethod | ||
@pytest.mark.django_db | ||
def setUp(cls): | ||
cls.user1 = User.objects.create_user( | ||
title='user1', | ||
email='[email protected]', | ||
password='Mypassword', | ||
) | ||
cls.user2 = User.objects.create_user( | ||
title='user2', | ||
email='[email protected]', | ||
password='Mypassword', | ||
) | ||
|
||
cls.product1 = Product.objects.create( | ||
title='product 1', | ||
description='description 1', | ||
created_by=cls.user1, | ||
) | ||
cls.comment1 = Comment.objects.create( | ||
body='comment1', | ||
is_approved=False, | ||
created_by=cls.user1, | ||
product=cls.product1, | ||
) | ||
cls.comment2 = Comment.objects.create( | ||
body='comment2', | ||
is_approved=True, | ||
created_by=cls.user1, | ||
product=cls.product1, | ||
) | ||
|
||
def test_get(self): | ||
"""getting Comment""" | ||
|
||
response = self.client.get( | ||
path=f'/api/products/{self.product1.id}/comments/{self.comment1.id}', | ||
content_type='application/json' | ||
) | ||
assert response.status_code == 200 | ||
assert response.data['id'] is not None | ||
assert response.data['body'] == self.comment1.body | ||
assert response.data['created_by'] == self.user1.id | ||
|
||
response = self.client.get( | ||
path=f'/api/products/{self.product1.id}/comments/{self.comment2.id}', | ||
content_type='application/json' | ||
) | ||
assert response.status_code == 200 | ||
assert response.data['id'] is not None | ||
assert response.data['body'] == self.comment2.body | ||
assert response.data['created_by'] == self.user1.id | ||
|
||
self.jwt_token = generate_jwt_token(self.user2.id) | ||
self.client.force_authenticate(user=self.user2, token=self.jwt_token) | ||
|
||
response = self.client.get( | ||
path=f'/api/products/{self.product1.id}/comments/{self.comment2.id}', | ||
content_type='application/json' | ||
) | ||
assert response.status_code == 200 | ||
assert response.data['id'] is not None | ||
assert response.data['body'] == self.comment2.body | ||
assert response.data['created_by'] == self.user1.id | ||
|
||
response = self.client.get( | ||
path=f'/api/products/{self.product1.id}/comments/{0}', | ||
content_type='application/json' | ||
) | ||
assert response.status_code == 404 | ||
|
||
response = self.client.get( | ||
path=f'/api/products/{self.product1.id}/comments/{"0"}', | ||
content_type='application/json' | ||
) | ||
assert response.status_code == 404 | ||
|
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,128 @@ | ||
import json | ||
|
||
import pytest | ||
from rest_framework.test import APITransactionTestCase | ||
|
||
from api.models import Product, Comment | ||
from authorize.helpers import generate_jwt_token | ||
from authorize.models import User | ||
|
||
|
||
class TestComment(APITransactionTestCase): | ||
|
||
@classmethod | ||
@pytest.mark.django_db | ||
def setUp(cls): | ||
cls.admin = User.objects.create_superuser( | ||
title='admin', | ||
email='[email protected]', | ||
password='Mypassword', | ||
) | ||
cls.user1 = User.objects.create_user( | ||
title='user1', | ||
email='[email protected]', | ||
password='Mypassword', | ||
) | ||
cls.user2 = User.objects.create_user( | ||
title='user2', | ||
email='[email protected]', | ||
password='Mypassword', | ||
) | ||
|
||
cls.product1 = Product.objects.create( | ||
title='product 1', | ||
description='description 1', | ||
created_by=cls.user1, | ||
) | ||
cls.comment1 = Comment.objects.create( | ||
body='comment1', | ||
is_approved=False, | ||
created_by=cls.user1, | ||
product=cls.product1, | ||
) | ||
cls.comment2 = Comment.objects.create( | ||
body='comment2', | ||
is_approved=True, | ||
created_by=cls.user1, | ||
product=cls.product1, | ||
) | ||
cls.comment3 = Comment.objects.create( | ||
body='comment3', | ||
is_approved=True, | ||
created_by=cls.user2, | ||
product=cls.product1, | ||
) | ||
|
||
def test_update(self): | ||
"""Updating Comment""" | ||
|
||
self.jwt_token = generate_jwt_token(self.user1.id) | ||
self.client.force_authenticate(user=self.user1, token=self.jwt_token) | ||
|
||
response = self.client.patch( | ||
path=f'/api/products/{self.product1.id}/comments/{self.comment1.id}', | ||
data=json.dumps({ | ||
'body': 'test', | ||
}), | ||
content_type='application/json' | ||
) | ||
assert response.status_code == 200 | ||
assert response.data['id'] is not None | ||
assert response.data['body'] == 'test' | ||
assert response.data['created_by'] == self.user1.id | ||
|
||
# when user is not admin, user can not update is_approved field | ||
response = self.client.patch( | ||
path=f'/api/products/{self.product1.id}/comments/{self.comment1.id}', | ||
data=json.dumps({ | ||
'body': 'test 2', | ||
'is_approved': True, | ||
}), | ||
content_type='application/json' | ||
) | ||
assert response.status_code == 200 | ||
assert response.data['id'] is not None | ||
assert response.data['body'] == 'test 2' | ||
assert response.data['created_by'] == self.user1.id | ||
assert response.data['is_approved'] is False | ||
|
||
self.jwt_token = generate_jwt_token(self.admin.id) | ||
self.client.force_authenticate(user=self.admin, token=self.jwt_token) | ||
|
||
# when user is admin, user can update is_approved field for comments | ||
response = self.client.patch( | ||
path=f'/api/products/{self.product1.id}/comments/{self.comment1.id}', | ||
data=json.dumps({'is_approved': True}), | ||
content_type='application/json' | ||
) | ||
assert response.status_code == 200 | ||
assert response.data['id'] is not None | ||
assert response.data['body'] == 'test 2' | ||
assert response.data['created_by'] == self.user1.id | ||
assert response.data['is_approved'] is True | ||
|
||
self.jwt_token = generate_jwt_token(self.user2.id) | ||
self.client.force_authenticate(user=self.user2, token=self.jwt_token) | ||
|
||
# user 2 do not have access to comment of user 1 | ||
response = self.client.patch( | ||
path=f'/api/products/{self.product1.id}/comments/{self.comment2.id}', | ||
data=json.dumps({'body': 'test 3'}), | ||
content_type='application/json' | ||
) | ||
assert response.status_code == 403 | ||
|
||
response = self.client.patch( | ||
path=f'/api/products/{self.product1.id}/comments/{0}', | ||
data=json.dumps({'body': 'test 4'}), | ||
content_type='application/json' | ||
) | ||
assert response.status_code == 404 | ||
|
||
response = self.client.patch( | ||
path=f'/api/products/{self.product1.id}/comments/{"0"}', | ||
data=json.dumps({'body': 'test 5'}), | ||
content_type='application/json' | ||
) | ||
assert response.status_code == 404 | ||
|