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.

5.3 KB
Raw Blame
# frozen_string_literal: true

require 'rails_helper'
require 'rake'

# What: `rake activities:drop_stored_emails`, a data backfill.
# Why:  project.member_added stored the member's login address in its parameters
#       until 2026-10-29. Every reader of the project's members area sees and
#       searches those rows, and a member now chooses which email the others see
#       (User::EmailDisplay). The task removes the key from the old rows.
RSpec.describe 'activities:drop_stored_emails', :ci_guard do
  before(:all) do
    Rails.application.load_tasks unless Rake::Task.task_defined?('activities:drop_stored_emails')
  end

  before { Rake::Task['activities:drop_stored_emails'].reenable }

  let(:organisation) { create(:organisation) }
  let(:project) { create(:project, organisation: organisation) }
  let(:member) { create(:user, email: '[email protected]') }

  def activity_with(parameters)
    ActsAsTenant.with_tenant(organisation) do
      create(:activity, trackable: project, organisation: organisation, secondary: member,
                        key: 'project.member_added', action: 'member_added', parameters: parameters)
    end
  end

  it 'removes the stored login address and keeps every other parameter' do
    old_row = activity_with('user_name' => 'Mia Mitglied', 'user_email' => '[email protected]', 'tickets_access' => 'read_only')

    expect { Rake::Task['activities:drop_stored_emails'].invoke }.to output(/1 activities/).to_stdout

    expect(old_row.reload.parameters).to eq('user_name' => 'Mia Mitglied', 'tickets_access' => 'read_only')
  end

  # FanoutService and OutboxEvent copy the activity's parameters into their
  # payload; the REST and MCP inbox hand the payload out, and the inbox search
  # reads it. Both or neither, as Posts::ExcerptRedaction does for excerpts.
  it 'removes the copied login address from notifications and outbox events, keeping the rest' do
    activity = activity_with('user_name' => 'Mia Mitglied', 'user_email' => '[email protected]')
    copied = { 'activity_id' => activity.id, 'action' => 'member_added',
               'parameters' => { 'user_name' => 'Mia Mitglied', 'user_email' => '[email protected]' } }
    notification, outbox = ActsAsTenant.with_tenant(organisation) do
      [create(:notification, organisation: organisation, recipient: create(:user), payload: copied),
       create(:outbox_event, organisation: organisation, activity: activity, payload: copied)]
    end

    expect { Rake::Task['activities:drop_stored_emails'].invoke }.to output(/1 activities/).to_stdout

    expected = { 'activity_id' => activity.id, 'action' => 'member_added', 'parameters' => { 'user_name' => 'Mia Mitglied' } }
    expect(notification.reload.payload).to eq(expected)
    expect(outbox.reload.payload).to eq(expected)
  end

  # ticket.assigned and ticket.unassigned stored the assignee's address under
  # assignee_email / unassignee_email before they read it from the user;
  # their notifications kept a copy at the top of the payload and under parameters.
  it 'removes the older assignee keys from activities and from both places of a notification' do
    ticket = ActsAsTenant.with_tenant(organisation) { create(:ticket, project: project) }
    activity = ActsAsTenant.with_tenant(organisation) do
      create(:activity, trackable: ticket, organisation: organisation, secondary: member, key: 'ticket.assigned',
                        action: 'assigned', parameters: { 'assignee_email' => '[email protected]', 'display_name' => 'Kanban' })
    end
    unassigned = ActsAsTenant.with_tenant(organisation) do
      create(:activity, trackable: ticket, organisation: organisation, secondary: member, key: 'ticket.unassigned',
                        action: 'unassigned', parameters: { 'unassignee_email' => '[email protected]' })
    end
    notification = ActsAsTenant.with_tenant(organisation) do
      create(:notification, organisation: organisation, recipient: create(:user),
                            payload: { 'activity_id' => activity.id, 'assignee_email' => '[email protected]',
                                       'parameters' => { 'assignee_email' => '[email protected]', 'display_name' => 'Kanban' } })
    end

    expect { Rake::Task['activities:drop_stored_emails'].invoke }.to output(/2 activities/).to_stdout

    expect(activity.reload.parameters).to eq('display_name' => 'Kanban')
    expect(unassigned.reload.parameters).to eq({})
    expect(notification.reload.payload).to eq('activity_id' => activity.id, 'parameters' => { 'display_name' => 'Kanban' })
  end

  it 'names every key it removes in one list' do
    expect(Activity::STORED_EMAIL_KEYS).to contain_exactly('user_email', 'assignee_email', 'unassignee_email')
  end

  it 'leaves rows without the key alone and touches nothing the second time' do
    new_row = activity_with('user_name' => 'Mia Mitglied')
    activity_with('user_email' => '[email protected]')

    expect { Rake::Task['activities:drop_stored_emails'].invoke }.to output(/1 activities/).to_stdout
    Rake::Task['activities:drop_stored_emails'].reenable
    expect { Rake::Task['activities:drop_stored_emails'].invoke }.to output(/0 activities/).to_stdout

    expect(new_row.reload.parameters).to eq('user_name' => 'Mia Mitglied')
  end

  it 'runs on every deploy' do
    expect(Lalabase::DataBackfills::TASKS).to include('activities:drop_stored_emails')
  end
end