docbrown/spec/models/forem_instance_spec.rb
Ridhwana 1025900d16
Improve the UX of the SMTP Section (#16223)
* feat: allow reply_to and email_from to be set for an SMTP config

* feat: use these SMTP values in the mailers

* spec: test the application_mailer

* fix: validate with email, and not url

* feat: mimic macs changes from https://github.com/forem/forem/pull/16216 to use in this PR

* setup packs for admin

* refactor: order the keys and add a const for the auth methods

* feat: rename the header to a more user friendlly name

* chore: move the section with Emails

* feat: add a toggle that will show and hide the SMTP form under certain conditions

* feat: add the javaScript to handle the toggles

* feat: add a better description until we convert to a dropdown

* feat: ensure that we have declared sendgrid_enabled

* chore: add  anote to the config controller

* chore: remove references of the email addresses to keep brnach scoped

* feat: tweak js

* test: cypress workflow to update smtp settings

* feat : update the smtp tests

* remove comments

* update test

* chore: rename NOTE

* feat: polisha dn test ForemInstance.only_sendgrid_enabled?

* chore: remove specs

* Update app/lib/constants/settings/smtp.rb

Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>

* Update spec/system/admin/config/admin_updates_smtp_settings_spec.rb

Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>

* Update spec/system/admin/config/admin_updates_smtp_settings_spec.rb

Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>

* Update spec/system/admin/config/admin_updates_smtp_settings_spec.rb

Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>

* Update app/javascript/packs/admin/config/smtp.js

Co-authored-by: Nick Taylor <nick@iamdeveloper.com>

* refactor js as per comments

* refactor as per comments

* Update cypress/integration/seededFlows/adminFlows/config/emailServerSettingsSection.spec.js

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* refactor: update the Cypress tests

Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>
Co-authored-by: Nick Taylor <nick@iamdeveloper.com>
Co-authored-by: Suzanne Aitchison <suzanne@forem.com>
2022-02-15 18:40:14 +02:00

136 lines
4.9 KiB
Ruby

require "rails_helper"
RSpec.describe ForemInstance, type: :model do
describe "deployed_at" do
before do
allow(ENV).to receive(:[])
described_class.instance_variable_set(:@deployed_at, nil)
end
after do
described_class.instance_variable_set(:@deployed_at, nil)
end
it "sets the RELEASE_FOOTPRINT if present" do
allow(ApplicationConfig).to receive(:[]).with("RELEASE_FOOTPRINT").and_return("A deploy date")
expect(described_class.deployed_at).to eq(ApplicationConfig["RELEASE_FOOTPRINT"])
end
it "sets the HEROKU_RELEASE_CREATED_AT if the RELEASE_FOOTPRINT is not present" do
allow(ApplicationConfig).to receive(:[]).with("RELEASE_FOOTPRINT").and_return("")
allow(ENV).to receive(:[]).with("HEROKU_RELEASE_CREATED_AT").and_return("A deploy date set on Heroku")
expect(described_class.deployed_at).to eq(ENV["HEROKU_RELEASE_CREATED_AT"])
end
it "sets to current time if HEROKU_RELEASE_CREATED_AT and RELEASE_FOOTPRINT are not present" do
Timecop.freeze do
allow(ApplicationConfig).to receive(:[]).with("RELEASE_FOOTPRINT").and_return("")
allow(ENV).to receive(:[]).with("HEROKU_RELEASE_CREATED_AT").and_return("")
expect(described_class.deployed_at).to eq(Time.current.to_s)
end
end
end
describe "latest_commit_id" do
before do
described_class.instance_variable_set(:@latest_commit_id, nil)
end
it "sets the FOREM_BUILD_SHA if present" do
allow(ApplicationConfig).to receive(:[]).with("FOREM_BUILD_SHA").and_return("A commit id")
stub_const("ENV", ENV.to_h.merge("HEROKU_SLUG_COMMIT" => ""))
expect(described_class.latest_commit_id).to eq(ApplicationConfig["FOREM_BUILD_SHA"])
end
it "sets the HEROKU_RELEASE_CREATED_AT if the RELEASE_FOOTPRINT is not present" do
allow(ApplicationConfig).to receive(:[]).with("FOREM_BUILD_SHA").and_return("")
stub_const("ENV", ENV.to_h.merge("HEROKU_SLUG_COMMIT" => "A Commit ID set from Heroku"))
expect(described_class.latest_commit_id).to eq(ENV["HEROKU_SLUG_COMMIT"])
end
end
describe ".local?" do
it "returns true if the .app_domain points to localhost" do
allow(Settings::General).to receive(:app_domain).and_return("localhost:3000")
expect(described_class.local?).to be(true)
end
it "returns false if the .app_domain points to a regular domain" do
allow(Settings::General).to receive(:app_domain).and_return("forem.dev")
expect(described_class.local?).to be(false)
end
end
describe ".dev_to?" do
it "returns true if the .app_domain is dev.to" do
allow(Settings::General).to receive(:app_domain).and_return("dev.to")
expect(described_class.dev_to?).to be(true)
end
it "returns false if the .app_domain is not dev.to" do
allow(Settings::General).to receive(:app_domain).and_return("forem.dev")
expect(described_class.dev_to?).to be(false)
end
end
describe ".smtp_enabled?" do
it "return false when no credential is provided" do
expect(described_class.smtp_enabled?).to be(false)
end
it "returns true if provided_minimum_settings?" do
allow(Settings::SMTP).to receive(:address).and_return("address")
allow(Settings::SMTP).to receive(:user_name).and_return("something")
allow(Settings::SMTP).to receive(:password).and_return("something")
expect(described_class.smtp_enabled?).to be(true)
end
it "returns true if sendgrid api key is available" do
ENV["SENDGRID_API_KEY"] = "something"
expect(described_class.smtp_enabled?).to be(true)
ENV["SENDGRID_API_KEY"] = nil
end
end
describe ".contact_email" do
let(:email) { "contact@dev.to" }
before do
allow(Settings::General).to receive(:contact_email).and_return(email)
end
it "sets the correct email" do
expect(described_class.contact_email).to be(email)
end
end
describe ".only_sendgrid_enabled?" do
it "returns false when the minimum SMTP settings are provided" do
allow(Settings::SMTP).to receive(:user_name).and_return("something")
allow(Settings::SMTP).to receive(:password).and_return("something")
allow(Settings::SMTP).to receive(:address).and_return("something")
expect(described_class.only_sendgrid_enabled?).to be(false)
end
it "returns false when Sendgrid is not enabled" do
allow(described_class).to receive(:sendgrid_enabled?).and_return(false)
expect(described_class.only_sendgrid_enabled?).to be(false)
end
it "returns true if Sendgrid is enabled and the minimum SMTP settings are not provided" do
allow(described_class).to receive(:sendgrid_enabled?).and_return(true)
allow(Settings::SMTP).to receive(:user_name).and_return(nil)
allow(Settings::SMTP).to receive(:password).and_return(nil)
allow(Settings::SMTP).to receive(:address).and_return(nil)
expect(described_class.only_sendgrid_enabled?).to be(true)
end
end
end