How do you impersonate a user when testing? (Mock a session) #2653
Unanswered
vincentrolfs
asked this question in
Q&A
Replies: 3 comments 4 replies
-
|
in export function getTestSession({
user,
orgId,
...rest
}: Partial<Ctx> & { user: User; orgId?: number }): Ctx {
return {
session: {
$authorize: jest.fn(),
$isAuthorized: jest.fn(() => true),
userId: user?.id,
roles: ["USER"],
$getPrivateData: jest.fn(),
$create: jest.fn(),
$handle: null,
$publicData: {
roles: ["USER"],
userId: user.id,
orgId,
},
$revoke: jest.fn(),
$revokeAll: jest.fn(),
$setPrivateData: jest.fn(),
$setPublicData: jest.fn(),
orgId,
...rest.session,
},
}
}and then use it like below. this is test assertions from my expect(async () =>
inviteUser(
{
asRole: "USER",
invitedUserEmail: "inviteduseremail@test.com",
invitedUserName: "Invited User",
},
getTestSession({ user: inviter })
)
).rejects.toThrow("User is not logged into any organization")const orgId = 123;
const result = await inviteUser(
{
asRole: "USER",
invitedUserEmail: "inviteduseremail@test.com",
invitedUserName: "Invited User",
},
getTestSession({ user: inviter, orgId })
)
// should succeed |
Beta Was this translation helpful? Give feedback.
4 replies
-
|
Fyi, I opened an issue on making this easier to do: #2654 |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Thanks a lot, my original issue is solved. I described the solution here: #2654 (comment) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I want to write an integration test where I create a user and then call a query while acting as this user. Essentially I need to mock the session for that user. What is the standard way to do this in blitz?
I could not find any information about this in the testing section or the session section.
Thanks a lot!
EDIT: More specifically, I need to test the authorization capabilities of the app, so mocking the session in the following is not sufficient:
I want to say "pretend I am logged in as this user and I call that query, what happens?"
Beta Was this translation helpful? Give feedback.
All reactions