How do I execute this DotNetOpenAuth Response object?
I have a method that accepts a DotNetOpenAuth (formally known as DotNetOpenId) Response
object. My method fetches any declared data, checks if this user exists on our system, yadda yadda yadda ... and upon completion returns an instance of the auth'd user.
Now .. how can I use moq to simulate this response object to test my authentication method ( AuthenticateUser()
)?
switch (response.Status)
{
case AuthenticationStatus.Authenticated:
User user = null;
try
{
// Extract the claimed information and
// check if this user is valid, etc.
// Any errors with be thrown as Authentication Errors.
user = _authenticationService.AuthenticateUser(response) as User;
}
catch (AuthenticationException exception)
{
ViewData.ModelState.AddModelError("AuthenticationError", exception);
}
.. other code, like forms auth, other response.status' etc. ..
}
Mocking framework: moq
Language: .NET C # 3.5 sp1
Response object: taken from DotNetOpenAuth framework
source to share
I'm not familiar with Moq in particular, but the response object is an implementing type, DotNetOpenAuth.OpenId.RelyingParty.IAuthenticationResponse
so it can be easily mocked by creating a class that implements the same interface and is willing to return the same kinds of value.
... just downloaded Moq and mocked the IAuthenticationResponse like so:
var response = new Mock<IAuthenticationResponse>(MockBehavior.Loose);
response.SetupGet(r => r.ClaimedIdentifier)
.Returns("http://blog.nerdbank.net/");
response.SetupGet(r => r.Status)
.Returns(AuthenticationStatus.Authenticated);
response.SetupGet(r => r.FriendlyIdentifierForDisplay)
.Returns("blog.nerdbank.net");
IAuthenticationResponse resp = response.Object;
Console.WriteLine(resp.ClaimedIdentifier);
Obviously, instead of sending the result to, Console.WriteLine
you need to pass the object to the resp
method you are testing.
source to share