Refactor: InstanceInfo
と絵文字リアクション利用可能判定テスト (#534)
* Refactor: `InstanceInfo`と絵文字リアクション利用可能判定テスト * Fix: 新規登録のテストが特定時刻で落ちる問題
This commit is contained in:
parent
e317edecb8
commit
ba776d3677
4 changed files with 68 additions and 37 deletions
|
@ -30,24 +30,31 @@ class InstanceInfo < ApplicationRecord
|
|||
sharkey
|
||||
).freeze
|
||||
|
||||
def self.emoji_reaction_available?(domain)
|
||||
return Setting.enable_emoji_reaction if domain.nil?
|
||||
class << self
|
||||
def emoji_reaction_available?(domain)
|
||||
return Setting.enable_emoji_reaction if domain.nil?
|
||||
|
||||
Rails.cache.fetch("emoji_reaction_available_domain:#{domain}") { fetch_emoji_reaction_available(domain) }
|
||||
Rails.cache.fetch("emoji_reaction_available_domain:#{domain}") { load_emoji_reaction_available(domain) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_emoji_reaction_available(domain)
|
||||
info = InstanceInfo.find_by(domain: domain)
|
||||
return false if info.nil?
|
||||
return true if EMOJI_REACTION_AVAILABLE_SOFTWARES.include?(info['software'])
|
||||
|
||||
return false unless info.data.is_a?(Hash)
|
||||
return false unless info.data['metadata'].is_a?(Hash)
|
||||
|
||||
features = info.data.dig('metadata', 'features')
|
||||
return false unless features.is_a?(Array)
|
||||
|
||||
features.include?('emoji_reaction')
|
||||
end
|
||||
end
|
||||
|
||||
def self.fetch_emoji_reaction_available(domain)
|
||||
info = InstanceInfo.find_by(domain: domain)
|
||||
return false if info.nil?
|
||||
|
||||
return true if EMOJI_REACTION_AVAILABLE_SOFTWARES.include?(info['software'])
|
||||
return false if info.data['metadata'].nil? || !info.data['metadata'].is_a?(Hash)
|
||||
|
||||
features = info.data.dig('metadata', 'features')
|
||||
return false if features.nil? || !features.is_a?(Array)
|
||||
|
||||
features.include?('emoji_reaction')
|
||||
end
|
||||
private
|
||||
|
||||
def reset_cache
|
||||
Rails.cache.delete("emoji_reaction_available_domain:#{domain}")
|
||||
|
|
|
@ -407,7 +407,10 @@ RSpec.describe Auth::RegistrationsController do
|
|||
Setting.registrations_secondary_end_hour = secondary_end_hour
|
||||
request.headers['Accept-Language'] = accept_language
|
||||
|
||||
travel_to Time.now.utc.beginning_of_day + 10.hours
|
||||
current = Time.now.utc
|
||||
today = current.beginning_of_day
|
||||
today += 1.day if current.hour > 10
|
||||
travel_to today + 10.hours
|
||||
end
|
||||
|
||||
if result
|
||||
|
|
|
@ -1,3 +1,44 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe InstanceInfo do
|
||||
describe '.emoji_reaction_availables_map' do
|
||||
subject { described_class.emoji_reaction_available?('example.com') }
|
||||
|
||||
it 'availables if features contains emoji_reaction' do
|
||||
Fabricate(:instance_info, domain: 'example.com', software: 'mastodon', data: { metadata: { features: ['emoji_reaction'] } })
|
||||
expect(subject).to be true
|
||||
end
|
||||
|
||||
it 'unavailables if features does not contain emoji_reaction' do
|
||||
Fabricate(:instance_info, domain: 'example.com', software: 'mastodon', data: { metadata: { features: ['ohagi'] } })
|
||||
expect(subject).to be false
|
||||
end
|
||||
|
||||
it 'unavailables if features is not valid' do
|
||||
Fabricate(:instance_info, domain: 'example.com', software: 'mastodon', data: { metadata: { features: 'good_for_ohagi' } })
|
||||
expect(subject).to be false
|
||||
end
|
||||
|
||||
it 'unavailables if features is nil' do
|
||||
Fabricate(:instance_info, domain: 'example.com', software: 'mastodon', data: { metadata: { features: nil } })
|
||||
expect(subject).to be false
|
||||
end
|
||||
|
||||
it 'unavailables if mastodon server' do
|
||||
Fabricate(:instance_info, domain: 'example.com', software: 'mastodon')
|
||||
expect(subject).to be false
|
||||
end
|
||||
|
||||
it 'availables if misskey server' do
|
||||
Fabricate(:instance_info, domain: 'example.com', software: 'misskey')
|
||||
expect(subject).to be true
|
||||
end
|
||||
|
||||
it 'unavailables if old mastodon server' do
|
||||
Fabricate(:instance_info, domain: 'example.com', software: 'mastodon', data: { metadata: [] })
|
||||
expect(subject).to be false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -463,34 +463,18 @@ RSpec.describe Status do
|
|||
describe '.emoji_reaction_availables_map' do
|
||||
subject { described_class.emoji_reaction_availables_map(domains) }
|
||||
|
||||
let(:domains) { %w(features_available.com features_unavailable.com features_invalid.com features_nil.com no_info.com mastodon.com misskey.com old_mastodon.com) }
|
||||
let(:domains) { %w(features_available.com mastodon.com misskey.com) }
|
||||
|
||||
before do
|
||||
Fabricate(:instance_info, domain: 'features_available.com', software: 'mastodon', data: { metadata: { features: ['emoji_reaction'] } })
|
||||
Fabricate(:instance_info, domain: 'features_unavailable.com', software: 'mastodon', data: { metadata: { features: ['ohagi'] } })
|
||||
Fabricate(:instance_info, domain: 'features_invalid.com', software: 'mastodon', data: { metadata: { features: 'good_for_ohagi' } })
|
||||
Fabricate(:instance_info, domain: 'features_nil.com', software: 'mastodon', data: { metadata: { features: nil } })
|
||||
Fabricate(:instance_info, domain: 'mastodon.com', software: 'mastodon')
|
||||
Fabricate(:instance_info, domain: 'misskey.com', software: 'misskey')
|
||||
Fabricate(:instance_info, domain: 'old_mastodon.com', software: 'mastodon', data: { metadata: [] })
|
||||
end
|
||||
|
||||
it 'availables if features contains emoji_reaction' do
|
||||
expect(subject['features_available.com']).to be true
|
||||
end
|
||||
|
||||
it 'unavailables if features does not contain emoji_reaction' do
|
||||
expect(subject['features_unavailable.com']).to be false
|
||||
end
|
||||
|
||||
it 'unavailables if features is not valid' do
|
||||
expect(subject['features_invalid.com']).to be false
|
||||
end
|
||||
|
||||
it 'unavailables if features is nil' do
|
||||
expect(subject['features_nil.com']).to be false
|
||||
end
|
||||
|
||||
it 'unavailables if mastodon server' do
|
||||
expect(subject['mastodon.com']).to be false
|
||||
end
|
||||
|
@ -498,10 +482,6 @@ RSpec.describe Status do
|
|||
it 'availables if misskey server' do
|
||||
expect(subject['misskey.com']).to be true
|
||||
end
|
||||
|
||||
it 'unavailables if old mastodon server' do
|
||||
expect(subject['old_mastodon.com']).to be false
|
||||
end
|
||||
end
|
||||
|
||||
describe '.tagged_with' do
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue