Search code examples
sql-servertestinguser-roles

How to simulate user rights problems on a SQL Server with trusted connection?


I developed a small C# utility that performs data verification, clean up and export. Now I am writing a manual for users and I would like to simulate the situation where users might not have enough rights to perform certain database operations (SELECT or UPDATE) on particular tables.

The utility is very small and just relies on current user's Windows credentials by using the trusted connection to the database, this means it uses the credential of my Windows account that are propagated from AD. I have full rights on the database server, but I can't revoke some rights from myself or map my login to another database user with less rights, because my login is already mapped to dbo and I can't unmap it (the error message "dbo user cannot be dropped" comes if I try it).

So, how can I simulate the lack of necessary rights for my utility?


Solution

  • You could create and throw a SqlException within your tests to simulate the condition of not having rights to perform a certain action.

    You'll need to use some reflection (check this out for a helper class) to get the proper errors added to the Errors property of the exception.