diff options
Diffstat (limited to 'src/DotNetOpenAuth.Test/OAuth2')
-rw-r--r-- | src/DotNetOpenAuth.Test/OAuth2/AuthorizeTests.cs | 50 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Test/OAuth2/OAuth2ChannelTests.cs | 21 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Test/OAuth2/OAuth2Coordinator.cs | 63 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Test/OAuth2/OAuth2TestBase.cs | 18 |
4 files changed, 131 insertions, 21 deletions
diff --git a/src/DotNetOpenAuth.Test/OAuth2/AuthorizeTests.cs b/src/DotNetOpenAuth.Test/OAuth2/AuthorizeTests.cs new file mode 100644 index 0000000..0f0ad8b --- /dev/null +++ b/src/DotNetOpenAuth.Test/OAuth2/AuthorizeTests.cs @@ -0,0 +1,50 @@ +//----------------------------------------------------------------------- +// <copyright file="AuthorizeTests.cs" company="Outercurve Foundation"> +// Copyright (c) Outercurve Foundation. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OAuth2 { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.Messaging.Bindings; + using DotNetOpenAuth.OAuth2; + using DotNetOpenAuth.OAuth2.ChannelElements; + using DotNetOpenAuth.OAuth2.Messages; + using Moq; + using NUnit.Framework; + + [TestFixture] + public class AuthorizeTests : OAuth2TestBase { + [TestCase] + public void Test1() { + var authHostMock = new Mock<IAuthorizationServer>(); + var cryptoStore = new MemoryCryptoKeyStore(); + authHostMock.Setup(m => m.GetClient(ClientId)).Returns(ClientDescription); + authHostMock.SetupGet(m => m.CryptoKeyStore).Returns(cryptoStore); + authHostMock.Setup(m => m.IsAuthorizationValid(It.Is<IAuthorizationDescription>(d => d.ClientIdentifier == ClientId && d.User == Username))).Returns(true); + var coordinator = new OAuth2Coordinator( + AuthorizationServerDescription, + authHostMock.Object, + client => { + var authState = new AuthorizationState { + Callback = ClientCallback, + }; + client.PrepareRequestUserAuthorization(authState).Respond(); + var result = client.ProcessUserAuthorization(); + Assert.IsNotNull(result.AccessToken); + }, + server => { + var request = server.ReadAuthorizationRequest(); + server.ApproveAuthorizationRequest(request, Username); + var tokenRequest = server.ReadAccessTokenRequest(); + var tokenResponse = server.PrepareAccessTokenResponse(tokenRequest); + server.Channel.Respond(tokenResponse); + }); + coordinator.Run(); + } + } +} diff --git a/src/DotNetOpenAuth.Test/OAuth2/OAuth2ChannelTests.cs b/src/DotNetOpenAuth.Test/OAuth2/OAuth2ChannelTests.cs deleted file mode 100644 index 309dfaf..0000000 --- a/src/DotNetOpenAuth.Test/OAuth2/OAuth2ChannelTests.cs +++ /dev/null @@ -1,21 +0,0 @@ -//----------------------------------------------------------------------- -// <copyright file="OAuth2ChannelTests.cs" company="Outercurve Foundation"> -// Copyright (c) Outercurve Foundation. All rights reserved. -// </copyright> -//----------------------------------------------------------------------- - -namespace DotNetOpenAuth.Test.OAuth2 { - using System; - using System.Collections.Generic; - using System.Linq; - using System.Text; - using DotNetOpenAuth.Messaging; - using DotNetOpenAuth.OAuth2; - using DotNetOpenAuth.OAuth2.ChannelElements; - using DotNetOpenAuth.OAuth2.Messages; - using NUnit.Framework; - - [TestFixture] - public class OAuth2ChannelTests : OAuth2TestBase { - } -} diff --git a/src/DotNetOpenAuth.Test/OAuth2/OAuth2Coordinator.cs b/src/DotNetOpenAuth.Test/OAuth2/OAuth2Coordinator.cs new file mode 100644 index 0000000..5af7fe8 --- /dev/null +++ b/src/DotNetOpenAuth.Test/OAuth2/OAuth2Coordinator.cs @@ -0,0 +1,63 @@ +//----------------------------------------------------------------------- +// <copyright file="OAuth2Coordinator.cs" company="Outercurve Foundation"> +// Copyright (c) Outercurve Foundation. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OAuth2 { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using DotNetOpenAuth.OAuth2; + using DotNetOpenAuth.Test.Mocks; + + internal class OAuth2Coordinator : CoordinatorBase<WebServerClient, AuthorizationServer> { + private readonly AuthorizationServerDescription serverDescription; + private readonly IAuthorizationServer authServerHost; + + internal OAuth2Coordinator(AuthorizationServerDescription serverDescription, IAuthorizationServer authServerHost, Action<WebServerClient> clientAction, Action<AuthorizationServer> authServerAction) + : base(clientAction, authServerAction) { + Requires.NotNull(serverDescription, "serverDescription"); + Requires.NotNull(authServerHost, "authServerHost"); + this.serverDescription = serverDescription; + this.authServerHost = authServerHost; + } + + internal override void Run() { + var client = new WebServerClient(this.serverDescription) { + ClientIdentifier = OAuth2TestBase.ClientId, + ClientSecret = OAuth2TestBase.ClientSecret, + }; + var authServer = new AuthorizationServer(this.authServerHost); + + var rpCoordinatingChannel = new CoordinatingChannel(client.Channel, this.IncomingMessageFilter, this.OutgoingMessageFilter); + var opCoordinatingChannel = new CoordinatingOAuth2AuthServerChannel(authServer.Channel, this.IncomingMessageFilter, this.OutgoingMessageFilter); + rpCoordinatingChannel.RemoteChannel = opCoordinatingChannel; + opCoordinatingChannel.RemoteChannel = rpCoordinatingChannel; + + client.Channel = rpCoordinatingChannel; + authServer.Channel = opCoordinatingChannel; + + this.RunCore(client, authServer); + } + + private static Action<WebServerClient> WrapAction(Action<WebServerClient> action) { + Requires.NotNull(action, "action"); + + return client => { + action(client); + ((CoordinatingChannel)client.Channel).Close(); + }; + } + + private static Action<AuthorizationServer> WrapAction(Action<AuthorizationServer> action) { + Requires.NotNull(action, "action"); + + return authServer => { + action(authServer); + ((CoordinatingChannel)authServer.Channel).Close(); + }; + } + } +} diff --git a/src/DotNetOpenAuth.Test/OAuth2/OAuth2TestBase.cs b/src/DotNetOpenAuth.Test/OAuth2/OAuth2TestBase.cs index 49fc40b..32de525 100644 --- a/src/DotNetOpenAuth.Test/OAuth2/OAuth2TestBase.cs +++ b/src/DotNetOpenAuth.Test/OAuth2/OAuth2TestBase.cs @@ -9,7 +9,25 @@ namespace DotNetOpenAuth.Test.OAuth2 { using System.Collections.Generic; using System.Linq; using System.Text; + using DotNetOpenAuth.OAuth2; public class OAuth2TestBase : TestBase { + protected internal const string ClientId = "TestClientId"; + + protected internal const string ClientSecret = "TestClientSecret"; + + protected const string Username = "TestUser"; + + protected static readonly Uri ClientCallback = new Uri("http://client/callback"); + + protected static readonly AuthorizationServerDescription AuthorizationServerDescription = new AuthorizationServerDescription { + AuthorizationEndpoint = new Uri("https://authserver/authorize"), + TokenEndpoint = new Uri("https://authserver/token"), + }; + + protected static readonly IClientDescription ClientDescription = new ClientDescription( + ClientSecret, + ClientCallback, + ClientType.Confidential); } } |