Commit 6651a2bb authored by Vladimir Shushlin's avatar Vladimir Shushlin

Move logic from view to presenter

+ specs
parent b0f676c9
# frozen_string_literal: true
class PagesDomainPresenter < Gitlab::View::Presenter::Delegated
presents :pages_domain
def needs_verification?
Gitlab::CurrentSettings.pages_domain_verification_enabled? && unverified?
end
def show_auto_ssl_failed_warning?
return false unless Feature.enabled?(:pages_letsencrypt_errors, pages_domain.project)
# validations prevents auto ssl from working, so there is no need to show that warning until
return false if needs_verification?
::Gitlab::LetsEncrypt.enabled? && auto_ssl_failed
end
end
......@@ -6,6 +6,7 @@
Domains (#{@domains.count})
%ul.list-group.list-group-flush.pages-domain-list{ class: ("has-verification-status" if verification_enabled) }
- @domains.each do |domain|
- domain = Gitlab::View::Presenter::Factory.new(domain, current_user: current_user).fabricate!
%li.pages-domain-list-item.list-group-item.d-flex.justify-content-between
- if verification_enabled
- tooltip, status = domain.unverified? ? [s_('GitLabPages|Unverified'), 'failed'] : [s_('GitLabPages|Verified'), 'success']
......@@ -13,24 +14,24 @@
= sprite_icon("status_#{status}", size: 16 )
.domain-name
= external_link(domain.url, domain.url)
- if domain.subject
- if domain.certificate
%div
%span.badge.badge-gray
= s_('GitLabPages|Certificate: %{subject}') % { subject: domain.subject }
= s_('GitLabPages|Certificate: %{subject}') % { subject: domain.pages_domain.subject }
- if domain.expired?
%span.badge.badge-danger
= s_('GitLabPages|Expired')
%div
= link_to s_('GitLabPages|Edit'), project_pages_domain_path(@project, domain), class: "btn btn-sm btn-grouped btn-success btn-inverted"
= link_to s_('GitLabPages|Remove'), project_pages_domain_path(@project, domain), data: { confirm: s_('GitLabPages|Are you sure?')}, method: :delete, class: "btn btn-remove btn-sm btn-grouped"
- if verification_enabled && domain.unverified?
- if domain.needs_verification?
%li.list-group-item.bs-callout-warning
- details_link_start = "<a href='#{project_pages_domain_path(@project, domain)}'>".html_safe
- details_link_end = '</a>'.html_safe
= s_('GitLabPages|%{domain} is not verified. To learn how to verify ownership, visit your %{link_start}domain details%{link_end}.').html_safe % { domain: domain.domain,
link_start: details_link_start,
link_end: details_link_end }
- elsif ::Gitlab::LetsEncrypt.enabled? && domain.auto_ssl_failed && Feature.enabled?(:pages_letsencrypt_errors, @project)
- if domain.show_auto_ssl_failed_warning?
%li.list-group-item.bs-callout-warning
- details_link_start = "<a href='#{project_pages_domain_path(@project, domain)}'>".html_safe
- details_link_end = '</a>'.html_safe
......
# frozen_string_literal: true
require 'spec_helper'
describe PagesDomainPresenter do
using RSpec::Parameterized::TableSyntax
include LetsEncryptHelpers
let(:presenter) { Gitlab::View::Presenter::Factory.new(domain).fabricate! }
describe 'needs_validation?' do
where(:pages_verification_enabled, :traits, :expected) do
false | :unverified | false
false | [] | false
true | :unverified | true
true | [] | false
end
with_them do
before do
stub_application_setting(pages_domain_verification_enabled: pages_verification_enabled)
end
let(:domain) { create(:pages_domain, *traits) }
it { expect(presenter.needs_verification?).to eq(expected) }
end
end
describe 'show_auto_ssl_failed_warning?' do
subject { presenter.show_auto_ssl_failed_warning? }
let(:domain) { create(:pages_domain) }
before do
stub_lets_encrypt_settings
end
it { is_expected.to eq(false) }
context "when we failed to obtain Let's Encrypt's certificate" do
before do
domain.update!(auto_ssl_failed: true)
end
it { is_expected.to eq(true) }
context 'when lets_encrypt_error feature flag is disabled' do
before do
stub_feature_flags(pages_letsencrypt_errors: false)
end
it { is_expected.to eq(false) }
end
context "when Let's Encrypt integration is disabled" do
before do
allow(::Gitlab::LetsEncrypt).to receive(:enabled?).and_return false
end
it { is_expected.to eq(false) }
end
context "when domain is unverified" do
before do
domain.update!(verified_at: nil)
end
it { is_expected.to eq(false) }
end
end
end
end
......@@ -8,10 +8,6 @@ describe 'projects/pages/show' do
let(:project) { create(:project, :repository) }
let(:user) { create(:user) }
let(:domain) { create(:pages_domain, project: project) }
let(:error_message) do
"Something went wrong while obtaining Let's Encrypt certificate for #{domain.domain}. "\
"To retry visit your domain details."
end
before do
allow(project).to receive(:pages_deployed?).and_return(true)
......@@ -24,37 +20,53 @@ describe 'projects/pages/show' do
assign(:domains, [domain])
end
describe 'validation warning' do
let(:warning_message) do
"#{domain.domain} is not verified. To learn how to verify ownership, "\
"visit your domain details."
end
it "doesn't show auto ssl error warning" do
render
expect(rendered).not_to have_content(error_message)
expect(rendered).not_to have_content(warning_message)
end
context "when we failed to obtain Let's Encrypt's certificate" do
context "when domain is not verified" do
before do
domain.update!(auto_ssl_failed: true)
domain.update!(verified_at: nil)
end
it 'shows auto ssl error warning' do
render
expect(rendered).to have_content(error_message)
expect(rendered).to have_content(warning_message)
end
end
end
it "doesn't show warning if lets_encrypt_error feature flag is disabled" do
stub_feature_flags(pages_letsencrypt_errors: false)
describe "warning about failed Let's Encrypt" do
let(:error_message) do
"Something went wrong while obtaining Let's Encrypt certificate for #{domain.domain}. "\
"To retry visit your domain details."
end
it "doesn't show auto ssl error warning" do
render
expect(rendered).not_to have_content(error_message)
end
it "doesn't show warning if Let's Encrypt integration is disabled" do
allow(::Gitlab::LetsEncrypt).to receive(:enabled?).and_return false
context "when we failed to obtain Let's Encrypt's certificate" do
before do
domain.update!(auto_ssl_failed: true)
end
it 'shows auto ssl error warning' do
render
expect(rendered).not_to have_content(error_message)
expect(rendered).to have_content(error_message)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment