Skip to content
This repository has been archived by the owner on Mar 20, 2019. It is now read-only.

Commit

Permalink
Applied TestSupport helps to more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Aug 4, 2008
1 parent 9508b01 commit 8448bff
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/DotNetOpenId.Test/EndToEndTesting.cs
Expand Up @@ -59,7 +59,7 @@ public class EndToEndTesting {
Assert.AreEqual(realm.ToString(), consumerToProviderQuery[protocol.openid.Realm]);
redirectToProviderUrl = request.RedirectingResponse.ExtractUrl();

OpenIdProvider provider = TestSupport.CreateProvider(rpMessageToOP.EncodedFields.ToNameValueCollection());
OpenIdProvider provider = TestSupport.CreateProviderForRequest(request);
var opAuthRequest = provider.Request as DotNetOpenId.Provider.IAuthenticationRequest;
Assert.IsNotNull(opAuthRequest);
opAuthRequest.IsAuthenticated = expectedResult == AuthenticationStatus.Authenticated;
Expand Down
41 changes: 10 additions & 31 deletions src/DotNetOpenId.Test/RelyingParty/AuthenticationResponseTests.cs
Expand Up @@ -16,7 +16,6 @@ public class AuthenticationResponseTests {
Realm realm = new Realm(TestSupport.GetFullUrl(TestSupport.ConsumerPage).AbsoluteUri);
Uri returnTo;
const string returnToRemovableParameter = "a";
ApplicationMemoryStore store;

public AuthenticationResponseTests() {
UriBuilder builder = new UriBuilder(TestSupport.GetFullUrl(TestSupport.ConsumerPage));
Expand All @@ -28,38 +27,18 @@ public class AuthenticationResponseTests {

[SetUp]
public void SetUp() {
store = new ApplicationMemoryStore();
if (!UntrustedWebRequest.WhitelistHosts.Contains("localhost"))
UntrustedWebRequest.WhitelistHosts.Add("localhost");
}

Uri getPositiveAssertion(ProtocolVersion version) {
try {
OpenIdRelyingParty rp = new OpenIdRelyingParty(store, null, null);
Identifier id = TestSupport.GetMockIdentifier(TestSupport.Scenarios.AutoApproval, version);
var request = rp.CreateRequest(id, realm, returnTo);
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());
}
}
throw;
}
return redirectUrl;
} catch (OpenIdException ex) {
Assert.Ignore("Test failed to verify good or bad behavior on account of failing to set itself up: {0}", ex);
return null; // Assert.Ignore will throw an exception anyway
}
OpenIdRelyingParty rp = TestSupport.CreateRelyingParty(null);
Identifier id = TestSupport.GetMockIdentifier(TestSupport.Scenarios.AutoApproval, version);
var request = rp.CreateRequest(id, realm, returnTo);
var provider = TestSupport.CreateProviderForRequest(request);
var opRequest = provider.Request as DotNetOpenId.Provider.IAuthenticationRequest;
opRequest.IsAuthenticated = true;
return opRequest.Response.ExtractUrl();
}
void removeQueryParameter(ref Uri uri, string parameterToRemove) {
UriBuilder builder = new UriBuilder(uri);
Expand Down Expand Up @@ -92,7 +71,7 @@ public class AuthenticationResponseTests {
void resign(ref Uri uri) {
UriBuilder builder = new UriBuilder(uri);
NameValueCollection nvc = HttpUtility.ParseQueryString(builder.Query);
TestSupport.Resign(nvc, store);
TestSupport.Resign(nvc, TestSupport.RelyingPartyStore);
builder.Query = UriUtil.CreateQueryString(nvc);
uri = builder.Uri;
}
Expand All @@ -106,7 +85,7 @@ public class AuthenticationResponseTests {
// which should cause a failure because the return_to argument
// says that parameter is supposed to be there.
removeQueryParameter(ref assertion, returnToRemovableParameter);
var response = new OpenIdRelyingParty(store, assertion, HttpUtility.ParseQueryString(assertion.Query)).Response;
var response = TestSupport.CreateRelyingParty(false, assertion, HttpUtility.ParseQueryString(assertion.Query)).Response;
Assert.AreEqual(AuthenticationStatus.Failed, response.Status);
Assert.IsNotNull(response.Exception);
}
Expand Down Expand Up @@ -140,7 +119,7 @@ public class AuthenticationResponseTests {
resign(ref assertion); // resign changed URL to simulate a contrived OP for breaking into RPs.

// (triggers exception) "... you're in trouble up to your ears."
var response = new OpenIdRelyingParty(store, assertion, HttpUtility.ParseQueryString(assertion.Query)).Response;
var response = TestSupport.CreateRelyingParty(false, assertion, HttpUtility.ParseQueryString(assertion.Query)).Response;
Assert.AreEqual(AuthenticationStatus.Failed, response.Status);
Assert.IsNotNull(response.Exception);
}
Expand Down
12 changes: 10 additions & 2 deletions src/DotNetOpenId.Test/TestSupport.cs
Expand Up @@ -103,7 +103,7 @@ public enum Scenarios {
/// will be automatically handled by an internal <see cref="OpenIdProvider"/>
/// that uses the shared <see cref="ProviderStore"/>.
/// </summary>
private static OpenIdRelyingParty CreateRelyingParty(bool stateless, Uri requestUrl, NameValueCollection fields) {
internal static OpenIdRelyingParty CreateRelyingParty(bool stateless, Uri requestUrl, NameValueCollection fields) {
var rp = new OpenIdRelyingParty(RelyingPartyStore, requestUrl ?? GetFullUrl(ConsumerPage), fields ?? new NameValueCollection());
rp.DirectMessageChannel = new DirectMessageTestRedirector(ProviderStore);
return rp;
Expand All @@ -129,6 +129,13 @@ public enum Scenarios {
GetFullUrl(ProviderPage), GetFullUrl(ProviderPage), fields);
return provider;
}
internal static OpenIdProvider CreateProviderForRequest(DotNetOpenId.RelyingParty.IAuthenticationRequest request) {
IResponse relyingPartyAuthenticationRequest = request.RedirectingResponse;
var rpWebMessageToOP = (Response)relyingPartyAuthenticationRequest;
var rpMessageToOP = (IndirectMessageRequest)rpWebMessageToOP.EncodableMessage;
var provider = CreateProvider(rpMessageToOP.EncodedFields.ToNameValueCollection());
return provider;
}

[SetUp]
public void SetUp() {
Expand All @@ -153,14 +160,15 @@ public enum Scenarios {
/// to simulate a Provider that deliberately sent a bad message in an attempt
/// to thwart RP security.
/// </summary>
internal static void Resign(NameValueCollection nvc, ApplicationMemoryStore store) {
internal static void Resign(NameValueCollection nvc, IRelyingPartyApplicationStore store) {
Debug.Assert(nvc != null);
Debug.Assert(store != null);
var dict = Util.NameValueCollectionToDictionary(nvc);
Protocol protocol = Protocol.Detect(dict);
Uri providerEndpoint = new Uri(nvc[protocol.openid.op_endpoint]);
string assoc_handle = nvc[protocol.openid.assoc_handle];
Association assoc = store.GetAssociation(providerEndpoint, assoc_handle);
Debug.Assert(assoc != null, "Association not found in RP's store. Maybe you're communicating with a hosted OP instead of the TestSupport one?");
IList<string> signed = nvc[protocol.openid.signed].Split(',');
var subsetDictionary = new Dictionary<string, string>();
foreach (string signedKey in signed) {
Expand Down

0 comments on commit 8448bff

Please sign in to comment.