Unit Test Web Api 2 Mock User

ozstudent

I'm trying to mock the User.Identity in my Api Controller test.

This is my api method:

    [Route(Urls.CustInfo.GetCustomerManagers)]
    public HttpResponseMessage GetCustomerManagers([FromUri]int groupId = -1)
    {
        var user = User.Identity.Name;
        if (IsStaff(user) && groupId == -1)
        {
            return ErrorMissingQueryStringParameter;
        }
        ...
    }

I followed the suggestion in this post: Set User property for an ApiController in Unit Test to set the User property.

This is my test:

    [TestMethod]
    public void User_Without_Group_Level_Access_Call_GetCustomerManagers_Should_Fail()
    {
        Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("Bob", "Passport"), new[] {"managers"}); 
        var response = m_controller.GetCustomerManagers();

        Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
    }

But when the test is run, the User property is always null.

I even tried moving the line for setting the CurrentPrincipal into the api method just before calling User.Identity, but it's still null.

What am I doing wrong? If this approach doesn't work for web api 2, what's the best way to simulate/mock the User property?

Thanks!

LostInComputer

You can set the user in ControllerContext.RequestContext.Principal:

controller.ControllerContext.RequestContext.Principal = new GenericPrincipal(new GenericIdentity("Bob", "Passport"), new[] {"managers"});

Or a shorthand equivalent:

controller.User = new GenericPrincipal(new GenericIdentity("Bob", "Passport"), new[] {"managers"});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mock Request Object for Web API unit test

From Dev

Unable to mock Owin context in C# WEB API REST service unit test

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

Mock controller in Symfony2 unit test

From Dev

Mock controller in Symfony2 unit test

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

Mock IMemoryCache in unit test

From Dev

Unit Test Web Api Controller fake ApiController.User with custom Identity doesn't work

From Dev

Delete an item unit test in web api

From Dev

Mock/Stub a RuntimeException in unit test

From Dev

Spock Mock not working for unit test

From Dev

How to mock springSecurityService in an unit test

From Dev

How to mock this unit test in Python?

From Dev

Unit test a void method with Mock?

From Dev

Unit Test of Retrofit 2 api call with Mockito

From Dev

Unit test for a Web API method without any injected dependency

From Dev

Unit test for a Web API method without any injected dependency

From Dev

Custom validator attribute works in unit test but not web api controller?

From Dev

Unit Test for Api

From Dev

Unit test command class with mock repository

From Dev

How to android unit test and mock a static method

From Dev

Correct way to mock an AngularJS Service in a unit test?

From Dev

how to mock $window to unit test AngularJS service?

From Java

Using Moq to mock an asynchronous method for a unit test

From Dev

Mock HttpContext using moq for unit test

From Dev

AngularJS override (mock) services for unit test

From Dev

How to mock the UserAgent property for an HttpRequest in a unit test?

From Dev

How to unit test this function without mock asserts?

From Dev

Angular js unit test mock document

Related Related

HotTag

Archive