15

I'm using the owin TestServer class to unit test my web api. It's working great except that I'm unsure how to authenticate requests, so I can't really test endpoints that require authentication, other than to make sure that unauthenticated requests are unauthorized.

I am configuring WebApi to use only bearer token authentication as indicated here:

// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

Any ideas how I could do this easily?

w.brian
  • 16,296
  • 14
  • 69
  • 118
  • 2
    Wow, I've just hit a very similar issue and one of the few google hits is asked just 8 hours ago :) I've just moved to OWIN - before I was just using in memory HttpServer for integration tests and "Thread.CurrentPrincipal = new ClientRolePrincipal" but this doesn't seem to work with Katana\OWIN. Did you find a solution to this? – mutex Nov 11 '13 at 01:14
  • I did not. If you vote up the question it should get it more exposure, and I plan on putting a bounty on it when eligible! – w.brian Nov 11 '13 at 15:39
  • 1
    Done. I found a workaround for my issue, no idea if it will help you and it's certainly not optimal, but perhaps it might help: http://stackoverflow.com/questions/19897705/authentication-with-microsoft-owin-testing-testserver-for-in-memory-integration/19920179#19920179 – mutex Nov 12 '13 at 03:12
  • Any solution is better than no solution! Thanks! – w.brian Nov 12 '13 at 22:15

1 Answers1

5

I found useful documentation to use TestServer+OAuth authentication
http://www.aaron-powell.com/posts/2014-01-12-integration-testing-katana-with-auth/

He also provides whole VS solution at GitHub.
https://github.com/aaronpowell/Owin.AuthenticatedTests/tree/a154931dd82e5180daf7163c2129f3a90401df55

Josh
  • 2,142
  • 2
  • 23
  • 20
Youngjae
  • 24,352
  • 18
  • 113
  • 198