write specs for StatusMessageCreationService

This commit is contained in:
Benjamin Neff 2016-03-05 23:47:36 +01:00
parent 5a46da47c3
commit b67b7cf8c6
2 changed files with 189 additions and 21 deletions

View file

@ -49,12 +49,12 @@ describe StatusMessagesController, :type => :controller do
end
describe '#create' do
let(:text) { "facebook, is that you?" }
let(:status_message_hash) {
{ :status_message => {
:public => "true",
:text => "facebook, is that you?",
},
:aspect_ids => [@aspect1.id.to_s] }
{
status_message: {text: text},
aspect_ids: [@aspect1.id.to_s]
}
}
it 'creates with valid html' do
@ -96,14 +96,52 @@ describe StatusMessagesController, :type => :controller do
post :create, status_message_hash
end
it 'takes public in aspect ids' do
post :create, status_message_hash.merge(:aspect_ids => ['public'])
expect(response.status).to eq(302)
end
context "with aspect_ids" do
before do
@aspect2 = alice.aspects.create(name: "another aspect")
end
it 'takes all_aspects in aspect ids' do
post :create, status_message_hash.merge(:aspect_ids => ['all_aspects'])
expect(response.status).to eq(302)
it "takes one aspect as array in aspect_ids" do
post :create, status_message_hash
expect(response.status).to eq(302)
status_message = StatusMessage.find_by_text(text)
expect(status_message.aspect_visibilities.map(&:aspect)).to eq([@aspect1])
end
it "takes one aspect as string in aspect_ids" do
post :create, status_message_hash.merge(aspect_ids: @aspect1.id.to_s)
expect(response.status).to eq(302)
status_message = StatusMessage.find_by_text(text)
expect(status_message.aspect_visibilities.map(&:aspect)).to eq([@aspect1])
end
it "takes public as array in aspect_ids" do
post :create, status_message_hash.merge(aspect_ids: ["public"])
expect(response.status).to eq(302)
status_message = StatusMessage.find_by_text(text)
expect(status_message.public).to be_truthy
end
it "takes public as string in aspect_ids" do
post :create, status_message_hash.merge(aspect_ids: "public")
expect(response.status).to eq(302)
status_message = StatusMessage.find_by_text(text)
expect(status_message.public).to be_truthy
end
it "takes all_aspects as array in aspect_ids" do
post :create, status_message_hash.merge(aspect_ids: ["all_aspects"])
expect(response.status).to eq(302)
status_message = StatusMessage.find_by_text(text)
expect(status_message.aspect_visibilities.map(&:aspect)).to match_array([@aspect1, @aspect2])
end
it "takes all_aspects as string in aspect_ids" do
post :create, status_message_hash.merge(aspect_ids: "all_aspects")
expect(response.status).to eq(302)
status_message = StatusMessage.find_by_text(text)
expect(status_message.aspect_visibilities.map(&:aspect)).to match_array([@aspect1, @aspect2])
end
end
it "dispatches the post to the specified services" do
@ -127,7 +165,7 @@ describe StatusMessagesController, :type => :controller do
it "doesn't overwrite author_id" do
status_message_hash[:status_message][:author_id] = bob.person.id
post :create, status_message_hash
new_message = StatusMessage.find_by_text(status_message_hash[:status_message][:text])
new_message = StatusMessage.find_by_text(text)
expect(new_message.author_id).to eq(alice.person.id)
end
@ -152,15 +190,9 @@ describe StatusMessagesController, :type => :controller do
expect(StatusMessage.first.provider_display_name).to eq('mobile')
end
# disabled to fix federation
# it 'sends the errors in the body on js' do
# post :create, status_message_hash.merge!(:format => 'js', :status_message => {:text => ''})
# response.body.should include('Status message requires a message or at least one photo')
# end
it "has one participation" do
post :create, status_message_hash
new_message = StatusMessage.find_by_text(status_message_hash[:status_message][:text])
new_message = StatusMessage.find_by_text(text)
expect(new_message.participations.count).to eq(1)
expect(new_message.participations.first.count).to eq(1)
end
@ -185,7 +217,7 @@ describe StatusMessagesController, :type => :controller do
it "attaches all referenced photos" do
post :create, @hash
status_message = StatusMessage.find_by_text(status_message_hash[:status_message][:text])
status_message = StatusMessage.find_by_text(text)
expect(status_message.photos.map(&:id)).to match_array([@photo1, @photo2].map(&:id))
end

View file

@ -0,0 +1,136 @@
require "spec_helper"
describe StatusMessageCreationService do
describe "#create" do
let(:aspect) { alice.aspects.first }
let(:text) { "I'm writing tests" }
let(:params) {
{
status_message: {text: text},
aspect_ids: [aspect.id.to_s]
}
}
it "returns the created StatusMessage" do
status_message = StatusMessageCreationService.new(alice).create(params)
expect(status_message).to_not be_nil
expect(status_message.text).to eq(text)
end
context "with aspect_ids" do
it "creates aspect_visibilities for the StatusMessages" do
alice.aspects.create(name: "another aspect")
status_message = StatusMessageCreationService.new(alice).create(params)
expect(status_message.aspect_visibilities.map(&:aspect)).to eq([aspect])
end
it "does not create aspect_visibilities if the post is public" do
status_message = StatusMessageCreationService.new(alice).create(params.merge(public: true))
expect(status_message.aspect_visibilities).to be_empty
end
end
context "with public" do
it "it creates a private StatusMessage by default" do
status_message = StatusMessageCreationService.new(alice).create(params)
expect(status_message.public).to be_falsey
end
it "it creates a private StatusMessage" do
status_message = StatusMessageCreationService.new(alice).create(params.merge(public: false))
expect(status_message.public).to be_falsey
end
it "it creates a public StatusMessage" do
status_message = StatusMessageCreationService.new(alice).create(params.merge(public: true))
expect(status_message.public).to be_truthy
end
end
context "with location" do
it "it creates a location" do
location_params = {location_address: "somewhere", location_coords: "1,2"}
status_message = StatusMessageCreationService.new(alice).create(params.merge(location_params))
location = status_message.location
expect(location.address).to eq("somewhere")
expect(location.lat).to eq("1")
expect(location.lng).to eq("2")
end
it "does not add a location without location params" do
status_message = StatusMessageCreationService.new(alice).create(params)
expect(status_message.location).to be_nil
end
end
context "with poll" do
it "it creates a poll" do
poll_params = {poll_question: "something?", poll_answers: %w(yes no maybe)}
status_message = StatusMessageCreationService.new(alice).create(params.merge(poll_params))
poll = status_message.poll
expect(poll.question).to eq("something?")
expect(poll.poll_answers.size).to eq(3)
poll_answers = poll.poll_answers.map(&:answer)
expect(poll_answers).to include("yes")
expect(poll_answers).to include("no")
expect(poll_answers).to include("maybe")
end
it "does not add a poll without poll params" do
status_message = StatusMessageCreationService.new(alice).create(params)
expect(status_message.poll).to be_nil
end
end
context "with photos" do
let(:photo1) {
alice.build_post(:photo, pending: true, user_file: File.open(photo_fixture_name), to: aspect.id).tap(&:save!)
}
let(:photo2) {
alice.build_post(:photo, pending: true, user_file: File.open(photo_fixture_name), to: aspect.id).tap(&:save!)
}
let(:photo_ids) { [photo1.id.to_s, photo2.id.to_s] }
it "it attaches all photos" do
status_message = StatusMessageCreationService.new(alice).create(params.merge(photos: photo_ids))
photos = status_message.photos
expect(photos.size).to eq(2)
expect(photos.map(&:id).map(&:to_s)).to eq(photo_ids)
end
it "it marks the photos as non-public if the post is non-public" do
status_message = StatusMessageCreationService.new(alice).create(params.merge(photos: photo_ids, public: false))
status_message.photos.each do |photo|
expect(photo.public).to be_falsey
end
end
it "it marks the photos as public if the post is public" do
status_message = StatusMessageCreationService.new(alice).create(params.merge(photos: photo_ids, public: true))
status_message.photos.each do |photo|
expect(photo.public).to be_truthy
end
end
it "does not attach photos without photos param" do
status_message = StatusMessageCreationService.new(alice).create(params)
expect(status_message.photos).to be_empty
end
end
context "dispatch" do
it "dispatches the StatusMessage" do
expect(alice).to receive(:dispatch_post).with(instance_of(StatusMessage), hash_including(service_types: []))
StatusMessageCreationService.new(alice).create(params)
end
it "dispatches the StatusMessage to services" do
expect(alice).to receive(:dispatch_post)
.with(instance_of(StatusMessage),
hash_including(service_types: array_including(%w(Services::Facebook Services::Twitter))))
StatusMessageCreationService.new(alice).create(params.merge(services: %w(twitter facebook)))
end
end
end
end