From b0f6131527a08b624da5cf4d49ec1b3de7b36c2c Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Tue, 4 Apr 2017 03:15:42 +0200 Subject: [PATCH] Remove active_record and save models in-memory Simple inmemory "database" to be independent from active_record. --- Gemfile | 8 -- Gemfile.lock | 10 -- Rakefile | 2 +- lib/tasks/tests.rake | 8 -- spec/factories.rb | 10 +- .../discovery/discovery_spec.rb | 4 + spec/spec_helper.rb | 6 -- test/dummy/app/models/entity.rb | 24 ++++- test/dummy/app/models/person.rb | 24 ++++- test/dummy/config/application.rb | 4 +- test/dummy/config/environments/development.rb | 10 +- test/dummy/config/environments/production.rb | 8 +- test/dummy/config/environments/test.rb | 6 +- .../initializers/diaspora_federation.rb | 12 +-- test/dummy/config/logging.rb | 93 ------------------- 15 files changed, 71 insertions(+), 158 deletions(-) delete mode 100644 lib/tasks/tests.rake delete mode 100644 test/dummy/config/logging.rb diff --git a/Gemfile b/Gemfile index e3dca6e..32843bb 100644 --- a/Gemfile +++ b/Gemfile @@ -59,12 +59,4 @@ group :development, :test do # unit tests gem "rspec-core", "~> 3.5.1" gem "rspec-rails", "~> 3.5.1" - - # test database - gem "sqlite3", "~> 1.3.11" -end - -group :development, :production do - # Logging (only for dummy-app, not for the gem) - gem "logging-rails", "0.5.0" end diff --git a/Gemfile.lock b/Gemfile.lock index 7e1b1ba..b007230 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -120,12 +120,6 @@ GEM rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) ruby_dep (~> 1.2) - little-plugger (1.1.4) - logging (2.2.0) - little-plugger (~> 1.1) - multi_json (~> 1.10) - logging-rails (0.5.0) - logging (>= 1.8) loofah (2.0.3) nokogiri (>= 1.5.9) lumberjack (1.0.11) @@ -139,7 +133,6 @@ GEM mime-types-data (3.2016.0521) mini_portile2 (2.1.0) minitest (5.10.1) - multi_json (1.12.1) multi_xml (0.6.0) multipart-post (2.0.0) nenv (0.3.0) @@ -265,7 +258,6 @@ GEM actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) - sqlite3 (1.3.13) systemu (2.6.5) terminal-table (1.7.3) unicode-display_width (~> 1.1.1) @@ -301,7 +293,6 @@ DEPENDENCIES guard-rspec guard-rubocop json-schema-rspec (= 0.0.4) - logging-rails (= 0.5.0) nyan-cat-formatter pronto (= 0.8.2) pronto-rubocop (= 0.8.0) @@ -317,7 +308,6 @@ DEPENDENCIES spring spring-commands-rspec spring-watcher-listen - sqlite3 (~> 1.3.11) webmock (~> 2.0) yard diff --git a/Rakefile b/Rakefile index b6dde4a..4e62d3b 100644 --- a/Rakefile +++ b/Rakefile @@ -22,5 +22,5 @@ Bundler::GemHelper.install_tasks name: "diaspora_federation" Rails.application.load_tasks -task test: %w(spec:prepare spec) +task test: :spec task default: :test diff --git a/lib/tasks/tests.rake b/lib/tasks/tests.rake deleted file mode 100644 index 18f001c..0000000 --- a/lib/tasks/tests.rake +++ /dev/null @@ -1,8 +0,0 @@ -if defined?(RSpec) - namespace :spec do - task prepare_db: %w(db:create db:test:load) - - desc "Prepare for rspec" - task prepare: Rails::VERSION::MAJOR == 5 ? %w(db:environment:set prepare_db) : %w(prepare_db) - end -end diff --git a/spec/factories.rb b/spec/factories.rb index 681052e..8bd3e67 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -17,26 +17,26 @@ Fabricator(:user, class_name: Person) do end Fabricator(:post, class_name: Entity) do - on_init { init_with(entity_type: "Post") } + on_init { init_with("Post") } author { Fabricate(:person) } end Fabricator(:comment, class_name: Entity) do - on_init { init_with(entity_type: "Comment") } + on_init { init_with("Comment") } author { Fabricate(:person) } end Fabricator(:poll, class_name: Entity) do - on_init { init_with(entity_type: "Poll") } + on_init { init_with("Poll") } author { Fabricate(:person) } end Fabricator(:event, class_name: Entity) do - on_init { init_with(entity_type: "Event") } + on_init { init_with("Event") } author { Fabricate(:person) } end Fabricator(:conversation, class_name: Entity) do - on_init { init_with(entity_type: "Conversation") } + on_init { init_with("Conversation") } author { Fabricate(:person) } end diff --git a/spec/lib/diaspora_federation/discovery/discovery_spec.rb b/spec/lib/diaspora_federation/discovery/discovery_spec.rb index 180523f..9ca8c24 100644 --- a/spec/lib/diaspora_federation/discovery/discovery_spec.rb +++ b/spec/lib/diaspora_federation/discovery/discovery_spec.rb @@ -54,6 +54,7 @@ module DiasporaFederation stub_request(:get, "http://localhost:3000/hcard/users/#{alice.guid}") .to_return(status: 200, body: hcard_html) + expect_callback(:save_person_after_webfinger, kind_of(Entities::Person)) person = Discovery::Discovery.new(account).fetch_and_save expect(person.guid).to eq(alice.guid) @@ -100,6 +101,7 @@ module DiasporaFederation stub_request(:get, "http://localhost:3000/hcard/users/#{alice.guid}") .to_return(status: 200, body: hcard_html) + expect_callback(:save_person_after_webfinger, kind_of(Entities::Person)) person = Discovery::Discovery.new(account).fetch_and_save expect(person.guid).to eq(alice.guid) @@ -116,6 +118,7 @@ module DiasporaFederation stub_request(:get, "http://localhost:3000/hcard/users/#{alice.guid}") .to_return(status: 200, body: hcard_html) + expect_callback(:save_person_after_webfinger, kind_of(Entities::Person)) person = Discovery::Discovery.new(account).fetch_and_save expect(person.guid).to eq(alice.guid) @@ -215,6 +218,7 @@ HTML stub_request(:get, "http://localhost:3000/hcard/users/#{alice.guid}") .to_return(status: 200, body: historic_hcard_html) + expect_callback(:save_person_after_webfinger, kind_of(Entities::Person)) person = Discovery::Discovery.new(account).fetch_and_save expect(person.guid).to eq(alice.guid) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 234cdf3..af47a81 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -63,16 +63,10 @@ RSpec.configure do |config| config.example_status_persistence_file_path = "spec/rspec-persistance.txt" - config.infer_spec_type_from_file_location! - - config.render_views - config.expect_with :rspec do |expect_config| expect_config.syntax = :expect end - config.use_transactional_fixtures = true - config.filter_run_excluding rails: (Rails::VERSION::MAJOR == 5 ? 4 : 5) # whitelist codeclimate.com so test coverage can be reported diff --git a/test/dummy/app/models/entity.rb b/test/dummy/app/models/entity.rb index d68e72d..b75ae0e 100644 --- a/test/dummy/app/models/entity.rb +++ b/test/dummy/app/models/entity.rb @@ -1,5 +1,23 @@ -class Entity < ActiveRecord::Base - include ::Diaspora::Guid +class Entity + attr_accessor :author, :guid + attr_reader :entity_type - belongs_to :author, class_name: "Person" + def initialize(entity_type) + @entity_type = entity_type + @guid = UUID.generate(:compact) + end + + def save! + Entity.database[entity_type][guid] = self + end + + class << self + def find_by(opts) + database[opts[:entity_type]][opts[:guid]] + end + + def database + @database ||= Hash.new({}) + end + end end diff --git a/test/dummy/app/models/person.rb b/test/dummy/app/models/person.rb index 00709a6..1fcfde6 100644 --- a/test/dummy/app/models/person.rb +++ b/test/dummy/app/models/person.rb @@ -1,5 +1,9 @@ -class Person < ActiveRecord::Base - include ::Diaspora::Guid +class Person + attr_accessor :diaspora_id, :url, :guid, :serialized_public_key, :serialized_private_key + + def initialize + @guid = UUID.generate(:compact) + end def private_key; OpenSSL::PKey::RSA.new(serialized_private_key) end def public_key; OpenSSL::PKey::RSA.new(serialized_public_key) end @@ -19,4 +23,20 @@ class Person < ActiveRecord::Base def full_name; "Dummy User" end def first_name; "Dummy" end def last_name; "User" end + + def save! + Person.database[:diaspora_id][diaspora_id] = self + Person.database[:guid][guid] = self + end + + class << self + def find_by(opts) + return database[:diaspora_id][opts[:diaspora_id]] if opts[:diaspora_id] + database[:guid][opts[:guid]] + end + + def database + @database ||= {diaspora_id: {}, guid: {}} + end + end end diff --git a/test/dummy/config/application.rb b/test/dummy/config/application.rb index b29a7e8..b3ddb09 100644 --- a/test/dummy/config/application.rb +++ b/test/dummy/config/application.rb @@ -1,6 +1,6 @@ require_relative "boot" -require "rails/all" +require "action_controller/railtie" Bundler.require(*Rails.groups) require "diaspora_federation/rails" @@ -20,7 +20,7 @@ module Dummy # config.i18n.default_locale = :de # Version of your assets, change this if you want to expire all your assets - config.assets.version = "1.0" + # config.assets.version = "1.0" # autoload files from test/dummy/lib config.autoload_once_paths += %W(#{config.root}/lib) diff --git a/test/dummy/config/environments/development.rb b/test/dummy/config/environments/development.rb index b885227..3554572 100644 --- a/test/dummy/config/environments/development.rb +++ b/test/dummy/config/environments/development.rb @@ -14,27 +14,27 @@ Rails.application.configure do config.action_controller.perform_caching = false # Don't care if the mailer can't send. - config.action_mailer.raise_delivery_errors = false + # config.action_mailer.raise_delivery_errors = false # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log # Raise an error on page load if there are pending migrations. - config.active_record.migration_error = :page_load + # config.active_record.migration_error = :page_load # Debug mode disables concatenation and preprocessing of assets. # This option may cause significant delays in view rendering with a large # number of complex assets. - config.assets.debug = true + # config.assets.debug = true # Asset digests allow you to set far-future HTTP expiration dates on all assets, # yet still be able to expire them through the digest params. - config.assets.digest = true + # config.assets.digest = true # Adds additional error checking when serving assets at runtime. # Checks for improperly declared sprockets dependencies. # Raises helpful error messages. - config.assets.raise_runtime_errors = true + # config.assets.raise_runtime_errors = true # Raises error for missing translations # config.action_view.raise_on_missing_translations = true diff --git a/test/dummy/config/environments/production.rb b/test/dummy/config/environments/production.rb index 577a255..08c8c8b 100644 --- a/test/dummy/config/environments/production.rb +++ b/test/dummy/config/environments/production.rb @@ -25,15 +25,15 @@ Rails.application.configure do config.serve_static_files = false # Compress JavaScripts and CSS. - config.assets.js_compressor = :uglifier + # config.assets.js_compressor = :uglifier # config.assets.css_compressor = :sass # Do not fallback to assets pipeline if a precompiled asset is missed. - config.assets.compile = false + # config.assets.compile = false # Asset digests allow you to set far-future HTTP expiration dates on all assets, # yet still be able to expire them through the digest params. - config.assets.digest = true + # config.assets.digest = true # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb @@ -81,5 +81,5 @@ Rails.application.configure do config.log_formatter = ::Logger::Formatter.new # Do not dump schema after migrations. - config.active_record.dump_schema_after_migration = false + # config.active_record.dump_schema_after_migration = false end diff --git a/test/dummy/config/environments/test.rb b/test/dummy/config/environments/test.rb index 76daa24..e7fbf43 100644 --- a/test/dummy/config/environments/test.rb +++ b/test/dummy/config/environments/test.rb @@ -34,13 +34,13 @@ Rails.application.configure do # Tell Action Mailer not to deliver emails to the real world. # The :test delivery method accumulates sent emails in the # ActionMailer::Base.deliveries array. - config.action_mailer.delivery_method = :test + # config.action_mailer.delivery_method = :test # Randomize the order test cases are executed. - config.active_support.test_order = :random + # config.active_support.test_order = :random # Print deprecation notices to the stderr. - config.active_support.deprecation = :stderr + # config.active_support.deprecation = :stderr # Raises error for missing translations # config.action_view.raise_on_missing_translations = true diff --git a/test/dummy/config/initializers/diaspora_federation.rb b/test/dummy/config/initializers/diaspora_federation.rb index 1442aab..d94c9a6 100644 --- a/test/dummy/config/initializers/diaspora_federation.rb +++ b/test/dummy/config/initializers/diaspora_federation.rb @@ -53,20 +53,16 @@ DiasporaFederation.configure do |config| end end - on :save_person_after_webfinger do |person| - unless Person.exists?(diaspora_id: person.diaspora_id) - Person.new(diaspora_id: person.diaspora_id, guid: person.guid, - serialized_public_key: person.exported_key, url: person.url).save! - end + on :save_person_after_webfinger do end on :fetch_private_key do |diaspora_id| - key = Person.where(diaspora_id: diaspora_id).pluck(:serialized_private_key).first - OpenSSL::PKey::RSA.new(key) unless key.nil? + person = Person.find_by(diaspora_id: diaspora_id) + OpenSSL::PKey::RSA.new(person.serialized_private_key) unless person.nil? end on :fetch_public_key do |diaspora_id| - key = Person.where(diaspora_id: diaspora_id).pluck(:serialized_public_key).first + key = Person.find_by(diaspora_id: diaspora_id).serialized_public_key key = DiasporaFederation::Discovery::Discovery.new(diaspora_id).fetch_and_save.exported_key if key.nil? OpenSSL::PKey::RSA.new(key) unless key.nil? end diff --git a/test/dummy/config/logging.rb b/test/dummy/config/logging.rb deleted file mode 100644 index 344865b..0000000 --- a/test/dummy/config/logging.rb +++ /dev/null @@ -1,93 +0,0 @@ -Logging::Rails.configure do |config| - # Configure the Logging framework with the default log levels - Logging.init %w(debug info warn error fatal) - - # Objects will be converted to strings using the :inspect method. - Logging.format_as :inspect - - # The default layout used by the appenders. - pattern = "[%d] %-5l PID-%p TID-%t %c: %m\n" - layout = Logging.layouts.pattern(pattern: pattern) - - # Setup a color scheme called 'bright' than can be used to add color codes - # to the pattern layout. Color schemes should only be used with appenders - # that write to STDOUT or STDERR; inserting terminal color codes into a file - # is generally considered bad form. - Logging.color_scheme( - "bright", - levels: { - info: :green, - warn: :yellow, - error: :red, - fatal: %i(white on_red) - }, - date: :blue, - logger: :cyan, - message: :magenta - ) - - # Configure an appender that will write log events to STDOUT. A colorized - # pattern layout is used to format the log events into strings before - # writing. - if config.log_to.include? "stdout" - Logging.appenders.stdout( - "stdout", - auto_flushing: true, - layout: Logging.layouts.pattern( - pattern: pattern, - color_scheme: "bright" - ) - ) - end - - # Configure an appender that will write log events to a file. The file will - # be rolled on a daily basis, and the past 7 rolled files will be kept. - # Older files will be deleted. The default pattern layout is used when - # formatting log events into strings. - if config.log_to.include? "file" - Logging.appenders.rolling_file( - "file", - filename: config.paths["log"].first, - keep: 7, - age: "daily", - truncate: false, - auto_flushing: true, - layout: layout - ) - end - - # Setup the root logger with the Rails log level and the desired set of - # appenders. The list of appenders to use should be set in the environment - # specific configuration file. - # - # For example, in a production application you would not want to log to - # STDOUT, but you would want to send an email for "error" and "fatal" - # messages: - # - # => config/environments/production.rb - # - # config.log_to = %w[file email] - # - # In development you would want to log to STDOUT and possibly to a file: - # - # => config/environments/development.rb - # - # config.log_to = %w[stdout file] - # - Logging.logger.root.appenders = config.log_to unless config.log_to.empty? - - # Default log-level (development=debug, production=info) - Logging.logger.root.level = config.log_level - - # log-levels for SQL and federation debug-logging - Logging.logger[ActiveRecord::Base].level = :debug - Logging.logger["XMLLogger"].level = :debug -end - -module ActiveSupport - module Dependencies - def self.logger=(_) - # This was remove in rails 5: https://github.com/rails/rails/commit/798dc5a92537ba4202a1a8e127a5ebdae87bc78d - end - end -end