Re: [pgtap-users] questions about user impersonization

9 views
Skip to first unread message

David E. Wheeler

unread,
Mar 14, 2025, 11:57:45 AMMar 14
to Point Lamont, pgTAP Users
On Feb 24, 2025, at 06:18, Point Lamont <point...@gmail.com> wrote:

> I was wondering on how to impersonate a user to test a function.
>
> Suppose I have a role 'user' and and two users: userA and userB with role 'user'.
> And a function that returns "TRUE" if it is executed by user userA and "FALSE" if by userB.
> Row level security is enabled.

Sure. Just use SET ROLE[1] to the user you want to use, something like:

```sql
SET ROLE userA;
SELECT ok(myfunc(), 'User A gets true');
SET ROLE userB;
SELECT ok(NOT myfunc(), 'User A gets true');

Best,

David

[1]: https://www.postgresql.org/docs/current/sql-set-role.html

signature.asc
Reply all
Reply to author
Forward
0 new messages