0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-12-01 21:11:10 +01:00
discourse-custom-wizard/spec/components/custom_wizard/notice_spec.rb

160 Zeilen
6,4 KiB
Ruby

2021-09-24 11:58:42 +02:00
# frozen_string_literal: true
require_relative '../../plugin_helper'
describe CustomWizard::Notice do
fab!(:user) { Fabricate(:user) }
let(:subscription_message) {
{
2021-11-02 08:29:31 +01:00
title: "Title of message about subscription",
2021-09-24 11:58:42 +02:00
message: "Message about subscription",
type: "info",
created_at: Time.now - 3.day,
expired_at: nil
}
}
let(:plugin_status) {
{
name: 'discourse-custom-wizard',
status: 'incompatible',
status_changed_at: Time.now - 3.day
}
}
context "subscription message" do
before do
freeze_time
2021-11-02 08:29:31 +01:00
stub_request(:get, described_class.subscription_message_url).to_return(status: 200, body: { messages: [subscription_message] }.to_json)
2021-09-24 11:58:42 +02:00
described_class.update(skip_plugin: true)
end
it "converts subscription messages into notices" do
notice = described_class.list.first
expect(notice.type).to eq(described_class.types[:info])
expect(notice.message).to eq(subscription_message[:message])
expect(notice.created_at.to_datetime).to be_within(1.second).of (subscription_message[:created_at].to_datetime)
end
2021-10-05 14:54:06 +02:00
2021-09-24 11:58:42 +02:00
it "expires notice if subscription message is expired" do
subscription_message[:expired_at] = Time.now
2021-11-02 08:29:31 +01:00
stub_request(:get, described_class.subscription_message_url).to_return(status: 200, body: { messages: [subscription_message] }.to_json)
2021-09-24 11:58:42 +02:00
described_class.update(skip_plugin: true)
2021-11-02 08:29:31 +01:00
notice = described_class.list(include_all: true).first
2021-09-24 11:58:42 +02:00
expect(notice.expired?).to eq(true)
end
2021-11-02 08:29:31 +01:00
it "dismisses informational subscription notices" do
notice = described_class.list(include_all: true).first
expect(notice.dismissed?).to eq(false)
notice.dismiss!
expect(notice.dismissed?).to eq(true)
end
it "dismisses all informational subscription notices" do
4.times do |index|
subscription_message[:title] += " #{index}"
stub_request(:get, described_class.subscription_message_url).to_return(status: 200, body: { messages: [subscription_message] }.to_json)
described_class.update(skip_plugin: true)
end
expect(described_class.list.count).to eq(5)
described_class.dismiss_all
expect(described_class.list.count).to eq(0)
end
2021-09-24 11:58:42 +02:00
end
context "plugin status" do
before do
freeze_time
2021-11-02 08:29:31 +01:00
stub_request(:get, described_class.plugin_status_url).to_return(status: 200, body: plugin_status.to_json)
2021-09-24 11:58:42 +02:00
described_class.update(skip_subscription: true)
end
2021-10-05 14:54:06 +02:00
it "converts warning into notice" do
2021-09-24 11:58:42 +02:00
notice = described_class.list.first
2021-11-02 08:29:31 +01:00
expect(notice.type).to eq(described_class.types[:warning])
expect(notice.message).to eq(I18n.t("wizard.notice.compatibility_issue.message", domain: described_class.plugin_status_domain))
2021-09-24 11:58:42 +02:00
expect(notice.created_at.to_datetime).to be_within(1.second).of (plugin_status[:status_changed_at].to_datetime)
end
2021-10-05 14:54:06 +02:00
it "expires warning notices if status is recommended or compatible" do
2021-09-24 11:58:42 +02:00
plugin_status[:status] = 'compatible'
plugin_status[:status_changed_at] = Time.now
2021-11-02 08:29:31 +01:00
stub_request(:get, described_class.plugin_status_url).to_return(status: 200, body: plugin_status.to_json)
2021-09-24 11:58:42 +02:00
described_class.update(skip_subscription: true)
2021-11-02 08:29:31 +01:00
notice = described_class.list(type: described_class.types[:warning], include_all: true).first
2021-09-24 11:58:42 +02:00
expect(notice.expired?).to eq(true)
end
2021-11-02 08:29:31 +01:00
it "hides plugin status warnings" do
notice = described_class.list.first
expect(notice.hidden?).to eq(false)
notice.hide!
expect(notice.hidden?).to eq(true)
end
2021-09-24 11:58:42 +02:00
end
it "lists notices not expired more than a day ago" do
subscription_message[:expired_at] = Time.now - 8.hours
2021-11-02 08:29:31 +01:00
stub_request(:get, described_class.subscription_message_url).to_return(status: 200, body: { messages: [subscription_message] }.to_json)
stub_request(:get, described_class.plugin_status_url).to_return(status: 200, body: plugin_status.to_json)
2021-09-24 11:58:42 +02:00
described_class.update
2021-11-02 08:29:31 +01:00
expect(described_class.list(include_all: true).length).to eq(2)
2021-10-05 14:54:06 +02:00
end
context "connection errors" do
before do
freeze_time
end
it "creates an error if connection to notice server fails" do
2021-11-02 08:29:31 +01:00
stub_request(:get, described_class.plugin_status_url).to_return(status: 400, body: plugin_status.to_json)
2021-10-05 14:54:06 +02:00
described_class.update(skip_subscription: true)
error = CustomWizard::Notice::ConnectionError.new(:plugin_status)
2021-11-02 08:29:31 +01:00
expect(error.current_error.present?).to eq(true)
2021-10-05 14:54:06 +02:00
end
it "only creates one connection error per type at a time" do
2021-11-02 08:29:31 +01:00
stub_request(:get, described_class.subscription_message_url).to_return(status: 400, body: { messages: [subscription_message] }.to_json)
stub_request(:get, described_class.plugin_status_url).to_return(status: 400, body: plugin_status.to_json)
2021-10-05 14:54:06 +02:00
5.times { described_class.update }
plugin_status_errors = CustomWizard::Notice::ConnectionError.new(:plugin_status)
2021-11-02 08:29:31 +01:00
subscription_message_errors = CustomWizard::Notice::ConnectionError.new(:subscription_message)
2021-10-05 14:54:06 +02:00
2021-11-02 08:29:31 +01:00
expect(plugin_status_errors.current_error[:count]).to eq(5)
expect(subscription_message_errors.current_error[:count]).to eq(5)
2021-10-05 14:54:06 +02:00
end
it "creates a connection error notice if connection errors reach limit" do
2021-11-02 08:29:31 +01:00
stub_request(:get, described_class.plugin_status_url).to_return(status: 400, body: plugin_status.to_json)
2021-10-05 14:54:06 +02:00
error = CustomWizard::Notice::ConnectionError.new(:plugin_status)
error.limit.times { described_class.update(skip_subscription: true) }
2021-11-02 08:29:31 +01:00
notice = described_class.list(type: described_class.types[:connection_error]).first
2021-10-05 14:54:06 +02:00
2021-11-02 08:29:31 +01:00
expect(error.current_error[:count]).to eq(error.limit)
expect(notice.type).to eq(described_class.types[:connection_error])
2021-10-05 14:54:06 +02:00
end
it "expires a connection error notice if connection succeeds" do
2021-11-02 08:29:31 +01:00
stub_request(:get, described_class.plugin_status_url).to_return(status: 400, body: plugin_status.to_json)
2021-10-05 14:54:06 +02:00
error = CustomWizard::Notice::ConnectionError.new(:plugin_status)
error.limit.times { described_class.update(skip_subscription: true) }
2021-11-02 08:29:31 +01:00
stub_request(:get, described_class.plugin_status_url).to_return(status: 200, body: plugin_status.to_json)
2021-10-05 14:54:06 +02:00
described_class.update(skip_subscription: true)
2021-11-02 08:29:31 +01:00
notice = described_class.list(type: described_class.types[:connection_error], include_all: true).first
2021-10-05 14:54:06 +02:00
2021-11-02 08:29:31 +01:00
expect(notice.type).to eq(described_class.types[:connection_error])
2021-10-05 14:54:06 +02:00
expect(notice.expired_at.present?).to eq(true)
end
2021-09-24 11:58:42 +02:00
end
2021-10-05 14:54:06 +02:00
end