From b9d7a27105b794dc452117fe1fc032d2dde49f57 Mon Sep 17 00:00:00 2001 From: f Date: Sat, 7 Aug 2021 18:25:54 -0300 Subject: [PATCH] Arreglar y agregar tests --- test/factories/site.rb | 10 ++-- test/jobs/deploy_job_test.rb | 6 +-- test/models/deploy_alternative_domain_test.rb | 44 ++++++++++++++++ test/models/deploy_hidden_service_test.rb | 36 +++++++++++++ test/models/deploy_local_test.rb | 50 +++++++++++++------ test/models/deploy_www_test.rb | 26 ++++++---- test/models/deploy_zip_test.rb | 33 ++++++++---- 7 files changed, 159 insertions(+), 46 deletions(-) create mode 100644 test/models/deploy_alternative_domain_test.rb create mode 100644 test/models/deploy_hidden_service_test.rb diff --git a/test/factories/site.rb b/test/factories/site.rb index d9d8720f..7d91f841 100644 --- a/test/factories/site.rb +++ b/test/factories/site.rb @@ -9,11 +9,11 @@ FactoryBot.define do licencia after :build do |site| - site.deploys << build(:deploy_local, site: site) - end - - after :create do |site| - site.deploys << create(:deploy_local, site: site) + # XXX: Generamos un DeployLocal normalmente y no a través de una + # Factory porque necesitamos que el sitio se genere solo. + # + # @see {https://github.com/thoughtbot/factory_bot/wiki/How-factory_bot-interacts-with-ActiveRecord} + site.deploys.build(type: 'DeployLocal') end end end diff --git a/test/jobs/deploy_job_test.rb b/test/jobs/deploy_job_test.rb index a0fabfc4..d5ef694e 100644 --- a/test/jobs/deploy_job_test.rb +++ b/test/jobs/deploy_job_test.rb @@ -2,11 +2,7 @@ class DeployJobTest < ActiveSupport::TestCase test 'se puede compilar' do - rol = create :rol - site = rol.site - site.deploys << create(:deploy_zip, site: site) - - site.save + site = create :site DeployJob.perform_async(site.id) diff --git a/test/models/deploy_alternative_domain_test.rb b/test/models/deploy_alternative_domain_test.rb new file mode 100644 index 00000000..b7bd6d07 --- /dev/null +++ b/test/models/deploy_alternative_domain_test.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require 'test_helper' + +class DeployAlternativeDomainTest < ActiveSupport::TestCase + setup do + @site = create :site + @deploy_alt = @site.deploys.build type: 'DeployAlternativeDomain' + end + + teardown do + @site&.destroy + end + + def random_tld + PAK::ValidatesHostname::ALLOWED_TLDS.sample + end + + test 'el hostname se ingresa manualmente' do + assert_nil @deploy_alt.hostname + end + + test 'el hostname es obligatorio' do + assert_not @deploy_alt.valid? + end + + test 'el hostname es válido' do + assert_not @deploy_alt.update(hostname: ' ') + assert_not @deploy_alt.update(hostname: 'custom.domain.root.') + assert_not @deploy_alt.update(hostname: 'custom.domain') + assert @deploy_alt.update(hostname: "custom.domain.#{random_tld}") + end + + test 'el hostname tiene que ser único' do + assert_not @deploy_alt.update(hostname: @site.hostname) + end + + test 'se puede deployear' do + assert @site.deploy_local.deploy + assert @deploy_alt.update(hostname: "#{SecureRandom.hex}.sutty.#{random_tld}") + assert @deploy_alt.deploy + assert File.symlink?(@deploy_alt.destination) + end +end diff --git a/test/models/deploy_hidden_service_test.rb b/test/models/deploy_hidden_service_test.rb new file mode 100644 index 00000000..799f3df3 --- /dev/null +++ b/test/models/deploy_hidden_service_test.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require 'test_helper' + +class DeployHiddenServiceTest < ActiveSupport::TestCase + setup do + @site = create :site + @deploy_hidden = @site.deploys.build type: 'DeployHiddenService' + end + + teardown do + @site&.destroy + end + + test 'el hostname es válido' do + assert_not @deploy_hidden.update(hostname: ' ') + assert_not @deploy_hidden.update(hostname: 'custom.domain.root.') + assert_not @deploy_hidden.update(hostname: 'custom.domain') + assert @deploy_hidden.update(hostname: "#{@deploy_hidden.send(:random_base32, 56)}.onion") + end + + test 'los hostnames pueden ser temporales' do + assert @deploy_hidden.hostname.start_with? 'temporal' + end + + test 'el hostname tiene que ser único' do + assert @deploy_hidden.save + assert_not @site.deploys.create(type: 'DeployHiddenService', hostname: @deploy_hidden.hostname).valid? + end + + test 'se puede deployear' do + assert @site.deploy_local.deploy + assert @deploy_hidden.deploy + assert File.symlink?(@deploy_hidden.destination) + end +end diff --git a/test/models/deploy_local_test.rb b/test/models/deploy_local_test.rb index 7e8712d7..25781bac 100644 --- a/test/models/deploy_local_test.rb +++ b/test/models/deploy_local_test.rb @@ -1,24 +1,44 @@ # frozen_string_literal: true +require 'test_helper' + class DeployLocalTest < ActiveSupport::TestCase + setup do + @site = create :site + end + + teardown do + @site&.destroy + end + + test 'se pueden crear' do + assert @site.deploy_local.valid? + assert_equal @site.hostname, @site.deploy_local.hostname + end + + test 'no se puede cambiar el hostname' do + hostname = @site.deploy_local.hostname + @site.deploy_local.hostname = SecureRandom.hex + + assert @site.deploy_local.save + assert_equal hostname, @site.deploy_local.hostname + end + test 'se puede deployear' do - site = create :site - local = create :deploy_local, site: site - deploy = create :deploy_zip, site: site + deploy_local = @site.deploy_local - # Primero tenemos que generar el sitio - local.deploy + assert deploy_local.deploy + assert File.directory?(deploy_local.destination) + assert File.exist?(File.join(deploy_local.destination, 'index.html')) + assert_equal 3, deploy_local.build_stats.count - escaped_path = Shellwords.escape(deploy.path) + assert deploy_local.build_stats.map(&:bytes).compact.inject(:+).positive? + assert deploy_local.build_stats.map(&:seconds).compact.inject(:+).positive? + end - assert deploy.deploy - assert File.file?(deploy.path) - assert_equal 'application/zip', - `file --mime-type "#{escaped_path}"`.split(' ').last - assert_equal 1, deploy.build_stats.count - assert deploy.build_stats.map(&:bytes).inject(:+).positive? - assert deploy.build_stats.map(&:seconds).inject(:+).positive? - - local.destroy + test 'al eliminarlos se elimina el directorio' do + deploy_local = @site.deploy_local + assert deploy_local.destroy + assert_not File.directory?(deploy_local.destination) end end diff --git a/test/models/deploy_www_test.rb b/test/models/deploy_www_test.rb index 8ae00ebe..08ff8c38 100644 --- a/test/models/deploy_www_test.rb +++ b/test/models/deploy_www_test.rb @@ -3,17 +3,23 @@ require 'test_helper' class DeployWwwTest < ActiveSupport::TestCase + setup do + @site = create :site + @deploy_www = @site.deploys.create type: 'DeployWww' + end + + teardown do + @site&.destroy + end + + test 'el hostname empieza con www' do + assert @deploy_www.hostname.start_with?('www.') + end + test 'se puede deployear' do - site = create :site - local = create :deploy_local, site: site - deploy = create :deploy_www, site: site + assert @site.deploy_local.deploy - # Primero tenemos que generar el sitio - local.deploy - - assert deploy.deploy - assert File.symlink?(deploy.destination) - - local.destroy + assert @deploy_www.deploy + assert File.symlink?(@deploy_www.destination) end end diff --git a/test/models/deploy_zip_test.rb b/test/models/deploy_zip_test.rb index d5ffe51f..2e4e55f6 100644 --- a/test/models/deploy_zip_test.rb +++ b/test/models/deploy_zip_test.rb @@ -3,18 +3,29 @@ require 'test_helper' class DeployZipTest < ActiveSupport::TestCase + setup do + @site = create :site + @deploy_zip = @site.deploys.create(type: 'DeployZip') + end + + teardown do + @site&.destroy + end + + test 'el nombre es el hostname.zip' do + assert_equal "#{@site.hostname}.zip", @deploy_zip.hostname + end + test 'se puede deployear' do - deploy_local = create :deploy_local + # Primero tenemos que generar el sitio + assert @site.deploy_local.deploy - assert deploy_local.deploy - assert File.directory?(deploy_local.destination) - assert File.exist?(File.join(deploy_local.destination, 'index.html')) - assert_equal 3, deploy_local.build_stats.count - - assert deploy_local.build_stats.map(&:bytes).compact.inject(:+).positive? - assert deploy_local.build_stats.map(&:seconds).compact.inject(:+).positive? - - assert deploy_local.destroy - assert_not File.directory?(deploy_local.destination) + assert @deploy_zip.deploy + assert File.file?(@deploy_zip.path) + assert_equal 'application/zip', + `file --mime-type "#{@deploy_zip.path}"`.split.last + assert_equal 1, @deploy_zip.build_stats.count + assert @deploy_zip.build_stats.map(&:bytes).inject(:+).positive? + assert @deploy_zip.build_stats.map(&:seconds).inject(:+).positive? end end