Problems with CSLA 4 Authorization, MSTest, and Authorization Rules Caching

Problems with CSLA 4 Authorization, MSTest, and Authorization Rules Caching

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


JonStonecash posted on Monday, June 14, 2010

I have implemented a "claims-based" authorization approach using the CSLA 4 authorization objects.  I am trying to unit test the various relationships at the object level between what is requested and what is authorized for object CRUD operations.  In each test method, I am setting up the rule object such that it permits Read, Edit, Creae, or Delete, and then calling the one of the Class.CanGet, CanEdit, CanCreate, and CanDelete methods to see if I get the correct result.  This results in 16 seperate tests in a single test class. 

My problem is that the individual tests ([TestMethod]) work if run by themselves, using VS2010 MSTest.  If I run the tests as a batch, some but not all of them fail.  I have tracked down the cause of the problem to the fact that CSLA is caching the rules.  The result is that while I am setting up new rules for each test method, CSLA is caching the first one that it sees and re-using it. 

I am not sure of how I would get around this.  Before I charge off in all directions, I thought that I would ask the forum for advice.  I have other unit tests of the authorization rules to ensure that the rules work.  These tests were to ensure that the wiring between the objects and the rules was working.  I could reduce this to a single test but it would be nice to have these tests working as well.

Any thoughts?

Jon Stonecash

RockfordLhotka replied on Monday, June 14, 2010

CSLA will clear the cache if the principal is different. I think it uses ReferenceEquals - so all you need to do is make sure there's a new principal object set at the top of each test method.

Copyright (c) Marimer LLC