Move ConsentServiceTests & CreatedPackagesRepositoryTests

I need to figure out why hostBuilder.StartAsync(); hangs
This commit is contained in:
Mole
2020-10-02 12:00:13 +02:00
parent 602e7d01e1
commit 89d2668b8f
4 changed files with 30 additions and 15 deletions

View File

@@ -0,0 +1,215 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.Models;
using Umbraco.Core.Hosting;
using Umbraco.Core.Models.Packaging;
using Umbraco.Core.Packaging;
using Umbraco.Core.Services;
using Umbraco.Tests.Integration.Testing;
using Umbraco.Tests.Testing;
namespace Umbraco.Tests.Packaging
{
[TestFixture]
[Explicit] // TODO: find out why tests only run one at at time, the tests get blocked after the first test
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerFixture)]
public class CreatedPackagesRepositoryTests : UmbracoIntegrationTest
{
private Guid _testBaseFolder;
public override Task Setup()
{
_testBaseFolder = Guid.NewGuid();
return base.Setup();
}
public override void TearDown()
{
base.TearDown();
//clear out files/folders
Directory.Delete(HostingEnvironment.MapPathContentRoot("~/" + _testBaseFolder), true);
}
private IContentService ContentService => GetRequiredService<IContentService>();
private IContentTypeService ContentTypeService => GetRequiredService<IContentTypeService>();
private IDataTypeService DataTypeService => GetRequiredService<IDataTypeService>();
private IFileService FileService => GetRequiredService<IFileService>();
private IMacroService MacroService => GetRequiredService<IMacroService>();
private ILocalizationService LocalizationService => GetRequiredService<ILocalizationService>();
private IEntityXmlSerializer EntityXmlSerializer => GetRequiredService<IEntityXmlSerializer>();
private IHostingEnvironment HostingEnvironment => GetRequiredService<IHostingEnvironment>();
private IUmbracoVersion UmbracoVersion => GetRequiredService<IUmbracoVersion>();
public ICreatedPackagesRepository PackageBuilder => new PackagesRepository(
ContentService, ContentTypeService, DataTypeService,
FileService, MacroService, LocalizationService,
HostingEnvironment,
EntityXmlSerializer, Logger,
UmbracoVersion,
Microsoft.Extensions.Options.Options.Create(new GlobalSettings()),
"createdPackages.config",
//temp paths
tempFolderPath: "~/" + _testBaseFolder + "/temp",
packagesFolderPath: "~/" + _testBaseFolder + "/packages",
mediaFolderPath: "~/" + _testBaseFolder + "/media");
[Test]
public void Delete()
{
var def1 = new PackageDefinition
{
Name = "test",
Url = "http://test.com",
Author = "Someone",
AuthorUrl = "http://test.com"
};
var result = PackageBuilder.SavePackage(def1);
Assert.IsTrue(result);
PackageBuilder.Delete(def1.Id);
def1 = PackageBuilder.GetById(def1.Id);
Assert.IsNull(def1);
}
[Test]
public void Create_New()
{
var def1 = new PackageDefinition
{
Name = "test",
Url = "http://test.com",
Author = "Someone",
AuthorUrl = "http://test.com"
};
var result = PackageBuilder.SavePackage(def1);
Assert.IsTrue(result);
Assert.AreEqual(1, def1.Id);
Assert.AreNotEqual(default(Guid).ToString(), def1.PackageId);
var def2 = new PackageDefinition
{
Name = "test2",
Url = "http://test2.com",
Author = "Someone2",
AuthorUrl = "http://test2.com"
};
result = PackageBuilder.SavePackage(def2);
Assert.IsTrue(result);
Assert.AreEqual(2, def2.Id);
Assert.AreNotEqual(default(Guid).ToString(), def2.PackageId);
}
[Test]
public void Update_Not_Found()
{
var def = new PackageDefinition
{
Id = 3, //doesn't exist
Name = "test",
Url = "http://test.com",
Author = "Someone",
AuthorUrl = "http://test.com"
};
var result = PackageBuilder.SavePackage(def);
Assert.IsFalse(result);
}
[Test]
public void Update()
{
var def = new PackageDefinition
{
Name = "test",
Url = "http://test.com",
Author = "Someone",
AuthorUrl = "http://test.com"
};
var result = PackageBuilder.SavePackage(def);
def.Name = "updated";
def.Files = new List<string> {"hello.txt", "world.png"};
result = PackageBuilder.SavePackage(def);
Assert.IsTrue(result);
//re-get
def = PackageBuilder.GetById(def.Id);
Assert.AreEqual("updated", def.Name);
Assert.AreEqual(2, def.Files.Count);
// TODO: There's a whole lot more assertions to be done
}
[Test]
public void Export()
{
var file1 = $"~/{_testBaseFolder}/App_Plugins/MyPlugin/package.manifest";
var file2 = $"~/{_testBaseFolder}/App_Plugins/MyPlugin/styles.css";
var mappedFile1 = IOHelper.MapPath(file1);
var mappedFile2 = IOHelper.MapPath(file2);
Directory.CreateDirectory(Path.GetDirectoryName(mappedFile1));
Directory.CreateDirectory(Path.GetDirectoryName(mappedFile2));
File.WriteAllText(mappedFile1, "hello world");
File.WriteAllText(mappedFile2, "hello world");
var def = new PackageDefinition
{
Name = "test",
Url = "http://test.com",
Author = "Someone",
AuthorUrl = "http://test.com",
Files = new List<string> { file1, file2 },
Actions = "<actions><Action alias='test' /></actions>"
};
var result = PackageBuilder.SavePackage(def);
Assert.IsTrue(result);
Assert.IsTrue(def.PackagePath.IsNullOrWhiteSpace());
var zip = PackageBuilder.ExportPackage(def);
def = PackageBuilder.GetById(def.Id); //re-get
Assert.IsNotNull(def.PackagePath);
using (var archive = ZipFile.OpenRead(IOHelper.MapPath(zip)))
{
Assert.AreEqual(3, archive.Entries.Count);
//the 2 files we manually added
Assert.IsNotNull(archive.Entries.Where(x => x.Name == "package.manifest"));
Assert.IsNotNull(archive.Entries.Where(x => x.Name == "styles.css"));
//this is the actual package definition/manifest (not the developer manifest!)
var packageXml = archive.Entries.FirstOrDefault(x => x.Name == "package.xml");
Assert.IsNotNull(packageXml);
using (var stream = packageXml.Open())
{
var xml = XDocument.Load(stream);
Assert.AreEqual("umbPackage", xml.Root.Name.ToString());
Assert.AreEqual(2, xml.Root.Element("files").Elements("file").Count());
Assert.AreEqual("<Actions><Action alias=\"test\" /></Actions>", xml.Element("umbPackage").Element("Actions").ToString(SaveOptions.DisableFormatting));
// TODO: There's a whole lot more assertions to be done
}
}
}
}
}

View File

@@ -0,0 +1,126 @@
using System;
using System.Linq;
using NUnit.Framework;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Tests.Integration.Testing;
using Umbraco.Tests.Testing;
namespace Umbraco.Tests.Services
{
[TestFixture]
[Explicit]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerFixture)]
public class ConsentServiceTests : UmbracoIntegrationTest
{
[Test]
public void CanCrudConsent()
{
var consentService = GetRequiredService<IConsentService>();
// can register
var consent = consentService.RegisterConsent("user/1234", "app1", "do-something", ConsentState.Granted, "no comment");
Assert.AreNotEqual(0, consent.Id);
Assert.IsTrue(consent.Current);
Assert.AreEqual("user/1234", consent.Source);
Assert.AreEqual("app1", consent.Context);
Assert.AreEqual("do-something", consent.Action);
Assert.AreEqual(ConsentState.Granted, consent.State);
Assert.AreEqual("no comment", consent.Comment);
Assert.IsTrue(consent.IsGranted());
// can register more
consentService.RegisterConsent("user/1234", "app1", "do-something-else", ConsentState.Granted, "no comment");
consentService.RegisterConsent("user/1236", "app1", "do-something", ConsentState.Granted, "no comment");
consentService.RegisterConsent("user/1237", "app2", "do-something", ConsentState.Granted, "no comment");
// can get by source
var consents = consentService.LookupConsent(source: "user/1235").ToArray();
Assert.IsEmpty(consents);
consents = consentService.LookupConsent(source: "user/1234").ToArray();
Assert.AreEqual(2, consents.Length);
Assert.IsTrue(consents.All(x => x.Source == "user/1234"));
Assert.IsTrue(consents.Any(x => x.Action == "do-something"));
Assert.IsTrue(consents.Any(x => x.Action == "do-something-else"));
// can get by context
consents = consentService.LookupConsent(context: "app3").ToArray();
Assert.IsEmpty(consents);
consents = consentService.LookupConsent(context: "app2").ToArray();
Assert.AreEqual(1, consents.Length);
consents = consentService.LookupConsent(context: "app1").ToArray();
Assert.AreEqual(3, consents.Length);
Assert.IsTrue(consents.Any(x => x.Action == "do-something"));
Assert.IsTrue(consents.Any(x => x.Action == "do-something-else"));
// can get by action
consents = consentService.LookupConsent(action: "do-whatever").ToArray();
Assert.IsEmpty(consents);
consents = consentService.LookupConsent(context: "app1", action: "do-something").ToArray();
Assert.AreEqual(2, consents.Length);
Assert.IsTrue(consents.All(x => x.Action == "do-something"));
Assert.IsTrue(consents.Any(x => x.Source == "user/1234"));
Assert.IsTrue(consents.Any(x => x.Source == "user/1236"));
// can revoke
consent = consentService.RegisterConsent("user/1234", "app1", "do-something", ConsentState.Revoked, "no comment");
consents = consentService.LookupConsent(source: "user/1234", context: "app1", action: "do-something").ToArray();
Assert.AreEqual(1, consents.Length);
Assert.IsTrue(consents[0].Current);
Assert.AreEqual(ConsentState.Revoked, consents[0].State);
// can filter
consents = consentService.LookupConsent(context: "app1", action: "do-", actionStartsWith: true).ToArray();
Assert.AreEqual(3, consents.Length);
Assert.IsTrue(consents.All(x => x.Context == "app1"));
Assert.IsTrue(consents.All(x => x.Action.StartsWith("do-")));
// can get history
consents = consentService.LookupConsent(source: "user/1234", context: "app1", action: "do-something", includeHistory: true).ToArray();
Assert.AreEqual(1, consents.Length);
Assert.IsTrue(consents[0].Current);
Assert.AreEqual(ConsentState.Revoked, consents[0].State);
Assert.IsTrue(consents[0].IsRevoked());
Assert.IsNotNull(consents[0].History);
var history = consents[0].History.ToArray();
Assert.AreEqual(1, history.Length);
Assert.IsFalse(history[0].Current);
Assert.AreEqual(ConsentState.Granted, history[0].State);
// cannot be stupid
Assert.Throws<ArgumentException>(() =>
consentService.RegisterConsent("user/1234", "app1", "do-something", ConsentState.Granted | ConsentState.Revoked, "no comment"));
}
[Test]
public void CanRegisterConsentWithoutComment()
{
var consentService = GetRequiredService<IConsentService>();
// Attept to add consent without a comment
consentService.RegisterConsent("user/1234", "app1", "consentWithoutComment", ConsentState.Granted);
// Attempt to retrieve the consent we just added without a comment
var consents = consentService.LookupConsent(source: "user/1234", action: "consentWithoutComment").ToArray();
// Confirm we got our expected consent record
Assert.AreEqual(1, consents.Length);
}
}
}