mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-08-10 17:18:49 +09:00
5c5c82b27a
Rooms::DirectsController relaxes ensure_can_administer to true, because every participant in a direct room may administer it. set_room was inherited unscoped, though, so that relaxation applied to any room the caller was merely a member of: DELETE /rooms/directs/<id> destroyed open and closed rooms and all their messages. The same unscoped lookup let a direct room be loaded by the opens and closeds controllers, where force_room_type promoted it. Promoting a DM to open grants every user on the account membership and republishes the whole conversation, including the other participant's messages; converting it to closed lets the initiator revise who is in it and lock the other participant out. Each controller now narrows room_scope to the types it may act on. Opens and closeds keep reach into each other, since converting between them is a feature. Neither can reach a direct room, and directs can only reach directs. Room also refuses to change type away from Rooms::Direct, so the invariant holds for any future caller of becomes! rather than only these two controllers.
80 lines
2.4 KiB
Ruby
80 lines
2.4 KiB
Ruby
require "test_helper"
|
|
|
|
class Rooms::OpensControllerTest < ActionDispatch::IntegrationTest
|
|
setup do
|
|
sign_in :david
|
|
end
|
|
|
|
test "show redirects to get general show" do
|
|
get rooms_open_url(users(:david).rooms.opens.last)
|
|
assert_redirected_to room_url(users(:david).rooms.opens.last)
|
|
end
|
|
|
|
test "new" do
|
|
get new_rooms_open_url
|
|
assert_response :success
|
|
end
|
|
|
|
test "create" do
|
|
assert_turbo_stream_broadcasts :rooms, count: 1 do
|
|
post rooms_opens_url, params: { room: { name: "My New Room" } }
|
|
end
|
|
|
|
assert_equal Room.last.memberships.count, User.count
|
|
assert_redirected_to room_url(Room.last)
|
|
end
|
|
|
|
test "create forbidden by non-admin when account restricts creation to admins" do
|
|
accounts(:signal).settings.restrict_room_creation_to_administrators = true
|
|
accounts(:signal).save!
|
|
|
|
sign_in :jz
|
|
post rooms_opens_url, params: { room: { name: "My New Room" } }
|
|
assert_response :forbidden
|
|
end
|
|
|
|
test "only admins or creators can update" do
|
|
sign_in :jz
|
|
|
|
assert_turbo_stream_broadcasts :rooms, count: 0 do
|
|
put rooms_open_url(rooms(:hq)), params: { room: { name: "New Name" } }
|
|
end
|
|
|
|
assert_response :forbidden
|
|
assert rooms(:hq).reload.name, "HQ"
|
|
end
|
|
|
|
test "update" do
|
|
assert_turbo_stream_broadcasts :rooms, count: 1 do
|
|
put rooms_open_url(rooms(:pets)), params: { room: { name: "New Name" } }
|
|
end
|
|
|
|
assert_redirected_to room_url(rooms(:pets))
|
|
assert rooms(:pets).reload.name, "New Name"
|
|
end
|
|
|
|
test "update a closed room to be open" do
|
|
put rooms_open_url(rooms(:designers)), params: { room: { name: "Doesn't matter" } }
|
|
assert_equal rooms(:designers).memberships.count, User.count
|
|
end
|
|
|
|
test "a direct room can't be promoted to open by its creator" do
|
|
sign_in :kevin
|
|
direct = rooms(:bender_and_kevin)
|
|
|
|
put rooms_open_url(direct), params: { room: { name: "Watercooler" } }
|
|
|
|
assert_equal "Rooms::Direct", Room.find(direct.id).type
|
|
assert_equal [ users(:bender).id, users(:kevin).id ].sort, Room.find(direct.id).user_ids.sort
|
|
end
|
|
|
|
test "a direct room can't be promoted to open by an administrator either" do
|
|
direct = rooms(:david_and_kevin)
|
|
|
|
put rooms_open_url(direct), params: { room: { name: "Watercooler" } }
|
|
|
|
assert_equal "Rooms::Direct", Room.find(direct.id).type
|
|
assert_equal [ users(:david).id, users(:kevin).id ].sort, Room.find(direct.id).user_ids.sort
|
|
end
|
|
end
|