Skip to content

Commit

Permalink
Modifications in review_modify_handler and its test
Browse files Browse the repository at this point in the history
- Skip updates for missing fields
  • Loading branch information
Pinank Solanki committed Sep 3, 2017
1 parent c0564f9 commit c467a77
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
20 changes: 14 additions & 6 deletions critiquebrainz/ws/review/views.py
Expand Up @@ -8,7 +8,7 @@
revision as db_revision,
users as db_users,
)
from critiquebrainz.ws.exceptions import NotFound, AccessDenied, InvalidRequest, LimitExceeded
from critiquebrainz.ws.exceptions import NotFound, AccessDenied, InvalidRequest, LimitExceeded, MissingDataError
from critiquebrainz.ws.oauth import oauth
from critiquebrainz.ws.parser import Parser
from critiquebrainz.decorators import crossdomain
Expand Down Expand Up @@ -233,11 +233,16 @@ def review_modify_handler(review_id, user):
:resheader Content-Type: *application/json*
"""
# TODO(psolanki): One caveat is that client will have to pass the unmodified parameter with previous revision's value
# otherwise it will be set to None

def fetch_params():
text = Parser.string('json', 'text', min=REVIEW_TEXT_MIN_LENGTH, max=REVIEW_TEXT_MAX_LENGTH, optional=True)
rating = Parser.int('json', 'rating', min=REVIEW_RATING_MIN, max=REVIEW_RATING_MAX, optional=True)
try:
text = Parser.string('json', 'text', min=REVIEW_TEXT_MIN_LENGTH, max=REVIEW_TEXT_MAX_LENGTH)
except MissingDataError:
text = 'same' # Assign temporary value which indicates no modification
try:
rating = Parser.int('json', 'rating', min=REVIEW_RATING_MIN, max=REVIEW_RATING_MAX)
except MissingDataError:
rating = 0 # Assign temporary value which indicates no modification
if text is None and rating is None:
raise InvalidRequest(desc='Review must have either text or rating')
return text, rating
Expand All @@ -248,7 +253,10 @@ def fetch_params():
if str(review["user_id"]) != user.id:
raise AccessDenied
text, rating = fetch_params()
# Check if contents of the review are updated
if text == 'same':
text = review['text']
if rating == 0:
rating = review['rating']
if (text == review['text']) and (rating == review['rating']):
raise InvalidRequest(desc='Either text or rating should be edited to update the review')

Expand Down
4 changes: 4 additions & 0 deletions critiquebrainz/ws/review/views_test.py
Expand Up @@ -65,9 +65,13 @@ def test_review_modify(self):
resp = self.client.post('/review/%s' % review["id"], headers=self.header(self.user), data=json.dumps(data))
self.assert400(resp, "Either text or rating should be edited to update the review.")

# Check if the passed parameter is modified and the other is not
data = dict(text="Some updated text with length more than twenty five.", rating="5")
resp = self.client.post('/review/%s' % review["id"], headers=self.header(self.user), data=json.dumps(data))
self.assert200(resp)
resp = self.client.get('/review/%s' % review["id"]).json
self.assertEqual(resp['review']['text'], data['text'])
self.assertEqual(resp['review']['rating'], review['rating'])

def test_review_list(self):
review = self.create_dummy_review()
Expand Down

0 comments on commit c467a77

Please sign in to comment.