asciinema.org/spec/controllers/api/comments_controller_spec.rb

155 lines
3.6 KiB
Ruby
Raw Normal View History

2012-03-01 23:25:55 +00:00
require 'spec_helper'
2012-03-04 15:48:24 +00:00
describe Api::CommentsController do
2012-03-01 23:25:55 +00:00
2012-12-01 17:45:06 +00:00
let(:user) { FactoryGirl.create(:user) }
let(:asciicast) { FactoryGirl.create(:asciicast) }
2012-03-01 23:25:55 +00:00
before do
login_as(user)
end
describe "#create" do
2012-03-04 13:37:30 +00:00
before do
Asciicast.stub(:find).and_return(asciicast)
end
2012-03-01 23:25:55 +00:00
context "given valid data" do
def dispatch
post :create,
:asciicast_id => asciicast.id,
2012-03-08 14:00:10 +00:00
:comment => { "body" => "Foo" },
2012-03-01 23:25:55 +00:00
:format => :json
end
before do
Comment.any_instance.should_receive(:save).and_return(true)
end
it "response status should be 201" do
2012-03-01 23:25:55 +00:00
dispatch
response.status.should == 201
end
it "notifies asciicast author via email" do
@controller.should_receive(:notify_via_email)
dispatch
end
2012-03-01 23:25:55 +00:00
end
context "given not valid data" do
def dispatch
post :create,
:asciicast_id => asciicast.id,
:comment => {},
:format => :json
end
it "response should be 422" do
dispatch
response.status.should == 422
end
end
end
describe "#index" do
2012-03-04 13:37:30 +00:00
before do
Asciicast.stub(:find).and_return(asciicast)
end
2012-03-01 23:25:55 +00:00
it "return comments" do
asciicast.should_receive(:comments).and_return([])
get :index, :asciicast_id => asciicast.id
end
end
2012-03-04 13:37:30 +00:00
describe "#destroy" do
let(:comment) { mock_model(Comment).as_null_object }
before do
Comment.stub(:find).with("1").and_return(comment)
end
context "when user is creator of comment" do
before do
comment.stub(:user).and_return(user)
end
2012-04-09 15:06:41 +00:00
it "calls destroy on comment" do
comment.should_receive(:destroy)
delete :destroy, :id => 1, :format => :json
2012-03-04 13:37:30 +00:00
end
end
context "when user is not creator of comment" do
2012-12-01 17:45:06 +00:00
let(:other_user) { FactoryGirl.create(:user) }
2012-03-04 13:37:30 +00:00
before do
comment.stub(:user).and_return(other_user)
end
2012-04-09 15:06:41 +00:00
it "doesn't call destroy on comment" do
comment.should_not_receive(:destroy)
delete :destroy, :id => 1, :format => :json
end
it "responses with 403 when xhr" do
xhr :delete, :destroy, :id => 1, :format => :json
response.status.should == 403
2012-03-04 13:37:30 +00:00
end
end
end
describe '#notify_via_email' do
let(:user) { stub_model(User) }
let(:comment) { stub_model(Comment) }
context 'when asciicast author has email' do
before do
user.email = "jolka@pamietasz.pl"
end
context 'and he is not comment author' do
before do
comment.user = stub_model(User)
end
it "sends email" do
mail = double('mail', :deliver => true)
UserMailer.should_receive(:new_comment_email).and_return(mail)
@controller.send(:notify_via_email, user, comment)
end
end
context 'and he is comment author' do
before do
comment.user = user
end
it "doesn't send email" do
UserMailer.should_not_receive(:new_comment_email)
@controller.send(:notify_via_email, user, comment)
end
end
end
context 'when asciicast author has no email' do
it "doesn't send email" do
UserMailer.should_not_receive(:new_comment_email)
@controller.send(:notify_via_email, user, comment)
end
end
context 'when asciicast author is unknown (nil)' do
let(:user) { nil }
it "doesn't send email" do
UserMailer.should_not_receive(:new_comment_email)
@controller.send(:notify_via_email, user, comment)
end
end
end
2012-03-01 23:25:55 +00:00
end