lundi 4 avril 2016

Unit Tesing Azure Mobile .NET Controller with Authorization

I try to unit test my api controller with authorization in Azure Mobile App .Net Server.

My controller method looks like this:

[Authorize]
public HttpResponseMessage SomeMethod()
{  
var claimsPrincipal = this.User as ClaimsPrincipal;
string userId = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier).Value;
return Request.CreateResponse(HttpStatusCode.OK, userId);
}

I configured my request in unit test method like this:

[TestMethod]
public void SomeMyAPIControllerTest(){
var config = new HttpConfiguration();
var request = new HttpRequestMessage();
request.RequestUri = new Uri("http://localhost:50268/api/MyAPI");
request.Headers.Add("x-zumo-auth", "_user_auth_token_");
request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
var controller = new MyAPIController(TestContext)
{
Request = request
};
var response = controller.SomeMethod();
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}

My controller method works fine via swagger on localhost, but via unittest method it does not work. When I try to retrieve authenticated user information, "claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier)" returns null

I guess there is mistake in request configuration. Any suggestions?

Aucun commentaire:

Enregistrer un commentaire