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
149
150
151
|
//-----------------------------------------------------------------------
// <copyright file="OpenIdProviderTests.cs" company="Outercurve Foundation">
// Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Test.OpenId.Provider {
using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
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>
[Test, ExpectedException(typeof(ArgumentNullException))]
public void CtorNull() {
new OpenIdProvider(null);
}
/// <summary>
/// Verifies that the SecuritySettings property throws when set to null.
/// </summary>
[Test, ExpectedException(typeof(ArgumentNullException))]
public void SecuritySettingsSetNull() {
this.provider.SecuritySettings = null;
}
/// <summary>
/// Verifies the SecuritySettings property can be set to a new instance.
/// </summary>
[Test]
public void SecuritySettings() {
var newSettings = new ProviderSecuritySettings();
this.provider.SecuritySettings = newSettings;
Assert.AreSame(newSettings, this.provider.SecuritySettings);
}
[Test]
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>
[Test]
public void ChannelGetter() {
Assert.IsNotNull(this.provider.Channel);
}
/// <summary>
/// Verifies the GetRequest method throws outside an HttpContext.
/// </summary>
[Test, ExpectedException(typeof(InvalidOperationException))]
public async Task GetRequestNoContext() {
HttpContext.Current = null;
await this.provider.GetRequestAsync();
}
/// <summary>
/// Verifies GetRequest throws on null input.
/// </summary>
[Test, ExpectedException(typeof(ArgumentNullException))]
public async Task GetRequestNull() {
await this.provider.GetRequestAsync((HttpRequestMessage)null);
}
/// <summary>
/// Verifies that GetRequest correctly returns the right messages.
/// </summary>
[Test]
public async Task GetRequest() {
var httpInfo = new HttpRequestMessage(HttpMethod.Get, "http://someUri");
Assert.IsNull(await this.provider.GetRequestAsync(httpInfo), "An irrelevant request should return null.");
var providerDescription = new ProviderEndpointDescription(OPUri, Protocol.Default.Version);
// Test some non-empty request scenario.
var coordinator = new CoordinatorBase(
CoordinatorBase.RelyingPartyDriver(async (rp, ct) => {
await rp.Channel.RequestAsync(AssociateRequestRelyingParty.Create(rp.SecuritySettings, providerDescription), ct);
}),
CoordinatorBase.HandleProvider(async (op, req, ct) => {
IRequest request = await op.GetRequestAsync(req);
Assert.IsInstanceOf<AutoResponsiveRequest>(request);
return await op.PrepareResponseAsync(request, ct);
}));
await coordinator.RunAsync();
}
[Test]
public async Task BadRequestsGenerateValidErrorResponses() {
var coordinator = new CoordinatorBase(
CoordinatorBase.RelyingPartyDriver(async (rp, ct) => {
var nonOpenIdMessage = new Mocks.TestDirectedMessage {
Recipient = OPUri,
HttpMethods = HttpDeliveryMethods.PostRequest
};
MessagingTestBase.GetStandardTestMessage(MessagingTestBase.FieldFill.AllRequired, nonOpenIdMessage);
var response = await rp.Channel.RequestAsync<DirectErrorResponse>(nonOpenIdMessage, ct);
Assert.IsNotNull(response.ErrorMessage);
Assert.AreEqual(Protocol.Default.Version, response.Version);
}),
AutoProvider);
await coordinator.RunAsync();
}
[Test, Category("HostASPNET")]
public async Task 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 = await rp.Channel.RequestAsync<DirectErrorResponse>(nonOpenIdMessage, CancellationToken.None);
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);
}
}
}
}
|