Commit 28447136 by Clinton Blackburn

Updated Abuse API spec

- Using factories to create only the data that is actually needed for the tests
- Replaced double quotes with single quotes
- Fixed spacing
- Consolidated helper methods
parent f1d81865
require 'spec_helper' require 'spec_helper'
def create_comment_flag(comment_id, user_id) describe 'Abuse API' do
create_flag("/api/v1/comments/" + comment_id + "/abuse_flag", user_id) before(:each) { set_api_key_header }
end
def create_thread_flag(thread_id, user_id) shared_examples 'an abuse endpoint' do
create_flag("/api/v1/threads/" + thread_id + "/abuse_flag", user_id) let(:affected_entity_id) { affected_entity.id }
end let(:user_id) { create(:user).id }
def remove_thread_flag(thread_id, user_id) it { should be_ok }
remove_flag("/api/v1/threads/" + thread_id + "/abuse_unflag", user_id)
end
def remove_comment_flag(comment_id, user_id) it 'updates the abuse flaggers' do
remove_flag("/api/v1/comments/" + comment_id + "/abuse_unflag", user_id) subject
end
def create_flag(put_command, user_id) affected_entity.reload
if user_id.nil? expect(affected_entity.abuse_flaggers).to eq expected_abuse_flaggers
put put_command expect(non_affected_entity.abuse_flaggers).to have(0).items
else end
put put_command, user_id: user_id
end
end
def remove_flag(put_command, user_id) context 'if the comment does not exist' do
if user_id.nil? let(:affected_entity_id) { 'does_not_exist' }
put put_command it { should be_bad_request }
else its(:body) { should eq "[\"#{I18n.t(:requested_object_not_found)}\"]" }
put put_command, user_id: user_id end
context 'if no user_id is provided' do
let(:user_id) { nil }
it { should be_bad_request }
its(:body) { should eq "[\"#{I18n.t(:user_id_is_required)}\"]" }
end
end end
end
describe "app" do describe 'comment actions' do
describe "abuse" do let(:affected_entity) { create(:comment, abuse_flaggers: []) }
let(:non_affected_entity) { affected_entity.comment_thread }
before(:each) do context 'when flagging a comment for abuse' do
init_without_subscriptions let(:expected_abuse_flaggers) { [user_id] }
set_api_key_header subject { put "/api/v1/comments/#{affected_entity_id}/abuse_flag", user_id: user_id }
end
describe "flag a comment as abusive" do it_behaves_like 'an abuse endpoint'
it "create or update the abuse_flags on the comment" do
comment = Comment.first
prev_abuse_flaggers_count = comment.abuse_flaggers.length
create_comment_flag("#{comment.id}", User.first.id)
comment = Comment.find(comment.id)
comment.abuse_flaggers.count.should == prev_abuse_flaggers_count + 1
# verify that the thread doesn't automatically get flagged
comment.comment_thread.abuse_flaggers.length.should == 0
end
it "returns 400 when the comment does not exist" do
create_comment_flag("does_not_exist", User.first.id)
last_response.status.should == 400
parse(last_response.body).first.should == I18n.t(:requested_object_not_found)
end
it "returns 400 when user_id is not provided" do
create_comment_flag("#{Comment.first.id}", nil)
last_response.status.should == 400
parse(last_response.body).first.should == I18n.t(:user_id_is_required)
end
#Would like to test the output of to_hash, but not sure how to deal with a Moped::BSON::Document object
#it "has a correct hash" do
# create_flag("#{Comment.first.id}", User.first.id)
# Comment.first.to_hash
#end
end end
describe "flag a thread as abusive" do
it "create or update the abuse_flags on the comment" do context 'when un-flagging a comment for abuse' do
comment = Comment.first let(:affected_entity) { create(:comment, abuse_flaggers: [user_id]) }
thread = comment.comment_thread let(:expected_abuse_flaggers) { [] }
prev_abuse_flaggers_count = thread.abuse_flaggers.count subject { put "/api/v1/comments/#{affected_entity_id}/abuse_unflag", user_id: user_id }
create_thread_flag("#{thread.id}", User.first.id)
it_behaves_like 'an abuse endpoint'
comment = Comment.find(comment.id)
comment.comment_thread.abuse_flaggers.count.should == prev_abuse_flaggers_count + 1
# verify that the comment doesn't automatically get flagged
comment.abuse_flaggers.length.should == 0
end
it "returns 400 when the thread does not exist" do
create_thread_flag("does_not_exist", User.first.id)
last_response.status.should == 400
parse(last_response.body).first.should == I18n.t(:requested_object_not_found)
end
it "returns 400 when user_id is not provided" do
create_thread_flag("#{Comment.first.comment_thread.id}", nil)
last_response.status.should == 400
parse(last_response.body).first.should == I18n.t(:user_id_is_required)
end
#Would like to test the output of to_hash, but not sure how to deal with a Moped::BSON::Document object
#it "has a correct hash" do
# create_thread_flag("#{Comment.first.comment_thread.id}", User.first.id)
# Comment.first.comment_thread.to_hash
#end
end end
describe "unflag a comment as abusive" do end
it "removes the user from the existing abuse_flaggers" do
comment = Comment.first describe 'comment thread actions' do
create_comment_flag("#{comment.id}", User.first.id) let(:affected_entity) { create(:comment_thread, abuse_flaggers: []) }
let(:non_affected_entity) { create(:comment, comment_thread: affected_entity) }
comment = Comment.first
prev_abuse_flaggers = comment.abuse_flaggers context 'when flagging a comment thread for abuse' do
prev_abuse_flaggers_count = prev_abuse_flaggers.count let(:expected_abuse_flaggers) { [user_id] }
subject { put "/api/v1/threads/#{affected_entity_id}/abuse_flag", user_id: user_id }
prev_abuse_flaggers.should include User.first.id
it_behaves_like 'an abuse endpoint'
remove_comment_flag("#{comment.id}", User.first.id)
comment = Comment.find(comment.id)
comment.abuse_flaggers.count.should == prev_abuse_flaggers_count - 1
comment.abuse_flaggers.to_a.should_not include User.first.id
end
it "returns 400 when the comment does not exist" do
remove_comment_flag("does_not_exist", User.first.id)
last_response.status.should == 400
parse(last_response.body).first.should == I18n.t(:requested_object_not_found)
end
it "returns 400 when the thread does not exist" do
remove_thread_flag("does_not_exist", User.first.id)
last_response.status.should == 400
parse(last_response.body).first.should == I18n.t(:requested_object_not_found)
end
it "returns 400 when user_id is not provided" do
remove_thread_flag("#{Comment.first.comment_thread.id}", nil)
last_response.status.should == 400
parse(last_response.body).first.should == I18n.t(:user_id_is_required)
end
#Would like to test the output of to_hash, but not sure how to deal with a Moped::BSON::Document object
#it "has a correct hash" do
# create_thread_flag("#{Comment.first.comment_thread.id}", User.first.id)
# Comment.first.comment_thread.to_hash
#end
end end
describe "unflag a thread as abusive" do
it "removes the user from the existing abuse_flaggers" do context 'when un-flagging a comment thread for abuse' do
thread = CommentThread.first let(:affected_entity) { create(:comment_thread, abuse_flaggers: [user_id]) }
create_thread_flag("#{thread.id}", User.first.id) let(:expected_abuse_flaggers) { [] }
subject { put "/api/v1/threads/#{affected_entity_id}/abuse_unflag", user_id: user_id }
thread = CommentThread.first
prev_abuse_flaggers = thread.abuse_flaggers it_behaves_like 'an abuse endpoint'
prev_abuse_flaggers_count = prev_abuse_flaggers.count
prev_abuse_flaggers.should include User.first.id
remove_thread_flag("#{thread.id}", User.first.id)
thread = CommentThread.find(thread.id)
thread.abuse_flaggers.count.should == prev_abuse_flaggers_count - 1
thread.abuse_flaggers.to_a.should_not include User.first.id
end
it "returns 400 when the thread does not exist" do
remove_thread_flag("does_not_exist", User.first.id)
last_response.status.should == 400
parse(last_response.body).first.should == I18n.t(:requested_object_not_found)
end
it "returns 400 when user_id is not provided" do
remove_thread_flag("#{Comment.first.comment_thread.id}", nil)
last_response.status.should == 400
parse(last_response.body).first.should == I18n.t(:user_id_is_required)
end
#Would like to test the output of to_hash, but not sure how to deal with a Moped::BSON::Document object
#it "has a correct hash" do
# create_thread_flag("#{Comment.first.comment_thread.id}", User.first.id)
# Comment.first.comment_thread.to_hash
#end
end end
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment