From df29c4ca95213c3e2ee365f1ba672251ebbcfb5b Mon Sep 17 00:00:00 2001 From: f Date: Sat, 14 Aug 2021 20:06:44 -0300 Subject: [PATCH] Al cambiar el nombre del sitio no puede haber un dominio alternativo igual --- app/models/site/deployment.rb | 14 ++++++++++++++ config/locales/en.yml | 1 + config/locales/es.yml | 1 + test/models/site/deployment_test.rb | 25 +++++++++++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 test/models/site/deployment_test.rb diff --git a/app/models/site/deployment.rb b/app/models/site/deployment.rb index f88d2a0..b3b8898 100644 --- a/app/models/site/deployment.rb +++ b/app/models/site/deployment.rb @@ -17,6 +17,7 @@ class Site validates :name, hostname: true validates_presence_of :canonical_deploy validate :deploy_local_presence + validate :name_changed_is_unique_hostname, if: :name_changed? has_one :canonical_deploy, class_name: 'Deploy' has_many :deploys, dependent: :destroy @@ -68,6 +69,19 @@ class Site private + # Validar que al cambiar el nombre no estemos utilizando un + # hostname reservado por otro sitio. + # + # Al cambiar el nombre del DeployLocal se va a validar que el + # hostname nuevo sea único. + def name_changed_is_unique_hostname + deploy_local.hostname = nil + + return if deploy_local.valid? + + errors.add :name, I18n.t('activerecord.errors.models.site.attributes.name.duplicated_hostname') + end + # Si cambia el nombre queremos actualizarlo en el DeployLocal y # recargar el deploy canónico para tomar el nombre que # corresponda. diff --git a/config/locales/en.yml b/config/locales/en.yml index b59a5f2..27ffd43 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -147,6 +147,7 @@ en: attributes: name: no_subdomains: 'Name cannot contain dots' + duplicated_hostname: 'There already is a site with this address' deploys: deploy_local_presence: 'We need to be build the site!' design_id: diff --git a/config/locales/es.yml b/config/locales/es.yml index 68ee366..f98fd7d 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -147,6 +147,7 @@ es: attributes: name: no_subdomains: 'El nombre no puede contener puntos' + duplicated_hostname: 'Ya existe un sitio con ese nombre' deploys: deploy_local_presence: '¡Necesitamos poder generar el sitio!' design_id: diff --git a/test/models/site/deployment_test.rb b/test/models/site/deployment_test.rb new file mode 100644 index 0000000..8ec61e8 --- /dev/null +++ b/test/models/site/deployment_test.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'test_helper' + +class Site::DeploymentTest < ActiveSupport::TestCase + def site + @site ||= create :site + end + + teardown do + @site&.destroy + end + + test 'al publicar el sitio se crea el directorio' do + assert site.deploy_local.deploy + assert site.deploy_local.exist? + end + + test 'al cambiar el nombre no puede pisar un dominio que ya existe' do + site_pre = create :site + dup_name = "test-#{SecureRandom.hex}" + assert site_pre.deploys.create(type: 'DeployAlternativeDomain', hostname: "#{dup_name}.#{Site.domain}") + assert_not site.update(name: dup_name) + end +end