From 814f7e0265b19eebde4bb9068d638256c2822b4d Mon Sep 17 00:00:00 2001 From: broothie Date: Sat, 23 Apr 2022 15:12:14 -0700 Subject: [PATCH 1/4] add message hourly deletion job --- .rspec | 1 + .ruby-version | 2 +- Gemfile | 13 +- Gemfile.lock | 259 +++++++++++------- app/jobs/message_deletion_job.rb | 21 ++ app/models/channel.rb | 2 +- app/models/user.rb | 20 +- config/sidekiq.yml | 4 + db/seeds.rb | 2 +- docker-compose.yml | 7 +- spec/factories/channels.rb | 6 + spec/factories/messages.rb | 7 + spec/factories/users.rb | 6 + spec/jobs/message_deletion_job_spec.rb | 49 ++++ spec/rails_helper.rb | 64 +++++ spec/spec_helper.rb | 94 +++++++ test/controllers/.keep | 0 .../api/channels_controller_test.rb | 7 - .../api/messages_controller_test.rb | 9 - .../api/sessions_controller_test.rb | 7 - .../api/subscriptions_controller_test.rb | 7 - test/controllers/api/users_controller_test.rb | 9 - .../static_pages_controller_test.rb | 9 - test/fixtures/.keep | 0 test/fixtures/channels.yml | 21 -- test/fixtures/files/.keep | 0 test/fixtures/messages.yml | 27 -- test/fixtures/subscriptions.yml | 20 -- test/fixtures/users.yml | 27 -- test/helpers/.keep | 0 test/integration/.keep | 0 test/mailers/.keep | 0 test/models/.keep | 0 test/models/channel_test.rb | 19 -- test/models/message_test.rb | 21 -- test/models/subscription_test.rb | 18 -- test/models/user_test.rb | 21 -- test/test_helper.rb | 10 - 38 files changed, 437 insertions(+), 352 deletions(-) create mode 100644 .rspec create mode 100644 app/jobs/message_deletion_job.rb create mode 100644 config/sidekiq.yml create mode 100644 spec/factories/channels.rb create mode 100644 spec/factories/messages.rb create mode 100644 spec/factories/users.rb create mode 100644 spec/jobs/message_deletion_job_spec.rb create mode 100644 spec/rails_helper.rb create mode 100644 spec/spec_helper.rb delete mode 100644 test/controllers/.keep delete mode 100644 test/controllers/api/channels_controller_test.rb delete mode 100644 test/controllers/api/messages_controller_test.rb delete mode 100644 test/controllers/api/sessions_controller_test.rb delete mode 100644 test/controllers/api/subscriptions_controller_test.rb delete mode 100644 test/controllers/api/users_controller_test.rb delete mode 100644 test/controllers/static_pages_controller_test.rb delete mode 100644 test/fixtures/.keep delete mode 100644 test/fixtures/channels.yml delete mode 100644 test/fixtures/files/.keep delete mode 100644 test/fixtures/messages.yml delete mode 100644 test/fixtures/subscriptions.yml delete mode 100644 test/fixtures/users.yml delete mode 100644 test/helpers/.keep delete mode 100644 test/integration/.keep delete mode 100644 test/mailers/.keep delete mode 100644 test/models/.keep delete mode 100644 test/models/channel_test.rb delete mode 100644 test/models/message_test.rb delete mode 100644 test/models/subscription_test.rb delete mode 100644 test/models/user_test.rb delete mode 100644 test/test_helper.rb diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..c99d2e7 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/.ruby-version b/.ruby-version index 2c9b4ef..49cdd66 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.7.3 +2.7.6 diff --git a/Gemfile b/Gemfile index 11310e6..cdb67be 100644 --- a/Gemfile +++ b/Gemfile @@ -1,10 +1,5 @@ source 'https://rubygems.org' -ruby '2.7.3' - -git_source(:github) do |repo_name| - repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") - "https://github.com/#{repo_name}.git" -end +ruby '2.7.6' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.0.4' @@ -45,6 +40,9 @@ gem 'rack-cors' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platform: :mri + + gem "rspec-rails", "~> 4.1" + gem "factory_bot_rails", "~> 6.2" end group :development do @@ -65,3 +63,6 @@ end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] + +gem "sidekiq", "~> 5.2" +gem "sidekiq-scheduler", "~> 3.2" diff --git a/Gemfile.lock b/Gemfile.lock index 57e515c..d892d3f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,58 +1,58 @@ GEM remote: https://rubygems.org/ specs: - actioncable (5.0.4) - actionpack (= 5.0.4) + actioncable (5.0.7.2) + actionpack (= 5.0.7.2) nio4r (>= 1.2, < 3.0) websocket-driver (~> 0.6.1) - actionmailer (5.0.4) - actionpack (= 5.0.4) - actionview (= 5.0.4) - activejob (= 5.0.4) + actionmailer (5.0.7.2) + actionpack (= 5.0.7.2) + actionview (= 5.0.7.2) + activejob (= 5.0.7.2) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 2.0) - actionpack (5.0.4) - actionview (= 5.0.4) - activesupport (= 5.0.4) + actionpack (5.0.7.2) + actionview (= 5.0.7.2) + activesupport (= 5.0.7.2) rack (~> 2.0) rack-test (~> 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) - actionview (5.0.4) - activesupport (= 5.0.4) + actionview (5.0.7.2) + activesupport (= 5.0.7.2) builder (~> 3.1) erubis (~> 2.7.0) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) - activejob (5.0.4) - activesupport (= 5.0.4) + activejob (5.0.7.2) + activesupport (= 5.0.7.2) globalid (>= 0.3.6) - activemodel (5.0.4) - activesupport (= 5.0.4) - activerecord (5.0.4) - activemodel (= 5.0.4) - activesupport (= 5.0.4) + activemodel (5.0.7.2) + activesupport (= 5.0.7.2) + activerecord (5.0.7.2) + activemodel (= 5.0.7.2) + activesupport (= 5.0.7.2) arel (~> 7.0) - activesupport (5.0.4) + activesupport (5.0.7.2) concurrent-ruby (~> 1.0, >= 1.0.2) - i18n (~> 0.7) + i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) - annotate (2.7.2) - activerecord (>= 3.2, < 6.0) - rake (>= 10.4, < 13.0) + annotate (3.2.0) + activerecord (>= 3.2, < 8.0) + rake (>= 10.4, < 14.0) arel (7.1.4) - bcrypt (3.1.16) - better_errors (2.1.1) + bcrypt (3.1.17) + better_errors (2.9.1) coderay (>= 1.0.0) - erubis (>= 2.6.6) + erubi (>= 1.0.0) rack (>= 0.9.0) - bindex (0.5.0) - binding_of_caller (0.7.2) + bindex (0.8.1) + binding_of_caller (1.0.0) debug_inspector (>= 0.0.1) builder (3.2.4) - byebug (9.0.6) - coderay (1.1.1) + byebug (11.1.3) + coderay (1.1.3) coffee-rails (4.2.2) coffee-script (>= 2.2.0) railties (>= 4.0.0) @@ -60,28 +60,42 @@ GEM coffee-script-source execjs coffee-script-source (1.12.2) - concurrent-ruby (1.1.8) + concurrent-ruby (1.1.10) + connection_pool (2.2.5) crass (1.0.6) - debug_inspector (0.0.3) - em-websocket (0.5.1) + debug_inspector (1.1.0) + diff-lcs (1.5.0) + e2mmap (0.1.0) + em-websocket (0.5.3) eventmachine (>= 0.12.9) - http_parser.rb (~> 0.6.0) + http_parser.rb (~> 0) + erubi (1.10.0) erubis (2.7.0) - eventmachine (1.2.3) - execjs (2.7.0) - faker (1.7.3) - i18n (~> 0.5) - ffi (1.15.1) - formatador (0.2.5) - globalid (0.4.2) - activesupport (>= 4.2.0) - guard (2.14.1) + et-orbi (1.2.7) + tzinfo + eventmachine (1.2.7) + execjs (2.8.1) + factory_bot (6.2.1) + activesupport (>= 5.0.0) + factory_bot_rails (6.2.0) + factory_bot (~> 6.2.0) + railties (>= 5.0.0) + faker (2.20.0) + i18n (>= 1.8.11, < 2) + ffi (1.15.5) + formatador (1.1.0) + fugit (1.5.3) + et-orbi (~> 1, >= 1.2.7) + raabro (~> 1.4) + globalid (1.0.0) + activesupport (>= 5.0) + guard (2.18.0) formatador (>= 0.2.4) listen (>= 2.7, < 4.0) - lumberjack (~> 1.0) + lumberjack (>= 1.0.12, < 2.0) nenv (~> 0.1) notiffany (~> 0.0) - pry (>= 0.9.12) + pry (>= 0.13.0) shellany (~> 0.0) thor (>= 0.18.1) guard-compat (1.2.1) @@ -90,13 +104,13 @@ GEM guard (~> 2.8) guard-compat (~> 1.0) multi_json (~> 1.8) - http_parser.rb (0.6.0) - i18n (0.9.5) + http_parser.rb (0.8.0) + i18n (1.10.0) concurrent-ruby (~> 1.0) - jbuilder (2.7.0) - activesupport (>= 4.2.0) - multi_json (>= 1.2) - jquery-rails (4.3.1) + jbuilder (2.11.5) + actionview (>= 5.0.0) + activesupport (>= 5.0.0) + jquery-rails (4.4.0) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) @@ -106,104 +120,133 @@ GEM listen (3.0.8) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) - loofah (2.9.1) + loofah (2.16.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) - lumberjack (1.0.12) - mail (2.6.6) - mime-types (>= 1.16, < 4) - method_source (0.8.2) - mime-types (3.1) - mime-types-data (~> 3.2015) - mime-types-data (3.2016.0521) - mini_portile2 (2.5.3) - minitest (5.14.4) - multi_json (1.12.1) + lumberjack (1.2.8) + mail (2.7.1) + mini_mime (>= 0.1.1) + method_source (1.0.0) + mini_mime (1.1.2) + minitest (5.15.0) + multi_json (1.15.0) nenv (0.3.0) - nio4r (2.1.0) - nokogiri (1.11.7) - mini_portile2 (~> 2.5.0) + nio4r (2.5.8) + nokogiri (1.13.4-x86_64-darwin) racc (~> 1.4) - notiffany (0.1.1) + notiffany (0.1.3) nenv (~> 0.1) shellany (~> 0.0) pg (0.21.0) - pry (0.10.4) - coderay (~> 1.1.0) - method_source (~> 0.8.1) - slop (~> 3.4) - pry-rails (0.3.6) + pry (0.14.1) + coderay (~> 1.1) + method_source (~> 1.0) + pry-rails (0.3.9) pry (>= 0.10.4) puma (3.12.6) - racc (1.5.2) + raabro (1.4.0) + racc (1.6.0) rack (2.2.3) rack-cors (1.1.1) rack (>= 2.0.0) - rack-livereload (0.3.16) + rack-livereload (0.3.17) + rack + rack-protection (2.2.0) rack rack-test (0.6.3) rack (>= 1.0) - rails (5.0.4) - actioncable (= 5.0.4) - actionmailer (= 5.0.4) - actionpack (= 5.0.4) - actionview (= 5.0.4) - activejob (= 5.0.4) - activemodel (= 5.0.4) - activerecord (= 5.0.4) - activesupport (= 5.0.4) - bundler (>= 1.3.0, < 2.0) - railties (= 5.0.4) + rails (5.0.7.2) + actioncable (= 5.0.7.2) + actionmailer (= 5.0.7.2) + actionpack (= 5.0.7.2) + actionview (= 5.0.7.2) + activejob (= 5.0.7.2) + activemodel (= 5.0.7.2) + activerecord (= 5.0.7.2) + activesupport (= 5.0.7.2) + bundler (>= 1.3.0) + railties (= 5.0.7.2) sprockets-rails (>= 2.0.0) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) - rails-html-sanitizer (1.3.0) + rails-html-sanitizer (1.4.2) loofah (~> 2.3) - railties (5.0.4) - actionpack (= 5.0.4) - activesupport (= 5.0.4) + railties (5.0.7.2) + actionpack (= 5.0.7.2) + activesupport (= 5.0.7.2) method_source rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) - rake (12.3.3) - rb-fsevent (0.10.2) - rb-inotify (0.9.10) - ffi (>= 0.5.0, < 2) - redis (3.3.3) - sass (3.5.1) + rake (13.0.6) + rb-fsevent (0.11.1) + rb-inotify (0.10.1) + ffi (~> 1.0) + redis (3.3.5) + rspec-core (3.11.0) + rspec-support (~> 3.11.0) + rspec-expectations (3.11.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.11.0) + rspec-mocks (3.11.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.11.0) + rspec-rails (4.1.2) + actionpack (>= 4.2) + activesupport (>= 4.2) + railties (>= 4.2) + rspec-core (~> 3.10) + rspec-expectations (~> 3.10) + rspec-mocks (~> 3.10) + rspec-support (~> 3.10) + rspec-support (3.11.0) + rufus-scheduler (3.8.1) + fugit (~> 1.1, >= 1.1.6) + sass (3.7.4) sass-listen (~> 4.0.0) sass-listen (4.0.0) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) - sass-rails (5.0.6) + sass-rails (5.0.7) railties (>= 4.0.0, < 6) sass (~> 3.1) sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) shellany (0.0.1) - slop (3.6.0) - spring (2.0.2) - activesupport (>= 4.2) + sidekiq (5.2.9) + connection_pool (~> 2.2, >= 2.2.2) + rack (~> 2.0) + rack-protection (>= 1.5.0) + redis (>= 3.3.5, < 4.2) + sidekiq-scheduler (3.2.0) + e2mmap + redis (>= 3, < 5) + rufus-scheduler (~> 3.2) + sidekiq (>= 3) + thwait + tilt (>= 1.4.0) + spring (2.1.1) spring-watcher-listen (2.0.1) listen (>= 2.7, < 4.0) spring (>= 1.2, < 3.0) sprockets (3.7.2) concurrent-ruby (~> 1.0) rack (> 1, < 3) - sprockets-rails (3.2.0) + sprockets-rails (3.2.2) actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) - thor (0.19.4) + thor (1.2.1) thread_safe (0.3.6) - tilt (2.0.7) + thwait (0.2.0) + e2mmap + tilt (2.0.10) tzinfo (1.2.9) thread_safe (~> 0.1) - uglifier (3.2.0) + uglifier (4.2.0) execjs (>= 0.3.0, < 3) - web-console (3.5.1) + web-console (3.7.0) actionview (>= 5.0) activemodel (>= 5.0) bindex (>= 0.4.0) @@ -213,7 +256,7 @@ GEM websocket-extensions (0.1.5) PLATFORMS - ruby + x86_64-darwin-20 DEPENDENCIES annotate @@ -222,6 +265,7 @@ DEPENDENCIES binding_of_caller byebug coffee-rails (~> 4.2) + factory_bot_rails (~> 6.2) faker guard-livereload (~> 2.5) jbuilder (~> 2.5) @@ -236,7 +280,10 @@ DEPENDENCIES rack-livereload rails (~> 5.0.4) redis (~> 3.0) + rspec-rails (~> 4.1) sass-rails (~> 5.0) + sidekiq (~> 5.2) + sidekiq-scheduler (~> 3.2) spring spring-watcher-listen (~> 2.0.0) tzinfo-data @@ -244,7 +291,7 @@ DEPENDENCIES web-console (>= 3.3.0) RUBY VERSION - ruby 2.7.3p183 + ruby 2.7.6p219 BUNDLED WITH - 1.14.6 + 2.3.12 diff --git a/app/jobs/message_deletion_job.rb b/app/jobs/message_deletion_job.rb new file mode 100644 index 0000000..8ebcd52 --- /dev/null +++ b/app/jobs/message_deletion_job.rb @@ -0,0 +1,21 @@ +class MessageDeletionJob + include Sidekiq::Worker + + def perform + message_surplus = Message.count - max_message_count + return if message_surplus <= 0 + + ids = Message + .order(created_at: :asc) + .limit(message_surplus) + .pluck(:id) + + Message.destroy(ids) + end + + private + + def max_message_count + ENV.fetch('MAX_MESSAGE_COUNT', 9000).to_i + end +end diff --git a/app/models/channel.rb b/app/models/channel.rb index ba0276a..828ae7c 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -45,7 +45,7 @@ def direct? end def with_smarter_child? - users.exists?(User.find_by(screenname: 'SmarterChild')) + users.include?(User.smarter_child) end def direct_with_smarter_child? diff --git a/app/models/user.rb b/app/models/user.rb index 833604c..ae605c7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -50,6 +50,16 @@ def self.find_by_credentials(screenname, password) return user if user && user.is_password?(password) end + def self.smarter_child + @smarter_child ||= + begin + smarter_child = User.find_by(screenname: 'SmarterChild') + smarter_child ||= User.create!(screenname: 'SmarterChild', password: SecureRandom.alphanumeric) + + smarter_child + end + end + def password=(password) @password = password self.password_digest = BCrypt::Password.create(password) @@ -66,6 +76,7 @@ def ensure_session_token def reset_session_token! self.session_token = SecureRandom.urlsafe_base64 self.save + self.session_token end @@ -74,16 +85,17 @@ def ensure_icon_url end def create_smarter_chat! - smarter_child = User.find_by(screenname: 'SmarterChild') + # smarter_child = User.find_by(screenname: 'SmarterChild') + # smarter_child ||= User.create(screenname: 'SmarterChild', password: SecureRandom.urlsafe_base64) smarter_chat = Channel.create( - name: [self, smarter_child].map(&:screenname).join(', '), - owner: smarter_child, + name: [self, User.smarter_child].map(&:screenname).join(', '), + owner: User.smarter_child, private: true ) smarter_chat.users << self - smarter_chat.users << smarter_child + smarter_chat.users << User.smarter_child end def check_profanity diff --git a/config/sidekiq.yml b/config/sidekiq.yml new file mode 100644 index 0000000..96c9a59 --- /dev/null +++ b/config/sidekiq.yml @@ -0,0 +1,4 @@ + +:schedule: + MessageDeletionJob: + cron: 0 * * * * diff --git a/db/seeds.rb b/db/seeds.rb index 81302cd..99bcb47 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,6 +1,6 @@ [Channel, Message, Subscription, User].each(&:destroy_all) -smarter_child = User.create(screenname: 'SmarterChild', password: 'drowssap') +# smarter_child = User.create(screenname: 'SmarterChild', password: 'drowssap') demo_user = User.create(screenname: 'demoUser', password: '123456') # Create world chat diff --git a/docker-compose.yml b/docker-compose.yml index 023e004..d97fe0d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,9 +7,14 @@ services: POSTGRES_USER: pguser POSTGRES_PASSWORD: pgpassword ports: - - 5432:5432 + - '5432:5432' volumes: - database:/var/lib/postgresql/data + redis: + image: redis:6 + ports: + - '6379:6379' + volumes: database: diff --git a/spec/factories/channels.rb b/spec/factories/channels.rb new file mode 100644 index 0000000..325a9cc --- /dev/null +++ b/spec/factories/channels.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :channel do + name { Faker::Movie.title } + owner factory: :user + end +end diff --git a/spec/factories/messages.rb b/spec/factories/messages.rb new file mode 100644 index 0000000..630611c --- /dev/null +++ b/spec/factories/messages.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :message do + body { Faker::TvShows::BojackHorseman.quote } + author factory: :user + channel + end +end diff --git a/spec/factories/users.rb b/spec/factories/users.rb new file mode 100644 index 0000000..bb52e90 --- /dev/null +++ b/spec/factories/users.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :user do + screenname { Faker::Internet.username(specifier: 6, separators: []) } + password { Faker::Internet.password(min_length: 6) } + end +end diff --git a/spec/jobs/message_deletion_job_spec.rb b/spec/jobs/message_deletion_job_spec.rb new file mode 100644 index 0000000..5b6f8e1 --- /dev/null +++ b/spec/jobs/message_deletion_job_spec.rb @@ -0,0 +1,49 @@ +require 'rails_helper' + +RSpec.describe MessageDeletionJob, type: :job do + let(:job) { MessageDeletionJob.new } + let(:perform!) { job.perform } + let(:channel) { FactoryBot.create(:channel) } + let(:max) { 10 } + let(:messages) { Array.new(count) { FactoryBot.create(:message, channel: channel) } } + + before do + allow(ENV).to receive(:fetch).with('MAX_MESSAGE_COUNT', 9000).and_return(max) + + messages + end + + context 'when no surplus' do + let(:count) { 5 } + + it 'deletes nothing' do + expect(Message).not_to receive(:destroy) + + expect { perform! }.not_to change { Message.count } + end + end + + context 'when surplus' do + let(:count) { 15 } + + it 'deletes message surplus' do + expect { perform! }.to change { Message.count }.from(count).to(max) + end + + it 'deletes the earliest messages' do + message_ids = messages.sort_by { |message| message.created_at }.map(&:id) + ids_to_be_deleted = message_ids.take(count - max) + + expect { perform! } + .to change { ids_to_be_deleted.all? { |id| Message.exists?(id) } }.from(true).to(false) + end + + it 'does not delete other messages' do + message_ids = messages.sort_by { |message| message.created_at }.map(&:id) + ids_to_be_deleted = message_ids.drop(count - max) + + expect { perform! } + .not_to change { ids_to_be_deleted.all? { |id| Message.exists?(id) } } + end + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000..00345af --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,64 @@ +# This file is copied to spec/ when you run 'rails generate rspec:install' +require 'spec_helper' +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../config/environment', __dir__) +# Prevent database truncation if the environment is production +abort("The Rails environment is running in production mode!") if Rails.env.production? +require 'rspec/rails' +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f } + +# Checks for pending migrations and applies them before tests are run. +# If you are not using ActiveRecord, you can remove these lines. +begin + ActiveRecord::Migration.maintain_test_schema! +rescue ActiveRecord::PendingMigrationError => e + puts e.to_s.strip + exit 1 +end +RSpec.configure do |config| + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{::Rails.root}/spec/fixtures" + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = true + + # You can uncomment this line to turn off ActiveRecord support entirely. + # config.use_active_record = false + + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, type: :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://relishapp.com/rspec/rspec-rails/docs + config.infer_spec_type_from_file_location! + + # Filter lines from Rails gems in backtraces. + config.filter_rails_from_backtrace! + # arbitrary gems may also be filtered via: + # config.filter_gems_from_backtrace("gem name") +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..a0d4080 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,94 @@ +# This file was generated by the `rails generate rspec:install` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # https://relishapp.com/rspec/rspec-core/docs/configuration/zero-monkey-patching-mode + config.disable_monkey_patching! + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = "doc" + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end diff --git a/test/controllers/.keep b/test/controllers/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/controllers/api/channels_controller_test.rb b/test/controllers/api/channels_controller_test.rb deleted file mode 100644 index e89dffb..0000000 --- a/test/controllers/api/channels_controller_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'test_helper' - -class Api::ChannelsControllerTest < ActionDispatch::IntegrationTest - # test "the truth" do - # assert true - # end -end diff --git a/test/controllers/api/messages_controller_test.rb b/test/controllers/api/messages_controller_test.rb deleted file mode 100644 index a40dc97..0000000 --- a/test/controllers/api/messages_controller_test.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'test_helper' - -class Api::MessagesControllerTest < ActionDispatch::IntegrationTest - test "should get create" do - get api_messages_create_url - assert_response :success - end - -end diff --git a/test/controllers/api/sessions_controller_test.rb b/test/controllers/api/sessions_controller_test.rb deleted file mode 100644 index d152721..0000000 --- a/test/controllers/api/sessions_controller_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'test_helper' - -class Api::SessionsControllerTest < ActionDispatch::IntegrationTest - # test "the truth" do - # assert true - # end -end diff --git a/test/controllers/api/subscriptions_controller_test.rb b/test/controllers/api/subscriptions_controller_test.rb deleted file mode 100644 index d4b8a65..0000000 --- a/test/controllers/api/subscriptions_controller_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'test_helper' - -class Api::SubscriptionsControllerTest < ActionDispatch::IntegrationTest - # test "the truth" do - # assert true - # end -end diff --git a/test/controllers/api/users_controller_test.rb b/test/controllers/api/users_controller_test.rb deleted file mode 100644 index 95bdd46..0000000 --- a/test/controllers/api/users_controller_test.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'test_helper' - -class Api::UsersControllerTest < ActionDispatch::IntegrationTest - test "should get create" do - get api_users_create_url - assert_response :success - end - -end diff --git a/test/controllers/static_pages_controller_test.rb b/test/controllers/static_pages_controller_test.rb deleted file mode 100644 index ea9a159..0000000 --- a/test/controllers/static_pages_controller_test.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'test_helper' - -class StaticPagesControllerTest < ActionDispatch::IntegrationTest - test "should get root" do - get static_pages_root_url - assert_response :success - end - -end diff --git a/test/fixtures/.keep b/test/fixtures/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/fixtures/channels.yml b/test/fixtures/channels.yml deleted file mode 100644 index b32f0e6..0000000 --- a/test/fixtures/channels.yml +++ /dev/null @@ -1,21 +0,0 @@ -# == Schema Information -# -# Table name: channels -# -# id :integer not null, primary key -# name :string not null -# created_at :datetime not null -# updated_at :datetime not null -# owner_id :integer default(1), not null -# private :boolean default(FALSE) -# - -# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html - -one: - name: MyString - team_id: 1 - -two: - name: MyString - team_id: 1 diff --git a/test/fixtures/files/.keep b/test/fixtures/files/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/fixtures/messages.yml b/test/fixtures/messages.yml deleted file mode 100644 index f87b86a..0000000 --- a/test/fixtures/messages.yml +++ /dev/null @@ -1,27 +0,0 @@ -# == Schema Information -# -# Table name: messages -# -# id :integer not null, primary key -# body :string not null -# timestamp :datetime not null -# author_id :integer not null -# channel_id :integer not null -# created_at :datetime not null -# updated_at :datetime not null -# reply_id :integer -# - -# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html - -one: - body: MyString - timestamp: 2017-07-19 11:25:08 - author_id: 1 - channel_id: 1 - -two: - body: MyString - timestamp: 2017-07-19 11:25:08 - author_id: 1 - channel_id: 1 diff --git a/test/fixtures/subscriptions.yml b/test/fixtures/subscriptions.yml deleted file mode 100644 index bd92e5b..0000000 --- a/test/fixtures/subscriptions.yml +++ /dev/null @@ -1,20 +0,0 @@ -# == Schema Information -# -# Table name: subscriptions -# -# id :integer not null, primary key -# user_id :integer not null -# channel_id :integer not null -# created_at :datetime not null -# updated_at :datetime not null -# - -# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html - -one: - user_id: MyString - channel_id: MyString - -two: - user_id: MyString - channel_id: MyString diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml deleted file mode 100644 index 6eb634b..0000000 --- a/test/fixtures/users.yml +++ /dev/null @@ -1,27 +0,0 @@ -# == Schema Information -# -# Table name: users -# -# id :integer not null, primary key -# screenname :string not null -# password_digest :string not null -# session_token :string not null -# icon_url :string -# created_at :datetime not null -# updated_at :datetime not null -# online :boolean default(FALSE) -# - -# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html - -one: - screenname: MyString - password_digest: MyString - session_token: MyString - icon_url: MyString - -two: - screenname: MyString - password_digest: MyString - session_token: MyString - icon_url: MyString diff --git a/test/helpers/.keep b/test/helpers/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/integration/.keep b/test/integration/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/mailers/.keep b/test/mailers/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/models/.keep b/test/models/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/models/channel_test.rb b/test/models/channel_test.rb deleted file mode 100644 index 9a94186..0000000 --- a/test/models/channel_test.rb +++ /dev/null @@ -1,19 +0,0 @@ -# == Schema Information -# -# Table name: channels -# -# id :integer not null, primary key -# name :string not null -# created_at :datetime not null -# updated_at :datetime not null -# owner_id :integer default(1), not null -# private :boolean default(FALSE) -# - -require 'test_helper' - -class ChannelTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end diff --git a/test/models/message_test.rb b/test/models/message_test.rb deleted file mode 100644 index 16e1318..0000000 --- a/test/models/message_test.rb +++ /dev/null @@ -1,21 +0,0 @@ -# == Schema Information -# -# Table name: messages -# -# id :integer not null, primary key -# body :string not null -# timestamp :datetime not null -# author_id :integer not null -# channel_id :integer not null -# created_at :datetime not null -# updated_at :datetime not null -# reply_id :integer -# - -require 'test_helper' - -class MessageTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end diff --git a/test/models/subscription_test.rb b/test/models/subscription_test.rb deleted file mode 100644 index cfb259d..0000000 --- a/test/models/subscription_test.rb +++ /dev/null @@ -1,18 +0,0 @@ -# == Schema Information -# -# Table name: subscriptions -# -# id :integer not null, primary key -# user_id :integer not null -# channel_id :integer not null -# created_at :datetime not null -# updated_at :datetime not null -# - -require 'test_helper' - -class SubscriptionTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end diff --git a/test/models/user_test.rb b/test/models/user_test.rb deleted file mode 100644 index 1c95c8e..0000000 --- a/test/models/user_test.rb +++ /dev/null @@ -1,21 +0,0 @@ -# == Schema Information -# -# Table name: users -# -# id :integer not null, primary key -# screenname :string not null -# password_digest :string not null -# session_token :string not null -# icon_url :string -# created_at :datetime not null -# updated_at :datetime not null -# online :boolean default(FALSE) -# - -require 'test_helper' - -class UserTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end diff --git a/test/test_helper.rb b/test/test_helper.rb deleted file mode 100644 index 92e39b2..0000000 --- a/test/test_helper.rb +++ /dev/null @@ -1,10 +0,0 @@ -ENV['RAILS_ENV'] ||= 'test' -require File.expand_path('../../config/environment', __FILE__) -require 'rails/test_help' - -class ActiveSupport::TestCase - # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. - fixtures :all - - # Add more helper methods to be used by all tests here... -end From 67db0ba096178629cd1306b079b8b8a4022c8dd4 Mon Sep 17 00:00:00 2001 From: broothie Date: Sat, 23 Apr 2022 15:16:09 -0700 Subject: [PATCH 2/4] remove commented out code --- app/models/user.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index ae605c7..f4c163f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -85,9 +85,6 @@ def ensure_icon_url end def create_smarter_chat! - # smarter_child = User.find_by(screenname: 'SmarterChild') - # smarter_child ||= User.create(screenname: 'SmarterChild', password: SecureRandom.urlsafe_base64) - smarter_chat = Channel.create( name: [self, User.smarter_child].map(&:screenname).join(', '), owner: User.smarter_child, From 4ac8bb83fee51d1380acdea4fb1cf7944c21e7e1 Mon Sep 17 00:00:00 2001 From: broothie Date: Sat, 23 Apr 2022 15:21:48 -0700 Subject: [PATCH 3/4] remove commented out code --- db/seeds.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/db/seeds.rb b/db/seeds.rb index 99bcb47..5660d6c 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,6 +1,5 @@ [Channel, Message, Subscription, User].each(&:destroy_all) -# smarter_child = User.create(screenname: 'SmarterChild', password: 'drowssap') demo_user = User.create(screenname: 'demoUser', password: '123456') # Create world chat From 7bd4d8d9a1abf26126b175f4b656c8c04bf04a76 Mon Sep 17 00:00:00 2001 From: broothie Date: Sat, 23 Apr 2022 15:26:27 -0700 Subject: [PATCH 4/4] refactor spec --- app/jobs/message_deletion_job.rb | 4 +++- spec/jobs/message_deletion_job_spec.rb | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/jobs/message_deletion_job.rb b/app/jobs/message_deletion_job.rb index 8ebcd52..2059c39 100644 --- a/app/jobs/message_deletion_job.rb +++ b/app/jobs/message_deletion_job.rb @@ -1,6 +1,8 @@ class MessageDeletionJob include Sidekiq::Worker + MAX_MESSAGE_COUNT = ENV.fetch('MAX_MESSAGE_COUNT', 9000).to_i + def perform message_surplus = Message.count - max_message_count return if message_surplus <= 0 @@ -16,6 +18,6 @@ def perform private def max_message_count - ENV.fetch('MAX_MESSAGE_COUNT', 9000).to_i + MAX_MESSAGE_COUNT end end diff --git a/spec/jobs/message_deletion_job_spec.rb b/spec/jobs/message_deletion_job_spec.rb index 5b6f8e1..75a92f9 100644 --- a/spec/jobs/message_deletion_job_spec.rb +++ b/spec/jobs/message_deletion_job_spec.rb @@ -8,7 +8,7 @@ let(:messages) { Array.new(count) { FactoryBot.create(:message, channel: channel) } } before do - allow(ENV).to receive(:fetch).with('MAX_MESSAGE_COUNT', 9000).and_return(max) + allow(job).to receive(:max_message_count).and_return(max) messages end