diff --git a/environment.rb b/environment.rb index 5e3a904d..83f080ae 100644 --- a/environment.rb +++ b/environment.rb @@ -13,6 +13,7 @@ Bundler.require :development if ENV['RACK_ENV'] == 'development' Dir['./ext/**/*.rb'].each {|f| require f} +# :nocov: if ENV['TRAVIS'] $config = YAML.load_file File.join(DIR_ROOT, 'config.yml.travis') else @@ -23,31 +24,37 @@ else exit end end +# :nocov: DB = Sequel.connect $config['database'], sslmode: 'disable', max_connections: $config['database_pool'] DB.extension :pagination +# :nocov: if defined?(Pry) Pry.commands.alias_command 'c', 'continue' Pry.commands.alias_command 's', 'step' Pry.commands.alias_command 'n', 'next' Pry.commands.alias_command 'f', 'finish' end +# :nocov: Sidekiq::Logging.logger = nil unless ENV['RACK_ENV'] == 'production' sidekiq_redis_config = {namespace: 'neocitiesworker'} sidekiq_redis_config[:url] = $config['sidekiq_url'] if $config['sidekiq_url'] +# :nocov: Sidekiq.configure_server do |config| config.redis = sidekiq_redis_config end +# :nocov: Sidekiq.configure_client do |config| config.logger = nil config.redis = sidekiq_redis_config end +# :nocov: if $config['pubsub_url'] $pubsub_pool = ConnectionPool.new(size: 10, timeout: 5) { Redis.new url: $config['pubsub_url'] @@ -57,10 +64,7 @@ end if $config['pubsub_url'].nil? && ENV['RACK_ENV'] == 'production' raise 'pubsub_url is missing from config' end - -#require File.join(DIR_ROOT, 'workers', 'thumbnail_worker.rb') -#require File.join(DIR_ROOT, 'workers', 'screenshot_worker.rb') -#require File.join(DIR_ROOT, 'workers', 'email_worker.rb') +# :nocov: Sequel.datetime_class = Time Sequel.extension :core_extensions @@ -79,13 +83,6 @@ Dir.glob('workers/*.rb').each {|w| require File.join(DIR_ROOT, "/#{w}") } DB.loggers << Logger.new(STDOUT) if ENV['RACK_ENV'] == 'development' -if ENV['RACK_ENV'] == 'development' - # If new, throw up a random Server for development. - if Server.count == 0 - Server.create ip: '127.0.0.1', slots_available: 999999 - end -end - Mail.defaults do #options = { :address => "smtp.gmail.com", # :port => 587, @@ -108,7 +105,7 @@ Sass::Plugin.options[:template_location] = 'sass' Sass::Plugin.options[:css_location] = './public/css' Sass::Plugin.options[:style] = :nested -if ENV['RACK_ENV'] == 'production' +if ENV['RACK_ENV'] != 'development' Sass::Plugin.options[:style] = :compressed Sass::Plugin.options[:never_update] = true Sass::Plugin.options[:full_exception] = false diff --git a/ext/numeric.rb b/ext/numeric.rb index 372799b8..adc6b5b7 100644 --- a/ext/numeric.rb +++ b/ext/numeric.rb @@ -2,8 +2,4 @@ class Numeric def roundup(nearest=10) self % nearest == 0 ? self : self + nearest - (self % nearest) end - - def rounddown(nearest=10) - self % nearest == 0 ? self : self - (self % nearest) - end -end \ No newline at end of file +end diff --git a/migrations/045_remove_servers.rb b/migrations/045_remove_servers.rb new file mode 100644 index 00000000..1bda9152 --- /dev/null +++ b/migrations/045_remove_servers.rb @@ -0,0 +1,15 @@ +Sequel.migration do + up { + DB.drop_table :servers + } + + down { + DB.create_table! :servers do + primary_key :id + String :ip + Integer :slots_available + DateTime :created_at + DateTime :updated_at + end + } +end \ No newline at end of file diff --git a/models/server.rb b/models/server.rb deleted file mode 100644 index 6597e281..00000000 --- a/models/server.rb +++ /dev/null @@ -1,7 +0,0 @@ -class Server < Sequel::Model - one_to_many :sites - - def self.with_slots_available - where{slots_available > 0}.first - end -end \ No newline at end of file diff --git a/models/site.rb b/models/site.rb index 7da54a82..cf0cefb3 100644 --- a/models/site.rb +++ b/models/site.rb @@ -118,8 +118,6 @@ class Site < Sequel::Model site_mounting: false ) - many_to_one :server - many_to_many :tags one_to_many :profile_comments @@ -216,11 +214,6 @@ class Site < Sequel::Model @new_tags_string = tags_string end - def before_validation - self.server ||= Server.with_slots_available - super - end - def save(validate={}) DB.transaction do is_new = new? @@ -547,11 +540,6 @@ class Site < Sequel::Model add_tag Tag[name: name] || Tag.create(name: name) end - def after_create - DB['update servers set slots_available=slots_available-1 where id=?', self.server.id].first - super - end - def before_create self.email_confirmation_token = SecureRandom.hex 3 super @@ -570,10 +558,6 @@ class Site < Sequel::Model def validate super - if server.nil? - errors.add :over_capacity, 'We are currently at capacity, and cannot create your home page. We will fix this shortly. Please come back later and try again, our apologies.' - end - if !self.class.valid_username?(values[:username]) errors.add :username, 'A valid user/site name is required.' end diff --git a/models/tag.rb b/models/tag.rb index 25b200ab..f709989e 100644 --- a/models/tag.rb +++ b/models/tag.rb @@ -9,7 +9,7 @@ class Tag < Sequel::Model end def self.create_unless_exists(name) - dataset.filter(name: name).first || create(name: name) + dataset.filter(name: name.downcase).first || create(name: name) end def self.suggestions(name, limit=3) diff --git a/tests/acceptance/settings_tests.rb b/tests/acceptance/settings_tests.rb index e521d9f3..7a09cc1c 100644 --- a/tests/acceptance/settings_tests.rb +++ b/tests/acceptance/settings_tests.rb @@ -79,7 +79,7 @@ def generate_ssl_certs(opts={}) combined_cert_tmpfile.close res[:combined_cert_path] = combined_cert_tmpfile.path - bad_combined_cert_tmpfile = Tempfile.new 'combined_cert' + bad_combined_cert_tmpfile = Tempfile.new 'bad_combined_cert' bad_combined_cert_tmpfile.write "#{File.read(res[:cert_intermediate_path])}\n#{File.read(res[:cert_path])}" bad_combined_cert_tmpfile.close res[:bad_combined_cert_path] = bad_combined_cert_tmpfile.path diff --git a/tests/environment.rb b/tests/environment.rb index b6662f43..7edf4983 100644 --- a/tests/environment.rb +++ b/tests/environment.rb @@ -38,11 +38,14 @@ Sequel.extension :migration Sequel::Migrator.apply DB, './migrations', 0 Sequel::Migrator.apply DB, './migrations' -Server.create ip: '127.0.0.1', slots_available: 999999 Fabrication.configure do |config| config.fabricator_path = 'tests/fabricators' config.path_prefix = DIR_ROOT end -I18n.enforce_available_locales = true \ No newline at end of file +I18n.enforce_available_locales = true + +Mail.defaults do + delivery_method :test +end \ No newline at end of file diff --git a/tests/ext_tests.rb b/tests/ext_tests.rb new file mode 100644 index 00000000..f61e3987 --- /dev/null +++ b/tests/ext_tests.rb @@ -0,0 +1,9 @@ +require_relative './environment.rb' + +describe Time do + describe 'ago' do + it 'should return the modified value' do + Time.now.ago.must_equal 'just now' + end + end +end \ No newline at end of file diff --git a/tests/tag_tests.rb b/tests/tag_tests.rb new file mode 100644 index 00000000..89ac8c48 --- /dev/null +++ b/tests/tag_tests.rb @@ -0,0 +1,13 @@ +require_relative './environment.rb' + +describe Tag do + describe 'creation' do + it 'should force downcase' do + tag_name = SecureRandom.hex(10).downcase + Tag.create_unless_exists tag_name + Tag[name: tag_name].wont_be_nil + Tag.create_unless_exists tag_name.upcase + Tag.filter(name: tag_name).count.must_equal 1 + end + end +end \ No newline at end of file diff --git a/tests/workers/email_worker_tests.rb b/tests/workers/email_worker_tests.rb new file mode 100644 index 00000000..3b168429 --- /dev/null +++ b/tests/workers/email_worker_tests.rb @@ -0,0 +1,23 @@ +require_relative '../environment.rb' + +describe EmailWorker do + before do + Mail::TestMailer.deliveries.clear + end + + it 'sends an email' do + worker = EmailWorker.new + worker.perform({ + 'from' => 'from@example.com', + 'to' => 'to@example.com', + 'subject' => 'Hello World', + 'body' => 'testing' + }) + + mail = Mail::TestMailer.deliveries.first + mail.from.first.must_equal 'from@example.com' + mail.to.first.must_equal 'to@example.com' + mail.subject.must_equal 'Hello World' + mail.body.to_s.must_equal 'testing' + end +end \ No newline at end of file diff --git a/workers/email_worker.rb b/workers/email_worker.rb index ae781616..e4980255 100644 --- a/workers/email_worker.rb +++ b/workers/email_worker.rb @@ -4,11 +4,12 @@ class EmailWorker def perform(args={}) Mail.deliver do - from args['from'] - reply_to args['reply_to'] - to args['to'] - subject args['subject'] - body args['body'] + # TODO this is not doing UTF-8 properly. + from args['from'] + reply_to args['reply_to'] + to args['to'] + subject args['subject'] + body args['body'] end end end diff --git a/workers/purge_cache_worker.rb b/workers/purge_cache_worker.rb index 461c92a7..4d053423 100644 --- a/workers/purge_cache_worker.rb +++ b/workers/purge_cache_worker.rb @@ -2,7 +2,9 @@ class PurgeCacheWorker include Sidekiq::Worker sidekiq_options queue: :purgecache, retry: 10, backtrace: true + def perform(payload) + # :nocov: attempt = 0 begin attempt += 1 @@ -15,5 +17,6 @@ class PurgeCacheWorker sleep 1 retry end + # :nocov: end end \ No newline at end of file diff --git a/workers/screenshot_worker.rb b/workers/screenshot_worker.rb index e18e28c4..d62cf7b3 100644 --- a/workers/screenshot_worker.rb +++ b/workers/screenshot_worker.rb @@ -16,6 +16,7 @@ module Phantomjs wait_thr.join return stdout.read end + # :nocov: rescue Timeout::Error stdin.close stdout.close @@ -23,6 +24,7 @@ module Phantomjs Process.kill 'QUIT', pid raise Timeout::Error end + # :nocov: end end @@ -45,6 +47,7 @@ class ScreenshotWorker height: 720 ) rescue Timeout::Error + # :nocov: puts "#{username}/#{path} is timing out, discontinuing" site = Site[username: username] site.update is_crashing: true @@ -68,6 +71,7 @@ class ScreenshotWorker end =end return + # :nocov: end img_list = Magick::ImageList.new