Fix possible duplication of AspectVisibility

No uniqueness control on AspectVisibility resulted in possible having
multiple AspectVisibility objects in the DB for the same aspect and
shareable which doesn't make sense. Introduce uniqueness validation
and fix up tests where duplication happened.
This commit is contained in:
cmrd Senya 2016-05-27 20:11:36 +03:00
parent ac70d72190
commit 717554edac
No known key found for this signature in database
GPG key ID: 5FCC5BA680E67BFE
4 changed files with 35 additions and 3 deletions

View file

@ -10,4 +10,5 @@ class AspectVisibility < ActiveRecord::Base
belongs_to :shareable, :polymorphic => true
validates :shareable, :presence => true
validates :aspect, uniqueness: {scope: %i(shareable_id shareable_type)}
end

View file

@ -43,9 +43,9 @@ describe 'mentioning', :type => :request do
expect(users_connected?(@user1, @user3)).to be false
status_msg = nil
expect do
expect {
status_msg = @user1.post(:status_message, {text: text_mentioning(@user3), to: default_aspect})
end.to change(Post, :count).by(1)
}.to change(Post, :count).by(1).and change(AspectVisibility, :count).by(1)
expect(status_msg).not_to be_nil
expect(status_msg.public?).to be false

View file

@ -0,0 +1,32 @@
require "spec_helper"
describe AspectVisibility, type: :model do
let(:status_message) { FactoryGirl.create(:status_message) }
let(:aspect) { FactoryGirl.create(:aspect) }
let(:status_message_in_aspect) { FactoryGirl.create(:status_message_in_aspect) }
let(:photo_with_same_id) {
Photo.find_by_id(status_message_in_aspect.id) || FactoryGirl.create(:photo, id: status_message_in_aspect.id)
}
describe ".create" do
it "creates object when attributes are fine" do
expect {
AspectVisibility.create(shareable: status_message, aspect: aspect)
}.to change(AspectVisibility, :count).by(1)
end
it "doesn't allow duplicating objects" do
expect {
AspectVisibility
.create(shareable: status_message_in_aspect, aspect: status_message_in_aspect.aspects.first)
.save!
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "makes difference between shareable types" do
expect {
AspectVisibility.create(shareable: photo_with_same_id, aspect: status_message_in_aspect.aspects.first).save!
}.not_to raise_error
end
end
end

View file

@ -16,7 +16,6 @@ class User
if p.save!
self.aspects.reload
add_to_streams(p, aspects)
dispatch_opts = {
url: Rails.application.routes.url_helpers.post_url(
p,