Commit 0ec1250d by Clinton Blackburn

Updated Commentable API spec

- Creating only the necessary data needed for testing
- Replaced double quotes with single quotes where applicable
- Using expect instead of should
parent 072d0409
require 'spec_helper' require 'spec_helper'
require 'unicode_shared_examples' require 'unicode_shared_examples'
describe "app" do describe 'app' do
describe "commentables" do describe 'commentables' do
before(:each) { set_api_key_header }
let(:commentable_id) { Faker::Lorem.word }
before(:each) do describe 'DELETE /api/v1/:commentable_id/threads' do
init_without_subscriptions it 'delete all associated threads and comments of a commentable' do
set_api_key_header thread_count = 2
create_list(:comment_thread, thread_count, commentable_id: commentable_id)
expect(Commentable.find(commentable_id).comment_threads.count).to eq thread_count
delete "/api/v1/#{commentable_id}/threads"
expect(last_response).to be_ok
expect(Commentable.find(commentable_id).comment_threads.count).to eq 0
end end
describe "DELETE /api/v1/:commentable_id/threads" do context 'if the commentable does not exist' do
it "delete all associated threads and comments of a commentable" do subject { delete '/api/v1/does_not_exist/threads' }
delete '/api/v1/question_1/threads'
last_response.should be_ok it { should be_ok }
Commentable.find("question_1").comment_threads.count.should == 0
end end
it "handle normally when commentable does not exist" do end
delete '/api/v1/does_not_exist/threads'
last_response.should be_ok describe 'GET /api/v1/:commentable_id/threads' do
let(:returned_threads) { parse(subject.body)['collection'] }
subject { get "/api/v1/#{commentable_id}/threads" }
shared_examples_for 'a filterable API endpoint' do
let!(:ignored_threads) { create_list(:comment_thread, 3, commentable_id: commentable_id) }
subject { get "/api/v1/#{commentable_id}/threads", parameters }
it { should be_ok }
it 'returns the correct CommentThreads' do
expect(returned_threads.length).to eq threads.length
threads.sort_by!(&:_id).reverse!
threads.each_with_index do |thread, index|
expect(returned_threads[index]).to include('id' => thread.id.to_s, 'body' => thread.body)
end end
end end
describe "GET /api/v1/:commentable_id/threads" do end
def thread_result(commentable_id, params={})
get "/api/v1/#{commentable_id}/threads", params context 'without filtering' do
last_response.should be_ok let(:parameters) { {} }
parse(last_response.body)["collection"] let!(:threads) { ignored_threads + create_list(:comment_thread, 3, :with_group_id, commentable_id: commentable_id) }
end
it "get all comment threads associated with a commentable object" do it_behaves_like 'a filterable API endpoint'
threads = thread_result "question_1" end
threads.length.should == 2
threads.index{|c| c["body"] == "can anyone help me?"}.should_not be_nil context 'when filtering by the standalone context' do
threads.index{|c| c["body"] == "it is unsolvable"}.should_not be_nil let(:parameters) { {context: :standalone} }
end let!(:threads) { create_list(:comment_thread, 3, commentable_id: commentable_id, context: :standalone) }
it "returns standalone threads if explicitly requested" do
threads = thread_result "question_1", context: "standalone" it_behaves_like 'a filterable API endpoint'
threads.length.should == 1 end
threads[0]["body"].should == "no one can see us"
end context 'when filtering by course_id' do
it "filters by course_id" do let(:course_id) { Faker::Lorem.word }
course1_threads = thread_result "question_1", course_id: "1" let(:parameters) { {course_id: course_id} }
course1_threads.length.should == 1 let!(:threads) { create_list(:comment_thread, 3, commentable_id: commentable_id, course_id: course_id) }
course2_threads = thread_result "question_1", course_id: "2"
course2_threads.length.should == 1
course1_threads.should_not == course2_threads it_behaves_like 'a filterable API endpoint'
end end
it "filters by group_id" do
group_thread = Commentable.find("question_1").comment_threads.sort(_id: 1).first context 'when filtering by group_id' do
threads = thread_result "question_1", group_id: 42 let(:group_id) { Faker::Number.number(4) }
threads.length.should == 2 let(:parameters) { {group_id: group_id} }
group_thread.group_id = 43 let!(:threads) { create_list(:comment_thread, 3, commentable_id: commentable_id, group_id: group_id) }
group_thread.save!
threads = thread_result "question_1", group_id: 42
threads.length.should == 1 it_behaves_like 'a filterable API endpoint'
group_thread.group_id = 42 end
group_thread.save!
threads = thread_result "question_1", group_id: 42 context 'when filtering by multiple group_id values' do
threads.length.should == 2 let(:group_ids) { [Faker::Number.number(4), Faker::Number.number(4)] }
end let(:parameters) { {group_ids: group_ids.join(',')} }
it "filters by group_ids" do
group_thread = Commentable.find("question_1").comment_threads.sort(_id: 1).first
group_thread.group_id = 42 it_behaves_like 'a filterable API endpoint' do
group_thread.save! let!(:threads) do
threads = thread_result "question_1", group_ids: "42,43" threads = []
threads.length.should == 2
group_thread.group_id = 43 group_ids.each do |group_id|
group_thread.save! threads += create_list(:comment_thread, 3, commentable_id: commentable_id, group_id: group_id)
threads = thread_result "question_1", group_ids: "42,43" end
threads.length.should == 2
group_thread.group_id = 44 threads
group_thread.save end
threads = thread_result "question_1", group_ids: "42,43" end
threads.length.should == 1 end
end
it "returns an empty array when the commentable object does not exist (no threads)" do context 'when the commentable does not exist' do
threads = thread_result "does_not_exist" subject { get '/api/v1/does_not_exist/threads' }
threads.length.should == 0
it { should be_ok }
it 'should not return any results' do
expect(returned_threads.length).to eq 0
end
end end
def test_unicode_data(text) def test_unicode_data(text)
commentable_id = "unicode_commentable" commentable_id = 'unicode_commentable'
thread = make_thread(User.first, text, "unicode_course", commentable_id) thread = create(:comment_thread, commentable_id: commentable_id, body: text)
make_comment(User.first, thread, text) create(:comment, comment_thread: thread, body: text)
get "/api/v1/#{commentable_id}/threads" get "/api/v1/#{commentable_id}/threads"
last_response.should be_ok last_response.should be_ok
result = parse(last_response.body)["collection"] result = parse(last_response.body)['collection']
result.should_not be_empty result.should_not be_empty
check_thread_result_json(nil, thread, result.first) check_thread_result_json(nil, thread, result.first)
end end
include_examples "unicode data" include_examples 'unicode data'
end end
describe "POST /api/v1/:commentable_id/threads" do
let(:default_params) do describe 'POST /api/v1/:commentable_id/threads' do
{title: "Interesting question", body: "cool", course_id: "1", user_id: "1"} let(:commentable_id) { Faker::Lorem.word }
let(:user) { create(:user) }
let(:parameters) { attributes_for(:comment_thread, user_id: user.id) }
subject { post "/api/v1/#{commentable_id}/threads", parameters }
shared_examples_for 'CommentThread creation API' do |context='course'|
it 'creates a new CommentThread' do
expect(CommentThread.count).to eq 0
body = parse(subject.body)
expect(body).to include('read' => false,
'unread_comments_count' => 0,
'endorsed' => false,
'resp_total' => 0)
expect(CommentThread.count).to eq 1
thread = CommentThread.find(body['id'])
expect(thread).to_not be_nil
expect(thread.context).to eq context
end end
it "create a new comment thread for the commentable object" do
old_count = CommentThread.count
post '/api/v1/question_1/threads', default_params
last_response.should be_ok
result = parse(last_response.body)
result["read"].should == false
result["unread_comments_count"].should == 0
result["endorsed"].should == false
result["resp_total"].should == 0
CommentThread.count.should == old_count + 1
thread = CommentThread.where(title: "Interesting question").first
thread.should_not be_nil
thread.context.should == "course"
end
it "can create a standalone thread" do
old_count = CommentThread.count
post '/api/v1/question_1/threads', default_params.merge(:context => "standalone")
CommentThread.count.should == old_count + 1
thread = CommentThread.where(title: "Interesting question").first
thread.should_not be_nil
thread.context.should == "standalone"
end end
it { should be_ok }
it_behaves_like 'CommentThread creation API'
it_behaves_like 'CommentThread creation API', 'standalone' do
let(:parameters) { attributes_for(:comment_thread, user_id: user.id, context: 'standalone') }
end
CommentThread.thread_type.values.each do |thread_type| CommentThread.thread_type.values.each do |thread_type|
it "can create a #{thread_type} thread" do it "can create a #{thread_type} thread" do
old_count = CommentThread.where(thread_type: thread_type).count old_count = CommentThread.where(thread_type: thread_type).count
post "/api/v1/question_1/threads", default_params.merge(thread_type: thread_type.to_s) post '/api/v1/question_1/threads', parameters.merge(thread_type: thread_type.to_s)
last_response.should be_ok last_response.should be_ok
parse(last_response.body)["thread_type"].should == thread_type.to_s parse(last_response.body)['thread_type'].should == thread_type.to_s
CommentThread.where(thread_type: thread_type).count.should == old_count + 1 CommentThread.where(thread_type: thread_type).count.should == old_count + 1
end end
end end
it "allows anonymous thread" do
old_count = CommentThread.count it 'allows anonymous thread' do
post '/api/v1/question_1/threads', default_params.merge(anonymous: true) post '/api/v1/question_1/threads', parameters.merge!(anonymous: true)
last_response.should be_ok
CommentThread.count.should == old_count + 1
c = CommentThread.where(title: "Interesting question").first
c.should_not be_nil
c["anonymous"].should be_true
end
it "create a new comment thread for a new commentable object" do
post '/api/v1/does_not_exist/threads', default_params
last_response.should be_ok last_response.should be_ok
Commentable.find("does_not_exist").comment_threads.length.should == 1 body = parse(subject.body)
Commentable.find("does_not_exist").comment_threads.first.body.should == "cool"
thread = CommentThread.find(body['id'])
expect(thread).to_not be_nil
expect(thread['anonymous']).to be_true
end end
it "returns error when title, body or course id does not exist" do
params = default_params.dup it 'returns error when title, body or course id does not exist' do
params.delete(:title) [:title, :body, :course_id].each do |parameter|
post '/api/v1/question_1/threads', params params = parameters.dup
last_response.status.should == 400 params.delete(parameter)
params = default_params.dup
params.delete(:body)
post '/api/v1/question_1/threads', params
last_response.status.should == 400
params = default_params.dup
params.delete(:course_id)
post '/api/v1/question_1/threads', params post '/api/v1/question_1/threads', params
last_response.status.should == 400 last_response.status.should == 400
end end
end
it "returns error when title or body is blank (only consists of spaces and new lines)" do it "returns error when title or body is blank (only consists of spaces and new lines)" do
post '/api/v1/question_1/threads', default_params.merge(title: " ") post '/api/v1/question_1/threads', parameters.merge(title: " ")
last_response.status.should == 400 last_response.status.should == 400
post '/api/v1/question_1/threads', default_params.merge(body: " \n \n") post '/api/v1/question_1/threads', parameters.merge(body: " \n \n")
last_response.status.should == 400 last_response.status.should == 400
end end
it "returns 503 and does not create when the post content is blocked" do
post '/api/v1/question_1/threads', default_params.merge(body: "BLOCKED POST") it 'returns 503 and does not create when the post content is blocked' do
body = 'BLOCKED POST'
hash = block_post_body
post '/api/v1/question_1/threads', parameters.merge!(body: body)
last_response.status.should == 503 last_response.status.should == 503
parse(last_response.body).first.should == I18n.t(:blocked_content_with_body_hash, :hash => Digest::MD5.hexdigest("blocked post")) parse(last_response.body).first.should == I18n.t(:blocked_content_with_body_hash, :hash => hash)
CommentThread.where(body: "BLOCKED POST").to_a.should be_empty expect(CommentThread.where(body: body).length).to eq 0
end end
def test_unicode_data(text) def test_unicode_data(text)
commentable_id = "unicode_commentable" commentable_id = 'unicode_commentable'
post "/api/v1/#{commentable_id}/threads", default_params.merge(body: text, title: text) post "/api/v1/#{commentable_id}/threads", parameters.merge!(body: text, title: text)
last_response.should be_ok last_response.should be_ok
CommentThread.where(commentable_id: commentable_id, body: text, title: text).should_not be_empty expect(CommentThread.where(commentable_id: commentable_id, body: text, title: text)).to_not be_empty
end end
include_examples "unicode data" include_examples 'unicode data'
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