From 08b71223e9093c0dc0293acf6f6b2c8819dc86a6 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Thu, 12 Nov 2015 15:37:33 -0800 Subject: [PATCH] Changes to run tests on coreclr with moq.netcore --- .../project.json | 7 ++- .../project.json | 7 ++- .../FunctionalTest.cs | 47 +++++++-------- .../project.json | 10 +++- .../ApiConsistencyTest.cs | 7 +-- .../project.json | 8 ++- test/Shared/ApiConsistencyTestBase.cs | 57 ++++--------------- 7 files changed, 62 insertions(+), 81 deletions(-) diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/project.json b/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/project.json index 2bc86cf38d..07a0372748 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/project.json +++ b/test/Microsoft.AspNet.Identity.EntityFramework.InMemory.Test/project.json @@ -14,12 +14,17 @@ "Microsoft.Extensions.OptionsModel": "1.0.0-*", "xunit.runner.aspnet": "2.0.0-aspnet-*" }, - "compile": "..\\Shared\\*.cs", + "compile": "../Shared/*.cs", "frameworks": { "dnx451": { "dependencies": { "Moq": "4.2.1312.1622" } + }, + "dnxcore50": { + "dependencies": { + "moq.netcore": "4.4.0-beta8" + } } }, "commands": { diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.Test/project.json b/test/Microsoft.AspNet.Identity.EntityFramework.Test/project.json index 4571112ab5..c036f91d37 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.Test/project.json +++ b/test/Microsoft.AspNet.Identity.EntityFramework.Test/project.json @@ -16,12 +16,17 @@ "Microsoft.Extensions.OptionsModel": "1.0.0-*", "xunit.runner.aspnet": "2.0.0-aspnet-*" }, - "compile": "..\\Shared\\*.cs", + "compile": "../Shared/*.cs", "frameworks": { "dnx451": { "dependencies": { "Moq": "4.2.1312.1622" } + }, + "dnxcore50": { + "dependencies": { + "moq.netcore": "4.4.0-beta8" + } } }, "commands": { diff --git a/test/Microsoft.AspNet.Identity.InMemory.Test/FunctionalTest.cs b/test/Microsoft.AspNet.Identity.InMemory.Test/FunctionalTest.cs index c462be82df..e0fd3b6068 100644 --- a/test/Microsoft.AspNet.Identity.InMemory.Test/FunctionalTest.cs +++ b/test/Microsoft.AspNet.Identity.InMemory.Test/FunctionalTest.cs @@ -15,11 +15,10 @@ using Microsoft.AspNet.Builder; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Authentication; using Microsoft.AspNet.Http.Features.Authentication; +using Microsoft.AspNet.Identity.Test; using Microsoft.AspNet.TestHost; using Microsoft.Extensions.DependencyInjection; -using Shouldly; using Xunit; -using Microsoft.AspNet.Identity.Test; namespace Microsoft.AspNet.Identity.InMemory { @@ -45,7 +44,9 @@ namespace Microsoft.AspNet.Identity.InMemory })); var transaction1 = await SendAsync(server, "http://example.com/createSimple"); - transaction1.Response.StatusCode.ShouldBe(HttpStatusCode.OK); + + // Assert + Assert.Equal(HttpStatusCode.OK, transaction1.Response.StatusCode); Assert.Null(transaction1.SetCookie); } @@ -61,28 +62,28 @@ namespace Microsoft.AspNet.Identity.InMemory })); var transaction1 = await SendAsync(server, "http://example.com/createMe"); - transaction1.Response.StatusCode.ShouldBe(HttpStatusCode.OK); + Assert.Equal(HttpStatusCode.OK, transaction1.Response.StatusCode); Assert.Null(transaction1.SetCookie); var transaction2 = await SendAsync(server, "http://example.com/pwdLogin/false"); - transaction2.Response.StatusCode.ShouldBe(HttpStatusCode.OK); + Assert.Equal(HttpStatusCode.OK, transaction2.Response.StatusCode); Assert.NotNull(transaction2.SetCookie); - transaction2.SetCookie.ShouldNotContain("; expires="); + Assert.DoesNotContain("; expires=", transaction2.SetCookie); var transaction3 = await SendAsync(server, "http://example.com/me", transaction2.CookieNameValue); - FindClaimValue(transaction3, ClaimTypes.Name).ShouldBe("hao"); + Assert.Equal("hao", FindClaimValue(transaction3, ClaimTypes.Name)); Assert.Null(transaction3.SetCookie); clock.Add(TimeSpan.FromMinutes(7)); var transaction4 = await SendAsync(server, "http://example.com/me", transaction2.CookieNameValue); - FindClaimValue(transaction4, ClaimTypes.Name).ShouldBe("hao"); + Assert.Equal("hao", FindClaimValue(transaction4, ClaimTypes.Name)); Assert.Null(transaction4.SetCookie); clock.Add(TimeSpan.FromMinutes(7)); var transaction5 = await SendAsync(server, "http://example.com/me", transaction2.CookieNameValue); - FindClaimValue(transaction5, ClaimTypes.Name).ShouldBe(null); + Assert.Null(FindClaimValue(transaction5, ClaimTypes.Name)); Assert.Null(transaction5.SetCookie); } @@ -98,29 +99,29 @@ namespace Microsoft.AspNet.Identity.InMemory })); var transaction1 = await SendAsync(server, "http://example.com/createMe"); - transaction1.Response.StatusCode.ShouldBe(HttpStatusCode.OK); + Assert.Equal(HttpStatusCode.OK, transaction1.Response.StatusCode); Assert.Null(transaction1.SetCookie); var transaction2 = await SendAsync(server, "http://example.com/pwdLogin/" + rememberMe); - transaction2.Response.StatusCode.ShouldBe(HttpStatusCode.OK); + Assert.Equal(HttpStatusCode.OK, transaction2.Response.StatusCode); Assert.NotNull(transaction2.SetCookie); if (rememberMe) { - transaction2.SetCookie.ShouldContain("; expires="); + Assert.Contains("; expires=", transaction2.SetCookie); } else { - transaction2.SetCookie.ShouldNotContain("; expires="); + Assert.DoesNotContain("; expires=", transaction2.SetCookie); } var transaction3 = await SendAsync(server, "http://example.com/me", transaction2.CookieNameValue); - FindClaimValue(transaction3, ClaimTypes.Name).ShouldBe("hao"); + Assert.Equal("hao", FindClaimValue(transaction3, ClaimTypes.Name)); Assert.Null(transaction3.SetCookie); // Make sure we don't get a new cookie yet clock.Add(TimeSpan.FromMinutes(10)); var transaction4 = await SendAsync(server, "http://example.com/me", transaction2.CookieNameValue); - FindClaimValue(transaction4, ClaimTypes.Name).ShouldBe("hao"); + Assert.Equal("hao", FindClaimValue(transaction4, ClaimTypes.Name)); Assert.Null(transaction4.SetCookie); // Go past SecurityStampValidation interval and ensure we get a new cookie @@ -128,11 +129,11 @@ namespace Microsoft.AspNet.Identity.InMemory var transaction5 = await SendAsync(server, "http://example.com/me", transaction2.CookieNameValue); Assert.NotNull(transaction5.SetCookie); - FindClaimValue(transaction5, ClaimTypes.Name).ShouldBe("hao"); + Assert.Equal("hao", FindClaimValue(transaction5, ClaimTypes.Name)); // Make sure new cookie is valid var transaction6 = await SendAsync(server, "http://example.com/me", transaction5.CookieNameValue); - FindClaimValue(transaction6, ClaimTypes.Name).ShouldBe("hao"); + Assert.Equal("hao", FindClaimValue(transaction6, ClaimTypes.Name)); } [Fact] @@ -141,18 +142,18 @@ namespace Microsoft.AspNet.Identity.InMemory var server = CreateServer(); var transaction1 = await SendAsync(server, "http://example.com/createMe"); - transaction1.Response.StatusCode.ShouldBe(HttpStatusCode.OK); + Assert.Equal(HttpStatusCode.OK, transaction1.Response.StatusCode); Assert.Null(transaction1.SetCookie); var transaction2 = await SendAsync(server, "http://example.com/twofactorRememeber"); - transaction2.Response.StatusCode.ShouldBe(HttpStatusCode.OK); + Assert.Equal(HttpStatusCode.OK, transaction2.Response.StatusCode); var setCookie = transaction2.SetCookie; - setCookie.ShouldContain(new IdentityCookieOptions().TwoFactorRememberMeCookieAuthenticationScheme + "="); - setCookie.ShouldContain("; expires="); + Assert.Contains(new IdentityCookieOptions().TwoFactorRememberMeCookieAuthenticationScheme + "=", setCookie); + Assert.Contains("; expires=", setCookie); var transaction3 = await SendAsync(server, "http://example.com/isTwoFactorRememebered", transaction2.CookieNameValue); - transaction3.Response.StatusCode.ShouldBe(HttpStatusCode.OK); + Assert.Equal(HttpStatusCode.OK, transaction3.Response.StatusCode); } private static string FindClaimValue(Transaction transaction, string claimType) @@ -278,7 +279,7 @@ namespace Microsoft.AspNet.Identity.InMemory } using (var memory = new MemoryStream()) { - using (var writer = new XmlTextWriter(memory, Encoding.UTF8)) + using (var writer = XmlWriter.Create(memory, new XmlWriterSettings { Encoding = Encoding.UTF8 })) { xml.WriteTo(writer); } diff --git a/test/Microsoft.AspNet.Identity.InMemory.Test/project.json b/test/Microsoft.AspNet.Identity.InMemory.Test/project.json index db656de09a..cb0462ef8c 100644 --- a/test/Microsoft.AspNet.Identity.InMemory.Test/project.json +++ b/test/Microsoft.AspNet.Identity.InMemory.Test/project.json @@ -14,14 +14,18 @@ "Microsoft.Extensions.Configuration": "1.0.0-*", "Microsoft.Extensions.DependencyInjection": "1.0.0-*", "Microsoft.Extensions.OptionsModel": "1.0.0-*", - "Moq": "4.2.1312.1622", "xunit.runner.aspnet": "2.0.0-aspnet-*" }, - "compile": "..\\Shared\\*.cs", + "compile": "../Shared/*.cs", "frameworks": { "dnx451": { "dependencies": { - "Shouldly": "1.1.1.1" + "Moq": "4.2.1312.1622" + } + }, + "dnxcore50": { + "dependencies": { + "moq.netcore": "4.4.0-beta8" } } }, diff --git a/test/Microsoft.AspNet.Identity.Test/ApiConsistencyTest.cs b/test/Microsoft.AspNet.Identity.Test/ApiConsistencyTest.cs index 0608d8c090..85c4d292db 100644 --- a/test/Microsoft.AspNet.Identity.Test/ApiConsistencyTest.cs +++ b/test/Microsoft.AspNet.Identity.Test/ApiConsistencyTest.cs @@ -1,17 +1,12 @@ // 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.Linq; using System.Reflection; -using Xunit; namespace Microsoft.AspNet.Identity.Test { public class ApiConsistencyTest : ApiConsistencyTestBase { - protected override Assembly TargetAssembly - { - get { return typeof(IdentityOptions).Assembly; } - } + protected override Assembly TargetAssembly => typeof(IdentityOptions).GetTypeInfo().Assembly; } } diff --git a/test/Microsoft.AspNet.Identity.Test/project.json b/test/Microsoft.AspNet.Identity.Test/project.json index 1f18d71501..64ec1b6aa3 100644 --- a/test/Microsoft.AspNet.Identity.Test/project.json +++ b/test/Microsoft.AspNet.Identity.Test/project.json @@ -13,12 +13,18 @@ "Microsoft.Extensions.OptionsModel": "1.0.0-*", "xunit.runner.aspnet": "2.0.0-aspnet-*" }, - "compile": "..\\Shared\\*.cs", + "compile": "../Shared/*.cs", "frameworks": { "dnx451": { "dependencies": { "Moq": "4.2.1312.1622" } + }, + "dnxcore50": { + "dependencies": { + "moq.netcore": "4.4.0-beta8", + "System.Xml.ReaderWriter": "4.0.10-*" + } } }, "commands": { diff --git a/test/Shared/ApiConsistencyTestBase.cs b/test/Shared/ApiConsistencyTestBase.cs index c8fd341cc5..6d16799bd9 100644 --- a/test/Shared/ApiConsistencyTestBase.cs +++ b/test/Shared/ApiConsistencyTestBase.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; using System.Linq; using System.Reflection; -using System.Threading; using System.Threading.Tasks; using Xunit; @@ -13,23 +12,17 @@ namespace Microsoft.AspNet.Identity.Test { public abstract class ApiConsistencyTestBase { - protected const BindingFlags PublicInstance - = BindingFlags.Instance | BindingFlags.Public; - - protected const BindingFlags AnyInstance - = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; - [Fact] public void Public_inheritable_apis_should_be_virtual() { var nonVirtualMethods - = (from type in GetAllTypes(TargetAssembly.GetTypes()) + = (from type in GetAllTypes(TargetAssembly.DefinedTypes) where type.IsVisible && !type.IsSealed - && type.GetConstructors(AnyInstance).Any(c => c.IsPublic || c.IsFamily || c.IsFamilyOrAssembly) + && type.DeclaredConstructors.Any(c => c.IsPublic || c.IsFamily || c.IsFamilyOrAssembly) && type.Namespace != null && !type.Namespace.EndsWith(".Compiled") - from method in type.GetMethods(PublicInstance) + from method in type.DeclaredMethods.Where(m => m.IsPublic && !m.IsStatic) where GetBasestTypeInAssembly(method.DeclaringType) == type && !(method.IsVirtual && !method.IsFinal) && !method.Name.StartsWith("get_") @@ -43,40 +36,13 @@ namespace Microsoft.AspNet.Identity.Test "\r\n-- Missing virtual APIs --\r\n" + string.Join("\r\n", nonVirtualMethods)); } - //[Fact] - //public void Public_api_arguments_should_have_not_null_annotation() - //{ - // var parametersMissingAttribute - // = (from type in GetAllTypes(TargetAssembly.GetTypes()) - // where type.IsVisible && !typeof(Delegate).IsAssignableFrom(type) - // let interfaceMappings = type.GetInterfaces().Select(type.GetInterfaceMap) - // let events = type.GetEvents() - // from method in type.GetMethods(PublicInstance | BindingFlags.Static) - // .Concat(type.GetConstructors()) - // where GetBasestTypeInAssembly(method.DeclaringType) == type - // where type.IsInterface || !interfaceMappings.Any(im => im.TargetMethods.Contains(method)) - // where !events.Any(e => e.AddMethod == method || e.RemoveMethod == method) - // from parameter in method.GetParameters() - // where !parameter.ParameterType.IsValueType - // && !parameter.GetCustomAttributes() - // .Any( - // a => a.GetType().Name == "NotNullAttribute" - // || a.GetType().Name == "CanBeNullAttribute") - // select type.Name + "." + method.Name + "[" + parameter.Name + "]") - // .ToList(); - - // Assert.False( - // parametersMissingAttribute.Any(), - // "\r\n-- Missing NotNull annotations --\r\n" + string.Join("\r\n", parametersMissingAttribute)); - //} - [Fact] public void Async_methods_should_end_with_async_suffix() { var asyncMethods - = (from type in GetAllTypes(TargetAssembly.GetTypes()) + = (from type in GetAllTypes(TargetAssembly.DefinedTypes) where type.IsVisible - from method in type.GetMethods(PublicInstance/* | BindingFlags.Static*/) + from method in type.DeclaredMethods.Where(m => m.IsPublic) where GetBasestTypeInAssembly(method.DeclaringType) == type where typeof(Task).IsAssignableFrom(method.ReturnType) select method).ToList(); @@ -105,28 +71,27 @@ namespace Microsoft.AspNet.Identity.Test protected abstract Assembly TargetAssembly { get; } - protected virtual IEnumerable GetAllTypes(IEnumerable types) + protected virtual IEnumerable GetAllTypes(IEnumerable types) { foreach (var type in types) { yield return type; - foreach (var nestedType in GetAllTypes(type.GetNestedTypes())) + foreach (var nestedType in GetAllTypes(type.DeclaredNestedTypes)) { yield return nestedType; } } } - protected Type GetBasestTypeInAssembly(Type type) + protected TypeInfo GetBasestTypeInAssembly(Type type) { - while (type.BaseType != null - && type.BaseType.Assembly == type.Assembly) + while (type.GetTypeInfo()?.BaseType?.GetTypeInfo()?.Assembly == type.GetTypeInfo().Assembly) { - type = type.BaseType; + type = type.GetTypeInfo().BaseType; } - return type; + return type.GetTypeInfo(); } } }