-
-
Notifications
You must be signed in to change notification settings - Fork 768
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Cross-Household Recipes (#4089)
- Loading branch information
1 parent
7ef2e91
commit 9acf9ec
Showing
16 changed files
with
545 additions
and
92 deletions.
There are no files selected for viewing
This file contains 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 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 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
81 changes: 81 additions & 0 deletions
81
frontend/composables/recipes/use-recipe-permissions.test.ts
This file contains 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,81 @@ | ||
import { describe, test, expect } from "vitest"; | ||
import { useRecipePermissions } from "./use-recipe-permissions"; | ||
import { Recipe } from "~/lib/api/types/recipe"; | ||
import { UserOut } from "~/lib/api/types/user"; | ||
|
||
describe("test use recipe permissions", () => { | ||
const commonUserId = "my-user-id"; | ||
const commonGroupId = "my-group-id"; | ||
const commonHouseholdId = "my-household-id"; | ||
|
||
const createRecipe = (overrides: Partial<Recipe>, isLocked = false): Recipe => ({ | ||
id: "my-recipe-id", | ||
userId: commonUserId, | ||
groupId: commonGroupId, | ||
householdId: commonHouseholdId, | ||
settings: { | ||
locked: isLocked, | ||
}, | ||
...overrides, | ||
}); | ||
|
||
const createUser = (overrides: Partial<UserOut>): UserOut => ({ | ||
id: commonUserId, | ||
groupId: commonGroupId, | ||
groupSlug: "my-group", | ||
group: "my-group", | ||
householdId: commonHouseholdId, | ||
householdSlug: "my-household", | ||
household: "my-household", | ||
email: "[email protected]", | ||
cacheKey: "1234", | ||
...overrides, | ||
}); | ||
|
||
test("when user is null, cannot edit", () => { | ||
const result = useRecipePermissions(createRecipe({}), null); | ||
expect(result.canEditRecipe.value).toBe(false); | ||
}); | ||
|
||
test("when user is recipe owner, can edit", () => { | ||
const result = useRecipePermissions(createRecipe({}), createUser({})); | ||
expect(result.canEditRecipe.value).toBe(true); | ||
}); | ||
|
||
test("when user is not recipe owner, is correct group and household, and recipe is unlocked, can edit", () => { | ||
const result = useRecipePermissions( | ||
createRecipe({}), | ||
createUser({ id: "other-user-id" }), | ||
); | ||
expect(result.canEditRecipe.value).toBe(true); | ||
}); | ||
|
||
test("when user is not recipe owner, and user is other group, cannot edit", () => { | ||
const result = useRecipePermissions( | ||
createRecipe({}), | ||
createUser({ id: "other-user-id", groupId: "other-group-id"}), | ||
); | ||
expect(result.canEditRecipe.value).toBe(false); | ||
}); | ||
|
||
test("when user is not recipe owner, and user is other household, cannot edit", () => { | ||
const result = useRecipePermissions( | ||
createRecipe({}), | ||
createUser({ id: "other-user-id", householdId: "other-household-id" }), | ||
); | ||
expect(result.canEditRecipe.value).toBe(false); | ||
}); | ||
|
||
test("when user is not recipe owner, and recipe is locked, cannot edit", () => { | ||
const result = useRecipePermissions( | ||
createRecipe({}, true), | ||
createUser({ id: "other-user-id"}), | ||
); | ||
expect(result.canEditRecipe.value).toBe(false); | ||
}); | ||
|
||
test("when user is recipe owner, and recipe is locked, can edit", () => { | ||
const result = useRecipePermissions(createRecipe({}, true), createUser({})); | ||
expect(result.canEditRecipe.value).toBe(true); | ||
}); | ||
}); |
This file contains 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,34 @@ | ||
import { computed } from "@nuxtjs/composition-api"; | ||
import { Recipe } from "~/lib/api/types/recipe"; | ||
import { UserOut } from "~/lib/api/types/user"; | ||
|
||
export function useRecipePermissions(recipe: Recipe, user: UserOut | null) { | ||
const canEditRecipe = computed(() => { | ||
// Check recipe owner | ||
if (!user?.id) { | ||
return false; | ||
} | ||
if (user.id === recipe.userId) { | ||
return true; | ||
} | ||
|
||
// Check group and household | ||
if (user.groupId !== recipe.groupId) { | ||
return false; | ||
} | ||
if (user.householdId !== recipe.householdId) { | ||
return false; | ||
} | ||
|
||
// Check recipe | ||
if (recipe.settings?.locked) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}); | ||
|
||
return { | ||
canEditRecipe, | ||
} | ||
} |
This file contains 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 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 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 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 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
Oops, something went wrong.