Skip to content

Commit d07ad3c

Browse files
Add Signout integration test
1 parent e1935e9 commit d07ad3c

19 files changed

Lines changed: 228 additions & 97 deletions

src/SchoolAccount.Web.Mvc/Authentication/Extensions/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ IConfigurationManager configuration
3535
.AddCookie(options =>
3636
{
3737
options.LoginPath = "/";
38-
options.LogoutPath = "/account/signout";
38+
options.LogoutPath = "/account/logout";
3939
options.AccessDeniedPath = "/error/403";
4040
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
4141
options.Cookie.Name = "sa-cookie";
@@ -54,7 +54,7 @@ IConfigurationManager configuration
5454
settings.SignedOutCallbackPath
5555
)
5656
? settings.SignedOutCallbackPath
57-
: "/account/signedout";
57+
: "/account/loggedout";
5858

5959
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
6060
options.ResponseType = OpenIdConnectResponseType.IdToken;

src/SchoolAccount.Web.Mvc/Features/Accounts/AccountController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace SchoolAccount.Web.Mvc.Features.Accounts;
1010
public class AccountController : Controller
1111
{
1212
[HttpPost]
13-
public IActionResult SignIn(Uri? returnUrl = null)
13+
public IActionResult Login(Uri? returnUrl = null)
1414
{
1515
returnUrl ??= new Uri(
1616
Url.Action("Dashboard", "Dashboard")
@@ -30,7 +30,7 @@ public IActionResult SignIn(Uri? returnUrl = null)
3030
}
3131

3232
[HttpPost]
33-
public new IActionResult SignOut()
33+
public IActionResult Logout()
3434
{
3535
if (!(User.Identity?.IsAuthenticated ?? false))
3636
{
@@ -46,7 +46,7 @@ public IActionResult SignIn(Uri? returnUrl = null)
4646
}
4747

4848
[HttpGet]
49-
public IActionResult SignedOut()
49+
public IActionResult LoggedOut()
5050
{
5151
HttpContext.Session.Clear();
5252

src/SchoolAccount.Web.Mvc/Features/Error/ErrorController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public IActionResult Error(HttpStatusCode statusCode)
1818
);
1919

2020
var errorViewModel = new ErrorViewModel(statusCode);
21+
Response.StatusCode = (int)errorViewModel.StatusCode;
2122

2223
return View(errorViewModel);
2324
}

src/SchoolAccount.Web.Mvc/Features/Shared/_Layout.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</a>
2626
@if (isAuthenticated)
2727
{
28-
using (Html.BeginForm("SignOut", "Account", FormMethod.Post))
28+
using (Html.BeginForm("Logout", "Account", FormMethod.Post))
2929
{
3030
<button role="button" type="submit" class="govuk-link dfe-school-account-header__signout">
3131
Sign out

src/SchoolAccount.Web.Mvc/Features/Start/Start.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Login with a DSI Account to access your School Account.
88
</p>
99

10-
@using (Html.BeginForm("SignIn", "Account", FormMethod.Post))
10+
@using (Html.BeginForm("Login", "Account", FormMethod.Post))
1111
{
1212
<input type="hidden" name="returnUrl" value="@Model.GoTo" />
1313
<button type="submit" role="button" class="govuk-button">

tests/SchoolAccount.IntegrationTests/Common/AngleSharpPage.cs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,54 @@
33

44
namespace SchoolAccount.IntegrationTests.Common;
55

6-
public class AngleSharpPage
6+
public abstract class AngleSharpPage
77
{
8-
private readonly IDocument _page;
8+
protected IDocument Page;
99

10-
public AngleSharpPage(string pageContent)
10+
protected AngleSharpPage() { }
11+
12+
protected AngleSharpPage(string pageContent)
13+
{
14+
Initialise(pageContent);
15+
}
16+
17+
protected void Initialise(string pageContent)
1118
{
1219
var context = BrowsingContext.New(Configuration.Default);
13-
_page = context
20+
Page = context
1421
.OpenAsync(req => req.Content(pageContent), TestContext.Current.CancellationToken)
1522
.Result;
1623
}
1724

18-
public string? GetTitle()
25+
public static async Task<T> FromResponseAsync<T>(
26+
HttpResponseMessage responseMessage,
27+
CancellationToken cancellationToken = default
28+
)
29+
where T : AngleSharpPage, new()
30+
{
31+
var html = await responseMessage.Content.ReadAsStringAsync(cancellationToken);
32+
33+
var page = new T();
34+
page.Initialise(html);
35+
36+
return page;
37+
}
38+
39+
public virtual string? GetTitle()
1940
{
20-
var pageTitle = _page.QuerySelector("title");
41+
var pageTitle = Page.QuerySelector("title");
2142
return pageTitle?.TextContent;
2243
}
2344

24-
public string? GetFirstHeading()
45+
public virtual string? GetFirstHeading()
2546
{
26-
var headingElement = _page.QuerySelector("h1.govuk-heading-l");
47+
var headingElement = Page.QuerySelector("h1.govuk-heading-l");
2748
return headingElement?.TextContent;
2849
}
2950

30-
public string? GetFirstBody()
51+
public virtual string? GetFirstBody()
3152
{
32-
var bodyElement = _page.QuerySelector("body");
53+
var bodyElement = Page.QuerySelector("body");
3354
return bodyElement?.TextContent;
3455
}
3556
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace SchoolAccount.IntegrationTests.Common;
2+
3+
public class ClientOptions
4+
{
5+
public bool AllowAutoRedirect { get; init; }
6+
7+
public static readonly ClientOptions AllowRedirects = new() { AllowAutoRedirect = true };
8+
}

tests/SchoolAccount.IntegrationTests/Common/MockAuthHandler.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class MockAuthHandler(
99
IOptionsMonitor<AuthenticationSchemeOptions> options,
1010
ILoggerFactory logger,
1111
UrlEncoder encoder
12-
) : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
12+
) : SignOutAuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
1313
{
1414
public const string SchemeName = "AuthScheme";
1515

@@ -37,4 +37,9 @@ protected override Task HandleChallengeAsync(AuthenticationProperties? propertie
3737
Response.Redirect(properties?.RedirectUri ?? "/");
3838
return Task.CompletedTask;
3939
}
40+
41+
protected override Task HandleSignOutAsync(AuthenticationProperties? properties)
42+
{
43+
return Task.CompletedTask;
44+
}
4045
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace SchoolAccount.IntegrationTests.Common.Pages;
2+
3+
/// <remarks>
4+
/// This will be replaced with their own pages eventually
5+
/// </remarks>
6+
public class CommonPage : AngleSharpPage { }
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using SchoolAccount.Web.Mvc.Features.Error;
2+
3+
namespace SchoolAccount.IntegrationTests.Common.Pages;
4+
5+
public class ErrorPage : AngleSharpPage
6+
{
7+
public bool IsNotFoundPageTitle()
8+
{
9+
var pageTitle = GetTitle();
10+
11+
return pageTitle?.Equals(ErrorViewModel.NotFoundTitle, StringComparison.Ordinal) == true;
12+
}
13+
14+
public bool IsNotFoundPageHeading()
15+
{
16+
var pageHeading = GetFirstHeading();
17+
18+
return pageHeading?.Equals("Page not found", StringComparison.Ordinal) == true;
19+
}
20+
21+
public bool IsServerErrorPageTitle()
22+
{
23+
var pageTitle = GetTitle();
24+
25+
return pageTitle?.Equals(ErrorViewModel.ErrorTitle, StringComparison.Ordinal) == true;
26+
}
27+
}

0 commit comments

Comments
 (0)