require File.expand_path(File.dirname(__FILE__) + '/spec_helper') require 'rest-client' 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 before do allow(RestClient).to receive(:post).and_return(true) end 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 it 'sends emails' do expect(RestClient).to receive(:post).with("https://api:key-aoidjoaijdoaijdoaijsd@api.mailgun.net/v3/foobartest1234.mailgun.org/messages", { :from =>"comments@domain.com", :to =>"test@example.com", :subject =>"Nuevo comentario de Some author at /another/blog/post.html", :text =>"New comment\n\n--\nhttp://domain.com/another/blog/post.html" }).and_return(true) post_comment 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 it 'does not send emails' do expect(RestClient).not_to receive(:post) post_comment({}, {'REMOTE_ADDR' => '12.12.12.12'}) 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 it 'sends admin emails' do expect(RestClient).to receive(:post).with("https://api:key-aoidjoaijdoaijdoaijsd@api.mailgun.net/v3/foobartest1234.mailgun.org/messages", { :from =>"comments@domain.com", :to =>"test@example.com", :subject =>"Nuevo comentario de Some author at /another/blog/post.html", :text =>"New comment\n\n--\nhttp://domain.com/another/blog/post.html" }).and_return(true) post_comment end describe 'if comment is response to parent' do before do post = Cachai::Post.find_by_path('/another/blog/post.html') @parent = post.responses.create!({ :content => 'Content', :author_name => 'Some guy', :author_email => 'some@email.com', :author_ip => '22.33.44.55' }) end it 'sends email both to admin and to parent comment guy' do expect(RestClient).to receive(:post).with("https://api:key-aoidjoaijdoaijdoaijsd@api.mailgun.net/v3/foobartest1234.mailgun.org/messages", { :from => "comments@domain.com", :to => "test@example.com", :subject => "Nuevo comentario de Some author at /another/blog/post.html", :text => "New comment\n\n--\nhttp://domain.com/another/blog/post.html" }).and_return(true) expect(RestClient).to receive(:post).with("https://api:key-aoidjoaijdoaijdoaijsd@api.mailgun.net/v3/foobartest1234.mailgun.org/messages", { :from => "comments@domain.com", :to => "some@email.com", :subject => "Respuesta de Some author a tu comentario en /another/blog/post.html", :text => "New comment\n\n--\nhttp://domain.com/another/blog/post.html" }).and_return(true) post_comment(parent_id: @parent.id) end 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 it 'does not send emails' do expect(RestClient).not_to receive(:post) post_comment({}, {'REMOTE_ADDR' => '12.12.12.12'}) 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