This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose form data to pre-registration handler (closes #68)
- Loading branch information
1 parent
f74a4d2
commit 5acaa96
Showing
4 changed files
with
251 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
using System.Collections.Generic; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Newtonsoft.Json; | ||
using Stormpath.Configuration.Abstractions; | ||
using Stormpath.Owin.Abstractions; | ||
using Stormpath.Owin.Middleware; | ||
using Stormpath.SDK; | ||
using Xunit; | ||
|
@@ -217,5 +220,217 @@ public async Task ReturnCustomErrorMessageDuringJsonPost() | |
response.StatusCode.Should().Be(HttpStatusCode.BadRequest); | ||
(await response.Content.ReadAsStringAsync()).Should().Contain("\"message\": \"Nice try, rebel scum!\""); | ||
} | ||
|
||
[Fact] | ||
public async Task AccessPostDataDuringFormPost() | ||
{ | ||
var handlerRun = false; | ||
|
||
// Arrange | ||
var fixture = new OwinTestFixture | ||
{ | ||
Options = new StormpathOwinOptions | ||
{ | ||
PreRegistrationHandler = (ctx, ct) => | ||
{ | ||
handlerRun = true; | ||
ctx.PostData["email"].Should().Be("[email protected]"); | ||
ctx.PostData["custom"].Should().Be("foobar!"); | ||
|
||
// Don't actually create an account | ||
ctx.Result = new PreRegistrationResult | ||
{ | ||
Success = false | ||
}; | ||
|
||
return Task.FromResult(0); | ||
}, | ||
Configuration = new StormpathConfiguration() | ||
{ | ||
Web = new WebConfiguration() | ||
{ | ||
Register = new WebRegisterRouteConfiguration() | ||
{ | ||
Form = new WebRegisterRouteFormConfiguration() | ||
{ | ||
Fields = new Dictionary<string, WebFieldConfiguration>() | ||
{ | ||
["custom"] = new WebFieldConfiguration() | ||
{ | ||
Required = true, | ||
Enabled = true, | ||
Label = "custom", | ||
Placeholder = "custom", | ||
Type = "text", | ||
Visible = true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
var server = Helpers.CreateServer(fixture); | ||
var csrfToken = await CsrfToken.GetTokenForRoute(server, "/register"); | ||
|
||
// Act | ||
var payload = new Dictionary<string, string>() | ||
{ | ||
["email"] = "[email protected]", | ||
["password"] = "Changeme123!!", | ||
["givenName"] = "Chewbacca", | ||
["surname"] = "Wookie", | ||
["custom"] = "foobar!", | ||
["st"] = csrfToken, | ||
}; | ||
|
||
var request = new HttpRequestMessage(HttpMethod.Post, "/register") | ||
{ | ||
Content = new FormUrlEncodedContent(payload) | ||
}; | ||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html")); | ||
|
||
await server.SendAsync(request); | ||
|
||
handlerRun.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public async Task AccessPostDataDuringJsonPost() | ||
{ | ||
var handlerRun = false; | ||
|
||
// Arrange | ||
var fixture = new OwinTestFixture | ||
{ | ||
Options = new StormpathOwinOptions | ||
{ | ||
PreRegistrationHandler = (ctx, ct) => | ||
{ | ||
handlerRun = true; | ||
ctx.PostData["email"].Should().Be("[email protected]"); | ||
ctx.PostData["custom"].Should().Be("foobar!"); | ||
|
||
// Don't actually create an account | ||
ctx.Result = new PreRegistrationResult | ||
{ | ||
Success = false | ||
}; | ||
|
||
return Task.FromResult(0); | ||
}, | ||
Configuration = new StormpathConfiguration() | ||
{ | ||
Web = new WebConfiguration() | ||
{ | ||
Register = new WebRegisterRouteConfiguration() | ||
{ | ||
Form = new WebRegisterRouteFormConfiguration() | ||
{ | ||
Fields = new Dictionary<string, WebFieldConfiguration>() | ||
{ | ||
["custom"] = new WebFieldConfiguration() | ||
{ | ||
Required = true, | ||
Enabled = true, | ||
Label = "custom", | ||
Placeholder = "custom", | ||
Type = "text", | ||
Visible = true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
var server = Helpers.CreateServer(fixture); | ||
|
||
// Act | ||
var payload = new | ||
{ | ||
email = "[email protected]", | ||
password = "Changeme123!!", | ||
givenName = "Chewbacca", | ||
surname = "Wookiee", | ||
custom = "foobar!" | ||
}; | ||
|
||
await server.PostAsync("/register", new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json")); | ||
|
||
handlerRun.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public async Task AccessPostDataDuringJsonPostWithCustomDataSubobject() | ||
{ | ||
var handlerRun = false; | ||
|
||
// Arrange | ||
var fixture = new OwinTestFixture | ||
{ | ||
Options = new StormpathOwinOptions | ||
{ | ||
PreRegistrationHandler = (ctx, ct) => | ||
{ | ||
handlerRun = true; | ||
ctx.PostData["email"].Should().Be("[email protected]"); | ||
ctx.PostData["custom"].Should().Be("foobar!"); | ||
|
||
// Don't actually create an account | ||
ctx.Result = new PreRegistrationResult | ||
{ | ||
Success = false | ||
}; | ||
|
||
return Task.FromResult(0); | ||
}, | ||
Configuration = new StormpathConfiguration() | ||
{ | ||
Web = new WebConfiguration() | ||
{ | ||
Register = new WebRegisterRouteConfiguration() | ||
{ | ||
Form = new WebRegisterRouteFormConfiguration() | ||
{ | ||
Fields = new Dictionary<string, WebFieldConfiguration>() | ||
{ | ||
["custom"] = new WebFieldConfiguration() | ||
{ | ||
Required = true, | ||
Enabled = true, | ||
Label = "custom", | ||
Placeholder = "custom", | ||
Type = "text", | ||
Visible = true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
var server = Helpers.CreateServer(fixture); | ||
|
||
// Act | ||
var payload = new | ||
{ | ||
email = "[email protected]", | ||
password = "Changeme123!!", | ||
givenName = "Chewbacca", | ||
surname = "Wookiee", | ||
customData = new | ||
{ | ||
custom = "foobar!" | ||
} | ||
}; | ||
|
||
await server.PostAsync("/register", new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json")); | ||
|
||
handlerRun.Should().BeTrue(); | ||
} | ||
} | ||
} |