From b0b0a11c307ad0243fe659111b25e4c710512c69 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Sat, 12 Apr 2025 13:06:32 +0200 Subject: [PATCH] Fix few errors --- Gemfile | 1 + Gemfile.lock | 1 + app/services/gpx/track_importer.rb | 2 + .../settings/subscriptions/index.html.erb | 2 +- ...0808133112_run_initial_visit_suggestion.rb | 4 +- spec/requests/sidekiq_spec.rb | 52 +++++++++++++++++++ 6 files changed, 60 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index ec872bfe..1b6b8ece 100644 --- a/Gemfile +++ b/Gemfile @@ -27,6 +27,7 @@ gem 'activerecord-postgis-adapter' gem 'puma' gem 'pundit' gem 'rails', '~> 8.0' +gem 'rexml' gem 'rgeo' gem 'rgeo-activerecord' gem 'rswag-api' diff --git a/Gemfile.lock b/Gemfile.lock index ff5a3ab7..bdd2bb76 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -506,6 +506,7 @@ DEPENDENCIES pundit rails (~> 8.0) redis + rexml rgeo rgeo-activerecord rspec-rails diff --git a/app/services/gpx/track_importer.rb b/app/services/gpx/track_importer.rb index 9abd1a56..24dd2798 100644 --- a/app/services/gpx/track_importer.rb +++ b/app/services/gpx/track_importer.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'rexml/document' + class Gpx::TrackImporter include Imports::Broadcaster diff --git a/app/views/settings/subscriptions/index.html.erb b/app/views/settings/subscriptions/index.html.erb index eeb23dad..9342fcde 100644 --- a/app/views/settings/subscriptions/index.html.erb +++ b/app/views/settings/subscriptions/index.html.erb @@ -7,7 +7,7 @@

Hello there!

- <% if current_user.active_until.future? %> + <% if current_user.active_until&.future? %>

You are currently subscribed to Dawarich, hurray!

diff --git a/db/data/20240808133112_run_initial_visit_suggestion.rb b/db/data/20240808133112_run_initial_visit_suggestion.rb index 46312397..33af8048 100644 --- a/db/data/20240808133112_run_initial_visit_suggestion.rb +++ b/db/data/20240808133112_run_initial_visit_suggestion.rb @@ -5,7 +5,9 @@ class RunInitialVisitSuggestion < ActiveRecord::Migration[7.1] start_at = 30.years.ago end_at = Time.current - VisitSuggestingJob.perform_later(start_at:, end_at:) + User.find_each do |user| + VisitSuggestingJob.perform_later(user_id: user.id, start_at:, end_at:) + end end def down diff --git a/spec/requests/sidekiq_spec.rb b/spec/requests/sidekiq_spec.rb index b1dbca16..0fc2d1fe 100644 --- a/spec/requests/sidekiq_spec.rb +++ b/spec/requests/sidekiq_spec.rb @@ -1,11 +1,24 @@ # frozen_string_literal: true require 'rails_helper' +require 'sidekiq/web' RSpec.describe '/sidekiq', type: :request do + before do + # Allow any ENV key to be accessed and return nil by default + allow(ENV).to receive(:[]).and_return(nil) + + # Stub Sidekiq::Web with a simple Rack app for testing + allow(Sidekiq::Web).to receive(:call) do |_env| + [200, { 'Content-Type' => 'text/html' }, ['Sidekiq Web UI']] + end + end + context 'when Dawarich is in self-hosted mode' do before do allow(DawarichSettings).to receive(:self_hosted?).and_return(true) + allow(ENV).to receive(:[]).with('SIDEKIQ_USERNAME').and_return(nil) + allow(ENV).to receive(:[]).with('SIDEKIQ_PASSWORD').and_return(nil) end context 'when user is not authenticated' do @@ -48,6 +61,8 @@ RSpec.describe '/sidekiq', type: :request do context 'when Dawarich is not in self-hosted mode' do before do allow(DawarichSettings).to receive(:self_hosted?).and_return(false) + allow(ENV).to receive(:[]).with('SIDEKIQ_USERNAME').and_return(nil) + allow(ENV).to receive(:[]).with('SIDEKIQ_PASSWORD').and_return(nil) Rails.application.reload_routes! end @@ -70,4 +85,41 @@ RSpec.describe '/sidekiq', type: :request do end end end + + context 'when SIDEKIQ_USERNAME and SIDEKIQ_PASSWORD are set' do + before do + allow(DawarichSettings).to receive(:self_hosted?).and_return(false) + allow(ENV).to receive(:[]).with('SIDEKIQ_USERNAME').and_return('admin') + allow(ENV).to receive(:[]).with('SIDEKIQ_PASSWORD').and_return('password') + end + + context 'when user is not authenticated' do + it 'redirects to sign in page' do + get sidekiq_url + + expect(response).to redirect_to('/users/sign_in') + end + end + + context 'when user is not admin' do + before { sign_in create(:user) } + + it 'redirects to root page' do + get sidekiq_url + + expect(response).to redirect_to(root_url) + expect(flash[:error]).to eq('You are not authorized to perform this action.') + end + end + + context 'when user is admin' do + before { sign_in create(:user, :admin) } + + it 'renders a successful response' do + get sidekiq_url + + expect(response).to be_successful + end + end + end end