diff options
Diffstat (limited to 'src/DotNetOpenId.Test/Extensions')
7 files changed, 414 insertions, 64 deletions
diff --git a/src/DotNetOpenId.Test/Extensions/AttributeExchangeTests.cs b/src/DotNetOpenId.Test/Extensions/AttributeExchangeTests.cs index 62efbc2..892d974 100644 --- a/src/DotNetOpenId.Test/Extensions/AttributeExchangeTests.cs +++ b/src/DotNetOpenId.Test/Extensions/AttributeExchangeTests.cs @@ -17,10 +17,10 @@ namespace DotNetOpenId.Test.Extensions { [Test]
public void None() {
var fetchResponse = ParameterizedTest<FetchResponse>(
- TestSupport.GetIdentityUrl(TestSupport.Scenarios.ExtensionFullCooperation, Version), null);
+ TestSupport.Scenarios.ExtensionFullCooperation, Version, null);
Assert.IsNull(fetchResponse);
var storeResponse = ParameterizedTest<StoreResponse>(
- TestSupport.GetIdentityUrl(TestSupport.Scenarios.ExtensionFullCooperation, Version), null);
+ TestSupport.Scenarios.ExtensionFullCooperation, Version, null);
Assert.IsNull(storeResponse);
}
@@ -30,7 +30,7 @@ namespace DotNetOpenId.Test.Extensions { request.AddAttribute(new AttributeRequest(nicknameTypeUri));
request.AddAttribute(new AttributeRequest(emailTypeUri, false, int.MaxValue));
var response = ParameterizedTest<FetchResponse>(
- TestSupport.GetIdentityUrl(TestSupport.Scenarios.ExtensionFullCooperation, Version), request);
+ TestSupport.Scenarios.ExtensionFullCooperation, Version, request);
Assert.IsNotNull(response);
var att = response.GetAttribute(nicknameTypeUri);
Assert.IsNotNull(att);
@@ -50,7 +50,7 @@ namespace DotNetOpenId.Test.Extensions { var request = new FetchRequest();
request.AddAttribute(new AttributeRequest { TypeUri = emailTypeUri, Count = 1 });
var response = ParameterizedTest<FetchResponse>(
- TestSupport.GetIdentityUrl(TestSupport.Scenarios.ExtensionFullCooperation, Version), request);
+ TestSupport.Scenarios.ExtensionFullCooperation, Version, request);
Assert.IsNotNull(response);
var att = response.GetAttribute(emailTypeUri);
Assert.IsNotNull(att);
@@ -69,7 +69,7 @@ namespace DotNetOpenId.Test.Extensions { request.AddAttribute(newAttribute);
var response = ParameterizedTest<StoreResponse>(
- TestSupport.GetIdentityUrl(TestSupport.Scenarios.ExtensionFullCooperation, Version), request);
+ TestSupport.Scenarios.ExtensionFullCooperation, Version, request);
Assert.IsNotNull(response);
Assert.IsTrue(response.Succeeded);
Assert.IsNull(response.FailureReason);
@@ -77,7 +77,7 @@ namespace DotNetOpenId.Test.Extensions { var fetchRequest = new FetchRequest();
fetchRequest.AddAttribute(new AttributeRequest { TypeUri = incrementingAttribute });
var fetchResponse = ParameterizedTest<FetchResponse>(
- TestSupport.GetIdentityUrl(TestSupport.Scenarios.ExtensionFullCooperation, Version), fetchRequest);
+ TestSupport.Scenarios.ExtensionFullCooperation, Version, fetchRequest);
Assert.IsNotNull(fetchResponse);
var att = fetchResponse.GetAttribute(incrementingAttribute);
Assert.IsNotNull(att);
@@ -92,11 +92,7 @@ namespace DotNetOpenId.Test.Extensions { /// </summary>
[Test, ExpectedException(typeof(OpenIdException))]
public void FetchAndStore() {
- var identityUrl = TestSupport.GetIdentityUrl(TestSupport.Scenarios.ExtensionFullCooperation, Version);
- var returnTo = TestSupport.GetFullUrl(TestSupport.ConsumerPage);
- var realm = new Realm(TestSupport.GetFullUrl(TestSupport.ConsumerPage).AbsoluteUri);
- var consumer = new OpenIdRelyingParty(AppStore, null, null);
- var request = consumer.CreateRequest(identityUrl, realm, returnTo);
+ var request = TestSupport.CreateRelyingPartyRequest(false, TestSupport.Scenarios.ExtensionFullCooperation, Version, false);
request.AddExtension(new FetchRequest());
request.AddExtension(new StoreRequest());
}
diff --git a/src/DotNetOpenId.Test/Extensions/ClaimsResponseTests.cs b/src/DotNetOpenId.Test/Extensions/ClaimsResponseTests.cs new file mode 100644 index 0000000..afc2e03 --- /dev/null +++ b/src/DotNetOpenId.Test/Extensions/ClaimsResponseTests.cs @@ -0,0 +1,144 @@ +/********************************************************
+ * Copyright (C) 2007 Andrew Arnott
+ * Released under the New BSD License
+ * License available here: http://www.opensource.org/licenses/bsd-license.php
+ * For news or support on this file: http://blog.nerdbank.net/
+ ********************************************************/
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Runtime.Serialization;
+using System.Runtime.Serialization.Formatters.Binary;
+using System.Xml.Serialization;
+using System.IO;
+using NUnit.Framework;
+using DotNetOpenId.Extensions.SimpleRegistration;
+using DotNetOpenId.Extensions;
+
+namespace DotNetOpenId.Test.Extensions {
+ [TestFixture]
+ public class ClaimsResponseTests {
+ ClaimsResponse getFilledData() {
+ return new ClaimsResponse(Constants.sreg_ns) {
+ BirthDate = new DateTime(2005, 2, 3),
+ Culture = new System.Globalization.CultureInfo("en-US"),
+ Email = "a@b.com",
+ FullName = "Jimmy buffet",
+ Gender = Gender.Male,
+ Nickname = "Jimbo",
+ PostalCode = "12345",
+ TimeZone = "PST",
+ };
+ }
+
+ [Test]
+ public void EmptyMailAddress() {
+ ClaimsResponse response = new ClaimsResponse(Constants.sreg_ns);
+ response.Email = "";
+ Assert.IsNull(response.MailAddress);
+ }
+
+ [Test]
+ public void BinarySerialization() {
+ ClaimsResponse fields = getFilledData();
+ MemoryStream ms = new MemoryStream();
+ IFormatter formatter = new BinaryFormatter();
+ formatter.Serialize(ms, fields);
+
+ ms.Position = 0;
+ ClaimsResponse fields2 = (ClaimsResponse)formatter.Deserialize(ms);
+ Assert.AreEqual(fields, fields2);
+ }
+
+ [Test]
+ public void XmlSerialization() {
+ ClaimsResponse fields = getFilledData();
+ MemoryStream ms = new MemoryStream();
+ XmlSerializer xs = new XmlSerializer(typeof(ClaimsResponse));
+ xs.Serialize(ms, fields);
+
+ ms.Position = 0;
+ ClaimsResponse fields2 = (ClaimsResponse)xs.Deserialize(ms);
+ Assert.AreEqual(fields, fields2);
+ }
+
+ [Test]
+ public void TestEquals() {
+ ClaimsResponse fields1 = getFilledData();
+
+ Assert.AreNotEqual(fields1, null);
+ Assert.AreNotEqual(fields1, "string");
+
+ ClaimsResponse fields2 = getFilledData();
+ Assert.AreNotSame(fields1, fields2, "Test sanity check.");
+ Assert.AreEqual(fields1, fields2);
+
+ // go through each property and change it slightly and make sure it causes inequality.
+ fields2.Email += "q";
+ Assert.AreNotEqual(fields1, fields2);
+ fields1.Email = fields2.Email;
+ Assert.AreEqual(fields1, fields2, "Test sanity check.");
+ fields2.BirthDate = DateTime.Now;
+ Assert.AreNotEqual(fields1, fields2);
+ fields2.BirthDate = fields1.BirthDate;
+ Assert.AreEqual(fields1, fields2, "Test sanity check.");
+ fields2.Country += "q";
+ Assert.AreNotEqual(fields1, fields2);
+ fields2.Country = fields1.Country;
+ Assert.AreEqual(fields1, fields2, "Test sanity check.");
+ fields2.FullName += "q";
+ Assert.AreNotEqual(fields1, fields2);
+ fields2.FullName = fields1.FullName;
+ Assert.AreEqual(fields1, fields2, "Test sanity check.");
+ fields2.Gender = Gender.Female;
+ Assert.AreNotEqual(fields1, fields2);
+ fields2.Gender = fields1.Gender;
+ Assert.AreEqual(fields1, fields2, "Test sanity check.");
+ fields2.Language = "gb";
+ Assert.AreNotEqual(fields1, fields2);
+ fields2.Language = fields1.Language;
+ Assert.AreEqual(fields1, fields2, "Test sanity check.");
+ fields2.Nickname += "q";
+ Assert.AreNotEqual(fields1, fields2);
+ fields2.Nickname = fields1.Nickname;
+ Assert.AreEqual(fields1, fields2, "Test sanity check.");
+ fields2.PostalCode += "q";
+ Assert.AreNotEqual(fields1, fields2);
+ fields2.PostalCode = fields1.PostalCode;
+ Assert.AreEqual(fields1, fields2, "Test sanity check.");
+ fields2.TimeZone += "q";
+ Assert.AreNotEqual(fields1, fields2);
+ }
+
+ void parameterizedPreserveVersionFromRequest(string versionTypeUri) {
+ Dictionary<string, string> fields = new Dictionary<string, string>{
+ {"optional", "nickname"},
+ };
+ var req = new ClaimsRequest();
+ Assert.IsTrue(((IExtensionRequest)req).Deserialize(fields, null, versionTypeUri));
+ Assert.AreEqual(DemandLevel.Request, req.Nickname);
+ ClaimsResponse resp = req.CreateResponse();
+ Assert.AreEqual(versionTypeUri, ((IExtensionResponse)resp).TypeUri);
+ }
+
+ [Test]
+ public void PreserveVersionFromRequest() {
+ // some unofficial type URIs...
+ parameterizedPreserveVersionFromRequest("http://openid.net/sreg/1.0");
+ parameterizedPreserveVersionFromRequest("http://openid.net/sreg/1.1");
+ // and the official one.
+ parameterizedPreserveVersionFromRequest("http://openid.net/extensions/sreg/1.1");
+ }
+
+ //[Test]
+ public void AddToResponse() {
+ // TODO
+ }
+
+ //[Test]
+ public void ReadFromResponse() {
+ // TODO
+ }
+ }
+}
diff --git a/src/DotNetOpenId.Test/Extensions/ExtensionTestBase.cs b/src/DotNetOpenId.Test/Extensions/ExtensionTestBase.cs index 825ef58..5958f0e 100644 --- a/src/DotNetOpenId.Test/Extensions/ExtensionTestBase.cs +++ b/src/DotNetOpenId.Test/Extensions/ExtensionTestBase.cs @@ -1,56 +1,128 @@ using System;
using System.Collections.Generic;
-using System.Linq;
-using System.Text;
+using DotNetOpenId.Extensions;
+using DotNetOpenId.Extensions.AttributeExchange;
+using DotNetOpenId.Extensions.ProviderAuthenticationPolicy;
+using DotNetOpenId.Extensions.SimpleRegistration;
using DotNetOpenId.RelyingParty;
using NUnit.Framework;
-using System.Net;
-using DotNetOpenId.Extensions;
-using System.IO;
-using System.Diagnostics;
-using System.Web;
+using OPRequest = DotNetOpenId.Provider.IAuthenticationRequest;
+using SregDemandLevel = DotNetOpenId.Extensions.SimpleRegistration.DemandLevel;
+using PapeConstants = DotNetOpenId.Extensions.ProviderAuthenticationPolicy.Constants;
namespace DotNetOpenId.Test.Extensions {
public class ExtensionTestBase {
- protected IRelyingPartyApplicationStore AppStore;
protected const ProtocolVersion Version = ProtocolVersion.V20;
+ Dictionary<string, AttributeValues> storedAttributes;
[SetUp]
public virtual void Setup() {
- AppStore = new ApplicationMemoryStore();
+ storedAttributes = new Dictionary<string, AttributeValues>();
+ }
+
+ [TearDown]
+ public virtual void TearDown() {
+ Mocks.MockHttpRequest.Reset();
}
- protected T ParameterizedTest<T>(Identifier identityUrl, IExtensionRequest extension)
+ protected T ParameterizedTest<T>(TestSupport.Scenarios scenario, ProtocolVersion version, IExtensionRequest extension)
where T : IExtensionResponse, new() {
- Debug.Assert(identityUrl != null);
- var returnTo = TestSupport.GetFullUrl(TestSupport.ConsumerPage);
- var realm = new Realm(TestSupport.GetFullUrl(TestSupport.ConsumerPage).AbsoluteUri);
- var consumer = new OpenIdRelyingParty(AppStore, null, null);
- var request = consumer.CreateRequest(identityUrl, realm, returnTo);
+ var rpRequest = TestSupport.CreateRelyingPartyRequest(false, scenario, version, false);
if (extension != null)
- request.AddExtension(extension);
-
- HttpWebRequest providerRequest = (HttpWebRequest)WebRequest.Create(request.RedirectingResponse.ExtractUrl());
- providerRequest.AllowAutoRedirect = false;
- Uri redirectUrl;
- try {
- using (HttpWebResponse providerResponse = (HttpWebResponse)providerRequest.GetResponse()) {
- Assert.AreEqual(HttpStatusCode.Redirect, providerResponse.StatusCode);
- redirectUrl = new Uri(providerResponse.Headers[HttpResponseHeader.Location]);
- }
- } catch (WebException ex) {
- Trace.WriteLine(ex);
- if (ex.Response != null) {
- using (StreamReader sr = new StreamReader(ex.Response.GetResponseStream())) {
- Trace.WriteLine(sr.ReadToEnd());
+ rpRequest.AddExtension(extension);
+
+ var response = TestSupport.CreateRelyingPartyResponseThroughProvider(rpRequest, request => {
+ TestSupport.SetAuthenticationFromScenario(scenario, request);
+ ExtensionsResponder(request);
+ });
+ Assert.AreEqual(AuthenticationStatus.Authenticated, response.Status);
+ return response.GetExtension<T>();
+ }
+
+ const string nicknameTypeUri = WellKnownAttributes.Name.Alias;
+ const string emailTypeUri = WellKnownAttributes.Contact.Email;
+
+ private void ExtensionsResponder(OPRequest request) {
+ var sregRequest = request.GetExtension<ClaimsRequest>();
+ var sregResponse = sregRequest != null ? sregRequest.CreateResponse() : null;
+ var aeFetchRequest = request.GetExtension<FetchRequest>();
+ var aeFetchResponse = new FetchResponse();
+ var aeStoreRequest = request.GetExtension<StoreRequest>();
+ var aeStoreResponse = new StoreResponse();
+ var papeRequest = request.GetExtension<PolicyRequest>();
+ var papeResponse = new PolicyResponse();
+
+ TestSupport.Scenarios scenario = (TestSupport.Scenarios)Enum.Parse(typeof(TestSupport.Scenarios),
+ new Uri(request.LocalIdentifier).AbsolutePath.TrimStart('/'));
+ switch (scenario) {
+ case TestSupport.Scenarios.ExtensionFullCooperation:
+ if (sregRequest != null) {
+ if (sregRequest.FullName != SregDemandLevel.NoRequest)
+ sregResponse.FullName = "Andrew Arnott";
+ if (sregRequest.Email != SregDemandLevel.NoRequest)
+ sregResponse.Email = "andrewarnott@gmail.com";
+ }
+ if (aeFetchRequest != null) {
+ var att = aeFetchRequest.GetAttribute(nicknameTypeUri);
+ if (att != null)
+ aeFetchResponse.AddAttribute(att.Respond("Andrew"));
+ att = aeFetchRequest.GetAttribute(emailTypeUri);
+ if (att != null) {
+ string[] emails = new[] { "a@a.com", "b@b.com" };
+ string[] subset = new string[Math.Min(emails.Length, att.Count)];
+ Array.Copy(emails, subset, subset.Length);
+ aeFetchResponse.AddAttribute(att.Respond(subset));
+ }
+ foreach (var att2 in aeFetchRequest.Attributes) {
+ if (storedAttributes.ContainsKey(att2.TypeUri))
+ aeFetchResponse.AddAttribute(storedAttributes[att2.TypeUri]);
+ }
+ }
+ if (papeRequest != null) {
+ if (papeRequest.MaximumAuthenticationAge.HasValue) {
+ papeResponse.AuthenticationTimeUtc = DateTime.UtcNow - (papeRequest.MaximumAuthenticationAge.Value - TimeSpan.FromSeconds(30));
+ }
+ if (papeRequest.PreferredAuthLevelTypes.Contains(PapeConstants.AuthenticationLevels.NistTypeUri)) {
+ papeResponse.NistAssuranceLevel = NistAssuranceLevel.Level1;
+ }
}
+ break;
+ case TestSupport.Scenarios.ExtensionPartialCooperation:
+ if (sregRequest != null) {
+ if (sregRequest.FullName == SregDemandLevel.Require)
+ sregResponse.FullName = "Andrew Arnott";
+ if (sregRequest.Email == SregDemandLevel.Require)
+ sregResponse.Email = "andrewarnott@gmail.com";
+ }
+ if (aeFetchRequest != null) {
+ var att = aeFetchRequest.GetAttribute(nicknameTypeUri);
+ if (att != null && att.IsRequired)
+ aeFetchResponse.AddAttribute(att.Respond("Andrew"));
+ att = aeFetchRequest.GetAttribute(emailTypeUri);
+ if (att != null && att.IsRequired) {
+ string[] emails = new[] { "a@a.com", "b@b.com" };
+ string[] subset = new string[Math.Min(emails.Length, att.Count)];
+ Array.Copy(emails, subset, subset.Length);
+ aeFetchResponse.AddAttribute(att.Respond(subset));
+ }
+ foreach (var att2 in aeFetchRequest.Attributes) {
+ if (att2.IsRequired && storedAttributes.ContainsKey(att2.TypeUri))
+ aeFetchResponse.AddAttribute(storedAttributes[att2.TypeUri]);
+ }
+ }
+ break;
+ }
+ if (aeStoreRequest != null) {
+ foreach (var att in aeStoreRequest.Attributes) {
+ storedAttributes[att.TypeUri] = att;
}
- throw;
+ aeStoreResponse.Succeeded = true;
}
- consumer = new OpenIdRelyingParty(AppStore, redirectUrl, HttpUtility.ParseQueryString(redirectUrl.Query));
- Assert.AreEqual(AuthenticationStatus.Authenticated, consumer.Response.Status);
- Assert.AreEqual(identityUrl, consumer.Response.ClaimedIdentifier);
- return consumer.Response.GetExtension<T>();
+
+ if (sregRequest != null) request.AddResponseExtension(sregResponse);
+ if (aeFetchRequest != null) request.AddResponseExtension(aeFetchResponse);
+ if (aeStoreRequest != null) request.AddResponseExtension(aeStoreResponse);
+ if (papeRequest != null) request.AddResponseExtension(papeResponse);
}
}
}
diff --git a/src/DotNetOpenId.Test/Extensions/PapeTests.cs b/src/DotNetOpenId.Test/Extensions/PapeTests.cs index f8acb79..6fb693d 100644 --- a/src/DotNetOpenId.Test/Extensions/PapeTests.cs +++ b/src/DotNetOpenId.Test/Extensions/PapeTests.cs @@ -11,7 +11,7 @@ namespace DotNetOpenId.Test.Extensions { [Test]
public void None() {
var response = ParameterizedTest<PolicyResponse>(
- TestSupport.GetIdentityUrl(TestSupport.Scenarios.ExtensionFullCooperation, Version), null);
+ TestSupport.Scenarios.ExtensionFullCooperation, Version, null);
Assert.IsNull(response);
}
@@ -19,11 +19,15 @@ namespace DotNetOpenId.Test.Extensions { public void Full() {
var request = new PolicyRequest();
request.MaximumAuthenticationAge = TimeSpan.FromMinutes(10);
+ request.PreferredAuthLevelTypes.Add(Constants.AuthenticationLevels.NistTypeUri);
var response = ParameterizedTest<PolicyResponse>(
- TestSupport.GetIdentityUrl(TestSupport.Scenarios.ExtensionFullCooperation, Version), request);
+ TestSupport.Scenarios.ExtensionFullCooperation, Version, request);
Assert.IsNotNull(response);
Assert.IsNotNull(response.AuthenticationTimeUtc);
Assert.IsTrue(response.AuthenticationTimeUtc.Value > DateTime.UtcNow - request.MaximumAuthenticationAge);
+ Assert.IsTrue(response.AssuranceLevels.ContainsKey(Constants.AuthenticationLevels.NistTypeUri));
+ Assert.AreEqual("1", response.AssuranceLevels[Constants.AuthenticationLevels.NistTypeUri]);
+ Assert.AreEqual(NistAssuranceLevel.Level1, response.NistAssuranceLevel);
}
}
}
diff --git a/src/DotNetOpenId.Test/Extensions/PolicyRequestTests.cs b/src/DotNetOpenId.Test/Extensions/PolicyRequestTests.cs index 5a46327..40155db 100644 --- a/src/DotNetOpenId.Test/Extensions/PolicyRequestTests.cs +++ b/src/DotNetOpenId.Test/Extensions/PolicyRequestTests.cs @@ -5,6 +5,7 @@ using System.Text; using NUnit.Framework;
using DotNetOpenId.Extensions.ProviderAuthenticationPolicy;
using DotNetOpenId.Extensions;
+using System.Globalization;
namespace DotNetOpenId.Test.Extensions {
[TestFixture]
@@ -50,6 +51,14 @@ namespace DotNetOpenId.Test.Extensions { }
[Test]
+ public void AddAuthLevelTypes() {
+ PolicyRequest req = new PolicyRequest();
+ req.PreferredAuthLevelTypes.Add(Constants.AuthenticationLevels.NistTypeUri);
+ Assert.AreEqual(1, req.PreferredAuthLevelTypes.Count);
+ Assert.IsTrue(req.PreferredAuthLevelTypes.Contains(Constants.AuthenticationLevels.NistTypeUri));
+ }
+
+ [Test]
public void EqualsTest() {
PolicyRequest req = new PolicyRequest();
PolicyRequest req2 = new PolicyRequest();
@@ -77,18 +86,27 @@ namespace DotNetOpenId.Test.Extensions { Assert.AreNotEqual(req, req2);
req2.MaximumAuthenticationAge = req.MaximumAuthenticationAge;
Assert.AreEqual(req, req2);
+
+ // Test PreferredAuthLevelTypes comparison.
+ req.PreferredAuthLevelTypes.Add("authlevel1");
+ Assert.AreNotEqual(req, req2);
+ req2.PreferredAuthLevelTypes.Add("authlevel2");
+ Assert.AreNotEqual(req, req2);
+ req.PreferredAuthLevelTypes.Add("authlevel2");
+ req2.PreferredAuthLevelTypes.Add("authlevel1");
+ Assert.AreEqual(req, req2);
}
[Test]
public void DeserializeNull() {
PolicyRequest req = new PolicyRequest();
- Assert.IsFalse(((IExtensionRequest)req).Deserialize(null, null));
+ Assert.IsFalse(((IExtensionRequest)req).Deserialize(null, null, Constants.TypeUri));
}
[Test]
public void DeserializeEmpty() {
PolicyRequest req = new PolicyRequest();
- Assert.IsFalse(((IExtensionRequest)req).Deserialize(new Dictionary<string, string>(), null));
+ Assert.IsFalse(((IExtensionRequest)req).Deserialize(new Dictionary<string, string>(), null, Constants.TypeUri));
}
[Test]
@@ -99,35 +117,78 @@ namespace DotNetOpenId.Test.Extensions { // Most basic test
PolicyRequest req = new PolicyRequest(), req2 = new PolicyRequest();
var fields = ((IExtensionRequest)req).Serialize(null);
- Assert.IsTrue(((IExtensionRequest)req2).Deserialize(fields, null));
+ Assert.IsTrue(((IExtensionRequest)req2).Deserialize(fields, null, Constants.TypeUri));
Assert.AreEqual(req, req2);
// Test with all fields set
req2 = new PolicyRequest();
req.PreferredPolicies.Add(AuthenticationPolicies.MultiFactor);
+ req.PreferredAuthLevelTypes.Add(Constants.AuthenticationLevels.NistTypeUri);
req.MaximumAuthenticationAge = TimeSpan.FromHours(1);
fields = ((IExtensionRequest)req).Serialize(null);
- Assert.IsTrue(((IExtensionRequest)req2).Deserialize(fields, null));
+ Assert.IsTrue(((IExtensionRequest)req2).Deserialize(fields, null, Constants.TypeUri));
Assert.AreEqual(req, req2);
- // Test with an extra policy
+ // Test with an extra policy and auth level
req2 = new PolicyRequest();
req.PreferredPolicies.Add(AuthenticationPolicies.PhishingResistant);
+ req.PreferredAuthLevelTypes.Add("customAuthLevel");
fields = ((IExtensionRequest)req).Serialize(null);
- Assert.IsTrue(((IExtensionRequest)req2).Deserialize(fields, null));
+ Assert.IsTrue(((IExtensionRequest)req2).Deserialize(fields, null, Constants.TypeUri));
Assert.AreEqual(req, req2);
// Test with a policy added twice. We should see it intelligently leave one of
// the doubled policies out.
req2 = new PolicyRequest();
req.PreferredPolicies.Add(AuthenticationPolicies.PhishingResistant);
+ req.PreferredAuthLevelTypes.Add(Constants.AuthenticationLevels.NistTypeUri);
fields = ((IExtensionRequest)req).Serialize(null);
- Assert.IsTrue(((IExtensionRequest)req2).Deserialize(fields, null));
+ Assert.IsTrue(((IExtensionRequest)req2).Deserialize(fields, null, Constants.TypeUri));
Assert.AreNotEqual(req, req2);
// Now go ahead and add the doubled one so we can do our equality test.
req2.PreferredPolicies.Add(AuthenticationPolicies.PhishingResistant);
+ req2.PreferredAuthLevelTypes.Add(Constants.AuthenticationLevels.NistTypeUri);
Assert.AreEqual(req, req2);
+ }
+ [Test]
+ public void Serialize() {
+ PolicyRequest req = new PolicyRequest();
+ var fields = ((IExtensionRequest)req).Serialize(null);
+ Assert.AreEqual(1, fields.Count);
+ Assert.IsTrue(fields.ContainsKey("preferred_auth_policies"));
+ Assert.IsEmpty(fields["preferred_auth_policies"]);
+
+ req.MaximumAuthenticationAge = TimeSpan.FromHours(1);
+ fields = ((IExtensionRequest)req).Serialize(null);
+ Assert.AreEqual(2, fields.Count);
+ Assert.IsTrue(fields.ContainsKey("max_auth_age"));
+ Assert.AreEqual(TimeSpan.FromHours(1).TotalSeconds.ToString(CultureInfo.InvariantCulture), fields["max_auth_age"]);
+
+ req.PreferredPolicies.Add("http://pol1/");
+ fields = ((IExtensionRequest)req).Serialize(null);
+ Assert.AreEqual("http://pol1/", fields["preferred_auth_policies"]);
+
+ req.PreferredPolicies.Add("http://pol2/");
+ fields = ((IExtensionRequest)req).Serialize(null);
+ Assert.AreEqual("http://pol1/ http://pol2/", fields["preferred_auth_policies"]);
+
+ req.PreferredAuthLevelTypes.Add("http://authtype1/");
+ fields = ((IExtensionRequest)req).Serialize(null);
+ Assert.AreEqual(4, fields.Count);
+ Assert.IsTrue(fields.ContainsKey("auth_level.ns.alias1"));
+ Assert.AreEqual("http://authtype1/", fields["auth_level.ns.alias1"]);
+ Assert.IsTrue(fields.ContainsKey("preferred_auth_level_types"));
+ Assert.AreEqual("alias1", fields["preferred_auth_level_types"]);
+
+ req.PreferredAuthLevelTypes.Add(Constants.AuthenticationLevels.NistTypeUri);
+ fields = ((IExtensionRequest)req).Serialize(null);
+ Assert.AreEqual(5, fields.Count);
+ Assert.IsTrue(fields.ContainsKey("auth_level.ns.alias2"));
+ Assert.AreEqual("http://authtype1/", fields["auth_level.ns.alias2"]);
+ Assert.IsTrue(fields.ContainsKey("auth_level.ns.nist"));
+ Assert.AreEqual(Constants.AuthenticationLevels.NistTypeUri, fields["auth_level.ns.nist"]);
+ Assert.AreEqual("alias2 nist", fields["preferred_auth_level_types"]);
}
}
}
diff --git a/src/DotNetOpenId.Test/Extensions/PolicyResponseTests.cs b/src/DotNetOpenId.Test/Extensions/PolicyResponseTests.cs index 6aefaaa..7fe240b 100644 --- a/src/DotNetOpenId.Test/Extensions/PolicyResponseTests.cs +++ b/src/DotNetOpenId.Test/Extensions/PolicyResponseTests.cs @@ -88,6 +88,19 @@ namespace DotNetOpenId.Test.Extensions { }
[Test]
+ public void AssuranceLevels() {
+ PolicyResponse resp = new PolicyResponse();
+ Assert.AreEqual(0, resp.AssuranceLevels.Count);
+ resp.NistAssuranceLevel = NistAssuranceLevel.Level2;
+ Assert.AreEqual(1, resp.AssuranceLevels.Count);
+ Assert.AreEqual("2", resp.AssuranceLevels[Constants.AuthenticationLevels.NistTypeUri]);
+ resp.AssuranceLevels[Constants.AuthenticationLevels.NistTypeUri] = "3";
+ Assert.AreEqual(NistAssuranceLevel.Level3, resp.NistAssuranceLevel);
+ resp.AssuranceLevels.Clear();
+ Assert.IsNull(resp.NistAssuranceLevel);
+ }
+
+ [Test]
public void EqualsTest() {
PolicyResponse resp = new PolicyResponse();
PolicyResponse resp2 = new PolicyResponse();
@@ -129,18 +142,30 @@ namespace DotNetOpenId.Test.Extensions { Assert.AreNotEqual(resp, resp2);
resp2.NistAssuranceLevel = NistAssuranceLevel.Level2;
Assert.AreEqual(resp, resp2);
+
+ // Test AssuranceLevels comparison.
+ resp.AssuranceLevels.Add("custom", "b");
+ Assert.AreNotEqual(resp, resp2);
+ resp2.AssuranceLevels.Add("custom", "2");
+ Assert.AreNotEqual(resp, resp2);
+ resp2.AssuranceLevels["custom"] = "b";
+ Assert.AreEqual(resp, resp2);
+ resp.AssuranceLevels[Constants.AuthenticationLevels.NistTypeUri] = "1";
+ Assert.AreNotEqual(resp, resp2);
+ resp2.AssuranceLevels[Constants.AuthenticationLevels.NistTypeUri] = "1";
+ Assert.AreEqual(resp, resp2);
}
[Test]
public void DeserializeNull() {
PolicyResponse resp = new PolicyResponse();
- Assert.IsFalse(((IExtensionResponse)resp).Deserialize(null, null));
+ Assert.IsFalse(((IExtensionResponse)resp).Deserialize(null, null, Constants.TypeUri));
}
[Test]
public void DeserializeEmpty() {
PolicyResponse resp = new PolicyResponse();
- Assert.IsFalse(((IExtensionResponse)resp).Deserialize(new Dictionary<string,string>(), null));
+ Assert.IsFalse(((IExtensionResponse)resp).Deserialize(new Dictionary<string, string>(), null, Constants.TypeUri));
}
[Test]
@@ -151,7 +176,7 @@ namespace DotNetOpenId.Test.Extensions { // Most basic test
PolicyResponse resp = new PolicyResponse(), resp2 = new PolicyResponse();
var fields = ((IExtensionResponse)resp).Serialize(null);
- Assert.IsTrue(((IExtensionResponse)resp2).Deserialize(fields, null));
+ Assert.IsTrue(((IExtensionResponse)resp2).Deserialize(fields, null, Constants.TypeUri));
Assert.AreEqual(resp, resp2);
// Test with all fields set
@@ -160,14 +185,15 @@ namespace DotNetOpenId.Test.Extensions { resp.AuthenticationTimeUtc = someUtcTime;
resp.NistAssuranceLevel = NistAssuranceLevel.Level2;
fields = ((IExtensionResponse)resp).Serialize(null);
- Assert.IsTrue(((IExtensionResponse)resp2).Deserialize(fields, null));
+ Assert.IsTrue(((IExtensionResponse)resp2).Deserialize(fields, null, Constants.TypeUri));
Assert.AreEqual(resp, resp2);
// Test with an extra policy
resp2 = new PolicyResponse();
resp.ActualPolicies.Add(AuthenticationPolicies.PhishingResistant);
+ resp.AssuranceLevels.Add("customlevel", "ABC");
fields = ((IExtensionResponse)resp).Serialize(null);
- Assert.IsTrue(((IExtensionResponse)resp2).Deserialize(fields, null));
+ Assert.IsTrue(((IExtensionResponse)resp2).Deserialize(fields, null, Constants.TypeUri));
Assert.AreEqual(resp, resp2);
// Test with a policy added twice. We should see it intelligently leave one of
@@ -175,11 +201,58 @@ namespace DotNetOpenId.Test.Extensions { resp2 = new PolicyResponse();
resp.ActualPolicies.Add(AuthenticationPolicies.PhishingResistant);
fields = ((IExtensionResponse)resp).Serialize(null);
- Assert.IsTrue(((IExtensionResponse)resp2).Deserialize(fields, null));
+ Assert.IsTrue(((IExtensionResponse)resp2).Deserialize(fields, null, Constants.TypeUri));
Assert.AreNotEqual(resp, resp2);
// Now go ahead and add the doubled one so we can do our equality test.
resp2.ActualPolicies.Add(AuthenticationPolicies.PhishingResistant);
Assert.AreEqual(resp, resp2);
}
+
+ [Test]
+ public void Serialize() {
+ PolicyResponse resp = new PolicyResponse(), resp2 = new PolicyResponse();
+ var fields = ((IExtensionResponse)resp).Serialize(null);
+ Assert.AreEqual(1, fields.Count);
+ Assert.IsTrue(fields.ContainsKey("auth_policies"));
+ Assert.AreEqual(AuthenticationPolicies.None, fields["auth_policies"]);
+
+ resp.ActualPolicies.Add(AuthenticationPolicies.PhishingResistant);
+ fields = ((IExtensionResponse)resp).Serialize(null);
+ Assert.AreEqual(1, fields.Count);
+ Assert.AreEqual(AuthenticationPolicies.PhishingResistant, fields["auth_policies"]);
+
+ resp.ActualPolicies.Add(AuthenticationPolicies.PhysicalMultiFactor);
+ fields = ((IExtensionResponse)resp).Serialize(null);
+ Assert.AreEqual(1, fields.Count);
+ Assert.AreEqual(
+ AuthenticationPolicies.PhishingResistant + " " + AuthenticationPolicies.PhysicalMultiFactor,
+ fields["auth_policies"]);
+
+ resp.AuthenticationTimeUtc = DateTime.UtcNow;
+ fields = ((IExtensionResponse)resp).Serialize(null);
+ Assert.AreEqual(2, fields.Count);
+ Assert.IsTrue(fields.ContainsKey("auth_time"));
+
+ resp.NistAssuranceLevel = NistAssuranceLevel.Level3;
+ fields = ((IExtensionResponse)resp).Serialize(null);
+ Assert.AreEqual(4, fields.Count);
+ Assert.IsTrue(fields.ContainsKey("auth_level.ns.nist"));
+ Assert.AreEqual(Constants.AuthenticationLevels.NistTypeUri, fields["auth_level.ns.nist"]);
+ Assert.IsTrue(fields.ContainsKey("auth_level.nist"));
+ Assert.AreEqual("3", fields["auth_level.nist"]);
+
+ resp.AssuranceLevels.Add("custom", "CU");
+ fields = ((IExtensionResponse)resp).Serialize(null);
+ Assert.AreEqual(6, fields.Count);
+ Assert.IsTrue(fields.ContainsKey("auth_level.ns.alias2"));
+ Assert.AreEqual("custom", fields["auth_level.ns.alias2"]);
+ Assert.IsTrue(fields.ContainsKey("auth_level.alias2"));
+ Assert.AreEqual("CU", fields["auth_level.alias2"]);
+ // and make sure the NIST is still there.
+ Assert.IsTrue(fields.ContainsKey("auth_level.ns.nist"));
+ Assert.AreEqual(Constants.AuthenticationLevels.NistTypeUri, fields["auth_level.ns.nist"]);
+ Assert.IsTrue(fields.ContainsKey("auth_level.nist"));
+ Assert.AreEqual("3", fields["auth_level.nist"]);
+ }
}
}
diff --git a/src/DotNetOpenId.Test/Extensions/SimpleRegistrationTests.cs b/src/DotNetOpenId.Test/Extensions/SimpleRegistrationTests.cs index 69942a5..9437f27 100644 --- a/src/DotNetOpenId.Test/Extensions/SimpleRegistrationTests.cs +++ b/src/DotNetOpenId.Test/Extensions/SimpleRegistrationTests.cs @@ -11,7 +11,7 @@ namespace DotNetOpenId.Test.Extensions { [Test]
public void None() {
var response = ParameterizedTest<ClaimsResponse>(
- TestSupport.GetIdentityUrl(TestSupport.Scenarios.ExtensionFullCooperation, Version), null);
+ TestSupport.Scenarios.ExtensionFullCooperation, Version, null);
Assert.IsNull(response);
}
@@ -21,7 +21,7 @@ namespace DotNetOpenId.Test.Extensions { request.FullName = DemandLevel.Request;
request.Email = DemandLevel.Require;
var response = ParameterizedTest<ClaimsResponse>(
- TestSupport.GetIdentityUrl(TestSupport.Scenarios.ExtensionFullCooperation, Version), request);
+ TestSupport.Scenarios.ExtensionFullCooperation, Version, request);
Assert.AreEqual("Andrew Arnott", response.FullName);
Assert.AreEqual("andrewarnott@gmail.com", response.Email);
}
@@ -31,7 +31,7 @@ namespace DotNetOpenId.Test.Extensions { request.FullName = DemandLevel.Request;
request.Email = DemandLevel.Require;
var response = ParameterizedTest<ClaimsResponse>(
- TestSupport.GetIdentityUrl(TestSupport.Scenarios.ExtensionPartialCooperation, Version), request);
+ TestSupport.Scenarios.ExtensionPartialCooperation, Version, request);
Assert.IsNull(response.FullName);
Assert.AreEqual("andrewarnott@gmail.com", response.Email);
}
|