diff options
Diffstat (limited to 'src/DotNetOpenAuth.Test/OAuth2')
5 files changed, 120 insertions, 50 deletions
diff --git a/src/DotNetOpenAuth.Test/OAuth2/AuthorizationServerTests.cs b/src/DotNetOpenAuth.Test/OAuth2/AuthorizationServerTests.cs new file mode 100644 index 0000000..f3d8feb --- /dev/null +++ b/src/DotNetOpenAuth.Test/OAuth2/AuthorizationServerTests.cs @@ -0,0 +1,44 @@ +//----------------------------------------------------------------------- +// <copyright file="AuthorizationServerTests.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. 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.OAuth2.Messages; + using NUnit.Framework; + + /// <summary> + /// Verifies authorization server functionality. + /// </summary> + [TestFixture] + public class AuthorizationServerTests : OAuth2TestBase { + /// <summary> + /// Verifies that authorization server responds with an appropriate error response. + /// </summary> + [Test] + public void ErrorResponseTest() { + var coordinator = new OAuth2Coordinator<UserAgentClient>( + AuthorizationServerDescription, + AuthorizationServerMock, + new UserAgentClient(AuthorizationServerDescription), + client => { + var request = new AccessTokenAuthorizationCodeRequest(AuthorizationServerDescription) + { ClientIdentifier = ClientId, ClientSecret = ClientSecret, AuthorizationCode = "foo" }; + + var response = client.Channel.Request<AccessTokenFailedResponse>(request); + Assert.That(response.Error, Is.Not.Null.And.Not.Empty); + Assert.That(response.Error, Is.EqualTo(Protocol.AccessTokenRequestErrorCodes.InvalidRequest)); + }, + server => { + server.HandleTokenRequest().Respond(); + }); + coordinator.Run(); + } + } +} diff --git a/src/DotNetOpenAuth.Test/OAuth2/MessageFactoryTests.cs b/src/DotNetOpenAuth.Test/OAuth2/MessageFactoryTests.cs index f46af33..bec85e2 100644 --- a/src/DotNetOpenAuth.Test/OAuth2/MessageFactoryTests.cs +++ b/src/DotNetOpenAuth.Test/OAuth2/MessageFactoryTests.cs @@ -33,7 +33,7 @@ namespace DotNetOpenAuth.Test.OAuth2 { #region End user authorization messages - [TestCase] + [Test] public void EndUserAuthorizationRequest() { var fields = new Dictionary<string, string> { { Protocol.response_type, "code" }, @@ -41,10 +41,10 @@ namespace DotNetOpenAuth.Test.OAuth2 { { Protocol.redirect_uri, "abc" }, }; IDirectedProtocolMessage request = this.messageFactory.GetNewRequestMessage(this.recipient, fields); - Assert.IsInstanceOf(typeof(EndUserAuthorizationRequest), request); + Assert.That(request, Is.InstanceOf(typeof(EndUserAuthorizationRequest))); } - [TestCase] + [Test] public void EndUserAuthorizationImplicitRequest() { var fields = new Dictionary<string, string> { { Protocol.response_type, "token" }, @@ -52,42 +52,42 @@ namespace DotNetOpenAuth.Test.OAuth2 { { Protocol.redirect_uri, "abc" }, }; IDirectedProtocolMessage request = this.messageFactory.GetNewRequestMessage(this.recipient, fields); - Assert.IsInstanceOf(typeof(EndUserAuthorizationImplicitRequest), request); + Assert.That(request, Is.InstanceOf(typeof(EndUserAuthorizationImplicitRequest))); } - [TestCase] + [Test] public void EndUserAuthorizationSuccessResponseWithCode() { var fields = new Dictionary<string, string> { { Protocol.code, "abc" }, }; IDirectedProtocolMessage request = this.messageFactory.GetNewRequestMessage(this.recipient, fields); - Assert.IsInstanceOf(typeof(EndUserAuthorizationSuccessResponseBase), request); + Assert.That(request, Is.InstanceOf(typeof(EndUserAuthorizationSuccessResponseBase))); } - [TestCase] + [Test] public void EndUserAuthorizationSuccessResponseWithAccessToken() { var fields = new Dictionary<string, string> { { Protocol.access_token, "abc" }, { Protocol.token_type, "bearer" }, }; IDirectedProtocolMessage request = this.messageFactory.GetNewRequestMessage(this.recipient, fields); - Assert.IsInstanceOf(typeof(EndUserAuthorizationSuccessResponseBase), request); + Assert.That(request, Is.InstanceOf(typeof(EndUserAuthorizationSuccessResponseBase))); } - [TestCase] + [Test] public void EndUserAuthorizationFailedResponse() { var fields = new Dictionary<string, string> { { Protocol.error, "access-denied" }, }; IDirectedProtocolMessage request = this.messageFactory.GetNewRequestMessage(this.recipient, fields); - Assert.IsInstanceOf(typeof(EndUserAuthorizationFailedResponse), request); + Assert.That(request, Is.InstanceOf(typeof(EndUserAuthorizationFailedResponse))); } #endregion #region Access token request messages - [TestCase] + [Test] public void AccessTokenRefreshRequest() { var fields = new Dictionary<string, string> { { Protocol.client_id, "abc" }, @@ -95,10 +95,10 @@ namespace DotNetOpenAuth.Test.OAuth2 { { Protocol.grant_type, "refresh-token" }, }; IDirectedProtocolMessage request = this.messageFactory.GetNewRequestMessage(this.recipient, fields); - Assert.IsInstanceOf(typeof(AccessTokenRefreshRequest), request); + Assert.That(request, Is.InstanceOf(typeof(AccessTokenRefreshRequest))); } - [TestCase] + [Test] public void AccessTokenAuthorizationCodeRequest() { var fields = new Dictionary<string, string> { { Protocol.client_id, "abc" }, @@ -107,10 +107,10 @@ namespace DotNetOpenAuth.Test.OAuth2 { { Protocol.redirect_uri, "http://someUri" }, }; IDirectedProtocolMessage request = this.messageFactory.GetNewRequestMessage(this.recipient, fields); - Assert.IsInstanceOf(typeof(AccessTokenAuthorizationCodeRequest), request); + Assert.That(request, Is.InstanceOf(typeof(AccessTokenAuthorizationCodeRequest))); } - [TestCase] + [Test] public void AccessTokenBasicCredentialsRequest() { var fields = new Dictionary<string, string> { { Protocol.client_id, "abc" }, @@ -120,10 +120,10 @@ namespace DotNetOpenAuth.Test.OAuth2 { { Protocol.password, "abc" }, }; IDirectedProtocolMessage request = this.messageFactory.GetNewRequestMessage(this.recipient, fields); - Assert.IsInstanceOf(typeof(AccessTokenResourceOwnerPasswordCredentialsRequest), request); + Assert.That(request, Is.InstanceOf(typeof(AccessTokenResourceOwnerPasswordCredentialsRequest))); } - [TestCase] + [Test] public void AccessTokenClientCredentialsRequest() { var fields = new Dictionary<string, string> { { Protocol.client_id, "abc" }, @@ -131,7 +131,7 @@ namespace DotNetOpenAuth.Test.OAuth2 { { Protocol.grant_type, "none" }, }; IDirectedProtocolMessage request = this.messageFactory.GetNewRequestMessage(this.recipient, fields); - Assert.IsInstanceOf(typeof(AccessTokenClientCredentialsRequest), request); + Assert.That(request, Is.InstanceOf(typeof(AccessTokenClientCredentialsRequest))); } #endregion diff --git a/src/DotNetOpenAuth.Test/OAuth2/OAuth2TestBase.cs b/src/DotNetOpenAuth.Test/OAuth2/OAuth2TestBase.cs index 1b5c329..87d91f7 100644 --- a/src/DotNetOpenAuth.Test/OAuth2/OAuth2TestBase.cs +++ b/src/DotNetOpenAuth.Test/OAuth2/OAuth2TestBase.cs @@ -9,6 +9,7 @@ namespace DotNetOpenAuth.Test.OAuth2 { using System.Collections.Generic; using System.Linq; using System.Text; + using DotNetOpenAuth.Messaging; using DotNetOpenAuth.Messaging.Bindings; using DotNetOpenAuth.OAuth2; using DotNetOpenAuth.OAuth2.ChannelElements; @@ -23,6 +24,8 @@ namespace DotNetOpenAuth.Test.OAuth2 { protected const string ResourceOwnerPassword = "TestUserPassword"; + protected static readonly string[] TestScopes = new[] { "Scope1", "Scope2" }; + protected static readonly Uri ClientCallback = new Uri("http://client/callback"); protected static readonly AuthorizationServerDescription AuthorizationServerDescription = new AuthorizationServerDescription { @@ -42,7 +45,13 @@ namespace DotNetOpenAuth.Test.OAuth2 { 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 == ResourceOwnerUsername))).Returns(true); + authHostMock.Setup( + m => + m.IsAuthorizationValid( + It.Is<IAuthorizationDescription>( + d => + d.ClientIdentifier == ClientId && d.User == ResourceOwnerUsername && + MessagingUtilities.AreEquivalent(d.Scope, TestScopes)))).Returns(true); authHostMock.Setup(m => m.IsResourceOwnerCredentialValid(ResourceOwnerUsername, ResourceOwnerPassword)).Returns(true); return authHostMock; } diff --git a/src/DotNetOpenAuth.Test/OAuth2/UserAgentClientAuthorizeTests.cs b/src/DotNetOpenAuth.Test/OAuth2/UserAgentClientAuthorizeTests.cs index 3a8944f..97c0f56 100644 --- a/src/DotNetOpenAuth.Test/OAuth2/UserAgentClientAuthorizeTests.cs +++ b/src/DotNetOpenAuth.Test/OAuth2/UserAgentClientAuthorizeTests.cs @@ -19,14 +19,14 @@ namespace DotNetOpenAuth.Test.OAuth2 { [TestFixture] public class UserAgentClientAuthorizeTests : OAuth2TestBase { - [TestCase] + [Test] public void AuthorizationCodeGrant() { var coordinator = new OAuth2Coordinator<UserAgentClient>( AuthorizationServerDescription, AuthorizationServerMock, new UserAgentClient(AuthorizationServerDescription), client => { - var authState = new AuthorizationState { + var authState = new AuthorizationState(TestScopes) { Callback = ClientCallback, }; var request = client.PrepareRequestUserAuthorization(authState); @@ -34,22 +34,19 @@ namespace DotNetOpenAuth.Test.OAuth2 { client.Channel.Respond(request); var incoming = client.Channel.ReadFromRequest(); var result = client.ProcessUserAuthorization(authState, incoming); - Assert.IsNotNullOrEmpty(result.AccessToken); - Assert.IsNotNullOrEmpty(result.RefreshToken); + Assert.That(result.AccessToken, Is.Not.Null.And.Not.Empty); + Assert.That(result.RefreshToken, Is.Not.Null.And.Not.Empty); }, server => { var request = server.ReadAuthorizationRequest(); + Assert.That(request, Is.Not.Null); server.ApproveAuthorizationRequest(request, ResourceOwnerUsername); - var tokenRequest = server.ReadAccessTokenRequest(); - IAccessTokenRequest accessTokenRequest = tokenRequest; - Assert.IsTrue(accessTokenRequest.ClientAuthenticated); - var tokenResponse = server.PrepareAccessTokenResponse(tokenRequest); - server.Channel.Respond(tokenResponse); + server.HandleTokenRequest().Respond(); }); coordinator.Run(); } - [TestCase] + [Test] public void ImplicitGrant() { var coordinatorClient = new UserAgentClient(AuthorizationServerDescription); var coordinator = new OAuth2Coordinator<UserAgentClient>( @@ -57,21 +54,22 @@ namespace DotNetOpenAuth.Test.OAuth2 { AuthorizationServerMock, coordinatorClient, client => { - var authState = new AuthorizationState { + var authState = new AuthorizationState(TestScopes) { Callback = ClientCallback, }; var request = client.PrepareRequestUserAuthorization(authState, implicitResponseType: true); - Assert.AreEqual(EndUserAuthorizationResponseType.AccessToken, request.ResponseType); + Assert.That(request.ResponseType, Is.EqualTo(EndUserAuthorizationResponseType.AccessToken)); client.Channel.Respond(request); var incoming = client.Channel.ReadFromRequest(); var result = client.ProcessUserAuthorization(authState, incoming); - Assert.IsNotNullOrEmpty(result.AccessToken); - Assert.IsNull(result.RefreshToken); + Assert.That(result.AccessToken, Is.Not.Null.And.Not.Empty); + Assert.That(result.RefreshToken, Is.Null); }, server => { var request = server.ReadAuthorizationRequest(); + Assert.That(request, Is.Not.Null); IAccessTokenRequest accessTokenRequest = (EndUserAuthorizationImplicitRequest)request; - Assert.IsFalse(accessTokenRequest.ClientAuthenticated); + Assert.That(accessTokenRequest.ClientAuthenticated, Is.False); server.ApproveAuthorizationRequest(request, ResourceOwnerUsername); }); diff --git a/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs b/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs index 1615f97..fe0abd2 100644 --- a/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs +++ b/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs @@ -9,54 +9,73 @@ namespace DotNetOpenAuth.Test.OAuth2 { 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 Moq; using NUnit.Framework; [TestFixture] public class WebServerClientAuthorizeTests : OAuth2TestBase { - [TestCase] + [Test] public void AuthorizationCodeGrant() { var coordinator = new OAuth2Coordinator<WebServerClient>( AuthorizationServerDescription, AuthorizationServerMock, new WebServerClient(AuthorizationServerDescription), client => { - var authState = new AuthorizationState { + var authState = new AuthorizationState(TestScopes) { Callback = ClientCallback, }; client.PrepareRequestUserAuthorization(authState).Respond(); var result = client.ProcessUserAuthorization(); - Assert.IsNotNullOrEmpty(result.AccessToken); - Assert.IsNotNullOrEmpty(result.RefreshToken); + Assert.That(result.AccessToken, Is.Not.Null.And.Not.Empty); + Assert.That(result.RefreshToken, Is.Not.Null.And.Not.Empty); }, server => { var request = server.ReadAuthorizationRequest(); + Assert.That(request, Is.Not.Null); server.ApproveAuthorizationRequest(request, ResourceOwnerUsername); - var tokenRequest = server.ReadAccessTokenRequest(); - IAccessTokenRequest accessTokenRequest = tokenRequest; - Assert.IsTrue(accessTokenRequest.ClientAuthenticated); - var tokenResponse = server.PrepareAccessTokenResponse(tokenRequest); - server.Channel.Respond(tokenResponse); + server.HandleTokenRequest().Respond(); }); coordinator.Run(); } - [TestCase] + [Test] public void ResourceOwnerPasswordCredentialGrant() { var coordinator = new OAuth2Coordinator<WebServerClient>( AuthorizationServerDescription, AuthorizationServerMock, new WebServerClient(AuthorizationServerDescription), client => { - var authState = client.ExchangeUserCredentialForToken(ResourceOwnerUsername, ResourceOwnerPassword); - Assert.IsNotNullOrEmpty(authState.AccessToken); - Assert.IsNotNullOrEmpty(authState.RefreshToken); + var authState = client.ExchangeUserCredentialForToken(ResourceOwnerUsername, ResourceOwnerPassword, TestScopes); + Assert.That(authState.AccessToken, Is.Not.Null.And.Not.Empty); + Assert.That(authState.RefreshToken, Is.Not.Null.And.Not.Empty); }, server => { - var request = server.ReadAccessTokenRequest(); - var response = server.PrepareAccessTokenResponse(request); - server.Channel.Respond(response); + server.HandleTokenRequest().Respond(); + }); + coordinator.Run(); + } + + [Test] + public void ClientCredentialGrant() { + var authServer = CreateAuthorizationServerMock(); + authServer.Setup( + a => a.IsAuthorizationValid(It.Is<IAuthorizationDescription>(d => d.User == null && d.ClientIdentifier == ClientId && MessagingUtilities.AreEquivalent(d.Scope, TestScopes)))) + .Returns(true); + var coordinator = new OAuth2Coordinator<WebServerClient>( + AuthorizationServerDescription, + authServer.Object, + new WebServerClient(AuthorizationServerDescription), + client => { + var authState = client.GetClientAccessToken(TestScopes); + Assert.That(authState.AccessToken, Is.Not.Null.And.Not.Empty); + Assert.That(authState.RefreshToken, Is.Null); + }, + server => { + server.HandleTokenRequest().Respond(); }); coordinator.Run(); } |