Example #1
0
            public TestableCuratedFeedsController()
            {
                Fakes = new Fakes();

                StubCuratedFeed = new CuratedFeed
                {
                    Key = 0, Name = "aName", Managers = new HashSet <User>(new[] { Fakes.User })
                };
                StubCuratedFeedService = new Mock <ICuratedFeedService>();

                SetOwinContextOverride(Fakes.CreateOwinContext());

                StubCuratedFeedService
                .Setup(stub => stub.GetFeedByName(It.IsAny <string>(), It.IsAny <bool>()))
                .Returns(StubCuratedFeed);

                CuratedFeedService = StubCuratedFeedService.Object;

                StubSearchService = new Mock <ISearchService>();
                SearchService     = StubSearchService.Object;

                var httpContext = new Mock <HttpContextBase>();

                TestUtility.SetupHttpContextMockForUrlGeneration(httpContext, this);

                this.SetCurrentUser(Fakes.User);
            }
        public TestableApiController(MockBehavior behavior = MockBehavior.Default)
        {
            OwinContext               = Fakes.CreateOwinContext();
            EntitiesContext           = (MockEntitiesContext = new Mock <IEntitiesContext>()).Object;
            PackageService            = (MockPackageService = new Mock <IPackageService>(behavior)).Object;
            UserService               = (MockUserService = new Mock <IUserService>(behavior)).Object;
            NugetExeDownloaderService = (MockNuGetExeDownloaderService = new Mock <INuGetExeDownloaderService>(MockBehavior.Strict)).Object;
            ContentService            = (MockContentService = new Mock <IContentService>()).Object;
            StatisticsService         = (MockStatisticsService = new Mock <IStatisticsService>()).Object;
            IndexingService           = (MockIndexingService = new Mock <IIndexingService>()).Object;
            AutoCuratePackage         = (MockAutoCuratePackage = new Mock <IAutomaticallyCuratePackageCommand>()).Object;

            MockPackageFileService = new Mock <IPackageFileService>(MockBehavior.Strict);
            MockPackageFileService.Setup(p => p.SavePackageFileAsync(It.IsAny <Package>(), It.IsAny <Stream>()))
            .Returns(Task.CompletedTask);
            PackageFileService = MockPackageFileService.Object;

            MessageService = (MockMessageService = new Mock <IMessageService>()).Object;

            MockConfigurationService = new Mock <IGalleryConfigurationService>();
            MockConfigurationService.SetupGet(s => s.Features.TrackPackageDownloadCountInLocalDatabase)
            .Returns(false);
            ConfigurationService = MockConfigurationService.Object;

            AuditingService = new TestAuditingService();

            TestUtility.SetupHttpContextMockForUrlGeneration(new Mock <HttpContextBase>(), this);
        }
Example #3
0
            public void GivenAUser_ItCreatesAnOwinAuthenticationTicketForTheUser()
            {
                // Arrange
                var service = Get <AuthenticationService>();
                var context = Fakes.CreateOwinContext();

                var passwordCred = Fakes.Admin.Credentials.SingleOrDefault(
                    c => String.Equals(c.Type, CredentialTypes.Password.Pbkdf2, StringComparison.OrdinalIgnoreCase));

                var authUser = new AuthenticatedUser(Fakes.Admin, passwordCred);

                // Act
                service.CreateSession(context, authUser.User, AuthenticationTypes.Password);

                // Assert
                var principal = context.Authentication.AuthenticationResponseGrant.Principal;
                var id        = principal.Identity;

                Assert.NotNull(principal);
                Assert.NotNull(id);
                Assert.Equal(Fakes.Admin.Username, id.Name);
                Assert.Equal(Fakes.Admin.Username, principal.GetClaimOrDefault(ClaimTypes.NameIdentifier));
                Assert.Equal(AuthenticationTypes.Password, id.AuthenticationType);
                Assert.True(principal.IsInRole(Constants.AdminRoleName));
            }
            public TestableCuratedPackagesController()
            {
                Fakes = new Fakes();

                StubCuratedFeed = new CuratedFeed
                {
                    Key = 0, Name = "aFeedName", Managers = new HashSet <User>(new[] { Fakes.User })
                };
                StubPackageRegistration = new PackageRegistration {
                    Key = 0, Id = "anId"
                };

                SetOwinContextOverride(Fakes.CreateOwinContext());

                EntitiesContext = new FakeEntitiesContext();
                EntitiesContext.CuratedFeeds.Add(StubCuratedFeed);
                EntitiesContext.PackageRegistrations.Add(StubPackageRegistration);

                var curatedFeedRepository = new EntityRepository <CuratedFeed>(
                    EntitiesContext);

                var curatedPackageRepository = new EntityRepository <CuratedPackage>(
                    EntitiesContext);

                base.CuratedFeedService = new CuratedFeedService(
                    curatedFeedRepository,
                    curatedPackageRepository);

                var httpContext = new Mock <HttpContextBase>();

                TestUtility.SetupHttpContextMockForUrlGeneration(httpContext, this);
            }
Example #5
0
            public TestableCuratedPackagesController()
            {
                StubCuratedFeed = new CuratedFeed
                {
                    Key = 0, Name = "aFeedName", Managers = new HashSet <User>(new[] { Fakes.User })
                };
                StubPackageRegistration = new PackageRegistration {
                    Key = 0, Id = "anId"
                };
                StubPackage = new Package
                {
                    Key = 34,
                    PackageRegistration    = StubPackageRegistration,
                    PackageRegistrationKey = StubPackageRegistration.Key,
                    Version = "1.0.0"
                };
                StubLatestPackage = new Package
                {
                    Key = 42,
                    PackageRegistration    = StubPackageRegistration,
                    PackageRegistrationKey = StubPackageRegistration.Key,
                    Version      = "2.0.1-alpha",
                    IsLatest     = true,
                    IsPrerelease = true
                };
                StubLatestStablePackage = new Package
                {
                    Key = 41,
                    PackageRegistration    = StubPackageRegistration,
                    PackageRegistrationKey = StubPackageRegistration.Key,
                    Version        = "2.0.0",
                    IsLatestStable = true
                };

                OwinContext = Fakes.CreateOwinContext();

                EntitiesContext = new FakeEntitiesContext();
                EntitiesContext.CuratedFeeds.Add(StubCuratedFeed);
                EntitiesContext.PackageRegistrations.Add(StubPackageRegistration);

                StubPackageRegistration.Packages.Add(StubPackage);
                StubPackageRegistration.Packages.Add(StubLatestPackage);
                StubPackageRegistration.Packages.Add(StubLatestStablePackage);

                var curatedFeedRepository = new EntityRepository <CuratedFeed>(
                    EntitiesContext);

                var curatedPackageRepository = new EntityRepository <CuratedPackage>(
                    EntitiesContext);

                base.CuratedFeedService = new CuratedFeedService(
                    curatedFeedRepository,
                    curatedPackageRepository);

                var httpContext = new Mock <HttpContextBase>();

                TestUtility.SetupHttpContextMockForUrlGeneration(httpContext, this);
            }
Example #6
0
        static SupportRequestController CreateController(ISupportRequestService supportRequestService, IUserService userService, User admin)
        {
            var controller = new Mock <SupportRequestController>(supportRequestService, userService);

            controller.CallBase = true;
            controller.Object.SetOwinContextOverride(Fakes.CreateOwinContext());
            TestUtility.SetupHttpContextMockForUrlGeneration(new Mock <HttpContextBase>(), controller.Object);
            controller.Object.SetCurrentUser(admin);
            return(controller.Object);
        }
Example #7
0
            public void GivenNoActiveUserPrincipal_ItReturnsNull()
            {
                // Arrange
                var ctrl = new TestableAppController();

                ctrl.SetOwinContextOverride(Fakes.CreateOwinContext());

                // Act
                var user = ctrl.InvokeGetCurrentUser();

                // Assert
                Assert.Null(user);
            }
            public static async Task <TestableApiKeyAuthenticationHandler> CreateAsync(ApiKeyAuthenticationOptions options)
            {
                // Always use passive mode for tests
                options.AuthenticationMode = AuthenticationMode.Passive;

                var handler = new TestableApiKeyAuthenticationHandler();

                var ctxt = Fakes.CreateOwinContext();

                await handler.InitializeAsync(options, ctxt);

                return(handler);
            }
Example #9
0
        public async Task GivenNoForceSslCookieAndNonSslRequest_ItPassesThrough()
        {
            // Arrange
            var context = Fakes.CreateOwinContext();
            var next    = Fakes.CreateOwinMiddleware();
            var app     = new AppBuilder();

            context.Request
            .SetUrl("http://nuget.local/foo/bar/baz?qux=qooz");
            var middleware = new ForceSslWhenAuthenticatedMiddleware(next.Object, app, "ForceSSL", 443);

            // Act
            await middleware.Invoke(context);

            // Assert
            next.Verify(n => n.Invoke(It.IsAny <IOwinContext>()));
        }
Example #10
0
        public TestableApiController(MockBehavior behavior = MockBehavior.Default)
        {
            OwinContext               = Fakes.CreateOwinContext();
            EntitiesContext           = (MockEntitiesContext = new Mock <IEntitiesContext>()).Object;
            PackageService            = (MockPackageService = new Mock <IPackageService>(behavior)).Object;
            UserService               = (MockUserService = new Mock <IUserService>(behavior)).Object;
            NugetExeDownloaderService = (MockNuGetExeDownloaderService = new Mock <INuGetExeDownloaderService>(MockBehavior.Strict)).Object;
            ContentService            = (MockContentService = new Mock <IContentService>()).Object;
            StatisticsService         = (MockStatisticsService = new Mock <IStatisticsService>()).Object;
            IndexingService           = (MockIndexingService = new Mock <IIndexingService>()).Object;

            MockPackageFileService = new Mock <IPackageFileService>(MockBehavior.Strict);
            MockPackageFileService.Setup(p => p.SavePackageFileAsync(It.IsAny <Package>(), It.IsAny <Stream>())).Returns(Task.FromResult(0));
            PackageFileService = MockPackageFileService.Object;

            TestUtility.SetupHttpContextMockForUrlGeneration(new Mock <HttpContextBase>(), this);
        }
Example #11
0
        public async Task GivenANonStandardSslPort_ItSpecifiesPortInUrl()
        {
            // Arrange
            var context = Fakes.CreateOwinContext();
            var next    = Fakes.CreateOwinMiddleware();
            var app     = new AppBuilder();

            context.Request
            .SetUrl("http://nuget.local/foo/bar/baz?qux=qooz")
            .SetCookie("ForceSSL", "bogus");
            var middleware = new ForceSslWhenAuthenticatedMiddleware(next.Object, app, "ForceSSL", 44300);

            // Act
            await middleware.Invoke(context);

            // Assert
            next.Verify(n => n.Invoke(It.IsAny <IOwinContext>()), Times.Never());
            OwinAssert.WillRedirect(context, "https://nuget.local:44300/foo/bar/baz?qux=qooz");
        }
Example #12
0
            public static async Task <TestableApiKeyAuthenticationHandler> CreateAsync(ApiKeyAuthenticationOptions options)
            {
                // Always use passive mode for tests
                options.AuthenticationMode = AuthenticationMode.Passive;

                var handler = new TestableApiKeyAuthenticationHandler();

                var ctxt = Fakes.CreateOwinContext();

                // Grr, have to make an internal call to initialize...
                await(Task)(typeof(AuthenticationHandler <ApiKeyAuthenticationOptions>)
                            .InvokeMember(
                                "Initialize",
                                BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod,
                                Type.DefaultBinder,
                                handler,
                                new object[] { options, ctxt }));
                return(handler);
            }
            public Facts()
            {
                SearchSideBySideService = new Mock <ISearchSideBySideService>();
                FeatureFlagService      = new Mock <IFeatureFlagService>();
                HttpContext             = new Mock <HttpContextBase>();

                FeatureFlagService.SetReturnsDefault(true);

                SearchTerm = "json";
                ViewModel  = new SearchSideBySideViewModel
                {
                    SearchTerm = SearchTerm,
                };

                Target = new ExperimentsController(
                    SearchSideBySideService.Object,
                    FeatureFlagService.Object);

                TestUtility.SetupHttpContextMockForUrlGeneration(HttpContext, Target);
                Target.SetOwinContextOverride(Fakes.CreateOwinContext());
                Target.SetCurrentUser(TestUtility.FakeUser);
            }
Example #14
0
        public async Task GivenNextMiddlewareRevokesAuth_ItRemovesForceSslCookie()
        {
            // Arrange
            var context = Fakes.CreateOwinContext();
            var next    = Fakes.CreateOwinMiddleware();
            var app     = new AppBuilder();
            var revoke  = new AuthenticationResponseRevoke(new string[0]);

            next.Setup(n => n.Invoke(context))
            .Returns <IOwinContext>(c =>
            {
                c.Authentication.AuthenticationResponseRevoke = revoke;
                return(Task.FromResult <object>(null));
            });
            context.Request
            .SetUrl("http://nuget.local/foo/bar/baz?qux=qooz");
            var middleware = new ForceSslWhenAuthenticatedMiddleware(next.Object, app, "ForceSSL", 443);

            // Act
            await middleware.Invoke(context);

            // Assert
            OwinAssert.DeletesCookie(context.Response, "ForceSSL");
        }
Example #15
0
        public async Task GivenNextMiddlewareGrantsAuth_ItDropsForceSslCookie(string protocol, bool secure)
        {
            // Arrange
            var context = Fakes.CreateOwinContext();
            var next    = Fakes.CreateOwinMiddleware();
            var app     = new AppBuilder();
            var grant   = new AuthenticationResponseGrant(new ClaimsIdentity(), new AuthenticationProperties());

            next.Setup(n => n.Invoke(context))
            .Returns <IOwinContext>(c =>
            {
                c.Authentication.AuthenticationResponseGrant = grant;
                return(Task.FromResult <object>(null));
            });
            context.Request
            .SetUrl(protocol + "://nuget.local/foo/bar/baz?qux=qooz");
            var middleware = new ForceSslWhenAuthenticatedMiddleware(next.Object, app, "ForceSSL", 443);

            // Act
            await middleware.Invoke(context);

            // Assert
            OwinAssert.SetsCookie(context.Response, "ForceSSL", "true", secure);
        }