wabbajack/Wabbajack.Server.Test/AuthoredFilesTests.cs

88 lines
3.3 KiB
C#
Raw Normal View History

2020-05-09 22:16:16 +00:00
using System;
2020-05-10 01:35:42 +00:00
using System.Linq;
2020-05-09 22:16:16 +00:00
using System.Threading.Tasks;
using Wabbajack.Common;
using Wabbajack.Common.Exceptions;
2020-05-09 22:16:16 +00:00
using Wabbajack.Lib;
using Wabbajack.Lib.AuthorApi;
using Wabbajack.Lib.Downloaders;
2020-05-10 01:35:42 +00:00
using Wabbajack.Server.DataLayer;
using Wabbajack.Server.Services;
2020-05-09 22:16:16 +00:00
using Xunit;
using Xunit.Abstractions;
namespace Wabbajack.BuildServer.Test
{
public class AuthoredFilesTests : ABuildServerSystemTest
{
public AuthoredFilesTests(ITestOutputHelper output, SingletonAdaptor<BuildServerFixture> fixture) : base(output, fixture)
{
}
[Fact]
public async Task CanUploadDownloadAndDeleteAuthoredFiles()
{
2021-02-06 16:43:11 +00:00
var cleanup = Fixture.GetService<AuthoredFilesCleanup>();
var sql = Fixture.GetService<SqlService>();
var toDelete = await cleanup.FindFilesToDelete();
await using var file = new TempFile();
await file.Path.WriteAllBytesAsync(RandomData(Consts.UploadedFileBlockSize * 4 + Consts.UploadedFileBlockSize / 3));
2020-05-09 22:16:16 +00:00
var originalHash = await file.Path.FileHashAsync();
var client = await Client.Create(Fixture.APIKey);
using var queue = new WorkQueue(2);
var uri = await client.UploadFile(queue, file.Path, (s, percent) => Utils.Log($"({percent}) {s}"));
2021-02-06 16:43:11 +00:00
var data = (await Fixture.GetService<SqlService>().AllAuthoredFiles()).ToArray();
2020-05-10 01:35:42 +00:00
Assert.Contains((string)file.Path.FileName, data.Select(f => f.OriginalFileName));
2021-02-06 16:43:11 +00:00
var listing = await cleanup.GetCDNMungedNames();
foreach (var d in data)
{
Assert.Contains(d.MungedName, listing);
}
// Just uploaded it, so it shouldn't be marked for deletion
toDelete = await cleanup.FindFilesToDelete();
foreach (var d in data)
{
Assert.DoesNotContain(d.MungedName, toDelete.CDNDelete);
Assert.DoesNotContain(d.ServerAssignedUniqueId, toDelete.SQLDelete);
}
var result = await _client.GetStringAsync(MakeURL("authored_files"));
Assert.Contains((string)file.Path.FileName, result);
2020-05-09 22:16:16 +00:00
var state = await DownloadDispatcher.Infer(uri);
Assert.IsType<WabbajackCDNDownloader.State>(state);
await state.Download(new Archive(state) {Name = (string)file.Path.FileName}, file.Path);
Assert.Equal(originalHash, await file.Path.FileHashAsync());
2021-02-06 16:43:11 +00:00
// Mark it as old
foreach (var d in data)
{
await sql.TouchAuthoredFile(await sql.GetCDNFileDefinition(d.ServerAssignedUniqueId), DateTime.Now - TimeSpan.FromDays(8));
}
// Now it should be marked for deletion
toDelete = await cleanup.FindFilesToDelete();
foreach (var d in data)
{
Assert.Contains(d.MungedName, toDelete.CDNDelete);
Assert.Contains(d.ServerAssignedUniqueId, toDelete.SQLDelete);
}
await cleanup.Execute();
toDelete = await cleanup.FindFilesToDelete();
Assert.Empty(toDelete.CDNDelete);
Assert.Empty(toDelete.SQLDelete);
2020-05-09 22:16:16 +00:00
}
}
}