Build/Test Issues

User not authenticated running integration tests

Started by Lee Rothman on 5,075 views

I'm having an issue running some integration tests with ncrunch. Within our unit of work save changes method we check that the user has been authenticated using the .net System.Security.Principal.GenericPrincipal.Current and inspecting the Identity.IsAuthenticated property. This is returning false, however the visual studio test runner returns true. I'm using version 2.14.0.8 with VS 2013.
Hi Lee,

Thanks for sharing this issue.

Is it possible that the test itself isn't performing authentication, and it's relying on a previously executed test to set this state? Note that NCrunch can run tests in any order, which means that sequence dependent tests can fail under NCrunch where they normally wouldn't fail under another runner. For more information, have a look at http://www.ncrunch.net/documentation/considerations-and-constraints_test-atomicity.
Hi,

Thanks for the response. I don't think so, even if I run one of the failing tests in isolation it still passes with the VS test runner, but fails in ncrunch.
Lee Rothman wrote:Hi,

Thanks for the response. I don't think so, even if I run one of the failing tests in isolation it still passes with the VS test runner, but fails in ncrunch.


Ok, sorry, that's a very different story then. Which test framework are you using? Is it MSTest? How is the initialisation code hooked into the test framework?
Remco wrote:
Ok, sorry, that's a very different story then. Which test framework are you using? Is it MSTest? How is the initialisation code hooked into the test framework?


Yes, test framework is MSTest. Not sure what you mean by hooked in? But the test code is as follows. (Sorry I can't find away of attaching files)


[TestInitialize]
public void Init()
{
ctx = new CMTContext(ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString);
unitOfWork = new UnitOfWork(ctx);
}

[TestMethod]
[TestCategory("Integration")]
public void GetChanges_Returns_Deleted_Entities()
{
var delivery = new DeliveryEntity(
Guid.NewGuid(),
"TEST UNIT OF WORK AUDIT (DELETED RECORDS)",
"USED ON INTEGRATION TEST FOR THE UNIT OF WORK",
"TEST",
new List<Guid> { Guid.NewGuid() },
DeliveryStatus.Active,
new DailyScheduleEntity(DateTime.Parse("19/03/2015 14:00"), 2));

unitOfWork.DeliveryRepository.Add(delivery);
unitOfWork.SaveChanges();

var retrieved = unitOfWork.DeliveryRepository.Find(delivery.Id);
unitOfWork.DeliveryRepository.Delete(retrieved);

var changes = ctx.GetChanges().ToList();

unitOfWork.SaveChanges();


var toCompare = changes.OfType<DeliveryEntity>().SingleOrDefault();
Assert.IsNotNull(toCompare);
Assert.AreEqual(retrieved, toCompare);

}

The code that the test runs is

public void SaveChanges()
{
var entitiesChanged = dbContext.GetChanges().OfType<IAggregate>().ToList();

var user = System.Security.Principal.GenericPrincipal.Current;

if(user == null || !user.Identity.IsAuthenticated) throw new InvalidOperationException("An unexpected exception occured. \nThere is no security principel defined.");

entitiesChanged.ForEach(a => {
var audit = new AuditEntity(
a,
user.Identity.Name
);
AuditRepository.Add(audit);
});

dbContext.SaveChanges();
}

Because the user is not authenticated an exception is thrown.

Hope that helps in identifying the problem.

Cheers,
Lee.

Edited

Thanks Lee.

It looks like there is some magic in MSTest that is responsible for setting the GenericPrincipal.Current value. Unfortunately as this behaviour isn't documented anywhere, I am not sure how to emulate it in NCrunch or what the expected behaviour truely should be.

It may be worth examining the objective of this code to see if there may be a risk of it being dependent on certain environment-specific behaviour. NCrunch will run tests in a standard .NET console application, so this could mean that the code would fail if executed in a production environment in this manner.

Is it possible you instead mean to establish the currently logged in user through System.Security.Principal.WindowsIdentity.GetCurrent() instead? This method seems to behave in a consistent manner under both VSTest and NCrunch.

Post a reply

Log in to reply.