Merge commit '425d77f8124a50fc033e8fb3bdf7b89a6a25f4fa' into glitch-soc/merge-upstream

Conflicts:
- `.rubocop_todo.yml`:
  Upstream regenerated this file, glitch-soc had a specific ignore.
- `README.md`:
  Upstream updated its README, but glitch-soc has a completely different one.
  Kept glitch-soc's README
This commit is contained in:
Claire
2023-08-11 22:15:41 +02:00
130 changed files with 1818 additions and 1188 deletions

View File

@@ -0,0 +1,60 @@
# frozen_string_literal: true
require 'rails_helper'
describe LanguageValidator do
let(:record_class) do
Class.new do
include ActiveModel::Validations
attr_accessor :locale
validates :locale, language: true
end
end
let(:record) { record_class.new }
describe '#validate_each' do
context 'with a nil value' do
it 'does not add errors' do
record.locale = nil
expect(record).to be_valid
expect(record.errors).to be_empty
end
end
context 'with an array of values' do
it 'does not add errors with array of existing locales' do
record.locale = %w(en fr)
expect(record).to be_valid
expect(record.errors).to be_empty
end
it 'adds errors with array having some non-existing locales' do
record.locale = %w(en fr missing)
expect(record).to_not be_valid
expect(record.errors.first.attribute).to eq(:locale)
expect(record.errors.first.type).to eq(:invalid)
end
end
context 'with a locale string' do
it 'does not add errors when string is an existing locale' do
record.locale = 'en'
expect(record).to be_valid
expect(record.errors).to be_empty
end
it 'adds errors when string is non-existing locale' do
record.locale = 'missing'
expect(record).to_not be_valid
expect(record.errors.first.attribute).to eq(:locale)
expect(record.errors.first.type).to eq(:invalid)
end
end
end
end

View File

@@ -2,32 +2,64 @@
require 'rails_helper'
RSpec.describe URLValidator, type: :validator do
describe '#validate_each' do
before do
allow(validator).to receive(:compliant?).with(value) { compliant }
validator.validate_each(record, attribute, value)
describe URLValidator do
let(:record_class) do
Class.new do
include ActiveModel::Validations
attr_accessor :profile
validates :profile, url: true
end
end
let(:record) { record_class.new }
let(:validator) { described_class.new(attributes: [attribute]) }
let(:record) { instance_double(Webhook, errors: errors) }
let(:errors) { instance_double(ActiveModel::Errors, add: nil) }
let(:value) { '' }
let(:attribute) { :foo }
describe '#validate_each' do
context 'with a nil value' do
it 'adds errors' do
record.profile = nil
context 'when not compliant?' do
let(:compliant) { false }
it 'calls errors.add' do
expect(errors).to have_received(:add).with(attribute, :invalid)
expect(record).to_not be_valid
expect(record.errors.first.attribute).to eq(:profile)
expect(record.errors.first.type).to eq(:invalid)
end
end
context 'when compliant?' do
let(:compliant) { true }
context 'with an invalid url scheme' do
it 'adds errors' do
record.profile = 'ftp://example.com/page'
it 'not calls errors.add' do
expect(errors).to_not have_received(:add).with(attribute, any_args)
expect(record).to_not be_valid
expect(record.errors.first.attribute).to eq(:profile)
expect(record.errors.first.type).to eq(:invalid)
end
end
context 'without a hostname' do
it 'adds errors' do
record.profile = 'https:///page'
expect(record).to_not be_valid
expect(record.errors.first.attribute).to eq(:profile)
expect(record.errors.first.type).to eq(:invalid)
end
end
context 'with an unparseable value' do
it 'adds errors' do
record.profile = 'https://host:port/page' # non-numeric port string causes invalid uri error
expect(record).to_not be_valid
expect(record.errors.first.attribute).to eq(:profile)
expect(record.errors.first.type).to eq(:invalid)
end
end
context 'with a valid url' do
it 'does not add errors' do
record.profile = 'https://example.com/page'
expect(record).to be_valid
expect(record.errors).to be_empty
end
end
end