Repository navigation
fix: persist group assignment when editing a non-group expense #772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mvanhorn
wants to merge
3
commits into
oss-apps:main
Choose a base branch
from
mvanhorn:fix/763-persist-expense-group-on-edit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+229
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
src/tests/integration/expenseGroupReassignment.integration.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import { db } from './database'; | ||
| import { accountingSnapshot, expenseInput, testGroup, testScenario, testUser } from './factories'; | ||
| import { callerFor } from './trpc'; | ||
|
|
||
| describe('assigning an ungrouped expense to a group', () => { | ||
| it('persists the group when an ungrouped expense is edited into a group', async () => { | ||
| const { owner, member, group } = await testScenario(); | ||
| const caller = callerFor(owner.id); | ||
| const input = expenseInput(owner.id, member.id); | ||
| const [expense] = await caller.expense.addOrEditExpense(input); | ||
|
|
||
| await caller.expense.addOrEditExpense({ ...input, expenseId: expense!.id, groupId: group.id }); | ||
|
|
||
| expect(await db.expense.findUnique({ where: { id: expense!.id } })).toMatchObject({ | ||
| groupId: group.id, | ||
| }); | ||
| expect(await caller.expense.getGroupExpenses({ groupId: group.id })).toEqual([ | ||
| expect.objectContaining({ id: expense!.id }), | ||
| ]); | ||
| expect( | ||
| await db.balanceView.findMany({ where: { groupId: group.id }, orderBy: { userId: 'asc' } }), | ||
| ).toEqual([ | ||
| expect.objectContaining({ userId: owner.id, friendId: member.id, amount: 1000n }), | ||
| expect.objectContaining({ userId: member.id, friendId: owner.id, amount: -1000n }), | ||
| ]); | ||
| }); | ||
|
|
||
| it('assigns both linked records of an ungrouped currency conversion', async () => { | ||
| const { owner, member, group } = await testScenario(); | ||
| const conversion = { | ||
| amount: 1000n, | ||
| rate: 0.9, | ||
| from: 'USD', | ||
| to: 'EUR', | ||
| senderId: owner.id, | ||
| receiverId: member.id, | ||
| groupId: null, | ||
| }; | ||
| await callerFor(owner.id).expense.addOrEditCurrencyConversion(conversion); | ||
| const expense = await db.expense.findFirstOrThrow({ where: { conversionToId: { not: null } } }); | ||
|
|
||
| await callerFor(owner.id).expense.addOrEditCurrencyConversion({ | ||
| ...conversion, | ||
| expenseId: expense.id, | ||
| groupId: group.id, | ||
| }); | ||
|
|
||
| expect( | ||
| (await db.expense.findMany({ select: { groupId: true } })).map(({ groupId }) => groupId), | ||
| ).toEqual([group.id, group.id]); | ||
| }); | ||
|
|
||
| it('rejects a caller who neither added nor participates in the expense', async () => { | ||
| const { owner, member, outsider } = await testScenario(); | ||
| const outsiderGroup = await testGroup(outsider.id, [owner.id, member.id]); | ||
| const input = expenseInput(owner.id, member.id); | ||
| const [expense] = await callerFor(owner.id).expense.addOrEditExpense(input); | ||
| const before = await accountingSnapshot(); | ||
|
|
||
| await expect( | ||
| callerFor(outsider.id).expense.addOrEditExpense({ | ||
| ...input, | ||
| expenseId: expense!.id, | ||
| groupId: outsiderGroup.id, | ||
| }), | ||
| ).rejects.toMatchObject({ code: 'UNAUTHORIZED' }); | ||
| expect(await accountingSnapshot()).toEqual(before); | ||
| }); | ||
|
|
||
| it.each([ | ||
| { | ||
| label: 'the caller is not a member', | ||
| payer: 'member', | ||
| groupMembers: ['other', 'member'], | ||
| code: 'FORBIDDEN', | ||
| message: 'Not a group member', | ||
| }, | ||
| { | ||
| label: 'the payer is not a member', | ||
| payer: 'member', | ||
| groupMembers: ['owner'], | ||
| code: 'BAD_REQUEST', | ||
| message: 'Payer is not a member of the group', | ||
| }, | ||
| { | ||
| label: 'a participant is not a member', | ||
| payer: 'owner', | ||
| groupMembers: ['owner'], | ||
| code: 'BAD_REQUEST', | ||
| message: 'Participant is not a member of the group', | ||
| }, | ||
| ] as const)( | ||
| 'rejects the assignment when $label', | ||
| async ({ payer, groupMembers, code, message }) => { | ||
| const users = { | ||
| owner: await testUser('Owner'), | ||
| member: await testUser('Member'), | ||
| other: await testUser('Other'), | ||
| }; | ||
| const otherParticipant = 'owner' === payer ? users.member : users.owner; | ||
| const input = expenseInput(users[payer].id, otherParticipant.id); | ||
| const [expense] = await callerFor(users.owner.id).expense.addOrEditExpense(input); | ||
| const [groupOwner, ...rest] = groupMembers.map((name) => users[name].id); | ||
| const group = await testGroup(groupOwner!, rest); | ||
| const before = await accountingSnapshot(); | ||
|
|
||
| await expect( | ||
| callerFor(users.owner.id).expense.addOrEditExpense({ | ||
| ...input, | ||
| expenseId: expense!.id, | ||
| groupId: group.id, | ||
| }), | ||
| ).rejects.toMatchObject({ code, message }); | ||
| expect(await accountingSnapshot()).toEqual(before); | ||
| }, | ||
| ); | ||
|
|
||
| it('rejects assigning an expense to an archived group', async () => { | ||
| const { owner, member, group } = await testScenario(); | ||
| const caller = callerFor(owner.id); | ||
| const input = expenseInput(owner.id, member.id); | ||
| const [expense] = await caller.expense.addOrEditExpense(input); | ||
| await db.group.update({ where: { id: group.id }, data: { archivedAt: new Date() } }); | ||
| const before = await accountingSnapshot(); | ||
|
|
||
| await expect( | ||
| caller.expense.addOrEditExpense({ ...input, expenseId: expense!.id, groupId: group.id }), | ||
| ).rejects.toMatchObject({ code: 'BAD_REQUEST', message: 'Group is archived' }); | ||
| expect(await accountingSnapshot()).toEqual(before); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
Establish expense-level permission before group reassignment. Target-group membership does not authorize editing the source expense. A caller with a known ungrouped expense ID can assign an expense they do not own to a group they belong to.
src/server/api/routers/expense.ts#L163-L163: fixvalidateEditExpensePermissionso itsaddedBybranch requiresaddedBy === ctx.session.user.id.src/server/api/routers/expense.ts#L235-L235: call the corrected expense-level permission check before editing a currency conversion.📍 Affects 1 file
src/server/api/routers/expense.ts#L163-L163(this comment)src/server/api/routers/expense.ts#L235-L235🤖 Prompt for AI Agents