Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cs_comments_service
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
cs_comments_service
Commits
09ec975a
Commit
09ec975a
authored
Sep 04, 2014
by
Alan Boudreault
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add validation for undo_vote_for to avoid a crash
parent
d26bc6b7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
1 deletions
+26
-1
lib/helpers.rb
+3
-1
spec/api/vote_spec.rb
+23
-0
No files found.
lib/helpers.rb
View file @
09ec975a
...
...
@@ -63,7 +63,9 @@ helpers do
def
undo_vote_for
(
obj
)
raise
ArgumentError
,
t
(
:user_id_is_required
)
unless
user
user
.
unvote
(
obj
)
if
user
.
voted?
(
obj
)
user
.
unvote
(
obj
)
end
obj
.
reload
.
to_hash
.
to_json
end
...
...
spec/api/vote_spec.rb
View file @
09ec975a
...
...
@@ -49,6 +49,18 @@ describe "app" do
comment
.
up_votes_count
.
should
==
prev_up_votes
-
1
comment
.
down_votes_count
.
should
==
prev_down_votes
end
it
"unvote on the comment is idempotent"
do
user
=
User
.
first
comment
=
Comment
.
first
prev_up_votes
=
comment
.
up_votes_count
prev_down_votes
=
comment
.
down_votes_count
delete
"/api/v1/comments/
#{
comment
.
id
}
/votes"
,
user_id:
user
.
id
# multiple calls to unvote endpoint should not change the data
delete
"/api/v1/comments/
#{
comment
.
id
}
/votes"
,
user_id:
user
.
id
comment
=
Comment
.
find
(
comment
.
id
)
comment
.
up_votes_count
.
should
==
prev_up_votes
-
1
comment
.
down_votes_count
.
should
==
prev_down_votes
end
it
"returns 400 when the comment does not exist"
do
delete
"/api/v1/comments/does_not_exist/votes"
,
user_id:
User
.
first
.
id
last_response
.
status
.
should
==
400
...
...
@@ -71,6 +83,17 @@ describe "app" do
thread
.
up_votes_count
.
should
==
prev_up_votes
-
1
thread
.
down_votes_count
.
should
==
prev_down_votes
+
1
end
it
"vote on the thread is idempotent"
do
user
=
User
.
first
thread
=
CommentThread
.
first
prev_up_votes
=
thread
.
up_votes_count
prev_down_votes
=
thread
.
down_votes_count
put
"/api/v1/threads/
#{
thread
.
id
}
/votes"
,
user_id:
user
.
id
,
value:
"down"
put
"/api/v1/threads/
#{
thread
.
id
}
/votes"
,
user_id:
user
.
id
,
value:
"down"
thread
=
CommentThread
.
find
(
thread
.
id
)
thread
.
up_votes_count
.
should
==
prev_up_votes
-
1
thread
.
down_votes_count
.
should
==
prev_down_votes
+
1
end
it
"returns 400 when the thread does not exist"
do
put
"/api/v1/threads/does_not_exist/votes"
,
user_id:
User
.
first
.
id
,
value:
"down"
last_response
.
status
.
should
==
400
...
...
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