Merge commit 'a27bafa59653328a0f06bedb1dfb2b6ee92af43d' into glitch-soc/merge-upstream

- `app/controllers/application_controller.rb`:
  Minor conflict due to glitch-soc's theming system.
- `spec/helpers/application_helper_spec.rb`:
  Minor conflict due to glitch-soc's theming system.
This commit is contained in:
Claire committed 2024-12-01 17:36:49 +01:00
commit 48827e1210
83 files changed
+1330 -441

No files matched your search

+63 -1
View File
@@ -3,9 +3,69 @@
require 'rails_helper'
RSpec.describe UserRole do
subject { described_class.create(name: 'Foo', position: 1) }
describe 'Validations' do
describe 'name' do
context 'when everyone' do
subject { described_class.everyone }
it { is_expected.to_not validate_presence_of(:name) }
end
context 'when not everyone' do
subject { Fabricate.build :user_role }
it { is_expected.to validate_presence_of(:name) }
end
end
describe 'color' do
it { is_expected.to allow_values('#112233', '#aabbcc', '').for(:color) }
it { is_expected.to_not allow_values('x', '112233445566', '#xxyyzz').for(:color) }
end
context 'when current_account is set' do
subject { Fabricate :user_role }
let(:account) { Fabricate :account }
before { subject.current_account = account }
it { is_expected.to_not allow_value(999_999).for(:position).with_message(:elevated) }
it { is_expected.to_not allow_value(999_999).for(:permissions).against(:permissions_as_keys).with_message(:elevated) }
context 'when current_account is changing their own role' do
let(:account) { Fabricate :account, user: Fabricate(:user, role: subject) }
it { is_expected.to_not allow_value(100).for(:permissions).against(:permissions_as_keys).with_message(:own_role) }
it { is_expected.to_not allow_value(100).for(:position).with_message(:own_role) }
end
end
end
describe 'Callback for position' do
context 'when everyone' do
subject { Fabricate.build :user_role, id: described_class::EVERYONE_ROLE_ID }
it 'sets the position to nobody position' do
expect { subject.valid? }
.to change(subject, :position).to(described_class::NOBODY_POSITION)
end
end
context 'when not everyone' do
subject { Fabricate.build :user_role }
it 'does not change the position' do
expect { subject.valid? }
.to_not change(subject, :position)
end
end
end
describe '#can?' do
subject { Fabricate :user_role }
context 'with a single flag' do
it 'returns true if any of them are present' do
subject.permissions = described_class::FLAGS[:manage_reports]
@@ -92,6 +152,8 @@ RSpec.describe UserRole do
end
describe '#computed_permissions' do
subject { Fabricate :user_role }
context 'when the role is nobody' do
subject { described_class.nobody }
+38 -11
View File
@@ -33,14 +33,12 @@ RSpec.describe User do
end
end
describe 'validations' do
describe 'Associations' do
it { is_expected.to belong_to(:account).required }
end
it 'is invalid without a valid email' do
user = Fabricate.build(:user, email: 'john@')
user.valid?
expect(user).to model_have_error_on_field(:email)
end
describe 'Validations' do
it { is_expected.to_not allow_value('john@').for(:email) }
it 'is valid with an invalid e-mail that has already been saved' do
user = Fabricate.build(:user, email: 'invalid-email')
@@ -48,11 +46,7 @@ RSpec.describe User do
expect(user.valid?).to be true
end
it 'is valid with a localhost e-mail address' do
user = Fabricate.build(:user, email: 'admin@localhost')
user.valid?
expect(user.valid?).to be true
end
it { is_expected.to allow_value('admin@localhost').for(:email) }
end
describe 'Normalizations' do
@@ -183,6 +177,39 @@ RSpec.describe User do
end
end
describe '#update_sign_in!' do
context 'with an existing user' do
let!(:user) { Fabricate :user, last_sign_in_at: 10.days.ago, current_sign_in_at: 1.hour.ago, sign_in_count: 123 }
context 'with new sign in false' do
it 'updates timestamps but not counts' do
expect { user.update_sign_in!(new_sign_in: false) }
.to change(user, :last_sign_in_at)
.and change(user, :current_sign_in_at)
.and not_change(user, :sign_in_count)
end
end
context 'with new sign in true' do
it 'updates timestamps and counts' do
expect { user.update_sign_in!(new_sign_in: true) }
.to change(user, :last_sign_in_at)
.and change(user, :current_sign_in_at)
.and change(user, :sign_in_count).by(1)
end
end
end
context 'with a new user' do
let(:user) { Fabricate.build :user }
it 'does not persist the user' do
expect { user.update_sign_in! }
.to_not change(user, :persisted?).from(false)
end
end
end
describe '#confirmed?' do
it 'returns true when a confirmed_at is set' do
user = Fabricate.build(:user, confirmed_at: Time.now.utc)
+107 -9
View File
@@ -6,20 +6,28 @@ RSpec.describe Webhook do
let(:webhook) { Fabricate(:webhook) }
describe 'Validations' do
subject { Fabricate.build :webhook }
it { is_expected.to validate_presence_of(:events) }
it 'requires non-empty events value' do
record = described_class.new(events: [])
record.valid?
it { is_expected.to_not allow_values([], %w(account.invalid)).for(:events) }
expect(record).to model_have_error_on_field(:events)
end
it { is_expected.to_not allow_values('{{account }').for(:template) }
it 'requires valid events value from EVENTS' do
record = described_class.new(events: ['account.invalid'])
record.valid?
context 'when current_account is assigned' do
subject { Fabricate.build :webhook, current_account: account }
expect(record).to model_have_error_on_field(:events)
context 'with account that has permissions' do
let(:account) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
it { is_expected.to allow_values(%w(account.created)).for(:events) }
end
context 'with account lacking permissions' do
let(:account) { Fabricate :account }
it { is_expected.to_not allow_values(%w(account.created)).for(:events) }
end
end
end
@@ -29,6 +37,96 @@ RSpec.describe Webhook do
end
end
describe 'Callbacks' do
describe 'Generating a secret' do
context 'when secret exists already' do
subject { described_class.new(secret: 'secret') }
it 'does not override' do
expect { subject.valid? }
.to_not change(subject, :secret)
end
end
context 'when secret does not exist' do
subject { described_class.new(secret: nil) }
it 'does not override' do
expect { subject.valid? }
.to change(subject, :secret)
end
end
end
end
describe '.permission_for_event' do
subject { described_class.permission_for_event(event) }
context 'with a nil value' do
let(:event) { nil }
it { is_expected.to be_nil }
end
context 'with an account approved event' do
let(:event) { 'account.approved' }
it { is_expected.to eq(:manage_users) }
end
context 'with an account created event' do
let(:event) { 'account.created' }
it { is_expected.to eq(:manage_users) }
end
context 'with an account updated event' do
let(:event) { 'account.updated' }
it { is_expected.to eq(:manage_users) }
end
context 'with an report created event' do
let(:event) { 'report.created' }
it { is_expected.to eq(:manage_reports) }
end
context 'with an report updated event' do
let(:event) { 'report.updated' }
it { is_expected.to eq(:manage_reports) }
end
context 'with an status created event' do
let(:event) { 'status.created' }
it { is_expected.to eq(:view_devops) }
end
context 'with an status updated event' do
let(:event) { 'status.updated' }
it { is_expected.to eq(:view_devops) }
end
end
describe '#required_permissions' do
subject { described_class.new(events:).required_permissions }
context 'with empty events' do
let(:events) { [] }
it { is_expected.to eq([]) }
end
context 'with multiple event types' do
let(:events) { %w(account.created account.updated status.created) }
it { is_expected.to eq %i(manage_users view_devops) }
end
end
describe '#rotate_secret!' do
it 'changes the secret' do
expect { webhook.rotate_secret! }