require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe 'post comment' do include Rack::Test::Methods def app TestApp end def post_comment(opts = {}) data = { :domain => 'domain.com', :protocol => 'http', :path => '/another/blog/post.html', :content => 'New comment', :author_name => 'Some author', :author_email => 'test@email.com', :author_url => 'http://url.com' }.merge(opts) post '/comments.json', data.to_json, { 'CONTENT_TYPE' => 'application/json' } end describe 'no domain' do it 'returns 400' do post_comment :domain => nil last_response.status.should == 400 end end describe 'no protocol' do it 'returns 400' do post_comment :protocol => nil last_response.status.should == 422 end end describe 'no path' do it 'returns 400' do post_comment :path => nil last_response.status.should == 422 end end describe 'existing domain and path' do describe 'invalid domain' do it 'returns 400' do post_comment :domain => 'invalid.com' last_response.status.should == 400 end end describe 'valid domain' do describe 'no author name' do it 'returns 422' do post_comment :author_name => nil last_response.status.should == 422 end end describe 'no email' do it 'returns 422' do post_comment :author_email => nil last_response.status.should == 422 end end describe 'no content' do it 'returns 422' do post_comment :content => nil last_response.status.should == 422 end end describe 'valid data' do describe 'unexisting post' do before do Cachai::Post.delete_all end it 'creates a new entry for that post' do expect do post_comment end.to change(Cachai::Post, :count).by(1) end it 'creates a new response for that post' do expect do post_comment end.to change(Cachai::Response, :count).by(1) end end describe 'existing post' do before do Cachai::Post.find_by_path('/another/blog/post.html').should be_a(Cachai::Post) end it 'does not create a new post entry' do expect do post_comment end.not_to change(Cachai::Post, :count).by(1) end describe 'if within comments duration' do it 'creates a new response for that post' do expect do post_comment end.to change(Cachai::Response, :count).by(1) end end describe 'if after comments duration' do before do post = Cachai::Post.find_by_path('/another/blog/post.html') post.update_attribute(:created_at, 100.days.ago) end it 'returns 400' do post_comment last_response.status.should == 400 end it 'does not create a new response for that post' do expect do post_comment end.not_to change(Cachai::Response, :count) end end end end end end end