Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

14.5 KB
Raw Blame
# frozen_string_literal: true

require 'rails_helper'

# Linking a meeting participant to a row of the people directory (S2b). The
# actor resolves every id itself: the participant through the locked meeting,
# the person and the member through what the writer may see in the directory.
# One key per participant: a member is linked by user_id, a contact by person_id.
RSpec.describe Actors::LinkMeetingParticipant, :ci_guard do
  include PeopleVisibilityHelpers

  let(:organisation) { create(:organisation) }

  before { ActsAsTenant.current_tenant = organisation }
  after { ActsAsTenant.current_tenant = nil }

  let(:project) { create(:project, organisation: organisation) }
  let(:admin_role) { create(:role, organisation: organisation, title: Role::ACCOUNT_ADMIN_TITLE) }
  let(:writer) { member(role: admin_role, first_name: 'Anna') }
  let(:meeting) { create(:meeting, organisation: organisation, project: project) }
  let!(:participant) { create(:meeting_participant, meeting: meeting, display_name: 'J. Weber (Hoster)') }
  let!(:contact) { create(:person, organisation: organisation, name: 'Jonas Weber') }

  def member(role: create(:role, organisation: organisation), first_name: 'Mia', org: organisation, active: true)
    user = create(:user, first_name: first_name, last_name: "Beispiel#{SecureRandom.hex(2)}")
    create(:membership, user: user, organisation: org, role: role, active: active)
    User.find(user.id)
  end

  def role_with(*actions)
    role = create(:role, organisation: organisation)
    actions.each { |action| create(:permission, role: role, category: 'people', action: action, allowed: true) }
    role
  end

  def link(user: writer, participant_id: participant.id, authorized_project: project, **choice)
    described_class.call(meeting: meeting, project: authorized_project, participant_id: participant_id,
                         current_user: user, **choice)
  end

  describe 'linking a contact' do
    it 'sets person_id and leaves user_id empty' do
      result = link(person_id: contact.id)

      expect(result).to be_success
      expect(result.resource).to eq(participant)
      expect(participant.reload.person_id).to eq(contact.id)
      expect(participant.user_id).to be_nil
      expect(participant.display_name).to eq('J. Weber (Hoster)')
    end

    it 'moves a linked contact participant to another contact' do
      other = create(:person, organisation: organisation, name: 'Jana Weber')
      link(person_id: contact.id)

      expect(link(person_id: other.id)).to be_success
      expect(participant.reload.person_id).to eq(other.id)
    end

    it 'writes no activity for the person' do
      expect { link(person_id: contact.id) }.not_to(change { Activity.where(trackable: contact).count })
    end

    it 'treats the row of a former member as a contact (person_id)' do
      former = member
      row = create(:person, organisation: organisation, name: 'Fritz Frueher', user: former)
      Membership.where(user: former, organisation: organisation).update_all(active: false)

      expect(link(person_id: row.id)).to be_success
      expect(participant.reload.person_id).to eq(row.id)
      expect(participant.user_id).to be_nil
    end
  end

  describe 'linking a member' do
    let(:colleague) { member(first_name: 'Carl') }

    it 'sets user_id and leaves person_id empty, by member id' do
      expect(link(member_id: colleague.id)).to be_success
      expect(participant.reload.user_id).to eq(colleague.id)
      expect(participant.person_id).to be_nil
    end

    it 'sets user_id when the chosen person row belongs to an active member' do
      row = create(:person, organisation: organisation, name: colleague.display_name, user: colleague)
      link(person_id: contact.id)

      expect(link(person_id: row.id)).to be_success
      expect(participant.reload.user_id).to eq(colleague.id)
      expect(participant.person_id).to be_nil
    end

    it 'refuses a member who already takes part in the meeting, changing nothing' do
      create(:meeting_participant, meeting: meeting, display_name: colleague.display_name, user: colleague)

      result = link(member_id: colleague.id)

      expect(result).not_to be_success
      expect(result.code).to eq(:member_already_participant)
      expect(result.errors).to eq([I18n.t('services.actors.link_meeting_participant.member_already_participant')])
      expect(participant.reload.user_id).to be_nil
    end

    it 'links a participant that already carries the member\'s name' do
      participant.update!(display_name: colleague.display_name)

      expect(link(member_id: colleague.id)).to be_success
      expect(participant.reload.user_id).to eq(colleague.id)
    end

    # The meeting form labels a member chip with the member's profile name: a second
    # participant of that name would make every later save of the meeting fail.
    it 'refuses a member whose name another participant of the meeting carries' do
      create(:meeting_participant, meeting: meeting, display_name: colleague.display_name)

      result = link(member_id: colleague.id)

      expect(result.code).to eq(:member_name_taken)
      expect(result.errors).to eq([I18n.t('services.actors.link_meeting_participant.member_name_taken')])
      expect(participant.reload.user_id).to be_nil
    end

    # The claim writes without the meeting lock, so its row can land after both the
    # actor's check and the model's uniqueness validation. The partial unique index on
    # (meeting_id, user_id) then refuses the write, and the actor answers as the check
    # would. Simulated at the database seam, so the checks themselves stay real.
    it 'turns the unique index into the same refusal when a write lands past both checks' do
      allow_any_instance_of(MeetingParticipant).to receive(:update!).and_raise(ActiveRecord::RecordNotUnique)

      result = link(member_id: colleague.id)

      expect(result.code).to eq(:member_already_participant)
      expect(participant.reload.user_id).to be_nil
    end

    # The meeting form shows a member under the profile name. A second member of the
    # same profile name would make the form show one name twice, and every later save
    # of the meeting would fail.
    it 'refuses a member whose profile name another member participant shows' do
      twin = member(first_name: colleague.first_name)
      twin.update!(last_name: colleague.last_name)
      create(:meeting_participant, meeting: meeting, display_name: 'C. B. (Vertrieb)', user: twin)

      result = link(member_id: colleague.id)

      expect(result.code).to eq(:member_name_taken)
      expect(participant.reload.user_id).to be_nil
    end
  end

  describe 'linking a contact that another participant has already' do
    it 'refuses it, changing nothing' do
      create(:meeting_participant, meeting: meeting, display_name: 'Jonas W.', person: contact)

      result = link(person_id: contact.id)

      expect(result.code).to eq(:person_already_participant)
      expect(result.errors).to eq([I18n.t('services.actors.link_meeting_participant.person_already_participant')])
      expect(participant.reload.person_id).to be_nil
    end

    it 'links the same contact in another meeting' do
      other_meeting = create(:meeting, organisation: organisation, project: project)
      create(:meeting_participant, meeting: other_meeting, display_name: 'Jonas W.', person: contact)

      expect(link(person_id: contact.id)).to be_success
    end
  end

  # The row was linked to a member after the participant was linked to it. The
  # participant is that member now: the dialog may neither relink nor unlink it, also
  # not for a writer who may not see that member.
  describe 'a contact whose row belongs to a member now' do
    before do
      link(person_id: contact.id)
      contact.update!(user: member(first_name: 'Jonas'))
    end

    it 'is neither relinked nor unlinked' do
      other = create(:person, organisation: organisation, name: 'Jana Weber')

      expect(link(person_id: other.id).code).to eq(:member_participant)
      expect(link(unlink: true).code).to eq(:member_participant)
      expect(participant.reload.person_id).to eq(contact.id)
    end
  end

  describe 'a participant that is a member' do
    it 'is not linked again (its account is the link)' do
      colleague = member(first_name: 'Carl')
      participant.update!(user: colleague)

      result = link(person_id: contact.id)

      expect(result.code).to eq(:member_participant)
      expect(participant.reload.person_id).to be_nil
    end
  end

  describe 'a participant of a deactivated member' do
    it 'is still a member participant (the account comes back with the membership)' do
      colleague = member(first_name: 'Dora')
      participant.update!(user: colleague)
      Membership.where(user: colleague, organisation: organisation).update_all(active: false)

      expect(link(person_id: contact.id).code).to eq(:member_participant)
      expect(participant.reload.slice(:user_id, :person_id)).to eq('user_id' => colleague.id, 'person_id' => nil)
    end
  end

  describe 'unlinking' do
    it 'sets person_id to nil and keeps the row and its name' do
      link(person_id: contact.id)

      expect(link(unlink: true)).to be_success
      expect(participant.reload.person_id).to be_nil
      expect(participant.display_name).to eq('J. Weber (Hoster)')
    end

    it 'leaves the account of a member participant alone' do
      colleague = member(first_name: 'Carl')
      participant.update!(user: colleague)

      expect(link(unlink: true).code).to eq(:member_participant)
      expect(participant.reload.user_id).to eq(colleague.id)
    end
  end

  describe 'creating a person and linking it' do
    it 'creates the row with the given name and links it in one step' do
      result = nil
      expect { result = link(new_person: { name: 'J. Weber' }) }.to change(Person, :count).by(1)

      expect(result).to be_success
      created = Person.order(:created_at).last
      expect(created.name).to eq('J. Weber')
      expect(created.creator).to eq(writer)
      expect(participant.reload.person_id).to eq(created.id)
    end

    # The dialog sends a name only; the rest of the row is the people page's.
    it 'takes only the name from the new person' do
      link(new_person: { name: 'J. Weber', email: '[email protected]', function: 'Hoster' })

      expect(participant.reload.person).to have_attributes(name: 'J. Weber', email: nil, function: nil)
    end

    it 'creates nothing when the name is blank' do
      result = nil
      expect { result = link(new_person: { name: ' ' }) }.not_to change(Person, :count)

      expect(result).not_to be_success
      expect(result.code).to eq(:validation_failed)
      expect(participant.reload.person_id).to be_nil
    end

    # A stale or internal failure of SavePerson is no validation failure: its code
    # comes through, so the controller can answer our own failure with a 500.
    it 'passes the code of a failed SavePerson through' do
      failure = Actors::BaseActor::Result.new(success: false, errors: ['Veraltet'], code: :stale_object)
      allow(Actors::SavePerson).to receive(:call).and_return(failure)

      result = link(new_person: { name: 'J. Weber' })

      expect(result.code).to eq(:stale_object)
      expect(result.errors).to eq(['Veraltet'])
    end

    it 'creates nothing when the link fails afterwards (one transaction)' do
      allow_any_instance_of(MeetingParticipant).to receive(:update!).and_raise(Actors::ActorError, 'link failed')

      result = nil
      expect { result = link(new_person: { name: 'J. Weber' }) }.not_to change(Person, :count)
      expect(result.errors).to eq(['link failed'])
    end

    it 'needs manage_people' do
      viewer = member(role: role_with('view_people'))

      result = nil
      expect { result = link(user: viewer, new_person: { name: 'J. Weber' }) }.not_to change(Person, :count)
      expect(result.code).to eq(:not_allowed)
    end

    it 'is allowed with manage_people' do
      manager = member(role: role_with('manage_people'))

      expect(link(user: manager, new_person: { name: 'J. Weber' })).to be_success
    end
  end

  describe 'what the writer cannot reach' do
    it 'refuses without a choice' do
      expect(link.code).to eq(:nothing_picked)
    end

    it 'does not find a person of another organisation' do
      foreign = ActsAsTenant.with_tenant(create(:organisation)) { create(:person, name: 'Jonas Weber') }

      expect(link(person_id: foreign.id).code).to eq(:not_found)
      expect(participant.reload.person_id).to be_nil
    end

    it 'does not find a person without view_people' do
      outsider = member

      expect(link(user: outsider, person_id: contact.id).code).to eq(:not_found)
      expect(participant.reload.person_id).to be_nil
    end

    it 'does not find the row of a member the writer may not see' do
      limited = member(role: role_with('view_people'))
      create(:project_permission, user: limited, project: project, meetings_access: :read_write)
      row = row_out_of_sight(organisation: organisation, name: 'Hanna Beispiel')

      expect(link(user: limited, person_id: row.id).code).to eq(:not_found)
      expect(link(user: limited, member_id: row.user_id).code).to eq(:not_found)
      expect(participant.reload.user_id).to be_nil
    end

    it 'does not find a member of another organisation' do
      stranger = member(org: create(:organisation))

      expect(link(member_id: stranger.id).code).to eq(:not_found)
    end

    it 'does not find a participant of another meeting' do
      other_meeting = create(:meeting, organisation: organisation, project: project)
      other = create(:meeting_participant, meeting: other_meeting, display_name: 'Jonas Weber')

      expect(link(participant_id: other.id, person_id: contact.id).code).to eq(:not_found)
      expect(other.reload.person_id).to be_nil
    end

    it 'treats a meeting moved out of the authorized project as gone' do
      target = create(:project, organisation: organisation)
      meeting.update_columns(project_id: target.id)

      result = link(person_id: contact.id)

      expect(result.code).to eq(:not_found)
      expect(participant.reload.person_id).to be_nil
    end

    it 'takes the meeting lock' do
      expect(meeting).to receive(:lock_in_project!).with(project.id).and_call_original

      link(person_id: contact.id)
    end

    # The claim writes participants without the meeting lock: the row is locked too.
    it 'locks the participant row' do
      statements = []
      collect = ->(*, payload) { statements << payload[:sql] }
      ActiveSupport::Notifications.subscribed(collect, 'sql.active_record') { link(person_id: contact.id) }

      expect(statements).to include(a_string_matching(/FROM "meeting_participants".*FOR UPDATE/m))
    end
  end
end