summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenId.Test/Extensions/ClaimsResponseTests.cs
blob: afc2e032178eb553cec1be3ed0340d353c44fcfc (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
/********************************************************
 * 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
		}
	}
}