DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Zero to AI Hero, Part 3: Unleashing the Power of Agents in Semantic Kernel
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core

Trending

  • The Hidden Latency of Autoscaling
  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  • Spring Boot Done Right: Lessons From a 400-Module Codebase
  • How Reactive Scaling Drains Your Cloud Budget Without Warning
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Faking Azure AD Identity in ASP.NET Core Unit Tests

Faking Azure AD Identity in ASP.NET Core Unit Tests

Unit testing ASP.NET apps that use Microsoft Azure AD usually means working with an authenticated user. Here's how to make one for your tests.

By 
Gunnar Peipman user avatar
Gunnar Peipman
·
Jul. 21, 17 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
5.8K Views

Join the DZone community and get the full member experience.

Join For Free

when testing asp.net core controllers in an application that uses azure ad, we usually need a current user, at least for some tests. as there is no authenticated user when unit testing, we need to create one on our own. this blog post shows how to create a claims identity for asp.net core unit tests.

to have an azure ad user available, we have to create a fake claims identity and fill it with claims that the test expects. then we create the instance of a constructor and initiate the controller and http context. the latter one contains identity.

consider the following controller action.

public iactionresult index()
{
    var name = user.identity.name;       // do something with the name
 
    return view();
}


the test like this will probably fail with an exception, as there is no code that deals with the current user identity.

[fact]
public void sampletest()
{
    var controller = new homecontroller();       controller.index();
}


let’s organize a typical claims identity with some claims to the controller.

[fact]
public void sampletest()
{
    var user = new claimsprincipal(new claimsidentity(new claim[]
    {
            new claim(claimtypes.nameidentifier, "somevaluehere"),
            new claim(claimtypes.name, "[email protected]")
            // other required and custom claims
    }));       var controller = new homecontroller();
    controller.controllercontext = new controllercontext()
    {
        httpcontext = new defaulthttpcontext { user = user }
    };       controller.index();
}


now we have a fake claims identity available, which our controller will use. when running this test in visual studio, we can see that controller has the current user now.

aspnet-core-unit-test-claims-identity

it is also possible to use factory classes for identity. in this case, we have one class for production, which returns the current identity the controller has. another class is for testing, and this class creates and returns fake identity. as i don’t see much benefit in using factories with no additional value, i suggest going with the directly created fake identity. creating the identity can be moved to some helper method in the test class if it is needed in multiple tests.

unit test ASP.NET Core ASP.NET ADS (motorcycle) azure

Published at DZone with permission of Gunnar Peipman. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Zero to AI Hero, Part 3: Unleashing the Power of Agents in Semantic Kernel
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook