From 31b0413931fae6745e5991f37482a4ddf88a0340 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Thu, 26 Apr 2018 13:31:39 -0700 Subject: [PATCH] Add basic initial functional tests for CustomPolicyProvider sample --- .../AuthSamples.FunctionalTests.csproj | 1 + .../CustomPolicyProviderTests.cs | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 test/AuthSamples.FunctionalTests/CustomPolicyProviderTests.cs diff --git a/test/AuthSamples.FunctionalTests/AuthSamples.FunctionalTests.csproj b/test/AuthSamples.FunctionalTests/AuthSamples.FunctionalTests.csproj index 6062a67de3..0cb04a92fd 100644 --- a/test/AuthSamples.FunctionalTests/AuthSamples.FunctionalTests.csproj +++ b/test/AuthSamples.FunctionalTests/AuthSamples.FunctionalTests.csproj @@ -14,6 +14,7 @@ + diff --git a/test/AuthSamples.FunctionalTests/CustomPolicyProviderTests.cs b/test/AuthSamples.FunctionalTests/CustomPolicyProviderTests.cs new file mode 100644 index 0000000000..86d6647c45 --- /dev/null +++ b/test/AuthSamples.FunctionalTests/CustomPolicyProviderTests.cs @@ -0,0 +1,56 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc.Testing; +using Xunit; + +namespace AuthSamples.FunctionalTests +{ + public class CustomPolicyProviderTests : IClassFixture> + { + public CustomPolicyProviderTests(WebApplicationFactory fixture) + { + Client = fixture.CreateDefaultClient(); + } + + public HttpClient Client { get; } + + [Fact] + public async Task DefaultReturns200() + { + // Arrange & Act + var response = await Client.GetAsync("/"); + var content = await response.Content.ReadAsStringAsync(); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + } + + [Fact] + public async Task MinimumAge10RedirectsWhenNotLoggedIn() + { + // Arrange & Act + var response = await Client.GetAsync("/Home/MinimumAge10"); + var content = await response.Content.ReadAsStringAsync(); + + // Assert + Assert.Equal(HttpStatusCode.Redirect, response.StatusCode); + Assert.Equal("http://localhost/account/signin?ReturnUrl=%2FHome%2FMinimumAge10", response.Headers.Location.ToString()); + } + + [Fact] + public async Task MinimumAge50RedirectsWhenNotLoggedIn() + { + // Arrange & Act + var response = await Client.GetAsync("/Home/MinimumAge50"); + var content = await response.Content.ReadAsStringAsync(); + + // Assert + Assert.Equal(HttpStatusCode.Redirect, response.StatusCode); + Assert.Equal("http://localhost/account/signin?ReturnUrl=%2FHome%2FMinimumAge50", response.Headers.Location.ToString()); + } + } +}