-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix errors when user has insufficient permissions (#5102)
* include private metadata when user has permissions * gift card only permission fix * changesets * fix crash * Add tests * fix ts errors * cr * improve tests * improve changeset * various gift card view improvements * json messages --------- Co-authored-by: Paweł Chyła <[email protected]>
- Loading branch information
Showing
24 changed files
with
424 additions
and
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"saleor-dashboard": patch | ||
--- | ||
|
||
Gift card details page and customer details page no longer crash when you have only some required permissions. This means that you can now view gift card details page if you have only gift card permissions and customer details page if you have only customer permissions. |
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
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
114 changes: 114 additions & 0 deletions
114
src/giftCards/GiftCardUpdate/GiftCardHistory/hooks/useGiftCardHistoryEvents.test.tsx
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,114 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
import React from "react"; | ||
|
||
import { giftCardsMocks } from "../../../../../testUtils/mocks/giftCards"; | ||
import { useUser } from "../../../../auth"; | ||
import { useGiftCardEventsQuery } from "../../../../graphql"; | ||
import { GiftCardDetailsContext } from "../../providers/GiftCardDetailsProvider"; | ||
import useGiftCardHistoryEvents from "./useGiftCardHistoryEvents"; | ||
|
||
// Mock the dependencies | ||
jest.mock("@dashboard/auth"); | ||
jest.mock("@dashboard/graphql"); | ||
|
||
const mockUseUser = useUser as jest.Mock; | ||
const mockUseGiftCardEventsQuery = useGiftCardEventsQuery as jest.Mock; | ||
|
||
const mockGiftCard = { | ||
...giftCardsMocks[0], | ||
id: "giftCardId", | ||
}; | ||
const mockUser = { | ||
userPermissions: [{ code: "MANAGE_USERS" }, { code: "MANAGE_APPS" }], | ||
}; | ||
|
||
mockUseUser.mockReturnValue({ user: mockUser }); | ||
|
||
describe("useGiftCardHistoryEvents", () => { | ||
it("should return gift card events", () => { | ||
// Arrange | ||
mockUseGiftCardEventsQuery.mockImplementation(() => ({ | ||
data: { | ||
giftCard: { | ||
events: [ | ||
{ id: "event1", app: {}, user: {} }, | ||
{ id: "event2", app: {}, user: {} }, | ||
], | ||
}, | ||
}, | ||
})); | ||
|
||
const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
<GiftCardDetailsContext.Provider value={{ giftCard: mockGiftCard, loading: false }}> | ||
{children} | ||
</GiftCardDetailsContext.Provider> | ||
); | ||
|
||
// Act | ||
const { result } = renderHook(() => useGiftCardHistoryEvents(), { wrapper }); | ||
|
||
// Assert | ||
expect(result.current.id).toBe("giftCardId"); | ||
expect(result.current.events).toEqual([ | ||
{ id: "event1", app: {}, user: {} }, | ||
{ id: "event2", app: {}, user: {} }, | ||
]); | ||
expect(mockUseGiftCardEventsQuery).toBeCalledWith({ | ||
variables: { id: "giftCardId", canSeeApp: true, canSeeUser: true }, | ||
skip: false, | ||
}); | ||
}); | ||
|
||
it("should return undefined when there is no gift card", () => { | ||
// Arrange | ||
mockUseGiftCardEventsQuery.mockImplementation(() => ({ | ||
data: undefined, | ||
})); | ||
|
||
const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
<GiftCardDetailsContext.Provider value={{ giftCard: undefined, loading: false }}> | ||
{children} | ||
</GiftCardDetailsContext.Provider> | ||
); | ||
|
||
// Act | ||
const { result } = renderHook(() => useGiftCardHistoryEvents(), { wrapper }); | ||
|
||
// Assert | ||
expect(result.current.id).toBeUndefined(); | ||
expect(result.current.events).toBeUndefined(); | ||
expect(mockUseGiftCardEventsQuery).toBeCalledWith({ | ||
variables: undefined, | ||
skip: true, | ||
}); | ||
}); | ||
|
||
it("should not return app and user when user does not have permissions", () => { | ||
// Arrange | ||
mockUseUser.mockReturnValue({ user: { userPermissions: [] } }); | ||
mockUseGiftCardEventsQuery.mockImplementation(() => ({ | ||
data: { | ||
giftCard: { | ||
events: [{ id: "event1" }, { id: "event2" }], | ||
}, | ||
}, | ||
})); | ||
|
||
const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
<GiftCardDetailsContext.Provider value={{ giftCard: mockGiftCard, loading: false }}> | ||
{children} | ||
</GiftCardDetailsContext.Provider> | ||
); | ||
|
||
// Act | ||
const { result } = renderHook(() => useGiftCardHistoryEvents(), { wrapper }); | ||
|
||
// Assert | ||
expect(result.current.id).toBe("giftCardId"); | ||
expect(result.current.events).toEqual([{ id: "event1" }, { id: "event2" }]); | ||
expect(mockUseGiftCardEventsQuery).toBeCalledWith({ | ||
variables: { id: "giftCardId", canSeeApp: false, canSeeUser: false }, | ||
skip: false, | ||
}); | ||
}); | ||
}); |
23 changes: 21 additions & 2 deletions
23
src/giftCards/GiftCardUpdate/GiftCardHistory/hooks/useGiftCardHistoryEvents.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { gql } from "@apollo/client"; | ||
|
||
export const giftCardEvents = gql` | ||
query GiftCardEvents($id: ID!, $canSeeApp: Boolean!, $canSeeUser: Boolean!) { | ||
giftCard(id: $id) { | ||
events { | ||
...GiftCardEvent | ||
app @include(if: $canSeeApp) { | ||
id | ||
name | ||
brand { | ||
logo { | ||
default(size: 128) | ||
} | ||
} | ||
} | ||
user @include(if: $canSeeUser) { | ||
...UserBase | ||
avatar(size: 128) { | ||
url | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`; |
Oops, something went wrong.