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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How are you handling the data revolution? We want your take on what's real, what's hype, and what's next in the world of data engineering.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

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

  • Understanding the 5 Levels of LeetCode to Crack Coding Interview
  • How to Build Your First Generative AI App With Langflow: A Step-by-Step Guide
  • AI Agent Architectures: Patterns, Applications, and Implementation Guide
  • How to Format Articles for DZone
  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.6K 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, DZone MVB. 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.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: