Visual Studio Test of BO - Custom Identity

Visual Studio Test of BO - Custom Identity

Old forum URL: forums.lhotka.net/forums/t/4537.aspx


stanc posted on Friday, March 21, 2008

Let me start by saying I am new to unit tests in general.

I'm trying to create a unit test for my BO using VS2008. I created a very simple test to just call my public factory method to return my Object. I am having a problem with my custom Identity, which makes sense that I would but I am lost as to how to get around it. Below is the error I am gettting.

DataPortal.Create failed (System.InvalidCastException: Unable to cast object of type 'System.Security.Principal.GenericIdentity' to type 'Company.Library.Security.CIdentity'.

Any help would be appreciated.
-Stan

RockfordLhotka replied on Monday, March 24, 2008

In nunit you need to "log in" before each test method. This could be (and probably is) the case with mstest too, because each test is run in an isolated environment, and so the principal is lost between each test.

In the case of nunit you also must log out after each test method or you get a serialization exception because .NET tries to serialize the principal back to the calling shell process, and that can't happen. I don't know if that's an issue with mstest - depends on how they manage their threads.

stanc replied on Monday, March 24, 2008

Thanks Rocky-

That's what I thought. I would like to test different Authorization levels in my test, and I was wondering if you have any thoughts on how I could accomplish this. I am using network authenication. I would like to have an XLS file or something with different username/password combination that the test could use to log on with to run the test. Could I use impersonation to accomplish this?

 

RockfordLhotka replied on Monday, March 24, 2008

If you are using a custom principal (which you are from your problem description), then you totally control how that principal is established. If you create a DataPortal_Fetch() in your identity object that can load from an XLS file or whatever, then you can do that. Or if you have a separate DAL behind your identity object, then perhaps you can use a provider model to load some other DAL implementation that reads the XLS instead of your normal security database.

 

Or if you want to use your normal security database, then perhaps you can have some code in your test project that loads the XLS and provides the appropriate username/password at the start of each test.

 

Those options come to me off the top of my head anyway.

 

Rocky

 

stanc replied on Monday, March 24, 2008

Thanks.

I am experimenting with the last option. I am using Impersonation to change my identity at the start of each test, and I am reading the login information from an XLS file. This is all in a test environment, so I don’t have any security concerns about storing username/passwords in the XLS file. After I am done, I hope to find the time to create a sample to post back to the site.

 

Thanks for your help and guidance.

Stan

Copyright (c) Marimer LLC