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 = {}, env_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' }.merge(env_opts) 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 'sets a created_at timestamp on new post' do post_comment Cachai::Post.last.created_at.should_not be_nil end describe 'if not blocked' do it 'creates a new response for that post' do expect do post_comment end.to change(Cachai::Response, :count).by(1) end it 'post is approved' do post_comment expect(Cachai::Response.last.approved).to eql(1) end end describe 'if blocked' do it 'creates a new response for that post' do expect do post_comment({}, {'REMOTE_ADDR' => '12.12.12.12'}) end.to change(Cachai::Response, :count).by(1) end it 'post is not approved' do post_comment({}, {'REMOTE_ADDR' => '12.12.12.12'}) expect(Cachai::Response.last.approved).to eql(0) end 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 describe 'if not blocked' do it 'creates a new response for that post' do expect do post_comment end.to change(Cachai::Response, :count).by(1) end it 'post is approved' do post_comment expect(Cachai::Response.last.approved).to eql(1) end end describe 'if blocked' do it 'creates a new response for that post' do expect do post_comment({}, {'REMOTE_ADDR' => '12.12.12.12'}) end.to change(Cachai::Response, :count).by(1) end it 'post is not approved' do post_comment({}, {'REMOTE_ADDR' => '12.12.12.12'}) expect(Cachai::Response.last.approved).to eql(0) end 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