summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/OpenId/Provider/OpenIdProviderTests.cs
blob: 75d871c0dd99a5398554b893761932d31b5e565a (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
147
148
//-----------------------------------------------------------------------
// <copyright file="OpenIdProviderTests.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.OpenId.Provider {
	using System;
	using System.IO;
	using System.Web;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.OpenId;
	using DotNetOpenAuth.OpenId.Extensions;
	using DotNetOpenAuth.OpenId.Messages;
	using DotNetOpenAuth.OpenId.Provider;
	using DotNetOpenAuth.OpenId.RelyingParty;
	using DotNetOpenAuth.Test.Hosting;
	using NUnit.Framework;

	[TestFixture]
	public class OpenIdProviderTests : OpenIdTestBase {
		private OpenIdProvider provider;

		[SetUp]
		public override void SetUp() {
			base.SetUp();

			this.provider = this.CreateProvider();
		}

		/// <summary>
		/// Verifies that the constructor throws an exception if the app store is null.
		/// </summary>
		[TestCase, ExpectedException(typeof(ArgumentNullException))]
		public void CtorNull() {
			new OpenIdProvider(null);
		}

		/// <summary>
		/// Verifies that the SecuritySettings property throws when set to null.
		/// </summary>
		[TestCase, ExpectedException(typeof(ArgumentNullException))]
		public void SecuritySettingsSetNull() {
			this.provider.SecuritySettings = null;
		}

		/// <summary>
		/// Verifies the SecuritySettings property can be set to a new instance.
		/// </summary>
		[TestCase]
		public void SecuritySettings() {
			var newSettings = new ProviderSecuritySettings();
			this.provider.SecuritySettings = newSettings;
			Assert.AreSame(newSettings, this.provider.SecuritySettings);
		}

		[TestCase]
		public void ExtensionFactories() {
			var factories = this.provider.ExtensionFactories;
			Assert.IsNotNull(factories);
			Assert.AreEqual(1, factories.Count);
			Assert.IsInstanceOf<StandardOpenIdExtensionFactory>(factories[0]);
		}

		/// <summary>
		/// Verifies the Channel property.
		/// </summary>
		[TestCase]
		public void ChannelGetter() {
			Assert.IsNotNull(this.provider.Channel);
		}

		/// <summary>
		/// Verifies the GetRequest method throws outside an HttpContext.
		/// </summary>
		[TestCase, ExpectedException(typeof(InvalidOperationException))]
		public void GetRequestNoContext() {
			HttpContext.Current = null;
			this.provider.GetRequest();
		}

		/// <summary>
		/// Verifies GetRequest throws on null input.
		/// </summary>
		[TestCase, ExpectedException(typeof(ArgumentNullException))]
		public void GetRequestNull() {
			this.provider.GetRequest(null);
		}

		/// <summary>
		/// Verifies that GetRequest correctly returns the right messages.
		/// </summary>
		[TestCase]
		public void GetRequest() {
			HttpRequestInfo httpInfo = new HttpRequestInfo();
			httpInfo.UrlBeforeRewriting = new Uri("http://someUri");
			Assert.IsNull(this.provider.GetRequest(httpInfo), "An irrelevant request should return null.");
			var providerDescription = new ProviderEndpointDescription(OpenIdTestBase.OPUri, Protocol.Default.Version);

			// Test some non-empty request scenario.
			OpenIdCoordinator coordinator = new OpenIdCoordinator(
				rp => {
					rp.Channel.Request(AssociateRequest.Create(rp.SecuritySettings, providerDescription));
				},
				op => {
					IRequest request = op.GetRequest();
					Assert.IsInstanceOf<AutoResponsiveRequest>(request);
					op.SendResponse(request);
				});
			coordinator.Run();
		}

		[TestCase]
		public void BadRequestsGenerateValidErrorResponses() {
			var coordinator = new OpenIdCoordinator(
				rp => {
					var nonOpenIdMessage = new Mocks.TestDirectedMessage();
					nonOpenIdMessage.Recipient = OPUri;
					nonOpenIdMessage.HttpMethods = HttpDeliveryMethods.PostRequest;
					MessagingTestBase.GetStandardTestMessage(MessagingTestBase.FieldFill.AllRequired, nonOpenIdMessage);
					var response = rp.Channel.Request<DirectErrorResponse>(nonOpenIdMessage);
					Assert.IsNotNull(response.ErrorMessage);
					Assert.AreEqual(Protocol.Default.Version, response.Version);
				},
				AutoProvider);

			coordinator.Run();
		}

		[TestCase, Category("HostASPNET")]
		public void BadRequestsGenerateValidErrorResponsesHosted() {
			try {
				using (AspNetHost host = AspNetHost.CreateHost(TestWebDirectory)) {
					Uri opEndpoint = new Uri(host.BaseUri, "/OpenIdProviderEndpoint.ashx");
					var rp = new OpenIdRelyingParty(null);
					var nonOpenIdMessage = new Mocks.TestDirectedMessage();
					nonOpenIdMessage.Recipient = opEndpoint;
					nonOpenIdMessage.HttpMethods = HttpDeliveryMethods.PostRequest;
					MessagingTestBase.GetStandardTestMessage(MessagingTestBase.FieldFill.AllRequired, nonOpenIdMessage);
					var response = rp.Channel.Request<DirectErrorResponse>(nonOpenIdMessage);
					Assert.IsNotNull(response.ErrorMessage);
				}
			} catch (FileNotFoundException ex) {
				Assert.Inconclusive("Unable to execute hosted ASP.NET tests because {0} could not be found.  {1}", ex.FileName, ex.FusionLog);
			}
		}
	}
}