Skip to content

Commit

Permalink
Merge branch 'reconfigure-unittests'
Browse files Browse the repository at this point in the history
  • Loading branch information
gpailler committed Sep 27, 2021
2 parents c695f4c + 81277c4 commit f556967
Show file tree
Hide file tree
Showing 15 changed files with 382 additions and 221 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ jobs:
uses: actions/checkout@v2

- name: Tests
run: dotnet test --configuration ${{ env.CONFIGURATION }} ./MegaApiClient.Tests/ --framework net46 --logger "console;verbosity=detailed" -p:CollectCoverage=true -p:CoverletOutputFormat=opencover -p:Exclude="[MegaApiClient]BigInteger"
run: dotnet test --configuration ${{ env.CONFIGURATION }} ./MegaApiClient.Tests/ --framework net50 --logger "console;verbosity=detailed" --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Exclude="[MegaApiClient]BigInteger"
env:
MEGAAPICLIENT_PASSWORD: ${{ secrets.MEGAAPICLIENT_PASSWORD }}

- name: Codecov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v2

doc:
name: Documentation 📚
Expand Down
110 changes: 28 additions & 82 deletions MegaApiClient.Tests/Context/AuthenticatedTestContext.cs
Original file line number Diff line number Diff line change
@@ -1,92 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace CG.Web.MegaApiClient.Tests.Context
{
using System.Dynamic;
using System.IO;
using Newtonsoft.Json;

[CollectionDefinition(nameof(AuthenticatedTestContext))]
public class AuthenticatedLoginTestsCollection : ICollectionFixture<AuthenticatedTestContext>, ICollectionFixture<AuthenticatedAsyncTestContext> { }

public class AuthenticatedTestContext : TestContext, IDisposable
{
private const string MegaApiClientPasswordEnvironment = "MEGAAPICLIENT_PASSWORD";
internal const string UsernameAccountV1 = "[email protected]";
internal const string UsernameAccountV2 = "[email protected]";
internal static readonly string Password = Environment.GetEnvironmentVariable(MegaApiClientPasswordEnvironment);
internal const string MasterKeyHashV1 = "Q7LYmSw2Et4GjL9KWZKcq1oEzC1dX3wZDEdhS3YJICk";
internal const string MasterKeyHashV2 = "pRYA3b7JExchAvcTPaGpgl5h9U42iKncvXhPku7YIcA";

internal const string FileLinkLegacy = "https://mega.nz/#!bkwkHC7D!AWJuto8_fhleAI2WG0RvACtKkL_s9tAtvBXXDUp2bQk";
internal const string FileLink = "https://mega.nz/file/bkwkHC7D#AWJuto8_fhleAI2WG0RvACtKkL_s9tAtvBXXDUp2bQk";
internal const string FolderLinkLegacy = "https://mega.nz/#F!e1ogxQ7T!ee4Q_ocD1bSLmNeg9B6kBw";
internal const string FolderLink = "https://mega.nz/folder/e1ogxQ7T#ee4Q_ocD1bSLmNeg9B6kBw";
internal const string FileId = "P8BBzaTS";
internal const int FileSize = 523265;
internal const string FolderId = "e5IjHLLJ";
internal const string SubFolderId = "CghQlTCa";
internal const string SubFolderFileId = "TlBDQIQY";
internal const int SubFolderFileSize = 112916;

/*
Storage layout
+-Root (bsxVBKLL)
| +-SharedFolder (e5IjHLLJ) (Outgoing Share)
| |-SharedFile.jpg (P8BBzaTS)
| +-SharedSubFolder (CghQlTCa) (Outgoing Share)
| |-SharedFileUpSideDown.jpg (TlBDQIQY)
+-Trash (j0wEGbTZ)
+-Inbox (zhITTbIJ)
+-Contacts
+-SharedRemoteFolder (b0I0QDhA) (Incoming Share)
|-SharedRemoteFile.jpg (e5wjkSJB)
+-SharedRemoteSubFolder (KhZSWI7C) (Incoming Share / Subfolder of SharedRemoteFolder)
|-SharedRemoteSubFile.jpg (HtonzYYY)
+-SharedRemoteSubSubFolder (z1YCibCT)
*/

private readonly string[] systemNodes =
{
"bsxVBKLL", // Root
"j0wEGbTZ", // Trash
"zhITTbIJ", // Inbox
};

private readonly string[] permanentFoldersRootNodes =
{
FolderId // SharedFolder
};
internal static readonly string InputsJson = File.ReadAllText("Data\\inputs.json");

private readonly string[] permanentFoldersNodes =
static AuthenticatedTestContext()
{
SubFolderId, // SharedSubFolder
};

private readonly string[] permanentRemoteFoldersNodes =
{
"b0I0QDhA", // SharedRemoteFolder
"KhZSWI7C", // SharedRemoteSubFolder
"z1YCibCT", // SharedRemoteSubSubFolder
};

private readonly string[] permanentFilesNodes =
{
FileId, // SharedFile.jpg
SubFolderFileId, // SharedFileUpSideDown.jpg
};
Inputs = JsonConvert.DeserializeObject<ExpandoObject>(InputsJson);
}

private readonly string[] permanentRemoteFilesNodes =
{
"e5wjkSJB", // SharedRemoteFile.jpg
"HtonzYYY", // SharedRemoteSubFile.jpg
};
private const string MegaApiClientPasswordEnvironment = "MEGAAPICLIENT_PASSWORD";
internal static readonly string Password = Environment.GetEnvironmentVariable(MegaApiClientPasswordEnvironment);

public string PermanentFilesNode
{
get { return this.permanentFilesNodes[0]; }
}
internal static dynamic Inputs { get; }

public virtual void Dispose()
{
Expand All @@ -96,22 +33,31 @@ public virtual void Dispose()
protected override void ConnectClient(IMegaApiClient client)
{
Assert.False(string.IsNullOrEmpty(Password), $"Environment variable {MegaApiClientPasswordEnvironment} not set.");
client.Login(UsernameAccountV1, Password);
client.Login(Inputs.UsernameAccount, Password);
}

protected override IEnumerable<string> GetProtectedNodes()
{
return this.systemNodes
.Concat(this.permanentFoldersRootNodes)
.Concat(this.permanentFoldersNodes)
.Concat(this.permanentFilesNodes)
.Concat(this.permanentRemoteFoldersNodes)
.Concat(this.permanentRemoteFilesNodes);
return new string[]
{
Inputs.Root.Id,
Inputs.Trash.Id,
Inputs.Inbox.Id,
Inputs.SharedFolder.Id,
Inputs.SharedSubFolder.Id,
Inputs.SharedFile.Id,
Inputs.SharedFileUpSideDown.Id,
Inputs.SharedRemoteFolder.Id,
Inputs.SharedRemoteSubFolder.Id,
Inputs.SharedRemoteSubSubFolder.Id,
Inputs.SharedRemoteFile.Id,
Inputs.SharedRemoteSubFile.Id
};
}

protected override IEnumerable<string> GetPermanentNodes()
{
return this.permanentFoldersRootNodes;
return new string[] { Inputs.SharedFolder.Id };
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified MegaApiClient.Tests/Data/SampleFile_preview.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified MegaApiClient.Tests/Data/SampleFile_thumbnail.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
103 changes: 103 additions & 0 deletions MegaApiClient.Tests/Data/inputs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{
"UsernameAccount": "[email protected]",
"TotalQuota": 21474836480,

"_comment_MasterKeyHash": "Hash of the master key (check GetRecoveryKeyAfterLogin_Succeeds to generate the hash from the masterkey)",
"MasterKeyHash": "AqZH2MufH0VgX-qcv9rQ2zoPQxxhxsMGEyRfeVrYWHk",

"_comment_StorageLayout": [
"+-Root",
"| +-SharedFolder (Outgoing Share)",
"| |-SharedFile.jpg",
"| +-SharedSubFolder (Outgoing Share)",
"| |-SharedFileUpSideDown.jpg",
",+-Trash",
",+-Inbox",
",+-Contacts",
", +-SharedRemoteFolder (Incoming Share)",
", |-SharedRemoteFile.jpg",
", +-SharedRemoteSubFolder (Incoming Share / Subfolder of SharedRemoteFolder)",
", |-SharedRemoteSubFile.jpg",
", +-SharedRemoteSubSubFolder"
],

"FileLink": "https://mega.nz/file/W0UAgJaK#XOYyTETrIy8daz3_dw3fdh6Hh8EFEdrnbyoop1r9R6g",
"FolderLink": "https://mega.nz/folder/e4diDZ7T#iJnegBO_m6OXBQp27lHCrg",

"Root": {
"Id": "39FwkLpK",
"CreationDate": "2021-09-24T17:18:01.0000000+08:00"
},

"Trash": {
"Id": "npM2zZiJ",
"CreationDate": "2021-09-24T17:18:01.0000000+08:00"
},

"Inbox": {
"Id": "iwEglZbZ",
"CreationDate": "2021-09-24T17:18:01.0000000+08:00"
},

"SharedFolder": {
"Id": "G5NikTgR",
"CreationDate": "2021-09-24T17:23:48.0000000+08:00"
},

"SharedFile": {
"Id": "KlVgwR4B",
"Size": 523265,
"CreationDate": "2021-09-24T17:24:21.0000000+08:00",
"ModificationDate": "2021-02-28T22:11:37.0000000+08:00",
"Thumbnail": {
"Id": "Br9xX2hAYqw",
"AttributeId": 882
},
"Preview": {
"Id": "IoZ69cbG8pk",
"AttributeId": 704
}
},

"SharedSubFolder": {
"Id": "zwNiSB7J",
"CreationDate": "2021-09-24T17:25:24.0000000+08:00"
},

"SharedFileUpSideDown": {
"Id": "y5cSkZrZ",
"Size": 112916,
"CreationDate": "2021-09-24T17:25:57.0000000+08:00",
"ModificationDate": "2021-09-24T17:22:20.0000000+08:00"
},

"SharedRemoteFolder": {
"ParentId": "zoUwhLYR",
"Id": "20d2XLIS",
"CreationDate": "2021-09-26T14:27:29.0000000+08:00"
},

"SharedRemoteSubFolder": {
"Id": "HgU2GRDB",
"CreationDate": "2021-09-26T14:28:36.0000000+08:00"
},

"SharedRemoteSubSubFolder": {
"Id": "bxMQkZhI",
"CreationDate": "2021-09-26T14:29:11.0000000+08:00"
},

"SharedRemoteFile": {
"Id": "qgFmnRDI",
"Size": 523265,
"CreationDate": "2021-09-26T14:28:31.0000000+08:00",
"ModificationDate": "2021-02-28T22:11:37.0000000+08:00"
},

"SharedRemoteSubFile": {
"Id": "qoNiELgb",
"Size": 523265,
"CreationDate": "2021-09-26T14:29:04.0000000+08:00",
"ModificationDate": "2021-02-28T22:11:37.0000000+08:00"
}
}
16 changes: 8 additions & 8 deletions MegaApiClient.Tests/DownloadUpload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,17 @@ public void DownloadLink_ToStream_Succeeds()

using (Stream stream = new FileStream(this.GetAbsoluteFilePath(expectedResultFile), FileMode.Open, FileAccess.Read))
{
this.AreStreamsEquivalent(this.context.Client.Download(new Uri(AuthenticatedTestContext.FileLink)), stream);
this.AreStreamsEquivalent(this.context.Client.Download(new Uri(AuthenticatedTestContext.Inputs.FileLink)), stream);
}
}

[RetryFact]
public void Download_ValidateStream_Succeeds()
{
using (Stream stream = this.context.Client.Download(new Uri(AuthenticatedTestContext.FileLink)))
using (Stream stream = this.context.Client.Download(new Uri(AuthenticatedTestContext.Inputs.FileLink)))
{
Assert.NotNull(stream);
Assert.Equal(523265, stream.Length);
Assert.Equal(AuthenticatedTestContext.Inputs.SharedFile.Size, stream.Length);
Assert.True(stream.CanRead);
Assert.False(stream.CanSeek);
Assert.False(stream.CanTimeout);
Expand Down Expand Up @@ -191,9 +191,9 @@ public static IEnumerable<object[]> DownloadLinkToFileInvalidParameter
yield return new object[] { "https://mega.nz", outFile, typeof(ArgumentException) };
yield return new object[] { "https://mega.nz/#!38JjRYIA", outFile, typeof(ArgumentException) };
yield return new object[] { "https://mega.nz/#!ulISSQIb!", outFile, typeof(ArgumentException) };
yield return new object[] { AuthenticatedTestContext.FileLink, null, typeof(ArgumentNullException) };
yield return new object[] { AuthenticatedTestContext.FileLink, string.Empty, typeof(ArgumentNullException) };
yield return new object[] { AuthenticatedTestContext.FileLink, outFile, typeof(IOException) };
yield return new object[] { AuthenticatedTestContext.Inputs.FileLink, null, typeof(ArgumentNullException) };
yield return new object[] { AuthenticatedTestContext.Inputs.FileLink, string.Empty, typeof(ArgumentNullException) };
yield return new object[] { AuthenticatedTestContext.Inputs.FileLink, outFile, typeof(IOException) };
}
}

Expand All @@ -203,7 +203,7 @@ public void DownloadLink_ToFile_Succeeds()
const string expectedResultFile = "Data/SampleFile.jpg";

string outFile = this.GetTempFileName();
this.context.Client.DownloadFile(new Uri(AuthenticatedTestContext.FileLink), outFile);
this.context.Client.DownloadFile(new Uri(AuthenticatedTestContext.Inputs.FileLink), outFile);

Assert.Equal(File.ReadAllBytes(this.GetAbsoluteFilePath(expectedResultFile)), File.ReadAllBytes(outFile));
}
Expand All @@ -212,7 +212,7 @@ public void DownloadLink_ToFile_Succeeds()
public void GetNodesFromLink_Download_Succeeds()
{
const string expectedResultFile = "Data/SampleFile.jpg";
var nodes = this.context.Client.GetNodesFromLink(new Uri(AuthenticatedTestContext.FolderLink));
var nodes = this.context.Client.GetNodesFromLink(new Uri(AuthenticatedTestContext.Inputs.FolderLink));
var node = nodes.Single(x => x.Name == "SharedFile.jpg");

using (Stream stream = new FileStream(this.GetAbsoluteFilePath(expectedResultFile), FileMode.Open, FileAccess.Read))
Expand Down
8 changes: 4 additions & 4 deletions MegaApiClient.Tests/DownloadUploadAuthenticated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public DownloadUploadAuthenticated(AuthenticatedTestContext context, ITestOutput
[Fact]
public void DownloadNode_ToStream_Succeeds()
{
var node = this.GetNode(((AuthenticatedTestContext)this.context).PermanentFilesNode);
var node = this.GetNode(AuthenticatedTestContext.Inputs.SharedFile.Id);

using (Stream stream = this.context.Client.Download(node))
{
Expand Down Expand Up @@ -74,8 +74,8 @@ public void UploadStream_DownloadLink_Succeeds()
}

[Theory]
[InlineData(AuthenticatedTestContext.FileId, AuthenticatedTestContext.FileLink)]
[InlineData(AuthenticatedTestContext.FolderId, AuthenticatedTestContext.FolderLink)]
[JsonInputsData("SharedFile.Id", "FileLink")]
[JsonInputsData("SharedFolder.Id", "FolderLink")]
public void GetDownloadLink_ExistingLinks_Succeeds(string id, string expectedLink)
{
var node = this.GetNode(id);
Expand Down Expand Up @@ -111,7 +111,7 @@ public void GetDownloadLink_FolderNewLink_Succeeds()
[InlineData(FileAttributeType.Preview, "Data/SampleFile_preview.jpg")]
public void DownloadFileAttribute_ToStream_Succeeds(FileAttributeType fileAttributeType, string expectedFileContent)
{
var node = this.GetNode(((AuthenticatedTestContext)this.context).PermanentFilesNode);
var node = this.GetNode(AuthenticatedTestContext.Inputs.SharedFile.Id);

using (Stream stream = this.context.Client.DownloadFileAttribute(node, fileAttributeType))
{
Expand Down
8 changes: 4 additions & 4 deletions MegaApiClient.Tests/DownloadUploadAuthenticatedAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void DownloadFileAsync_FromNode_Succeeds(long? reportProgressChunkSize, l
{
// Arrange
this.context.Options.ReportProgressChunkSize = reportProgressChunkSize.GetValueOrDefault(this.context.Options.ReportProgressChunkSize);
var node = this.GetNode(((AuthenticatedTestContext)this.context).PermanentFilesNode);
var node = this.GetNode(AuthenticatedTestContext.Inputs.SharedFile.Id);

EventTester<double> eventTester = new EventTester<double>();
IProgress<double> progress = new SyncProgress<double>(eventTester.OnRaised);
Expand Down Expand Up @@ -65,7 +65,7 @@ public void DownloadFileAsync_FromLink_Succeeds()
string outputFile = this.GetTempFileName();

// Act
Task task = this.context.Client.DownloadFileAsync(new Uri(AuthenticatedTestContext.FileLink), outputFile, progress);
Task task = this.context.Client.DownloadFileAsync(new Uri(AuthenticatedTestContext.Inputs.FileLink), outputFile, progress);
bool result = task.Wait(this.Timeout);

// Assert
Expand Down Expand Up @@ -119,8 +119,8 @@ public void UploadStreamAsync_DownloadLink_Succeeds(int dataSize, int expectedPr
public void AsyncMethods_WithoutProgression_Succeeds()
{
var root = this.GetNode(NodeType.Root);
var node = this.GetNode(((AuthenticatedTestContext)this.context).PermanentFilesNode);
var uri = new Uri(AuthenticatedTestContext.FileLink);
var node = this.GetNode(AuthenticatedTestContext.Inputs.SharedFile.Id);
var uri = new Uri(AuthenticatedTestContext.Inputs.FileLink);
var sampleFilePath = this.GetAbsoluteFilePath("Data/SampleFile.jpg");
var sampleFileStream = new FileStream(sampleFilePath, FileMode.Open, FileAccess.Read);

Expand Down
Loading

0 comments on commit f556967

Please sign in to comment.