diff options
Diffstat (limited to 'src/DotNetOpenAuth.Test/OpenId')
-rw-r--r-- | src/DotNetOpenAuth.Test/OpenId/ChannelElements/ExtensionsBindingElementTests.cs | 22 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/AttributeExchangeRoundtripTests.cs | 53 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/AttributeRequestTests.cs | 85 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/AttributeValuesTests.cs | 62 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/FetchRequestTests.cs | 99 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/FetchResponseTests.cs | 63 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/StoreRequestTests.cs | 70 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/StoreResponseTests.cs | 50 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Test/OpenId/Extensions/ExtensionTestUtilities.cs (renamed from src/DotNetOpenAuth.Test/OpenId/Extensions/ExtensionTestBase.cs) | 9 |
9 files changed, 512 insertions, 1 deletions
diff --git a/src/DotNetOpenAuth.Test/OpenId/ChannelElements/ExtensionsBindingElementTests.cs b/src/DotNetOpenAuth.Test/OpenId/ChannelElements/ExtensionsBindingElementTests.cs index 298b6d4..a9bc5d2 100644 --- a/src/DotNetOpenAuth.Test/OpenId/ChannelElements/ExtensionsBindingElementTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/ChannelElements/ExtensionsBindingElementTests.cs @@ -94,6 +94,28 @@ namespace DotNetOpenAuth.Test.OpenId.ChannelElements { Assert.Inconclusive("Not yet implemented."); } + /// <summary> + /// Verifies that two extensions with the same TypeURI cannot be applied to the same message. + /// </summary> + /// <remarks> + /// OpenID Authentication 2.0 section 12 states that + /// "A namespace MUST NOT be assigned more than one alias in the same message". + /// </remarks> + [TestMethod] + public void TwoExtensionsSameTypeUri() { + IOpenIdMessageExtension request1 = new MockOpenIdExtension("requestPart1", "requestData1"); + IOpenIdMessageExtension request2 = new MockOpenIdExtension("requestPart2", "requestData2"); + try { + ExtensionTestUtilities.Roundtrip( + Protocol.Default, + new IOpenIdMessageExtension[] { request1, request2 }, + new IOpenIdMessageExtension[0]); + Assert.Fail("Expected ProtocolException not thrown."); + } catch (AssertFailedException ex) { + Assert.IsInstanceOfType(ex.InnerException, typeof(ProtocolException)); + } + } + private static IEnumerable<string> GetAliases(IDictionary<string, string> extraData) { Regex regex = new Regex(@"^openid\.ns\.(\w+)"); return from key in extraData.Keys diff --git a/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/AttributeExchangeRoundtripTests.cs b/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/AttributeExchangeRoundtripTests.cs new file mode 100644 index 0000000..ac86156 --- /dev/null +++ b/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/AttributeExchangeRoundtripTests.cs @@ -0,0 +1,53 @@ +//----------------------------------------------------------------------- +// <copyright file="AttributeExchangeRoundtripTests.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OpenId.Extensions { + using DotNetOpenAuth.OpenId; + using DotNetOpenAuth.OpenId.Extensions.AttributeExchange; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class AttributeExchangeRoundtripTests : OpenIdTestBase { + private const string NicknameTypeUri = WellKnownAttributes.Name.Alias; + private const string EmailTypeUri = WellKnownAttributes.Contact.Email; + private const string IncrementingAttribute = "http://incatt"; + private int incrementingAttributeValue = 1; + + [TestMethod] + public void Fetch() { + var request = new FetchRequest(); + request.AddAttribute(new AttributeRequest(NicknameTypeUri)); + request.AddAttribute(new AttributeRequest(EmailTypeUri, false, int.MaxValue)); + + var response = new FetchResponse(); + response.AddAttribute(new AttributeValues(NicknameTypeUri, "Andrew")); + response.AddAttribute(new AttributeValues(EmailTypeUri, "a@a.com", "b@b.com")); + + ExtensionTestUtilities.Roundtrip(Protocol.Default, new[] { request }, new[] { response }); + } + + [TestMethod] + public void Store() { + var request = new StoreRequest(); + var newAttribute = new AttributeValues( + IncrementingAttribute, + "val" + (incrementingAttributeValue++).ToString(), + "val" + (incrementingAttributeValue++).ToString()); + request.AddAttribute(newAttribute); + + var successResponse = new StoreResponse(); + successResponse.Succeeded = true; + + ExtensionTestUtilities.Roundtrip(Protocol.Default, new[] { request }, new[] { successResponse }); + + var failureResponse = new StoreResponse(); + failureResponse.Succeeded = false; + failureResponse.FailureReason = "Some error"; + + ExtensionTestUtilities.Roundtrip(Protocol.Default, new[] { request }, new[] { failureResponse }); + } + } +} diff --git a/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/AttributeRequestTests.cs b/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/AttributeRequestTests.cs new file mode 100644 index 0000000..228bda3 --- /dev/null +++ b/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/AttributeRequestTests.cs @@ -0,0 +1,85 @@ +//----------------------------------------------------------------------- +// <copyright file="AttributeRequestTests.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OpenId.Extensions { + using System; + using DotNetOpenAuth.OpenId.Extensions.AttributeExchange; + using DotNetOpenAuth.Test.OpenId; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class AttributeRequestTests : OpenIdTestBase { + [TestMethod] + public void CtorDefault() { + AttributeRequest req = new AttributeRequest(); + Assert.AreEqual(1, req.Count); + Assert.IsNull(req.TypeUri); + Assert.IsFalse(req.IsRequired); + } + + [TestMethod, ExpectedException(typeof(ArgumentException))] + public void CtorEmptyTypeUri() { + new AttributeRequest(string.Empty); + } + + [TestMethod, ExpectedException(typeof(ArgumentNullException))] + public void CtorNullTypeUri() { + new AttributeRequest(null); + } + + [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))] + public void CtorCountZero() { + new AttributeRequest(WellKnownAttributes.Contact.Email, false, 0); + } + + [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))] + public void CtorCountNegative() { + new AttributeRequest(WellKnownAttributes.Contact.Email, false, -1); + } + + [TestMethod] + public void CtorFull() { + var req = new AttributeRequest(WellKnownAttributes.Contact.Email, true, 5); + Assert.AreEqual(WellKnownAttributes.Contact.Email, req.TypeUri); + Assert.IsTrue(req.IsRequired); + Assert.AreEqual(5, req.Count); + } + + [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))] + public void SetCountZero() { + var req = new AttributeRequest(); + req.Count = 0; + } + + [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))] + public void SetCountNegative() { + var req = new AttributeRequest(); + req.Count = -1; + } + + [TestMethod] + public void EqualityTests() { + var req1 = new AttributeRequest(); + var req2 = new AttributeRequest(); + Assert.AreEqual(req1, req2); + + req1.IsRequired = true; + Assert.AreNotEqual(req1, req2); + req2.IsRequired = true; + Assert.AreEqual(req1, req2); + + req1.Count = 3; + Assert.AreNotEqual(req1, req2); + req2.Count = 3; + Assert.AreEqual(req1, req2); + + req1.TypeUri = "http://hi"; + Assert.AreNotEqual(req1, req2); + req2.TypeUri = "http://hi"; + Assert.AreEqual(req1, req2); + } + } +} diff --git a/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/AttributeValuesTests.cs b/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/AttributeValuesTests.cs new file mode 100644 index 0000000..1f7e17c --- /dev/null +++ b/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/AttributeValuesTests.cs @@ -0,0 +1,62 @@ +//----------------------------------------------------------------------- +// <copyright file="AttributeValuesTests.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OpenId.Extensions.AttributeExchange { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using DotNetOpenAuth.OpenId.Extensions.AttributeExchange; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class AttributeValuesTests : OpenIdTestBase { + [TestMethod] + public void Ctor() { + var att = new AttributeValues(); + Assert.IsNull(att.TypeUri); + Assert.IsNotNull(att.Values); + Assert.AreEqual(0, att.Values.Count); + + att = new AttributeValues("http://att"); + Assert.AreEqual("http://att", att.TypeUri); + Assert.IsNotNull(att.Values); + Assert.AreEqual(0, att.Values.Count); + + att = new AttributeValues("http://att", "value1", "value2"); + Assert.AreEqual("http://att", att.TypeUri); + Assert.IsNotNull(att.Values); + Assert.AreEqual(2, att.Values.Count); + Assert.AreEqual("value1", att.Values[0]); + Assert.AreEqual("value2", att.Values[1]); + } + + /// <summary> + /// Verifies the Equals method. + /// </summary> + [TestMethod] + public void EqualityTests() { + var att1 = new AttributeValues(); + var att2 = new AttributeValues(); + Assert.AreEqual(att1, att2); + + att1.TypeUri = "http://att1"; + Assert.AreNotEqual(att1, att2); + att2.TypeUri = "http://att1"; + Assert.AreEqual(att1, att2); + + att1.Values.Add("value1"); + Assert.AreNotEqual(att1, att2); + att2.Values.Add("value1"); + Assert.AreEqual(att1, att2); + + // Values that are out of order should not be considered equal. + att1.Values.Add("value2"); + att2.Values.Insert(0, "value2"); + Assert.AreNotEqual(att1, att2); + } + } +} diff --git a/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/FetchRequestTests.cs b/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/FetchRequestTests.cs new file mode 100644 index 0000000..5374794 --- /dev/null +++ b/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/FetchRequestTests.cs @@ -0,0 +1,99 @@ +//----------------------------------------------------------------------- +// <copyright file="FetchRequestTests.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OpenId.Extensions { + using System; + using DotNetOpenAuth.OpenId.Extensions.AttributeExchange; + using DotNetOpenAuth.Test.OpenId; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class FetchRequestTests : OpenIdTestBase { + [TestMethod, ExpectedException(typeof(ArgumentNullException))] + public void AddAttributeRequestNull() { + new FetchRequest().AddAttribute(null); + } + + [TestMethod] + public void AddAttributeRequest() { + var req = new FetchRequest(); + req.AddAttribute(new AttributeRequest() { TypeUri = "http://someUri" }); + } + + [TestMethod] + public void AddAttributeRequestStrangeUri() { + var req = new FetchRequest(); + req.AddAttribute(new AttributeRequest() { TypeUri = "=someUri*who*knows*but*this*is*legal" }); + } + + [TestMethod, ExpectedException(typeof(ArgumentException))] + public void AddAttributeRequestAgain() { + var req = new FetchRequest(); + req.AddAttribute(new AttributeRequest() { TypeUri = "http://UriTwice" }); + req.AddAttribute(new AttributeRequest() { TypeUri = "http://UriTwice" }); + } + + [TestMethod] + public void RespondSimpleValue() { + var req = new AttributeRequest(); + req.TypeUri = "http://someType"; + var resp = req.Respond("value"); + Assert.AreEqual(req.TypeUri, resp.TypeUri); + Assert.AreEqual(1, resp.Values.Count); + Assert.AreEqual("value", resp.Values[0]); + } + + [TestMethod] + public void RespondTwoValues() { + var req = new AttributeRequest(); + req.TypeUri = "http://someType"; + req.Count = 2; + var resp = req.Respond("value1", "value2"); + Assert.AreEqual(req.TypeUri, resp.TypeUri); + Assert.AreEqual(2, resp.Values.Count); + Assert.AreEqual("value1", resp.Values[0]); + Assert.AreEqual("value2", resp.Values[1]); + } + + [TestMethod, ExpectedException(typeof(ArgumentException))] + public void RespondTooManyValues() { + var req = new AttributeRequest(); + req.TypeUri = "http://someType"; + req.Count = 1; + req.Respond("value1", "value2"); + } + + [TestMethod, ExpectedException(typeof(ArgumentNullException))] + public void RespondNull() { + var req = new AttributeRequest(); + req.TypeUri = "http://someType"; + req.Count = 1; + req.Respond(null); + } + + [TestMethod] + public void EqualityTests() { + var req1 = new FetchRequest(); + var req2 = new FetchRequest(); + Assert.AreEqual(req1, req2); + + req1.UpdateUrl = new Uri("http://hi"); + Assert.AreNotEqual(req1, req2); + req2.UpdateUrl = new Uri("http://hi"); + Assert.AreEqual(req1, req2); + + // Add attributes in different orders deliberately. + req1.AddAttribute(new AttributeRequest("http://att1")); + Assert.AreNotEqual(req1, req2); + req2.AddAttribute(new AttributeRequest("http://att2")); + Assert.AreNotEqual(req1, req2); + req1.AddAttribute(new AttributeRequest("http://att2")); + Assert.AreNotEqual(req1, req2); + req2.AddAttribute(new AttributeRequest("http://att1")); + Assert.AreEqual(req1, req2); + } + } +} diff --git a/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/FetchResponseTests.cs b/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/FetchResponseTests.cs new file mode 100644 index 0000000..3f01498 --- /dev/null +++ b/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/FetchResponseTests.cs @@ -0,0 +1,63 @@ +//----------------------------------------------------------------------- +// <copyright file="FetchResponseTests.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenId.Test.OpenId.Extensions { + using System; + using DotNetOpenAuth.OpenId.Extensions.AttributeExchange; + using DotNetOpenAuth.Test.OpenId; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class FetchResponseTests : OpenIdTestBase { + [TestMethod] + public void AddAttribute() { + var response = new FetchResponse(); + response.AddAttribute(new AttributeValues("http://someattribute", "Value1")); + } + + [TestMethod] + public void AddTwoAttributes() { + var response = new FetchResponse(); + response.AddAttribute(new AttributeValues("http://someattribute", "Value1")); + response.AddAttribute(new AttributeValues("http://someOtherAttribute", "Value2")); + } + + [TestMethod, ExpectedException(typeof(ArgumentException))] + public void AddAttributeTwice() { + var response = new FetchResponse(); + response.AddAttribute(new AttributeValues("http://someattribute", "Value1")); + response.AddAttribute(new AttributeValues("http://someattribute", "Value1")); + } + + [TestMethod, ExpectedException(typeof(ArgumentNullException))] + public void AddAttributeNull() { + var response = new FetchResponse(); + response.AddAttribute(null); + } + + [TestMethod] + public void EqualityTests() { + var response1 = new FetchResponse(); + var response2 = new FetchResponse(); + Assert.AreEqual(response1, response2); + + response1.UpdateUrl = new Uri("http://updateurl"); + Assert.AreNotEqual(response1, response2); + response2.UpdateUrl = new Uri("http://updateurl"); + Assert.AreEqual(response1, response2); + + // Add attributes in different orders deliberately. + response1.AddAttribute(new AttributeValues("http://att1")); + Assert.AreNotEqual(response1, response2); + response2.AddAttribute(new AttributeValues("http://att2")); + Assert.AreNotEqual(response1, response2); + response1.AddAttribute(new AttributeValues("http://att2")); + Assert.AreNotEqual(response1, response2); + response2.AddAttribute(new AttributeValues("http://att1")); + Assert.AreEqual(response1, response2); + } + } +} diff --git a/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/StoreRequestTests.cs b/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/StoreRequestTests.cs new file mode 100644 index 0000000..e0fe064 --- /dev/null +++ b/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/StoreRequestTests.cs @@ -0,0 +1,70 @@ +//----------------------------------------------------------------------- +// <copyright file="StoreRequestTests.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OpenId.Extensions.AttributeExchange { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OpenId.Extensions.AttributeExchange; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class StoreRequestTests { + /// <summary> + /// Verifies the constructor behavior. + /// </summary> + [TestMethod] + public void Ctor() { + var req = new StoreRequest(); + Assert.IsFalse(req.Attributes.Any()); + } + + /// <summary> + /// Verifies the AddAttribute method. + /// </summary> + [TestMethod] + public void AddAttributeByValue() { + var req = new StoreRequest(); + AttributeValues value = new AttributeValues(); + req.AddAttribute(value); + Assert.AreSame(value, req.Attributes.Single()); + } + + /// <summary> + /// Verifies the AddAttribute method. + /// </summary> + [TestMethod] + public void AddAttributeByPrimitives() { + var req = new StoreRequest(); + req.AddAttribute("http://att1", "value1", "value2"); + AttributeValues value = req.Attributes.Single(); + Assert.AreEqual("http://att1", value.TypeUri); + Assert.IsTrue(MessagingUtilities.AreEquivalent(new[] { "value1", "value2" }, value.Values)); + } + + /// <summary> + /// Verifies the Equals method. + /// </summary> + [TestMethod] + public void EqualityTests() { + var req1 = new StoreRequest(); + var req2 = new StoreRequest(); + Assert.AreEqual(req1, req2); + + // Add attributes in different orders deliberately. + req1.AddAttribute("http://att1"); + Assert.AreNotEqual(req1, req2); + req2.AddAttribute("http://att2"); + Assert.AreNotEqual(req1, req2); + req1.AddAttribute("http://att2"); + Assert.AreNotEqual(req1, req2); + req2.AddAttribute("http://att1"); + Assert.AreEqual(req1, req2); + } + } +} diff --git a/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/StoreResponseTests.cs b/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/StoreResponseTests.cs new file mode 100644 index 0000000..5dab1b7 --- /dev/null +++ b/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/StoreResponseTests.cs @@ -0,0 +1,50 @@ +//----------------------------------------------------------------------- +// <copyright file="StoreResponseTests.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OpenId.Extensions.AttributeExchange { + using DotNetOpenAuth.OpenId.Extensions.AttributeExchange; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class StoreResponseTests { + /// <summary> + /// Verifies the constructor's behavior. + /// </summary> + [TestMethod] + public void Ctor() { + var response = new StoreResponse(); + Assert.IsTrue(response.Succeeded, "The default status should be Succeeded."); + Assert.IsNull(response.FailureReason); + + response = new StoreResponse("failed"); + Assert.IsFalse(response.Succeeded); + Assert.AreEqual("failed", response.FailureReason); + } + + /// <summary> + /// Verifies the Equals method. + /// </summary> + [TestMethod] + public void EqualityTests() { + var response1 = new StoreResponse(); + var response2 = new StoreResponse(); + Assert.AreEqual(response1, response2); + + response1.Succeeded = true; + response2.Succeeded = false; + Assert.AreNotEqual(response1, response2); + + response1.Succeeded = false; + Assert.AreEqual(response1, response2); + + response1.FailureReason = "bad code"; + Assert.AreNotEqual(response1, response2); + + response2.FailureReason = "bad code"; + Assert.AreEqual(response1, response2); + } + } +} diff --git a/src/DotNetOpenAuth.Test/OpenId/Extensions/ExtensionTestBase.cs b/src/DotNetOpenAuth.Test/OpenId/Extensions/ExtensionTestUtilities.cs index a052e55..a765d57 100644 --- a/src/DotNetOpenAuth.Test/OpenId/Extensions/ExtensionTestBase.cs +++ b/src/DotNetOpenAuth.Test/OpenId/Extensions/ExtensionTestUtilities.cs @@ -1,5 +1,5 @@ //----------------------------------------------------------------------- -// <copyright file="ExtensionTestBase.cs" company="Andrew Arnott"> +// <copyright file="ExtensionTestUtilities.cs" company="Andrew Arnott"> // Copyright (c) Andrew Arnott. All rights reserved. // </copyright> //----------------------------------------------------------------------- @@ -17,6 +17,13 @@ namespace DotNetOpenAuth.Test.OpenId.Extensions { using DotNetOpenAuth.Test.Messaging; public static class ExtensionTestUtilities { + /// <summary> + /// Simulates an extension request and response. + /// </summary> + /// <remarks> + /// This method relies on the extension objects' Equals methods to verify + /// accurate transport. The Equals methods should be verified by separate tests. + /// </remarks> internal static void Roundtrip( Protocol protocol, IEnumerable<IOpenIdMessageExtension> requests, |