From e01aa6be32c2d0437821459c97e4aa6d5e445ca6 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Sat, 10 Jan 2009 14:57:27 -0800 Subject: [PATCH] Added some tests for OpenIdRelyingParty. --- .../OpenId/RelyingParty/OpenIdRelyingPartyTests.cs | 37 +++++++++++++++- .../PositiveAuthenticationResponseTests.cs | 2 + src/DotNetOpenAuth.vsmdi | 50 ++++++++++++---------- src/DotNetOpenAuth/Messaging/ErrorUtilities.cs | 9 ++++ src/DotNetOpenAuth/Messaging/MessagingUtilities.cs | 6 +-- .../OpenId/RelyingParty/AuthenticationRequest.cs | 10 +++++ .../OpenId/RelyingParty/OpenIdRelyingParty.cs | 15 +------ 7 files changed, 87 insertions(+), 42 deletions(-) diff --git a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/OpenIdRelyingPartyTests.cs b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/OpenIdRelyingPartyTests.cs index 3f8ff22..0fcb750 100644 --- a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/OpenIdRelyingPartyTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/OpenIdRelyingPartyTests.cs @@ -6,9 +6,8 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { using System; - using System.Collections.Generic; using System.Linq; - using System.Text; + using DotNetOpenAuth.Messaging; using DotNetOpenAuth.Messaging.Bindings; using DotNetOpenAuth.OpenId; using DotNetOpenAuth.OpenId.RelyingParty; @@ -31,5 +30,39 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { var rp = new OpenIdRelyingParty(new AssociationMemoryStore(), new NonceMemoryStore(TimeSpan.FromMinutes(5)), new PrivateSecretMemoryStore()); rp.SecuritySettings = null; } + + [TestMethod] + public void CreateRequest() { + var rp = this.CreateRelyingParty(); + rp.AssociationStore.StoreAssociation(TestSupport.GetFullUrl("/" + TestSupport.ProviderPage), HmacShaAssociation.Create("somehandle", new byte[20], TimeSpan.FromDays(1))); + Identifier id = Identifier.Parse(GetMockIdentifier(TestSupport.Scenarios.AutoApproval, ProtocolVersion.V20)); + var req = rp.CreateRequest(id, TestSupport.Realm, TestSupport.ReturnTo); + Assert.IsNotNull(req); + } + + [TestMethod] + public void CreateRequests() { + var rp = this.CreateRelyingParty(); + rp.AssociationStore.StoreAssociation(TestSupport.GetFullUrl("/" + TestSupport.ProviderPage), HmacShaAssociation.Create("somehandle", new byte[20], TimeSpan.FromDays(1))); + Identifier id = Identifier.Parse(GetMockIdentifier(TestSupport.Scenarios.AutoApproval, ProtocolVersion.V20)); + var requests = rp.CreateRequests(id, TestSupport.Realm, TestSupport.ReturnTo); + Assert.AreEqual(1, requests.Count()); + } + [TestMethod, ExpectedException(typeof(ProtocolException))] + public void CreateRequestOnNonOpenID() { + Uri nonOpenId = new Uri("http://www.microsoft.com/"); + var rp = this.CreateRelyingParty(); + this.MockResponder.RegisterMockResponse(nonOpenId, "text/html", ""); + rp.CreateRequest(nonOpenId, TestSupport.Realm, TestSupport.ReturnTo); + } + + [TestMethod] + public void CreateRequestsOnNonOpenID() { + Uri nonOpenId = new Uri("http://www.microsoft.com/"); + var rp = this.CreateRelyingParty(); + this.MockResponder.RegisterMockResponse(nonOpenId, "text/html", ""); + var requests = rp.CreateRequests(nonOpenId, TestSupport.Realm, TestSupport.ReturnTo); + Assert.AreEqual(0, requests.Count()); + } } } diff --git a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/PositiveAuthenticationResponseTests.cs b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/PositiveAuthenticationResponseTests.cs index 7ebd0a9..b3812ce 100644 --- a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/PositiveAuthenticationResponseTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/PositiveAuthenticationResponseTests.cs @@ -40,6 +40,8 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { Assert.AreEqual(authResponseAccessor.endpoint.FriendlyIdentifierForDisplay, authResponse.FriendlyIdentifierForDisplay); Assert.AreSame(extension, authResponse.GetExtension(typeof(ClaimsResponse))); Assert.AreSame(extension, authResponse.GetExtension()); + Assert.IsNull(authResponse.GetCallbackArgument("a")); + Assert.AreEqual(0, authResponse.GetCallbackArguments().Count); } /// diff --git a/src/DotNetOpenAuth.vsmdi b/src/DotNetOpenAuth.vsmdi index 62fddf8..c6492b5 100644 --- a/src/DotNetOpenAuth.vsmdi +++ b/src/DotNetOpenAuth.vsmdi @@ -10,27 +10,30 @@ + - + + - + + @@ -46,7 +49,7 @@ - + @@ -63,12 +66,11 @@ + - - @@ -79,7 +81,6 @@ - @@ -99,6 +100,7 @@ + @@ -110,7 +112,6 @@ - @@ -122,8 +123,10 @@ + + @@ -140,7 +143,7 @@ - + @@ -187,12 +190,11 @@ - - + - + @@ -200,12 +202,11 @@ - + - @@ -220,16 +221,18 @@ + - + - + + @@ -246,7 +249,7 @@ - + @@ -257,7 +260,7 @@ - + @@ -272,18 +275,19 @@ - + - + + @@ -293,7 +297,7 @@ - + @@ -314,7 +318,7 @@ - + @@ -339,11 +343,11 @@ - + - + diff --git a/src/DotNetOpenAuth/Messaging/ErrorUtilities.cs b/src/DotNetOpenAuth/Messaging/ErrorUtilities.cs index a00292d..2b7fd34 100644 --- a/src/DotNetOpenAuth/Messaging/ErrorUtilities.cs +++ b/src/DotNetOpenAuth/Messaging/ErrorUtilities.cs @@ -8,6 +8,7 @@ namespace DotNetOpenAuth.Messaging { using System; using System.Diagnostics; using System.Globalization; + using System.Web; /// /// A collection of error checking and reporting methods. @@ -218,5 +219,13 @@ namespace DotNetOpenAuth.Messaging { throw new ArgumentException(MessagingStrings.UnexpectedEmptyString, paramName); } } + + /// + /// Verifies that != null. + /// + /// Thrown if == null + internal static void VerifyHttpContext() { + ErrorUtilities.VerifyOperation(HttpContext.Current != null, MessagingStrings.HttpContextRequired); + } } } diff --git a/src/DotNetOpenAuth/Messaging/MessagingUtilities.cs b/src/DotNetOpenAuth/Messaging/MessagingUtilities.cs index 5086caf..571d54b 100644 --- a/src/DotNetOpenAuth/Messaging/MessagingUtilities.cs +++ b/src/DotNetOpenAuth/Messaging/MessagingUtilities.cs @@ -35,10 +35,8 @@ namespace DotNetOpenAuth.Messaging { [SuppressMessage("Microsoft.Usage", "CA2234:PassSystemUriObjectsInsteadOfStrings", Justification = "The Uri merging requires use of a string value.")] [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Expensive call should not be a property.")] public static Uri GetRequestUrlFromContext() { + ErrorUtilities.VerifyHttpContext(); HttpContext context = HttpContext.Current; - if (context == null) { - throw new InvalidOperationException(MessagingStrings.CurrentHttpContextRequired); - } // We use Request.Url for the full path to the server, and modify it // with Request.RawUrl to capture both the cookieless session "directory" if it exists @@ -55,7 +53,7 @@ namespace DotNetOpenAuth.Messaging { /// /// A containing all the parameters in the query string. public static NameValueCollection GetQueryFromContextNVC() { - ErrorUtilities.VerifyOperation(HttpContext.Current != null, MessagingStrings.HttpContextRequired); + ErrorUtilities.VerifyHttpContext(); HttpRequest request = HttpContext.Current.Request; diff --git a/src/DotNetOpenAuth/OpenId/RelyingParty/AuthenticationRequest.cs b/src/DotNetOpenAuth/OpenId/RelyingParty/AuthenticationRequest.cs index 8fd7f60..20ac2cb 100644 --- a/src/DotNetOpenAuth/OpenId/RelyingParty/AuthenticationRequest.cs +++ b/src/DotNetOpenAuth/OpenId/RelyingParty/AuthenticationRequest.cs @@ -232,6 +232,16 @@ namespace DotNetOpenAuth.OpenId.RelyingParty { ErrorUtilities.VerifyArgumentNotNull(relyingParty, "relyingParty"); ErrorUtilities.VerifyArgumentNotNull(realm, "realm"); + // Normalize the portion of the return_to path that correlates to the realm for capitalization. + // (so that if a web app base path is /MyApp/, but the URL of this request happens to be + // /myapp/login.aspx, we bump up the return_to Url to use /MyApp/ so it matches the realm. + UriBuilder returnTo = new UriBuilder(returnToUrl); + if (returnTo.Path.StartsWith(realm.AbsolutePath, StringComparison.OrdinalIgnoreCase) && + !returnTo.Path.StartsWith(realm.AbsolutePath, StringComparison.Ordinal)) { + returnTo.Path = realm.AbsolutePath + returnTo.Path.Substring(realm.AbsolutePath.Length); + returnToUrl = returnTo.Uri; + } + userSuppliedIdentifier = userSuppliedIdentifier.TrimFragment(); if (relyingParty.SecuritySettings.RequireSsl) { // Rather than check for successful SSL conversion at this stage, diff --git a/src/DotNetOpenAuth/OpenId/RelyingParty/OpenIdRelyingParty.cs b/src/DotNetOpenAuth/OpenId/RelyingParty/OpenIdRelyingParty.cs index b73e01f..f97ca5c 100644 --- a/src/DotNetOpenAuth/OpenId/RelyingParty/OpenIdRelyingParty.cs +++ b/src/DotNetOpenAuth/OpenId/RelyingParty/OpenIdRelyingParty.cs @@ -340,16 +340,7 @@ namespace DotNetOpenAuth.OpenId.RelyingParty { ErrorUtilities.VerifyArgumentNotNull(realm, "realm"); ErrorUtilities.VerifyArgumentNotNull(returnToUrl, "returnToUrl"); - // Normalize the portion of the return_to path that correlates to the realm for capitalization. - // (so that if a web app base path is /MyApp/, but the URL of this request happens to be - // /myapp/login.aspx, we bump up the return_to Url to use /MyApp/ so it matches the realm. - UriBuilder returnTo = new UriBuilder(returnToUrl); - if (returnTo.Path.StartsWith(realm.AbsolutePath, StringComparison.OrdinalIgnoreCase) && - !returnTo.Path.StartsWith(realm.AbsolutePath, StringComparison.Ordinal)) { - returnTo.Path = realm.AbsolutePath + returnTo.Path.Substring(realm.AbsolutePath.Length); - } - - return AuthenticationRequest.Create(userSuppliedIdentifier, this, realm, returnTo.Uri, true).Cast(); + return AuthenticationRequest.Create(userSuppliedIdentifier, this, realm, returnToUrl, true).Cast(); } /// @@ -376,9 +367,7 @@ namespace DotNetOpenAuth.OpenId.RelyingParty { /// An empty enumerable is returned instead. /// internal IEnumerable CreateRequests(Identifier userSuppliedIdentifier, Realm realm) { - if (HttpContext.Current == null) { - throw new InvalidOperationException(MessagingStrings.HttpContextRequired); - } + ErrorUtilities.VerifyHttpContext(); // Build the return_to URL UriBuilder returnTo = new UriBuilder(MessagingUtilities.GetRequestUrlFromContext()); -- 2.11.4.GIT