Files
PaperClipAI/ui/src/lib/company-members.test.ts
T
Svetlana ZolotenkovaandPaperclip 1b0f78100d fix(access): return membershipRole in the company user directory (#15183)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Agents often check a human approval. The check must confirm the role
of the approving user in the company.
> - An agent can read the company user directory. The directory removes
the membership role from the server record.
> - The admin member list requires `users:manage_permissions`. An
approval check must not need that permission.
> - This pull request returns `membershipRole` in the existing user
directory and keeps all access limits.
> - The benefit is that agents can verify an approver's role without
admin permissions.

## Linked Issues or Issue Description

**What happened?**

`GET /api/companies/{companyId}/user-directory` returns active users
without `membershipRole`. An agent that checks the authority of an
approving user cannot tell an `owner` from other roles. `/members`
requires `users:manage_permissions`.

**Expected behavior**

The directory returns the exact `membershipRole` from
`companyMemberships`. A missing role stays `null`. Reading this field
does not require admin permissions. Member management keeps its current
limits.

**Steps to reproduce**

1. Use a company with an active `owner` user and an active user without
a role.
2. Call `/user-directory` with an agent key.
3. Before this change, neither record has `membershipRole`. After this
change, the field is `owner` and `null`.
4. The same agent still cannot read `/members` or change a member.

## What Changed

- Add `membershipRole` to the select and result of
`loadCompanyUserDirectory`.
- Test the exact `owner`/`null` values, agent read access, denied admin
read and change, denied other company, and denied unauthenticated
request.
- Keep the existing `assertCompanyAccess` and the company, user type,
and active filters.
- The client type includes a nullable `membershipRole`. The management
test rejects only `users:manage_permissions` and checks the exact calls.
The first review findings are fixed in a0d1ad3.

## Verification

- `pnpm --filter @paperclipai/server typecheck`: pass.
- `pnpm --filter @paperclipai/server build`: pass.
- `pnpm --filter @paperclipai/ui typecheck`: pass.
- `pnpm exec vitest run
server/src/__tests__/company-user-directory-route.test.ts
server/src/__tests__/access-service.test.ts
server/src/__tests__/access-routes-hidden-floor.test.ts`: 16 passed, 17
skipped. `ui/src/lib/company-members.test.ts` is added.
- The regression test fails on the old `access.ts` because
`membershipRole` is missing. It passes after the change.
- `git diff --check`: pass.

## Risks

The directory response gets one more field. There are no migrations. The
role comes directly from the server record, with no `owner` fallback.
User management permissions do not change.

## Model Used

OpenAI Codex, GPT-6, with shell tools, code editing, and test runs. The
environment does not expose the exact model identifier.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-11-05 14:43:02 -06:00

150 lines
5.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type { CompanyMember, CompanyUserDirectoryEntry } from "@/api/access";
import {
buildCompanyUserInlineOptions,
buildCompanyUserLabelMap,
buildCompanyUserProfileMap,
buildIssueMentionOptions,
buildMarkdownMentionOptions,
} from "./company-members";
const activeMember = (overrides: Partial<CompanyMember>): CompanyMember => ({
id: overrides.id ?? "member-1",
companyId: overrides.companyId ?? "company-1",
principalType: "user",
principalId: overrides.principalId ?? "user-1",
status: overrides.status ?? "active",
membershipRole: overrides.membershipRole ?? "operator",
createdAt: overrides.createdAt ?? "2026-01-01T00:00:00.000Z",
updatedAt: overrides.updatedAt ?? "2026-01-01T00:00:00.000Z",
user: overrides.user === undefined
? { id: overrides.principalId ?? "user-1", name: "Taylor", email: "taylor@example.com", image: null }
: overrides.user,
grants: overrides.grants ?? [],
});
describe("company-members helpers", () => {
it("builds labels from company member profiles", () => {
const labels = buildCompanyUserLabelMap([
activeMember({ principalId: "user-1", user: { id: "user-1", name: "Taylor", email: "taylor@example.com", image: null } }),
activeMember({ id: "member-2", principalId: "local-board", user: null }),
]);
expect(labels.get("user-1")).toBe("Taylor");
expect(labels.get("local-board")).toBe("Board");
});
it("builds user profiles with labels and avatars", () => {
const profiles = buildCompanyUserProfileMap([
activeMember({
principalId: "user-1",
user: { id: "user-1", name: "Taylor", email: "taylor@example.com", image: "https://example.com/taylor.png" },
}),
activeMember({ id: "member-2", principalId: "local-board", user: null }),
]);
expect(profiles.get("user-1")).toEqual({
label: "Taylor",
image: "https://example.com/taylor.png",
});
expect(profiles.get("local-board")).toEqual({
label: "Board",
image: null,
});
});
it("builds inline options for active users and excludes requested ids", () => {
const options = buildCompanyUserInlineOptions([
activeMember({ principalId: "user-1", user: { id: "user-1", name: "Taylor", email: "taylor@example.com", image: null } }),
activeMember({ id: "member-2", principalId: "user-2", user: { id: "user-2", name: "Jordan", email: "jordan@example.com", image: null } }),
activeMember({ id: "member-3", principalId: "user-3", status: "suspended" }),
], { excludeUserIds: ["user-1"] });
expect(options).toEqual([
{
id: "user:user-2",
label: "Jordan",
searchText: "Jordan jordan@example.com user-2",
},
]);
});
it("includes human users in markdown mention options", () => {
const options = buildMarkdownMentionOptions({
members: [activeMember({ principalId: "user-1", user: { id: "user-1", name: "Taylor", email: "taylor@example.com", image: null } })],
agents: [{ id: "agent-1", name: "CodexCoder", status: "active", icon: "code" }],
projects: [{ id: "project-1", name: "Paperclip App", color: "#336699" }],
});
expect(options).toEqual([
{ id: "user:user-1", name: "Taylor", kind: "user", userId: "user-1" },
{ id: "agent:agent-1", name: "CodexCoder", kind: "agent", agentId: "agent-1", agentIcon: "code" },
{ id: "project:project-1", name: "Paperclip App", kind: "project", projectId: "project-1", projectColor: "#336699" },
]);
});
it("builds issue mention options with identifier + title search text", () => {
const options = buildIssueMentionOptions([
{ id: "issue-1", identifier: "PAP-102", title: "@task references" },
{ id: "issue-2", identifier: "PAP-7", title: "" },
]);
expect(options).toEqual([
{
id: "issue:issue-1",
name: "PAP-102 @task references",
kind: "issue",
issueId: "issue-1",
issueIdentifier: "PAP-102",
},
{
id: "issue:issue-2",
name: "PAP-7",
kind: "issue",
issueId: "issue-2",
issueIdentifier: "PAP-7",
},
]);
});
it("appends issue mention options after agents and projects, preserving order", () => {
const options = buildMarkdownMentionOptions({
agents: [{ id: "agent-1", name: "CodexCoder", status: "active", icon: "code" }],
projects: [{ id: "project-1", name: "Paperclip App", color: "#336699" }],
issues: [
{ id: "issue-2", identifier: "PAP-50", title: "Newer" },
{ id: "issue-1", identifier: "PAP-3", title: "Older" },
],
});
expect(options.map((option) => option.id)).toEqual([
"agent:agent-1",
"project:project-1",
"issue:issue-2",
"issue:issue-1",
]);
});
it("accepts read-only directory entries for assignee and mention helpers", () => {
const users: CompanyUserDirectoryEntry[] = [
{
principalId: "user-1",
status: "active",
membershipRole: null,
user: { id: "user-1", name: "Taylor", email: "taylor@example.com", image: null },
},
];
expect(buildCompanyUserInlineOptions(users)).toEqual([
{
id: "user:user-1",
label: "Taylor",
searchText: "Taylor taylor@example.com user-1",
},
]);
expect(buildMarkdownMentionOptions({ members: users })).toEqual([
{ id: "user:user-1", name: "Taylor", kind: "user", userId: "user-1" },
]);
});
});