patch a few missing tests, deprecate server model, fix a few bugs

This commit is contained in:
Kyle Drake 2014-09-12 19:09:58 -07:00
parent 86990f3535
commit 50c1f67bdf
14 changed files with 90 additions and 49 deletions

View file

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

View file

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

View file

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

View file

@ -1,7 +0,0 @@
class Server < Sequel::Model
one_to_many :sites
def self.with_slots_available
where{slots_available > 0}.first
end
end

View file

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

View file

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

View file

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

View file

@ -38,7 +38,6 @@ 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'
@ -46,3 +45,7 @@ Fabrication.configure do |config|
end
I18n.enforce_available_locales = true
Mail.defaults do
delivery_method :test
end

9
tests/ext_tests.rb Normal file
View file

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

13
tests/tag_tests.rb Normal file
View file

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

View file

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

View file

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

View file

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

View file

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