fix pod table migration if someone deleted a user (owner) manually

This commit is contained in:
Benjamin Neff 2016-03-06 20:57:33 +01:00
parent e90f72ff37
commit 10af3a8b11
5 changed files with 44 additions and 8 deletions

View file

@ -27,6 +27,8 @@ class Pod < ActiveRecord::Base
where(arel_table[:status].gt(Pod.statuses[:no_errors])).where.not(status: Pod.statuses[:version_failed])
}
validate :not_own_pod
class << self
def find_or_create_by(opts) # Rename this method to not override an AR method
uri = URI.parse(opts.fetch(:url))
@ -112,4 +114,10 @@ class Pod < ActiveRecord::Base
@uri ||= (ssl ? URI::HTTPS : URI::HTTP).build(host: host, port: port)
@uri.dup
end
def not_own_pod
pod_uri = AppConfig.pod_uri
pod_port = DEFAULT_PORTS.include?(pod_uri.port) ? nil : pod_uri.port
errors.add(:base, "own pod not allowed") if pod_uri.host == host && pod_port == port
end
end

View file

@ -46,9 +46,9 @@ class ExtendPods < ActiveRecord::Migration
add_column :people, :pod_id, :integer
add_index :people, :url, length: 190
add_foreign_key :people, :pods, name: :people_pod_id_fk, on_delete: :cascade
Person.where(owner: nil).group_by {|person| person[:url] }.each do |url, _|
Person.where(owner: nil).distinct(:url).pluck(:url).each do |url|
pod = Pod.find_or_create_by(url: url)
Person.where(url: url).update_all(pod_id: pod.id)
Person.where(url: url, owner_id: nil).update_all(pod_id: pod.id) if pod.persisted?
end
# cleanup unused pods

View file

@ -369,7 +369,7 @@ FactoryGirl.define do
factory(:federation_person_from_webfinger, class: DiasporaFederation::Entities::Person) do
sequence(:guid) { UUID.generate :compact }
sequence(:diaspora_id) {|n| "bob-person-#{n}#{r_str}@example.net" }
url AppConfig.pod_uri.to_s
url "https://example.net/"
exported_key OpenSSL::PKey::RSA.generate(1024).public_key.export
profile {
DiasporaFederation::Entities::Profile.new(

View file

@ -21,7 +21,7 @@ describe Postzord::Receiver::Public do
xml = Salmon::Slap.create_by_user_and_activity(bob, comment.to_diaspora_xml).xml_for(nil)
person = bob.person
person.owner = nil
person.pod = Pod.find_or_create_by(url: AppConfig.pod_uri)
person.pod = Pod.find_or_create_by(url: "https://example.org/")
person.save
bob.destroy
comment.destroy

View file

@ -25,18 +25,46 @@ describe Pod, type: :model do
end
it "updates ssl boolean if upgraded to https" do
pod = Pod.find_or_create_by(url: "http://joindiaspora.com/")
pod = Pod.find_or_create_by(url: "http://example.org/")
expect(pod.ssl).to be false
pod = Pod.find_or_create_by(url: "https://joindiaspora.com/")
pod = Pod.find_or_create_by(url: "https://example.org/")
expect(pod.ssl).to be true
end
it "does not update ssl boolean if downgraded to http" do
pod = Pod.find_or_create_by(url: "https://joindiaspora.com/")
pod = Pod.find_or_create_by(url: "https://example.org/")
expect(pod.ssl).to be true
pod = Pod.find_or_create_by(url: "http://joindiaspora.com/")
pod = Pod.find_or_create_by(url: "http://example.org/")
expect(pod.ssl).to be true
end
context "validation" do
it "is valid" do
pod = Pod.find_or_create_by(url: "https://example.org/")
expect(pod).to be_valid
end
it "doesn't allow own pod" do
pod = Pod.find_or_create_by(url: AppConfig.url_to("/"))
expect(pod).not_to be_valid
end
it "doesn't allow own pod with default port" do
uri = URI.parse("https://example.org/")
allow(AppConfig).to receive(:pod_uri).and_return(uri)
pod = Pod.find_or_create_by(url: AppConfig.url_to("/"))
expect(pod).not_to be_valid
end
it "doesn't allow own pod with other scheme" do
uri = URI.parse("https://example.org/")
allow(AppConfig).to receive(:pod_uri).and_return(uri)
pod = Pod.find_or_create_by(url: "http://example.org/")
expect(pod).not_to be_valid
end
end
end
describe ".check_all!" do