Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

🔧 Use settings class instead environment checks #187

Merged
merged 1 commit into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Layout/MultilineMethodCallBraceLayout:
Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented

Layout/MultilineOperationIndentation:
EnforcedStyle: indented

Lint/MissingSuper:
Enabled: false

Expand All @@ -51,6 +54,7 @@ Metrics/BlockLength:
- "spec/**/*"
- "config/environments/*"
- "config/routes.rb"
- "config/initializers/devise.rb"

Metrics/MethodLength:
CountAsOne: ["array", "hash", "heredoc"]
Expand Down
6 changes: 2 additions & 4 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

module Users
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
skip_before_action :verify_authenticity_token, only: :developer unless Rails.env.production? # rubocop:disable Rails/LexicallyScopedActionFilter

%i[twitter discord developer].each do |provider|
next if provider == :developer && Rails.env.production?
skip_before_action :verify_authenticity_token, only: :developer # rubocop:disable Rails/LexicallyScopedActionFilter

Devise.omniauth_providers.each do |provider|
define_method provider do
result = Users::GetFromOmniauth.result(auth: request.env["omniauth.auth"])

Expand Down
2 changes: 1 addition & 1 deletion app/operations/admin/import_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def call
wrap_task("Import items") { RawItems::Import.call }
wrap_task("Import prices") { Prices::Import.call }
wrap_task("Update flags") { Items::UpdateFlags.call }
wrap_task("Send Telegram notifications") { EventDispatches::SendToTelegram.call } if Rails.env.production?
wrap_task("Send Telegram notifications") { EventDispatches::SendToTelegram.call }
end

private
Expand Down
2 changes: 2 additions & 0 deletions app/operations/event_dispatches/send_to_telegram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class SendToTelegram < Actor
input :client, type: TelegramClient, default: -> { TelegramClient.new }

def call
fail! unless Settings.enable_telegram_notifications?

pending_dispatches_grouped_by_item_id.each_with_index do |(_item_id, dispatches), index|
send_message(dispatches, index)
sleep 1
Expand Down
18 changes: 11 additions & 7 deletions app/views/devise/sessions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@
<h2 class="card-title"><%= t(".sign_in") %></h2>

<div class="flex flex-col gap-5 text-center">
<%= button_to user_twitter_omniauth_authorize_path, method: :post, "data-turbo": false, class: "btn btn-block gap-4 text-white bg-[#1D9BF9] hover:bg-[#1D9BF9]/80" do %>
<svg xmlns="http://www.w3.org/2000/svg" class="fill-white" viewBox="0 0 24 24" width="24" height="24"><path fill="none" d="M0 0h24v24H0z"/><path d="M22.162 5.656a8.384 8.384 0 0 1-2.402.658A4.196 4.196 0 0 0 21.6 4c-.82.488-1.719.83-2.656 1.015a4.182 4.182 0 0 0-7.126 3.814 11.874 11.874 0 0 1-8.62-4.37 4.168 4.168 0 0 0-.566 2.103c0 1.45.738 2.731 1.86 3.481a4.168 4.168 0 0 1-1.894-.523v.052a4.185 4.185 0 0 0 3.355 4.101 4.21 4.21 0 0 1-1.89.072A4.185 4.185 0 0 0 7.97 16.65a8.394 8.394 0 0 1-6.191 1.732 11.83 11.83 0 0 0 6.41 1.88c7.693 0 11.9-6.373 11.9-11.9 0-.18-.005-.362-.013-.54a8.496 8.496 0 0 0 2.087-2.165z"/></svg>
<%= t(".sign_in_with", provider: "Twitter") %>
<% if Settings.enable_twitter_sign_in? %>
<%= button_to user_twitter_omniauth_authorize_path, method: :post, "data-turbo": false, class: "btn btn-block gap-4 text-white bg-[#1D9BF9] hover:bg-[#1D9BF9]/80" do %>
<svg xmlns="http://www.w3.org/2000/svg" class="fill-white" viewBox="0 0 24 24" width="24" height="24"><path fill="none" d="M0 0h24v24H0z"/><path d="M22.162 5.656a8.384 8.384 0 0 1-2.402.658A4.196 4.196 0 0 0 21.6 4c-.82.488-1.719.83-2.656 1.015a4.182 4.182 0 0 0-7.126 3.814 11.874 11.874 0 0 1-8.62-4.37 4.168 4.168 0 0 0-.566 2.103c0 1.45.738 2.731 1.86 3.481a4.168 4.168 0 0 1-1.894-.523v.052a4.185 4.185 0 0 0 3.355 4.101 4.21 4.21 0 0 1-1.89.072A4.185 4.185 0 0 0 7.97 16.65a8.394 8.394 0 0 1-6.191 1.732 11.83 11.83 0 0 0 6.41 1.88c7.693 0 11.9-6.373 11.9-11.9 0-.18-.005-.362-.013-.54a8.496 8.496 0 0 0 2.087-2.165z"/></svg>
<%= t(".sign_in_with", provider: "Twitter") %>
<% end %>
<% end %>

<%= button_to user_discord_omniauth_authorize_path, method: :post, "data-turbo": false, class: "btn btn-block gap-4 text-white bg-[#7289DA] hover:bg-[#7289DA]/80" do %>
<svg xmlns="http://www.w3.org/2000/svg" class="fill-white" viewBox="0 0 24 24" width="24" height="24"><path fill="none" d="M0 0h24v24H0z"/><path d="M10.076 11c.6 0 1.086.45 1.075 1 0 .55-.474 1-1.075 1C9.486 13 9 12.55 9 12s.475-1 1.076-1zm3.848 0c.601 0 1.076.45 1.076 1s-.475 1-1.076 1c-.59 0-1.075-.45-1.075-1s.474-1 1.075-1zm4.967-9C20.054 2 21 2.966 21 4.163V23l-2.211-1.995-1.245-1.176-1.317-1.25.546 1.943H5.109C3.946 20.522 3 19.556 3 18.359V4.163C3 2.966 3.946 2 5.109 2H18.89zm-3.97 13.713c2.273-.073 3.148-1.596 3.148-1.596 0-3.381-1.482-6.122-1.482-6.122-1.48-1.133-2.89-1.102-2.89-1.102l-.144.168c1.749.546 2.561 1.334 2.561 1.334a8.263 8.263 0 0 0-3.096-1.008 8.527 8.527 0 0 0-2.077.02c-.062 0-.114.011-.175.021-.36.032-1.235.168-2.335.662-.38.178-.607.305-.607.305s.854-.83 2.705-1.376l-.103-.126s-1.409-.031-2.89 1.103c0 0-1.481 2.74-1.481 6.121 0 0 .864 1.522 3.137 1.596 0 0 .38-.472.69-.871-1.307-.4-1.8-1.24-1.8-1.24s.102.074.287.179c.01.01.02.021.041.031.031.022.062.032.093.053.257.147.514.262.75.357.422.168.926.336 1.513.452a7.06 7.06 0 0 0 2.664.01 6.666 6.666 0 0 0 1.491-.451c.36-.137.761-.337 1.183-.62 0 0-.514.861-1.862 1.25.309.399.68.85.68.85z"/></svg>
<%= t(".sign_in_with", provider: "Discord") %>
<% if Settings.enable_discord_sign_in? %>
<%= button_to user_discord_omniauth_authorize_path, method: :post, "data-turbo": false, class: "btn btn-block gap-4 text-white bg-[#7289DA] hover:bg-[#7289DA]/80" do %>
<svg xmlns="http://www.w3.org/2000/svg" class="fill-white" viewBox="0 0 24 24" width="24" height="24"><path fill="none" d="M0 0h24v24H0z"/><path d="M10.076 11c.6 0 1.086.45 1.075 1 0 .55-.474 1-1.075 1C9.486 13 9 12.55 9 12s.475-1 1.076-1zm3.848 0c.601 0 1.076.45 1.076 1s-.475 1-1.076 1c-.59 0-1.075-.45-1.075-1s.474-1 1.075-1zm4.967-9C20.054 2 21 2.966 21 4.163V23l-2.211-1.995-1.245-1.176-1.317-1.25.546 1.943H5.109C3.946 20.522 3 19.556 3 18.359V4.163C3 2.966 3.946 2 5.109 2H18.89zm-3.97 13.713c2.273-.073 3.148-1.596 3.148-1.596 0-3.381-1.482-6.122-1.482-6.122-1.48-1.133-2.89-1.102-2.89-1.102l-.144.168c1.749.546 2.561 1.334 2.561 1.334a8.263 8.263 0 0 0-3.096-1.008 8.527 8.527 0 0 0-2.077.02c-.062 0-.114.011-.175.021-.36.032-1.235.168-2.335.662-.38.178-.607.305-.607.305s.854-.83 2.705-1.376l-.103-.126s-1.409-.031-2.89 1.103c0 0-1.481 2.74-1.481 6.121 0 0 .864 1.522 3.137 1.596 0 0 .38-.472.69-.871-1.307-.4-1.8-1.24-1.8-1.24s.102.074.287.179c.01.01.02.021.041.031.031.022.062.032.093.053.257.147.514.262.75.357.422.168.926.336 1.513.452a7.06 7.06 0 0 0 2.664.01 6.666 6.666 0 0 0 1.491-.451c.36-.137.761-.337 1.183-.62 0 0-.514.861-1.862 1.25.309.399.68.85.68.85z"/></svg>
<%= t(".sign_in_with", provider: "Discord") %>
<% end %>
<% end %>

<% unless Rails.env.production? %>
<% if Settings.enable_dev_sign_in? %>
<%= button_to user_developer_omniauth_authorize_path, method: :post, "data-turbo": false, class: "btn btn-block gap-4" do %>
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 stroke-current" fill="none" viewBox="0 0 24 24" stroke-width="2"> <path stroke-linecap="round" stroke-linejoin="round" d="M8 9l3 3-3 3m5 0h3M5 20h14a2 2 0 002-2V6a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" /> </svg>
<%= t(".sign_in_with", provider: "Developer") %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<%= stylesheet_link_tag "application", media: "all", "data-turbo-track": "reload" %>
<%= javascript_include_tag "application", defer: true, "data-turbo-track": "reload" %>

<% if Rails.env.production? %>
<% if Settings.enable_analytics? %>
<script
data-domain="<%= Rails.application.credentials.plausible_data_domain %>"
src="<%= Rails.application.credentials.plausible_src %>"
Expand Down
4 changes: 4 additions & 0 deletions bin/setup_dev_credentials
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ CONTENT="secret_key_base: $(rake secret)
# TO ENABLE SIGN IN WITH DISCORD. (OPTIONAL)
# discord_app_id: <FILL HERE>
# discord_app_secret: <FILL HERE>

# TO ENABLE TELEGRAM NOTIFICATIONS. (OPTIONAL)
# telegram_bots: ['<FILL HERE>', '<FILL HERE>', ...]
# telegram_channel_id: <FILL HERE>
"

EDITOR="echo \"$CONTENT\" > " rails credentials:edit --environment development
Expand Down
37 changes: 37 additions & 0 deletions config/initializers/01_settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

# rubocop:disable Style/ClassVars
class Settings
def self.enable_analytics?
@@enable_analytics ||= Rails.application.credentials.plausible_data_domain.present? &&
Rails.application.credentials.plausible_src.present?
end

def self.enable_sentry?
@@enable_sentry ||= Rails.application.credentials.sentry_dsn.present?
end

def self.enable_telegram_notifications?
@@enable_telegram_notifications ||= Rails.application.credentials.telegram_channel_id.present? &&
Rails.application.credentials.telegram_bots.present?
end

def self.enable_analytics_import?
@@enable_analytics_import ||= Rails.application.credentials.plausible_api_key.present?
end

def self.enable_twitter_sign_in?
@@enable_twitter_sign_in ||= Rails.application.credentials.twitter_app_id.present? &&
Rails.application.credentials.twitter_app_secret.present?
end

def self.enable_discord_sign_in?
@@enable_discord_sign_in ||= Rails.application.credentials.discord_app_id.present? &&
Rails.application.credentials.discord_app_secret.present?
end

def self.enable_dev_sign_in?
@@enable_dev_sign_in ||= Rails.env.development? || Rails.env.test?
end
end
# rubocop:enable Style/ClassVars
24 changes: 14 additions & 10 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,20 @@
# Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks.
# config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo'
config.omniauth :twitter,
Rails.application.credentials.twitter_app_id,
Rails.application.credentials.twitter_app_secret

config.omniauth :discord,
Rails.application.credentials.discord_app_id,
Rails.application.credentials.discord_app_secret,
scope: "identify email"

config.omniauth :developer unless Rails.env.production?
if Settings.enable_twitter_sign_in?
config.omniauth :twitter,
Rails.application.credentials.twitter_app_id,
Rails.application.credentials.twitter_app_secret
end

if Settings.enable_discord_sign_in?
config.omniauth :discord,
Rails.application.credentials.discord_app_id,
Rails.application.credentials.discord_app_secret,
scope: "identify email"
end

config.omniauth :developer if Settings.enable_dev_sign_in?

# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/sentry.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

if Rails.env.production?
if Settings.enable_sentry?
Sentry.init do |config|
config.dsn = Rails.application.credentials.sentry_dsn
config.breadcrumbs_logger = %i[active_support_logger http_logger]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "rails_helper"

RSpec.describe Users::OmniauthCallbacksController, type: :controller do
%i[twitter discord].each do |provider|
%i[developer].each do |provider|
describe "POST /users/auth/#{provider}/callback" do
let(:auth) { OmniAuth::AuthHash.new(Faker::Omniauth.twitter) }

Expand Down
190 changes: 190 additions & 0 deletions spec/initializers/settings_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
# frozen_string_literal: true

require "rails_helper"

# rubocop:disable Style/ClassVars
RSpec.describe Settings, type: :initializer do
describe ".enable_analytics?" do
context "when plausible_data_domain credential is blank" do
it "returns false" do
described_class.class_variable_set(:@@enable_analytics, nil)
allow(Rails.application.credentials).to receive(:plausible_data_domain).and_return(nil)
allow(Rails.application.credentials).to receive(:plausible_src).and_return("value")

expect(described_class.enable_analytics?).to be false
end
end

context "when plausible_src credential is blank" do
it "returns false" do
described_class.class_variable_set(:@@enable_analytics, nil)
allow(Rails.application.credentials).to receive(:plausible_data_domain).and_return("value")
allow(Rails.application.credentials).to receive(:plausible_src).and_return(nil)

expect(described_class.enable_analytics?).to be false
end
end

context "when plausible_data_domain and plausible_src credential are present" do
it "returns true" do
described_class.class_variable_set(:@@enable_analytics, nil)
allow(Rails.application.credentials).to receive(:plausible_data_domain).and_return("value")
allow(Rails.application.credentials).to receive(:plausible_src).and_return("value")

expect(described_class.enable_analytics?).to be true
end
end
end

describe ".enable_telegram_notifications?" do
context "when telegram_channel_id credential is blank" do
it "returns false" do
described_class.class_variable_set(:@@enable_telegram_notifications, nil)
allow(Rails.application.credentials).to receive(:telegram_channel_id).and_return(nil)
allow(Rails.application.credentials).to receive(:telegram_bots).and_return("value")

expect(described_class.enable_telegram_notifications?).to be false
end
end

context "when telegram_bots credential is blank" do
it "returns false" do
described_class.class_variable_set(:@@enable_telegram_notifications, nil)
allow(Rails.application.credentials).to receive(:telegram_channel_id).and_return("value")
allow(Rails.application.credentials).to receive(:telegram_bots).and_return(nil)

expect(described_class.enable_telegram_notifications?).to be false
end
end

context "when telegram_channel_id and telegram_bots credential are present" do
it "returns true" do
described_class.class_variable_set(:@@enable_telegram_notifications, nil)
allow(Rails.application.credentials).to receive(:telegram_channel_id).and_return("value")
allow(Rails.application.credentials).to receive(:telegram_bots).and_return("value")

expect(described_class.enable_telegram_notifications?).to be true
end
end
end

describe ".enable_analytics_import?" do
context "when plausible_api_key credential is blank" do
it "returns false" do
described_class.class_variable_set(:@@enable_analytics_import, nil)
allow(Rails.application.credentials).to receive(:plausible_api_key).and_return(nil)

expect(described_class.enable_analytics_import?).to be false
end
end

context "when plausible_api_key is present" do
it "returns true" do
described_class.class_variable_set(:@@enable_analytics_import, nil)
allow(Rails.application.credentials).to receive(:plausible_api_key).and_return("value")

expect(described_class.enable_analytics_import?).to be true
end
end
end

describe ".enable_twitter_sign_in?" do
context "when twitter_app_id credential is blank" do
it "returns false" do
described_class.class_variable_set(:@@enable_twitter_sign_in, nil)
allow(Rails.application.credentials).to receive(:twitter_app_id).and_return(nil)
allow(Rails.application.credentials).to receive(:twitter_app_secret).and_return("value")

expect(described_class.enable_twitter_sign_in?).to be false
end
end

context "when twitter_app_secret credential is blank" do
it "returns false" do
described_class.class_variable_set(:@@enable_twitter_sign_in, nil)
allow(Rails.application.credentials).to receive(:twitter_app_id).and_return("value")
allow(Rails.application.credentials).to receive(:twitter_app_secret).and_return(nil)

expect(described_class.enable_twitter_sign_in?).to be false
end
end

context "when twitter_app_id and twitter_app_secret credential are present" do
it "returns true" do
described_class.class_variable_set(:@@enable_twitter_sign_in, nil)
allow(Rails.application.credentials).to receive(:twitter_app_id).and_return("value")
allow(Rails.application.credentials).to receive(:twitter_app_secret).and_return("value")

expect(described_class.enable_twitter_sign_in?).to be true
end
end
end

describe ".enable_discord_sign_in?" do
context "when discord_app_id credential is blank" do
it "returns false" do
described_class.class_variable_set(:@@enable_discord_sign_in, nil)
allow(Rails.application.credentials).to receive(:discord_app_id).and_return(nil)
allow(Rails.application.credentials).to receive(:discord_app_secret).and_return("value")

expect(described_class.enable_discord_sign_in?).to be false
end
end

context "when discord_app_secret credential is blank" do
it "returns false" do
described_class.class_variable_set(:@@enable_discord_sign_in, nil)
allow(Rails.application.credentials).to receive(:discord_app_id).and_return("value")
allow(Rails.application.credentials).to receive(:discord_app_secret).and_return(nil)

expect(described_class.enable_discord_sign_in?).to be false
end
end

context "when discord_app_id and discord_app_secret credential are present" do
it "returns true" do
described_class.class_variable_set(:@@enable_discord_sign_in, nil)
allow(Rails.application.credentials).to receive(:discord_app_id).and_return("value")
allow(Rails.application.credentials).to receive(:discord_app_secret).and_return("value")

expect(described_class.enable_discord_sign_in?).to be true
end
end
end

describe ".enable_dev_sign_in?" do
context "when environment is development" do
it "returns true" do
described_class.class_variable_set(:@@enable_dev_sign_in, nil)
allow(Rails.env).to receive(:development?).and_return(true)
allow(Rails.env).to receive(:test?).and_return(false)
allow(Rails.env).to receive(:production?).and_return(false)

expect(described_class.enable_dev_sign_in?).to be true
end
end

context "when environment is test" do
it "returns true" do
described_class.class_variable_set(:@@enable_dev_sign_in, nil)
allow(Rails.env).to receive(:development?).and_return(false)
allow(Rails.env).to receive(:test?).and_return(true)
allow(Rails.env).to receive(:production?).and_return(false)

expect(described_class.enable_dev_sign_in?).to be true
end
end

context "when environment is production" do
it "returns false" do
described_class.class_variable_set(:@@enable_dev_sign_in, nil)
allow(Rails.env).to receive(:development?).and_return(false)
allow(Rails.env).to receive(:test?).and_return(false)
allow(Rails.env).to receive(:production?).and_return(true)

expect(described_class.enable_dev_sign_in?).to be false
end
end
end
end
# rubocop:enable Style/ClassVars
Loading