|
| 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 | + |
| 4 | +using System.Linq; |
| 5 | +using Moq; |
| 6 | +using NuGetGallery.Configuration; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace NuGetGallery |
| 10 | +{ |
| 11 | + public class CuratedFeedServiceFacts |
| 12 | + { |
| 13 | + public class TheGetFeedByNameMethod : Facts |
| 14 | + { |
| 15 | + [Fact] |
| 16 | + public void ReturnsNullWhenFeedIsDisabled() |
| 17 | + { |
| 18 | + DisableFeed(_curatedFeed.Name); |
| 19 | + |
| 20 | + var output = _target.GetFeedByName(_curatedFeed.Name); |
| 21 | + |
| 22 | + Assert.Null(output); |
| 23 | + _entityRepository.Verify(x => x.GetAll(), Times.Never); |
| 24 | + } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public void ReturnsNullWhenFeedIsDisabledWithDifferentCasing() |
| 28 | + { |
| 29 | + _curatedFeed.Name = "curated-feed"; |
| 30 | + DisableFeed("CURATED-Feed"); |
| 31 | + |
| 32 | + var output = _target.GetFeedByName(_curatedFeed.Name); |
| 33 | + |
| 34 | + Assert.Null(output); |
| 35 | + _entityRepository.Verify(x => x.GetAll(), Times.Never); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void ReturnsFeedWhenNameMatchesAndFeedIsNotDisabled() |
| 40 | + { |
| 41 | + var output = _target.GetFeedByName(_curatedFeed.Name); |
| 42 | + |
| 43 | + Assert.Same(_curatedFeed, output); |
| 44 | + _entityRepository.Verify(x => x.GetAll(), Times.Once); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void ReturnsNullWhenFeedDoesNotExist() |
| 50 | + { |
| 51 | + var output = _target.GetFeedByName("something-else"); |
| 52 | + |
| 53 | + Assert.Null(output); |
| 54 | + _entityRepository.Verify(x => x.GetAll(), Times.Once); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public class TheGetPackagesMethod : Facts |
| 59 | + { |
| 60 | + [Fact] |
| 61 | + public void ReturnsEmptyListWhenFeedIsDisabled() |
| 62 | + { |
| 63 | + DisableFeed(_curatedFeed.Name); |
| 64 | + |
| 65 | + var output = _target.GetPackages(_curatedFeed.Name); |
| 66 | + |
| 67 | + Assert.Empty(output); |
| 68 | + _entityRepository.Verify(x => x.GetAll(), Times.Never); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void ReturnsPackagesWhenNameMatchesAndFeedIsNotDisabled() |
| 73 | + { |
| 74 | + var output = _target.GetPackages(_curatedFeed.Name); |
| 75 | + |
| 76 | + var package = Assert.Single(output); |
| 77 | + Assert.Same(_package, package); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public void ReturnsNullWhenFeedDoesNotExist() |
| 83 | + { |
| 84 | + var output = _target.GetPackages("something-else"); |
| 85 | + |
| 86 | + Assert.Empty(output); |
| 87 | + _entityRepository.Verify(x => x.GetAll(), Times.Once); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public abstract class Facts |
| 92 | + { |
| 93 | + protected Package _package; |
| 94 | + protected CuratedFeed _curatedFeed; |
| 95 | + protected readonly Mock<IEntityRepository<CuratedFeed>> _entityRepository; |
| 96 | + protected readonly Mock<IAppConfiguration> _config; |
| 97 | + protected readonly CuratedFeedService _target; |
| 98 | + |
| 99 | + protected Facts() |
| 100 | + { |
| 101 | + _package = new Package(); |
| 102 | + _curatedFeed = new CuratedFeed |
| 103 | + { |
| 104 | + Name = "curated-feed", |
| 105 | + Packages = new[] |
| 106 | + { |
| 107 | + new CuratedPackage |
| 108 | + { |
| 109 | + PackageRegistration = new PackageRegistration |
| 110 | + { |
| 111 | + Packages = new[] |
| 112 | + { |
| 113 | + _package, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }; |
| 119 | + |
| 120 | + _entityRepository = new Mock<IEntityRepository<CuratedFeed>>(); |
| 121 | + _config = new Mock<IAppConfiguration>(); |
| 122 | + |
| 123 | + _entityRepository |
| 124 | + .Setup(x => x.GetAll()) |
| 125 | + .Returns(() => new[] { _curatedFeed }.AsQueryable()); |
| 126 | + |
| 127 | + _target = new CuratedFeedService( |
| 128 | + _entityRepository.Object, |
| 129 | + _config.Object); |
| 130 | + } |
| 131 | + |
| 132 | + protected void DisableFeed(string feedName) |
| 133 | + { |
| 134 | + _config |
| 135 | + .Setup(x => x.DisabledCuratedFeeds) |
| 136 | + .Returns(new[] { feedName }); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments