Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

User not authenticated running integration tests
Lee Rothman
#1 Posted : Wednesday, April 29, 2015 2:32:39 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 4/29/2015(UTC)
Posts: 3
Location: United States of America

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.
Remco
#2 Posted : Wednesday, April 29, 2015 10:52:21 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,982

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
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.
Lee Rothman
#3 Posted : Friday, May 1, 2015 1:10:10 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 4/29/2015(UTC)
Posts: 3
Location: United States of America

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.
Remco
#4 Posted : Friday, May 1, 2015 10:54:29 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,982

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
Lee Rothman;7274 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?
Lee Rothman
#5 Posted : Friday, May 8, 2015 8:32:12 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 4/29/2015(UTC)
Posts: 3
Location: United States of America

Remco;7275 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.
Remco
#6 Posted : Friday, May 8, 2015 12:08:23 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,982

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
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.
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.051 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download