Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Moved View/Negotiate to INancyModule #1653

Closed
wants to merge 6 commits into from

Conversation

phillip-haydon
Copy link
Member

Superseeds PR #1472

Add's missing XML Comment and moves View/Negotiate to the INancyModule

This allows the module to be mocked for use in 3rd party app testing.

i.e in SimpleAuthentication library we have an IAuthenticationProviderCallback class which has the module passed into, however this cannot be tested without attempting to mock the entire NancyModule.

@phillip-haydon phillip-haydon added this to the 1.0Alpha milestone Aug 10, 2014
NancyModule.ViewRenderer View { get; }

/// <summary>
/// Used to nedotiate the content returned based on Accepts header.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Negotiate is spelled wrong

@PureKrome
Copy link
Contributor

Hi guys,

I'm out of league here with trying to help resolve this issue - especially since I sorta raised this issue.

What I've got here are some real test code which I think might help highlight the problem more. I know you guys know what's going on .. but I feel that if I can provide you with some use-case tests .. it might help in some way.

To start off, this code is from my new SimpleAuthentication branch. In this library, the developer needs to implement an interface to complete the callback from the Provider. Only they know what they want to do when some user information comes back from Google, FB, etc.

Here's the interface signature:

dynamic Process(INancyModule nancyModule, AuthenticateCallbackResult result);

Notice how I'm passing in an INancyModule ? this is so we can pass in a faked version.

Now - lets see some test code which i would like to have ...

[Fact(Skip = "TODO")]
public void GivenAnAuthenticatedClientAndNoReturnUrl_Process_ReturnsAView()
{
    // Arrange.
    var nancyModule = A.Fake<INancyModule>();
    var accessToken = new AccessToken(...);
    var userInformation = new UserInformation(...);
    var authenticationProviderCallback = new FakeAuthenticationProviderCallback();

    var authenticationCallbackResult = new AuthenticateCallbackResult
    {
        AuthenticatedClient = new AuthenticatedClient("Google",
            accessToken,
            userInformation,
            "some raw info goes here pew pew pew")
    };

    // Act.
    var result = authenticationProviderCallback.Process(nancyModule, authenticationCallbackResult);

    // Assert.
    // Not sure how to do this yet.
}

So pretty simple so far... but we need some context ..

so .. for it's time for the money shot ...

public dynamic Process(INancyModule nancyModule, AuthenticateCallbackResult result)
{
    var model = new UserViewModel
    {
        Name = result.AuthenticatedClient.UserInformation.Name,
        Email = result.AuthenticatedClient.UserInformation.Email
    };

    // User cancelled during the Authentication process?
    if (result.AuthenticatedClient == null)
    {
        return nancyModule.Response.AsRedirect(result.ReturnUrl,
           RedirectResponse.RedirectType.Temporary);
    }

    // We have a user, so lets do something with their data :)
    if (string.IsNullOrWhiteSpace(result.ReturnUrl))
    {
        return nancyModule.Response.View[model];
    }

    return nancyModule.Response.AsRedirect(result.ReturnUrl);
}

It's a pretty simplistic and contrite example, but this is a unit test after all.
What's important here is that a user might need to do the following:

  • Render some view (ie. thanks for signing in or we need more info to complete this sign in process).
  • Redirect somewhere

And there is no View property on the current interface.

image

So yeah - I totally get that you guys know all this .. I just thought that by adding some code examples it might give some other clarity or help to the problem.

And right now - I can't go on until this is resolved 😢

@phillip-haydon
Copy link
Member Author

@NancyFx/most-valued-minions Ok this is ready to be pulled.

@PureKrome when this is in the next release (or CI builds) you can do:

return nancyModule.View[model];

@PureKrome
Copy link
Contributor

Yay!

yay

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants