Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
course-discovery
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
course-discovery
Commits
c4db3c8c
Commit
c4db3c8c
authored
Dec 06, 2016
by
Tasawer Nawaz
Committed by
tasawernawaz
Dec 08, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
API endpoint for editing comment
ECOM-6336
parent
0a1ab4e5
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
130 additions
and
2 deletions
+130
-2
course_discovery/apps/publisher_comments/api/__init__.py
+0
-0
course_discovery/apps/publisher_comments/api/permissions.py
+9
-0
course_discovery/apps/publisher_comments/api/serializers.py
+9
-0
course_discovery/apps/publisher_comments/api/tests/__init__.py
+0
-0
course_discovery/apps/publisher_comments/api/tests/test_permissions.py
+36
-0
course_discovery/apps/publisher_comments/api/tests/test_serializers.py
+14
-0
course_discovery/apps/publisher_comments/api/tests/test_views.py
+39
-0
course_discovery/apps/publisher_comments/api/urls.py
+8
-0
course_discovery/apps/publisher_comments/api/views.py
+12
-0
course_discovery/apps/publisher_comments/urls.py
+3
-2
No files found.
course_discovery/apps/publisher_comments/api/__init__.py
0 → 100644
View file @
c4db3c8c
course_discovery/apps/publisher_comments/api/permissions.py
0 → 100644
View file @
c4db3c8c
from
rest_framework
import
permissions
class
IsOwner
(
permissions
.
BasePermission
):
"""
Custom permission to only allow owners of the object.
"""
def
has_object_permission
(
self
,
request
,
view
,
obj
):
return
obj
.
user
==
request
.
user
course_discovery/apps/publisher_comments/api/serializers.py
0 → 100644
View file @
c4db3c8c
from
rest_framework
import
serializers
from
course_discovery.apps.publisher_comments.models
import
Comments
class
CommentSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
Comments
fields
=
(
'comment'
,
)
course_discovery/apps/publisher_comments/api/tests/__init__.py
0 → 100644
View file @
c4db3c8c
course_discovery/apps/publisher_comments/api/tests/test_permissions.py
0 → 100644
View file @
c4db3c8c
from
unittest
import
TestCase
from
rest_framework.request
import
Request
from
rest_framework.test
import
APIRequestFactory
,
force_authenticate
from
course_discovery.apps.core.tests.factories
import
UserFactory
from
course_discovery.apps.publisher_comments.api.permissions
import
IsOwner
from
course_discovery.apps.publisher_comments.tests.factories
import
CommentFactory
class
IsOwnerTests
(
TestCase
):
def
setUp
(
self
):
super
(
IsOwnerTests
,
self
)
.
setUp
()
self
.
permissions_class
=
IsOwner
()
self
.
user
=
UserFactory
.
create
()
self
.
comment
=
CommentFactory
.
create
(
user
=
self
.
user
,
comment
=
'test comment'
)
def
test_is_owner_permission
(
self
):
""" If object.user matches request.user, return True. """
# users has access to their own objects
request
=
self
.
_make_request
(
user
=
self
.
user
,
data
=
{
'comment'
:
'update_comment'
})
self
.
assertTrue
(
self
.
permissions_class
.
has_object_permission
(
request
,
None
,
self
.
comment
))
# users CANNOT have access to object of other users
user
=
UserFactory
.
create
()
request
=
self
.
_make_request
(
user
=
user
,
data
=
{
'username'
:
'other_guy'
})
self
.
assertFalse
(
self
.
permissions_class
.
has_object_permission
(
request
,
None
,
self
.
comment
))
def
_make_request
(
self
,
user
=
None
,
data
=
None
):
request
=
APIRequestFactory
()
.
put
(
'/'
,
data
)
if
user
:
force_authenticate
(
request
,
user
=
user
)
return
Request
(
request
)
course_discovery/apps/publisher_comments/api/tests/test_serializers.py
0 → 100644
View file @
c4db3c8c
from
django.test
import
TestCase
from
course_discovery.apps.publisher_comments.api.serializers
import
CommentSerializer
from
course_discovery.apps.publisher_comments.tests.factories
import
CommentFactory
class
CommentSerializerTests
(
TestCase
):
def
test_data
(
self
):
""" Verify that CommentsSerializer serialize the comment object. """
comment
=
CommentFactory
.
create
(
comment
=
'test comment'
)
serializer
=
CommentSerializer
(
comment
)
expected
=
{
'comment'
:
'test comment'
}
self
.
assertDictEqual
(
serializer
.
data
,
expected
)
course_discovery/apps/publisher_comments/api/tests/test_views.py
0 → 100644
View file @
c4db3c8c
import
json
from
django.test
import
TestCase
from
rest_framework.reverse
import
reverse
from
course_discovery.apps.core.tests.factories
import
UserFactory
,
USER_PASSWORD
from
course_discovery.apps.publisher.tests
import
JSON_CONTENT_TYPE
from
course_discovery.apps.publisher_comments.models
import
Comments
from
course_discovery.apps.publisher_comments.tests.factories
import
CommentFactory
class
UpdateCommentTests
(
TestCase
):
def
setUp
(
self
):
super
(
UpdateCommentTests
,
self
)
.
setUp
()
self
.
user
=
UserFactory
.
create
()
self
.
comment
=
CommentFactory
.
create
(
user
=
self
.
user
)
self
.
path
=
reverse
(
'publisher_comments:api:comments'
,
kwargs
=
{
'pk'
:
self
.
comment
.
id
})
self
.
data
=
{
'comment'
:
'updated comment'
}
def
test_update
(
self
):
""" Verify update endpoint allows to update 'comment'. """
self
.
client
.
login
(
username
=
self
.
user
.
username
,
password
=
USER_PASSWORD
)
response
=
self
.
client
.
patch
(
self
.
path
,
json
.
dumps
(
self
.
data
),
JSON_CONTENT_TYPE
)
comment
=
Comments
.
objects
.
get
(
id
=
self
.
comment
.
id
)
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertEqual
(
comment
.
comment
,
self
.
data
[
'comment'
])
def
test_update_without_editing_permission
(
self
):
""" Verify that non owner user of the comment can not edit. """
dummy_user
=
UserFactory
.
create
()
self
.
client
.
login
(
username
=
dummy_user
.
username
,
password
=
USER_PASSWORD
)
response
=
self
.
client
.
patch
(
self
.
path
,
json
.
dumps
(
self
.
data
),
JSON_CONTENT_TYPE
)
self
.
assertEqual
(
response
.
status_code
,
403
)
course_discovery/apps/publisher_comments/api/urls.py
0 → 100644
View file @
c4db3c8c
""" Publisher comments API URLs. """
from
django.conf.urls
import
url
from
course_discovery.apps.publisher_comments.api
import
views
urlpatterns
=
[
url
(
r'^comments/(?P<pk>\d+)/$'
,
views
.
UpdateCommentView
.
as_view
(),
name
=
'comments'
),
]
course_discovery/apps/publisher_comments/api/views.py
0 → 100644
View file @
c4db3c8c
from
rest_framework.generics
import
UpdateAPIView
from
rest_framework.permissions
import
IsAuthenticated
from
course_discovery.apps.publisher_comments.api.permissions
import
IsOwner
from
course_discovery.apps.publisher_comments.api.serializers
import
CommentSerializer
from
course_discovery.apps.publisher_comments.models
import
Comments
class
UpdateCommentView
(
UpdateAPIView
):
serializer_class
=
CommentSerializer
queryset
=
Comments
.
objects
.
all
()
permission_classes
=
(
IsAuthenticated
,
IsOwner
)
course_discovery/apps/publisher_comments/urls.py
View file @
c4db3c8c
"""
URLs for the course publisher views.
URLs for the course publisher
comments
views.
"""
from
django.conf.urls
import
url
from
django.conf.urls
import
url
,
include
from
course_discovery.apps.publisher_comments
import
views
urlpatterns
=
[
url
(
r'^api/'
,
include
(
'course_discovery.apps.publisher_comments.api.urls'
,
namespace
=
'api'
)),
url
(
r'^(?P<pk>\d+)/edit/$'
,
views
.
UpdateCommentView
.
as_view
(),
name
=
'comment_edit'
),
]
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment