Arreglar y agregar tests

This commit is contained in:
f 2021-08-07 18:25:54 -03:00
parent 4ea2169c68
commit b9d7a27105
7 changed files with 159 additions and 46 deletions

View file

@ -9,11 +9,11 @@ FactoryBot.define do
licencia licencia
after :build do |site| after :build do |site|
site.deploys << build(:deploy_local, site: site) # XXX: Generamos un DeployLocal normalmente y no a través de una
end # Factory porque necesitamos que el sitio se genere solo.
#
after :create do |site| # @see {https://github.com/thoughtbot/factory_bot/wiki/How-factory_bot-interacts-with-ActiveRecord}
site.deploys << create(:deploy_local, site: site) site.deploys.build(type: 'DeployLocal')
end end
end end
end end

View file

@ -2,11 +2,7 @@
class DeployJobTest < ActiveSupport::TestCase class DeployJobTest < ActiveSupport::TestCase
test 'se puede compilar' do test 'se puede compilar' do
rol = create :rol site = create :site
site = rol.site
site.deploys << create(:deploy_zip, site: site)
site.save
DeployJob.perform_async(site.id) DeployJob.perform_async(site.id)

View file

@ -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

View file

@ -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

View file

@ -1,24 +1,44 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'test_helper'
class DeployLocalTest < ActiveSupport::TestCase 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 test 'se puede deployear' do
site = create :site deploy_local = @site.deploy_local
local = create :deploy_local, site: site
deploy = create :deploy_zip, site: site
# Primero tenemos que generar el sitio assert deploy_local.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 test 'al eliminarlos se elimina el directorio' do
assert File.file?(deploy.path) deploy_local = @site.deploy_local
assert_equal 'application/zip', assert deploy_local.destroy
`file --mime-type "#{escaped_path}"`.split(' ').last assert_not File.directory?(deploy_local.destination)
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
end end
end end

View file

@ -3,17 +3,23 @@
require 'test_helper' require 'test_helper'
class DeployWwwTest < ActiveSupport::TestCase 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 test 'se puede deployear' do
site = create :site assert @site.deploy_local.deploy
local = create :deploy_local, site: site
deploy = create :deploy_www, site: site
# Primero tenemos que generar el sitio assert @deploy_www.deploy
local.deploy assert File.symlink?(@deploy_www.destination)
assert deploy.deploy
assert File.symlink?(deploy.destination)
local.destroy
end end
end end

View file

@ -3,18 +3,29 @@
require 'test_helper' require 'test_helper'
class DeployZipTest < ActiveSupport::TestCase 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 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 @deploy_zip.deploy
assert File.directory?(deploy_local.destination) assert File.file?(@deploy_zip.path)
assert File.exist?(File.join(deploy_local.destination, 'index.html')) assert_equal 'application/zip',
assert_equal 3, deploy_local.build_stats.count `file --mime-type "#{@deploy_zip.path}"`.split.last
assert_equal 1, @deploy_zip.build_stats.count
assert deploy_local.build_stats.map(&:bytes).compact.inject(:+).positive? assert @deploy_zip.build_stats.map(&:bytes).inject(:+).positive?
assert deploy_local.build_stats.map(&:seconds).compact.inject(:+).positive? assert @deploy_zip.build_stats.map(&:seconds).inject(:+).positive?
assert deploy_local.destroy
assert_not File.directory?(deploy_local.destination)
end end
end end