summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/OpenId/Extensions/SimpleRegistration/ClaimsResponseTests.cs
blob: 0bdc36ed0fbedf93b37fc53e1c828186d2b7ad98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//-----------------------------------------------------------------------
// <copyright file="ClaimsResponseTests.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.OpenId.Extensions {
	using System;
	using System.Globalization;
	using System.IO;
	using System.Runtime.Serialization;
	using System.Runtime.Serialization.Formatters.Binary;
	using System.Xml.Serialization;
	using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
	using NUnit.Framework;

	[TestFixture]
	public class ClaimsResponseTests {
		[TestCase]
		public void EmptyMailAddress() {
			ClaimsResponse response = new ClaimsResponse(Constants.sreg_ns);
			response.Email = string.Empty;
			Assert.IsNull(response.MailAddress);
		}

		[TestCase, Ignore("serialization no longer supported")]
		public void BinarySerialization() {
			ClaimsResponse fields = this.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);
		}

		[TestCase, Ignore("serialization no longer supported")]
		public void XmlSerialization() {
			ClaimsResponse fields = this.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);
		}

		[TestCase]
		public void EqualityTest() {
			ClaimsResponse fields1 = this.GetFilledData();

			Assert.AreNotEqual(fields1, null);
			Assert.AreNotEqual(fields1, "string");

			ClaimsResponse fields2 = this.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);
		}

		[TestCase]
		public void Birthdates() {
			var response = new ClaimsResponse();
			// Verify that they both start out as null
			Assert.IsNull(response.BirthDateRaw);
			Assert.IsFalse(response.BirthDate.HasValue);

			// Verify that null can be set.
			response.BirthDate = null;
			response.BirthDateRaw = null;
			Assert.IsNull(response.BirthDateRaw);
			Assert.IsFalse(response.BirthDate.HasValue);

			// Verify that the strong-typed BirthDate property can be set and that it affects the raw property.
			response.BirthDate = DateTime.Parse("April 4, 1984");
			Assert.AreEqual(4, response.BirthDate.Value.Month);
			Assert.AreEqual("1984-04-04", response.BirthDateRaw);

			// Verify that the raw property can be set with a complete birthdate and that it affects the strong-typed property.
			response.BirthDateRaw = "1998-05-08";
			Assert.AreEqual("1998-05-08", response.BirthDateRaw);
			Assert.AreEqual(DateTime.Parse("May 8, 1998", CultureInfo.InvariantCulture), response.BirthDate);

			// Verify that an partial raw birthdate works, and sets the strong-typed property to null since it cannot be represented.
			response.BirthDateRaw = "2000-00-00";
			Assert.AreEqual("2000-00-00", response.BirthDateRaw);
			Assert.IsFalse(response.BirthDate.HasValue);
		}

		[TestCase, ExpectedException(typeof(ArgumentException))]
		public void InvalidRawBirthdate() {
			var response = new ClaimsResponse();
			response.BirthDateRaw = "2008";
		}

		private 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",
			};
		}
	}
}