/tests/NuGetGallery.Facts/Framework/Fakes.cs

https://github.com/ferventcoder/NuGetGallery · C# · 129 lines · 109 code · 15 blank · 5 comment · 0 complexity · ddcf71e133e9df827ef086e1db2bf802 MD5 · raw file

  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Security.Claims;
  9. using System.Security.Principal;
  10. using Microsoft.Owin;
  11. using Microsoft.Owin.Security;
  12. using Moq;
  13. using NuGetGallery.Authentication;
  14. namespace NuGetGallery.Framework
  15. {
  16. public static class Fakes
  17. {
  18. private static readonly MethodInfo SetMethod = typeof(IEntitiesContext).GetMethod("Set");
  19. public static readonly string Password = "p@ssw0rd!";
  20. public static readonly User User = new User("testUser") {
  21. Key = 42,
  22. EmailAddress = "confirmed1@example.com",
  23. Credentials = new List<Credential>() {
  24. CredentialBuilder.CreatePbkdf2Password(Password),
  25. CredentialBuilder.CreateV1ApiKey(Guid.Parse("519e180e-335c-491a-ac26-e83c4bd31d65"))
  26. }
  27. };
  28. public static readonly User ShaUser = new User("testShaUser")
  29. {
  30. Key = 42,
  31. EmailAddress = "confirmed2@example.com",
  32. Credentials = new List<Credential>() {
  33. CredentialBuilder.CreateSha1Password(Password),
  34. CredentialBuilder.CreateV1ApiKey(Guid.Parse("b9704a41-4107-4cd2-bcfa-70d84e021ab2"))
  35. }
  36. };
  37. public static readonly User Admin = new User("testAdmin") {
  38. Key = 43,
  39. EmailAddress = "confirmed3@example.com",
  40. Credentials = new List<Credential>() { CredentialBuilder.CreatePbkdf2Password(Password) },
  41. Roles = new List<Role>() { new Role() { Name = Constants.AdminRoleName } }
  42. };
  43. public static readonly User Owner = new User("testPackageOwner") {
  44. Key = 44,
  45. Credentials = new List<Credential>() { CredentialBuilder.CreatePbkdf2Password(Password) },
  46. EmailAddress = "confirmed@example.com" //package owners need confirmed email addresses, obviously.
  47. };
  48. public static readonly PackageRegistration Package = new PackageRegistration()
  49. {
  50. Id = "FakePackage",
  51. Owners = new List<User>() { Owner },
  52. Packages = new List<Package>() {
  53. new Package() { Version = "1.0" },
  54. new Package() { Version = "2.0" }
  55. }
  56. };
  57. public static User CreateUser(string userName, params Credential[] credentials)
  58. {
  59. return new User(userName)
  60. {
  61. UnconfirmedEmailAddress = "un@confirmed.com",
  62. Credentials = new List<Credential>(credentials)
  63. };
  64. }
  65. public static ClaimsPrincipal ToPrincipal(this User user)
  66. {
  67. ClaimsIdentity identity = new ClaimsIdentity(
  68. claims: Enumerable.Concat(new[] {
  69. new Claim(ClaimsIdentity.DefaultNameClaimType, user.Username),
  70. }, user.Roles.Select(r => new Claim(ClaimsIdentity.DefaultRoleClaimType, r.Name))),
  71. authenticationType: "Test",
  72. nameType: ClaimsIdentity.DefaultNameClaimType,
  73. roleType: ClaimsIdentity.DefaultRoleClaimType);
  74. return new ClaimsPrincipal(identity);
  75. }
  76. public static IIdentity ToIdentity(this User user)
  77. {
  78. return new GenericIdentity(user.Username);
  79. }
  80. internal static void ConfigureEntitiesContext(FakeEntitiesContext ctxt)
  81. {
  82. // Add Users
  83. var users = ctxt.Set<User>();
  84. users.Add(User);
  85. users.Add(ShaUser);
  86. users.Add(Admin);
  87. users.Add(Owner);
  88. // Add Credentials and link to users
  89. var creds = ctxt.Set<Credential>();
  90. foreach (var user in users)
  91. {
  92. foreach (var cred in user.Credentials)
  93. {
  94. cred.User = user;
  95. creds.Add(cred);
  96. }
  97. }
  98. }
  99. public static IOwinContext CreateOwinContext()
  100. {
  101. var ctx = new OwinContext();
  102. ctx.Request.SetUrl("http://nuget.local/");
  103. // Fill in some values that cause exceptions if not present
  104. ctx.Set<Action<Action<object>, object>>("server.OnSendingHeaders", (_, __) => { });
  105. return ctx;
  106. }
  107. public static Mock<OwinMiddleware> CreateOwinMiddleware()
  108. {
  109. var middleware = new Mock<OwinMiddleware>(new object[] { null });
  110. middleware.Setup(m => m.Invoke(It.IsAny<OwinContext>())).Completes();
  111. return middleware;
  112. }
  113. }
  114. }