当前位置: 首页>>代码示例>>C#>>正文


C# NuGetGallery.TestableApiController类代码示例

本文整理汇总了C#中NuGetGallery.TestableApiController的典型用法代码示例。如果您正苦于以下问题:C# TestableApiController类的具体用法?C# TestableApiController怎么用?C# TestableApiController使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


TestableApiController类属于NuGetGallery命名空间,在下文中一共展示了TestableApiController类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreatePackageWillSavePackageFileToFileStorage

            public async Task CreatePackageWillSavePackageFileToFileStorage()
            {
                // Arrange
                var guid = Guid.NewGuid().ToString();
                var user = new User() { EmailAddress = "[email protected]" }; 
                var userService = new Mock<IUserService>();
                var packageRegistration = new PackageRegistration();
                packageRegistration.Owners.Add(user);

                var controller = new TestableApiController();
                controller.MockPackageFileService.Setup(p => p.SavePackageFileAsync(It.IsAny<Package>(), It.IsAny<Stream>())).Returns(Task.FromResult(0)).Verifiable();
                controller.MockUserService.Setup(x => x.FindByApiKey(It.IsAny<Guid>())).Returns(user);
                controller.MockPackageService.Setup(p => p.FindPackageRegistrationById(It.IsAny<string>())).Returns(packageRegistration);

                var nuGetPackage = new Mock<INupkg>();
                nuGetPackage.Setup(x => x.Metadata.Id).Returns("theId");
                nuGetPackage.Setup(x => x.Metadata.Version).Returns(new SemanticVersion("1.0.42"));
                controller.SetupPackageFromInputStream(nuGetPackage);

                // Act
                await controller.CreatePackagePut(guid);

                // Assert
                controller.MockPackageFileService.Verify();
            }
开发者ID:nicholaspei,项目名称:NuGetGallery,代码行数:25,代码来源:ApiControllerFacts.cs

示例2: CreatePackageWillSavePackageFileToFileStorage

            public async Task CreatePackageWillSavePackageFileToFileStorage()
            {
                // Arrange
                var user = new User() { EmailAddress = "[email protected]" };
                var packageRegistration = new PackageRegistration();
                packageRegistration.Owners.Add(user);

                var controller = new TestableApiController();
                controller.SetCurrentUser(user);
                controller.MockPackageFileService.Setup(p => p.SavePackageFileAsync(It.IsAny<Package>(), It.IsAny<Stream>())).Returns(Task.FromResult(0)).Verifiable();
                controller.MockPackageService.Setup(p => p.FindPackageRegistrationById(It.IsAny<string>())).Returns(packageRegistration);

                var nuGetPackage = TestPackage.CreateTestPackageStream("theId", "1.0.42");
                controller.SetupPackageFromInputStream(nuGetPackage);

                // Act
                await controller.CreatePackagePut();

                // Assert
                controller.MockPackageFileService.Verify();
            }
开发者ID:atrevisan,项目名称:NuGetGallery,代码行数:21,代码来源:ApiControllerFacts.cs

示例3: VerifyStatsDownloadsReturnsNotFoundWhenStatsNotAvailable

            public async Task VerifyStatsDownloadsReturnsNotFoundWhenStatsNotAvailable()
            {
                var controller = new TestableApiController();
                controller.MockStatisticsService.Setup(x => x.LoadDownloadPackageVersions()).Returns(Task.FromResult(StatisticsReportResult.Failed));

                TestUtility.SetupUrlHelperForUrlGeneration(controller, new Uri("http://nuget.org"));

                ActionResult actionResult = await controller.GetStatsDownloads(null);

                HttpStatusCodeResult httpStatusResult = (HttpStatusCodeResult)actionResult;

                Assert.True(httpStatusResult.StatusCode == (int)HttpStatusCode.NotFound, "unexpected StatusCode");
            }
开发者ID:segilbert,项目名称:NuGetGallery,代码行数:13,代码来源:ApiControllerFacts.cs

示例4: VerifyPackageKeyReturns200IfUserIsAnOwner

            public void VerifyPackageKeyReturns200IfUserIsAnOwner()
            {
                // Arrange
                var apiKey = Guid.NewGuid();
                var user = new User();
                var package = new Package { PackageRegistration = new PackageRegistration() };
                package.PackageRegistration.Owners.Add(user);
                var controller = new TestableApiController();
                controller.SetCurrentUser(user);
                controller.MockPackageService.Setup(s => s.FindPackageByIdAndVersion("foo", "1.0.0", true)).Returns(package);

                // Act
                var result = controller.VerifyPackageKey("foo", "1.0.0");

                // Assert
                ResultAssert.IsEmpty(result);
            }
开发者ID:segilbert,项目名称:NuGetGallery,代码行数:17,代码来源:ApiControllerFacts.cs

示例5: VerifyPackageKeyReturnsEmptyResultIfApiKeyExistsButIdAndVersionAreEmpty

            public void VerifyPackageKeyReturnsEmptyResultIfApiKeyExistsButIdAndVersionAreEmpty()
            {
                // Arrange
                var controller = new TestableApiController();
                controller.SetCurrentUser(new User());
                
                // Act
                var result = controller.VerifyPackageKey(null, null);

                // Assert
                ResultAssert.IsEmpty(result);
            }
开发者ID:segilbert,项目名称:NuGetGallery,代码行数:12,代码来源:ApiControllerFacts.cs

示例6: WillNotListThePackageIfApiKeyDoesNotBelongToAnOwner

            public void WillNotListThePackageIfApiKeyDoesNotBelongToAnOwner()
            {
                // Arrange
                var owner = new User { Key = 1 };
                var package = new Package
                    {
                        PackageRegistration = new PackageRegistration { Owners = new[] { new User() } }
                    };
                
                var controller = new TestableApiController();
                controller.MockPackageService.Setup(x => x.FindPackageByIdAndVersion("theId", "1.0.42", true)).Returns(package);
                controller.SetCurrentUser(owner);
                
                // Act
                var result = controller.PublishPackage("theId", "1.0.42");

                // Assert
                ResultAssert.IsStatusCode(
                    result,
                    HttpStatusCode.Forbidden,
                    String.Format(Strings.ApiKeyNotAuthorized, "publish"));

                controller.MockPackageService.Verify(x => x.MarkPackageListed(package, It.IsAny<bool>()), Times.Never());
            }
开发者ID:segilbert,项目名称:NuGetGallery,代码行数:24,代码来源:ApiControllerFacts.cs

示例7: GetPackageReturns503IfNoVersionIsProvidedAndDatabaseUnavailable

            public async Task GetPackageReturns503IfNoVersionIsProvidedAndDatabaseUnavailable()
            {
                // Arrange
                var package = new Package();
                var actionResult = new EmptyResult();
                var controller = new TestableApiController(MockBehavior.Strict);
                controller.MockPackageService.Setup(x => x.FindPackageByIdAndVersion("Baz", "", false)).Throws(new DataException("Oh noes, database broked!"));

                NameValueCollection headers = new NameValueCollection();
                headers.Add("NuGet-Operation", "Install");

                var httpRequest = new Mock<HttpRequestBase>(MockBehavior.Strict);
                httpRequest.SetupGet(r => r.UserHostAddress).Returns("Foo");
                httpRequest.SetupGet(r => r.UserAgent).Returns("Qux");
                httpRequest.SetupGet(r => r.Headers).Returns(headers);
                httpRequest.SetupGet(r => r.Url).Returns(HttpRequestUrl);
                var httpContext = new Mock<HttpContextBase>(MockBehavior.Strict);
                httpContext.SetupGet(c => c.Request).Returns(httpRequest.Object);

                var controllerContext = new ControllerContext(new RequestContext(httpContext.Object, new RouteData()), controller);
                controller.ControllerContext = controllerContext;

                // Act
                var result = await controller.GetPackage("Baz", "");

                // Assert
                ResultAssert.IsStatusCode(result, HttpStatusCode.ServiceUnavailable, Strings.DatabaseUnavailable_TrySpecificVersion);
                controller.MockPackageFileService.Verify();
                controller.MockPackageService.Verify();
                controller.MockUserService.Verify();
            }
开发者ID:segilbert,项目名称:NuGetGallery,代码行数:31,代码来源:ApiControllerFacts.cs

示例8: GetPackageReturnsSpecificPackageEvenIfDatabaseIsOffline

            public async Task GetPackageReturnsSpecificPackageEvenIfDatabaseIsOffline()
            {
                // Arrange
                var package = new Package();
                var actionResult = new EmptyResult();

                var controller = new TestableApiController(MockBehavior.Strict);
                controller.MockPackageService.Setup(x => x.FindPackageByIdAndVersion("Baz", "1.0.0", false)).Throws(new DataException("Can't find the database")).Verifiable();
                controller.MockPackageFileService.Setup(s => s.CreateDownloadPackageActionResultAsync(HttpRequestUrl, "Baz", "1.0.0"))
                              .Returns(Task.FromResult<ActionResult>(actionResult))
                              .Verifiable();

                NameValueCollection headers = new NameValueCollection();
                headers.Add("NuGet-Operation", "Install");

                var httpRequest = new Mock<HttpRequestBase>(MockBehavior.Strict);
                httpRequest.SetupGet(r => r.UserHostAddress).Returns("Foo");
                httpRequest.SetupGet(r => r.UserAgent).Returns("Qux");
                httpRequest.SetupGet(r => r.Headers).Returns(headers);
                httpRequest.SetupGet(r => r.Url).Returns(HttpRequestUrl);
                var httpContext = new Mock<HttpContextBase>(MockBehavior.Strict);
                httpContext.SetupGet(c => c.Request).Returns(httpRequest.Object);

                var controllerContext = new ControllerContext(new RequestContext(httpContext.Object, new RouteData()), controller);
                controller.ControllerContext = controllerContext;

                // Act
                var result = await controller.GetPackage("Baz", "1.0.0");

                // Assert
                Assert.Same(actionResult, result);
                controller.MockPackageFileService.Verify();
                controller.MockPackageService.Verify();
            }
开发者ID:segilbert,项目名称:NuGetGallery,代码行数:34,代码来源:ApiControllerFacts.cs

示例9: VerifyPackageKeyReturns200IfUserHasCredentialRecordAndIsAnOwner

            public void VerifyPackageKeyReturns200IfUserHasCredentialRecordAndIsAnOwner()
            {
                // Arrange
                var guid = Guid.NewGuid();
                var user = new User();
                var package = new Package { PackageRegistration = new PackageRegistration() };
                package.PackageRegistration.Owners.Add(user);
                var controller = new TestableApiController();
                controller.MockUserService
                    .Setup(s => s.AuthenticateCredential(
                        CredentialTypes.ApiKeyV1,
                        guid.ToString().ToLowerInvariant()))
                    .Returns(new Credential() { User = user });
                controller.MockUserService.Setup(s => s.FindByApiKey(guid)).ReturnsNull();
                controller.MockPackageService.Setup(s => s.FindPackageByIdAndVersion("foo", "1.0.0", true)).Returns(package);

                // Act
                var result = controller.VerifyPackageKey(guid.ToString(), "foo", "1.0.0");

                // Assert
                ResultAssert.IsEmpty(result);
            }
开发者ID:nicholaspei,项目名称:NuGetGallery,代码行数:22,代码来源:ApiControllerFacts.cs

示例10: WillFindTheUserThatMatchesTheApiKey

            public void WillFindTheUserThatMatchesTheApiKey()
            {
                var nuGetPackage = new Mock<INupkg>();
                nuGetPackage.Setup(x => x.Metadata.Id).Returns("theId");
                nuGetPackage.Setup(x => x.Metadata.Version).Returns(new SemanticVersion("1.0.42"));

                var controller = new TestableApiController();
                controller.MockUserService.Setup(x => x.FindByApiKey(It.IsAny<Guid>())).Returns(new User());
                controller.SetupPackageFromInputStream(nuGetPackage);

                var apiKey = Guid.NewGuid();

                controller.CreatePackagePut(apiKey.ToString());

                controller.MockUserService.Verify(x => x.FindByApiKey(apiKey));
            }
开发者ID:nicholaspei,项目名称:NuGetGallery,代码行数:16,代码来源:ApiControllerFacts.cs

示例11: VerifyPackageKeyReturns403IfUserInCredentialsTableIsNotAnOwner

            public void VerifyPackageKeyReturns403IfUserInCredentialsTableIsNotAnOwner()
            {
                // Arrange
                var guid = Guid.NewGuid();
                var controller = new TestableApiController();
                var owner = new User();
                var nonOwner = new User();
                controller.MockUserService.Setup(s => s.FindByApiKey(guid)).Returns(owner);
                controller.MockUserService
                    .Setup(s => s.AuthenticateCredential(
                        CredentialTypes.ApiKeyV1,
                        guid.ToString().ToLowerInvariant()))
                    .Returns(new Credential() { User = nonOwner });
                controller.MockPackageService.Setup(s => s.FindPackageByIdAndVersion("foo", "1.0.0", true)).Returns(
                    new Package { PackageRegistration = new PackageRegistration() });

                // Act
                var result = controller.VerifyPackageKey(guid.ToString(), "foo", "1.0.0");

                // Assert
                ResultAssert.IsStatusCode(
                    result,
                    HttpStatusCode.Forbidden, 
                    "The specified API key does not provide the authority to push packages.");
            }
开发者ID:nicholaspei,项目名称:NuGetGallery,代码行数:25,代码来源:ApiControllerFacts.cs

示例12: VerifyPackageKeyReturnsEmptyResultIfApiKeyExistsInCredentialsAndIdAndVersionAreEmpty

            public void VerifyPackageKeyReturnsEmptyResultIfApiKeyExistsInCredentialsAndIdAndVersionAreEmpty()
            {
                // Arrange
                var guid = Guid.NewGuid();
                var controller = new TestableApiController();
                controller.MockUserService
                    .Setup(s => s.AuthenticateCredential(
                        CredentialTypes.ApiKeyV1,
                        guid.ToString().ToLowerInvariant()))
                    .Returns(new Credential() { User = new User() });

                // Act
                var result = controller.VerifyPackageKey(guid.ToString(), null, null);

                // Assert
                ResultAssert.IsEmpty(result);
            }
开发者ID:nicholaspei,项目名称:NuGetGallery,代码行数:17,代码来源:ApiControllerFacts.cs

示例13: VerifyPackageKeyReturns403IfUserDoesNotExistByFindByApiKeyOrAuthorizeCredential

            public void VerifyPackageKeyReturns403IfUserDoesNotExistByFindByApiKeyOrAuthorizeCredential()
            {
                // Arrange
                var guid = Guid.NewGuid();
                var controller = new TestableApiController();
                controller.MockUserService.Setup(s => s.FindByApiKey(guid)).Returns<User>(null);
                controller.MockUserService
                    .Setup(s => s.AuthenticateCredential(
                        CredentialTypes.ApiKeyV1,
                        guid.ToString().ToLowerInvariant()))
                    .ReturnsNull();
                controller.MockPackageService
                    .Setup(p => p.FindPackageByIdAndVersion("foo", "1.0.0", true))
                    .Returns(new Package());

                // Act
                var result = controller.VerifyPackageKey(guid.ToString(), "foo", "1.0.0");

                // Assert
                ResultAssert.IsStatusCode(
                    result,
                    HttpStatusCode.Forbidden, 
                    "The specified API key does not provide the authority to push packages.");
            }
开发者ID:nicholaspei,项目名称:NuGetGallery,代码行数:24,代码来源:ApiControllerFacts.cs

示例14: WillFailIfUserFromAuthenticateCredentialIsNotOwner

            public void WillFailIfUserFromAuthenticateCredentialIsNotOwner()
            {
                // Arrange
                var apiKey = Guid.NewGuid();
                var nonOwner = new Credential { User = new User { Key = 1 } };
                var owner = new User();
                var package = new Package
                {
                    PackageRegistration = new PackageRegistration { Owners = new[] { new User(), owner } }
                };

                var controller = new TestableApiController();
                controller.MockPackageService.Setup(x => x.FindPackageByIdAndVersion(It.IsAny<string>(), It.IsAny<string>(), true)).Returns(package);
                controller.MockUserService.Setup(x => x.FindByApiKey(apiKey)).Returns(owner);
                controller.MockUserService
                    .Setup(x => x.AuthenticateCredential(
                        CredentialTypes.ApiKeyV1,
                        apiKey.ToString().ToLowerInvariant()))
                    .Returns(nonOwner);

                // Act
                var result = controller.PublishPackage(apiKey.ToString(), "theId", "1.0.42");

                // Assert
                ResultAssert.IsStatusCode(
                    result,
                    HttpStatusCode.Forbidden,
                    String.Format(Strings.ApiKeyNotAuthorized, "publish"));

                controller.MockPackageService.Verify(x => x.MarkPackageListed(package, It.IsAny<bool>()), Times.Never());
            }
开发者ID:nicholaspei,项目名称:NuGetGallery,代码行数:31,代码来源:ApiControllerFacts.cs

示例15: WillListThePackageIfApiKeyBelongsToAnOwnerUsingAuthorizeCredential

            public void WillListThePackageIfApiKeyBelongsToAnOwnerUsingAuthorizeCredential()
            {
                // Arrange

                var apiKey = Guid.NewGuid();
                var owner = new Credential { User = new User { Key = 1 } };
                var package = new Package
                {
                    PackageRegistration = new PackageRegistration { Owners = new[] { new User(), owner.User } }
                };

                var controller = new TestableApiController();
                controller.MockPackageService.Setup(x => x.FindPackageByIdAndVersion(It.IsAny<string>(), It.IsAny<string>(), true)).Returns(package);
                controller.MockUserService
                    .Setup(x => x.AuthenticateCredential(
                        CredentialTypes.ApiKeyV1,
                        apiKey.ToString().ToLowerInvariant()))
                    .Returns(owner);

                // Act
                var result = controller.PublishPackage(apiKey.ToString(), "theId", "1.0.42");

                // Assert
                ResultAssert.IsEmpty(result);
                controller.MockPackageService.Verify(x => x.MarkPackageListed(package, It.IsAny<bool>()));
                controller.MockIndexingService.Verify(i => i.UpdatePackage(package));
            }
开发者ID:nicholaspei,项目名称:NuGetGallery,代码行数:27,代码来源:ApiControllerFacts.cs


注:本文中的NuGetGallery.TestableApiController类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。