Files
once-campfire/test/models/user_test.rb
T
Marcello Costagliola 462eff10df Reset former members' connections and grant open rooms in one statement
Deleting the memberships with delete_all skips Membership's after_destroy_commit, which resets a member's
connections when one membership is revoked. Until the job ran, a member who had the room open kept its
streams, and a message that still landed in the room (a request already past the membership check, a bot's
reply) reached them. The request now reads the members' ids in the transaction that deletes their
memberships, and the job resets their connections before it destroys the messages. It costs a Redis round
trip per member, about 1.5 s for 10,000, so it's done in the job rather than in the request.

User#grant_membership_to_open_rooms read the open rooms and inserted in a separate statement, so a user
created while a room was being closed could read it as open and be granted it after the close. It's now a
single insert ... select, which SQLite runs under the write lock, skipping duplicates as insert_all did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Bj8KnxpTf9sj2Ysa8aLAVa
2026-10-06 14:45:18 +02:00

50 lines
1.7 KiB
Ruby

require "test_helper"
class UserTest < ActiveSupport::TestCase
test "user does not prevent very long passwords" do
users(:david).update(password: "secret" * 50)
assert users(:david).valid?
end
test "creating users grants membership to the open rooms" do
assert_difference -> { Membership.count }, +Rooms::Open.count do
create_new_user
end
end
test "memberships granted to the open rooms are like any other" do
freeze_time
user = create_new_user
assert_equal Rooms::Open.ids.sort, user.memberships.pluck(:room_id).sort
user.memberships.each do |membership|
assert membership.involved_in_mentions?
assert_equal Time.current, membership.created_at
assert_equal Time.current, membership.updated_at
end
end
test "deactivating a user deletes push subscriptions, searches, memberships for non-direct rooms, and changes their email address" do
assert_difference -> { Membership.count }, -users(:david).memberships.without_direct_rooms.count do
assert_difference -> { Push::Subscription.count }, -users(:david).push_subscriptions.count do
assert_difference -> { Search.count }, -users(:david).searches.count do
SecureRandom.stubs(:uuid).returns("2e7de450-cf04-4fa8-9b02-ff5ab2d733e7")
users(:david).deactivate
assert_equal "david-deactivated-2e7de450-cf04-4fa8-9b02-ff5ab2d733e7@37signals.com", users(:david).reload.email_address
end
end
end
end
test "deactivating a user deletes their sessions" do
assert_changes -> { users(:david).sessions.count }, from: 1, to: 0 do
users(:david).deactivate
end
end
private
def create_new_user
User.create!(name: "User", email_address: "user@example.com", password: "secret123456")
end
end