Skip to content

Commit d3d05de

Browse files
authored
Handle null fileattributes (#193)
* Return an empty collection if SerializedFileAttributes is null * Fix related UT because of the addition of SampleZipFile
1 parent 002284e commit d3d05de

File tree

6 files changed

+51
-28
lines changed

6 files changed

+51
-28
lines changed

MegaApiClient.Tests/Context/AuthenticatedTestContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ protected override IEnumerable<string> GetProtectedNodes()
4747
Inputs.SharedSubFolder.Id,
4848
Inputs.SharedFile.Id,
4949
Inputs.SharedFileUpSideDown.Id,
50+
Inputs.SampleZipFile.Id,
5051
Inputs.SharedRemoteFolder.Id,
5152
Inputs.SharedRemoteSubFolder.Id,
5253
Inputs.SharedRemoteSubSubFolder.Id,
@@ -57,7 +58,7 @@ protected override IEnumerable<string> GetProtectedNodes()
5758

5859
protected override IEnumerable<string> GetPermanentNodes()
5960
{
60-
return new string[] { Inputs.SharedFolder.Id };
61+
return new string[] { Inputs.SharedFolder.Id, Inputs.SampleZipFile.Id };
6162
}
6263
}
6364
}

MegaApiClient.Tests/Data/inputs.json

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@
1111
"| |-SharedFile.jpg",
1212
"| +-SharedSubFolder (Outgoing Share)",
1313
"| |-SharedFileUpSideDown.jpg",
14-
",+-Trash",
15-
",+-Inbox",
16-
",+-Contacts",
17-
", +-SharedRemoteFolder (Incoming Share)",
18-
", |-SharedRemoteFile.jpg",
19-
", +-SharedRemoteSubFolder (Incoming Share / Subfolder of SharedRemoteFolder)",
20-
", |-SharedRemoteSubFile.jpg",
21-
", +-SharedRemoteSubSubFolder"
14+
"+-SampleFile.zip",
15+
"+-Trash",
16+
"+-Inbox",
17+
"+-Contacts",
18+
" +-SharedRemoteFolder (Incoming Share)",
19+
" |-SharedRemoteFile.jpg",
20+
" +-SharedRemoteSubFolder (Incoming Share / Subfolder of SharedRemoteFolder)",
21+
" |-SharedRemoteSubFile.jpg",
22+
" +-SharedRemoteSubSubFolder"
2223
],
2324

2425
"FileLink": "https://mega.nz/file/W0UAgJaK#XOYyTETrIy8daz3_dw3fdh6Hh8EFEdrnbyoop1r9R6g",
26+
"ZipFileLink": "https://mega.nz/file/WkNUzRzK#4aKRNvHSlkPPW3zHMYuRQANh1CNueAd6dmw5rOlwurY",
2527
"FolderLink": "https://mega.nz/folder/e4diDZ7T#iJnegBO_m6OXBQp27lHCrg",
2628

2729
"Root": {
@@ -73,6 +75,14 @@
7375
"Fingerprint": "hDL3WKxHmI6lMM92r43BWQTMmE1h"
7476
},
7577

78+
"SampleZipFile": {
79+
"Id": "e5U0XbgQ",
80+
"Size": 513604,
81+
"CreationDate": "2021-11-22T08:12:43.0000000+08:00",
82+
"ModificationDate": "2021-11-22T08:09:59.0000000+08:00",
83+
"Fingerprint": "sKBj43s_QyradRQ1YlmxhATX35ph"
84+
},
85+
7686
"SharedRemoteFolder": {
7787
"ParentId": "zoUwhLYR",
7888
"Id": "20d2XLIS",

MegaApiClient.Tests/Login.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public void GetAccountInformation_AuthenticatedUser_Succeeds(string email, strin
261261

262262
Assert.NotNull(accountInformation);
263263
Assert.Equal(AuthenticatedTestContext.Inputs.TotalQuota, accountInformation.TotalQuota);
264-
Assert.Equal(AuthenticatedTestContext.Inputs.SharedFile.Size + AuthenticatedTestContext.Inputs.SharedFileUpSideDown.Size, accountInformation.UsedQuota);
264+
Assert.Equal(AuthenticatedTestContext.Inputs.SharedFile.Size + AuthenticatedTestContext.Inputs.SharedFileUpSideDown.Size + AuthenticatedTestContext.Inputs.SampleZipFile.Size, accountInformation.UsedQuota);
265265
}
266266

267267
[Theory, MemberData(nameof(GetCredentials), false)]

MegaApiClient.Tests/NodeOperations.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public void Rename_Succeeds(NodeType nodeType)
289289

290290
[Theory]
291291
[JsonInputsDataAttribute("FileLink")]
292-
public void GetNodeFromLink_Browse_Succeeds(string fileLink)
292+
public void GetNodeFromLink_WithFileAttributes_Succeeds(string fileLink)
293293
{
294294
var node = Context.Client.GetNodeFromLink(new Uri(fileLink));
295295

@@ -302,6 +302,21 @@ public void GetNodeFromLink_Browse_Succeeds(string fileLink)
302302
Assert.Equal(2, node.FileAttributes.Length);
303303
}
304304

305+
[Theory]
306+
[JsonInputsDataAttribute("ZipFileLink")]
307+
public void GetNodeFromLink_WithoutFileAttributes_Succeeds(string fileLink)
308+
{
309+
var node = Context.Client.GetNodeFromLink(new Uri(fileLink));
310+
311+
Assert.NotNull(node);
312+
Assert.Equal("SampleFile.zip", node.Name);
313+
Assert.Equal(AuthenticatedTestContext.Inputs.SampleZipFile.Size, node.Size);
314+
Assert.Equal(AuthenticatedTestContext.Inputs.SampleZipFile.ModificationDate, node.ModificationDate);
315+
Assert.Null(node.CreationDate);
316+
Assert.Equal(AuthenticatedTestContext.Inputs.SampleZipFile.Fingerprint, node.Fingerprint);
317+
Assert.Empty(node.FileAttributes);
318+
}
319+
305320
[Theory]
306321
[JsonInputsDataAttribute(new object[] { null }, new string[] { "FolderLink" })]
307322
[JsonInputsDataAttribute(new object[] { "/file/SELECTED_FILE_NODE_ID" }, new string[] { "FolderLink" })]
@@ -330,7 +345,7 @@ public void GetNodesFromLink_Succeeds(string suffix, string folderLink)
330345
Assert.Equal(AuthenticatedTestContext.Inputs.SharedFolder.CreationDate, node.CreationDate);
331346
Assert.Null(node.ModificationDate);
332347
Assert.Null(node.Fingerprint);
333-
Assert.Null(node.FileAttributes);
348+
Assert.Empty(node.FileAttributes);
334349

335350
node = Assert.Single(nodes, x => x.Name == "SharedSubFolder");
336351
Assert.Equal(NodeType.Directory, node.Type);
@@ -340,7 +355,7 @@ public void GetNodesFromLink_Succeeds(string suffix, string folderLink)
340355
Assert.Equal(AuthenticatedTestContext.Inputs.SharedSubFolder.CreationDate, node.CreationDate);
341356
Assert.Null(node.ModificationDate);
342357
Assert.Null(node.Fingerprint);
343-
Assert.Null(node.FileAttributes);
358+
Assert.Empty(node.FileAttributes);
344359

345360
node = Assert.Single(nodes, x => x.Name == "SharedFileUpSideDown.jpg");
346361
Assert.Equal(NodeType.File, node.Type);

MegaApiClient.Tests/NodeOperationsAuthenticated.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ string expectedFingerprint
5050
}
5151

5252
[Theory]
53-
[JsonInputsData(new object[] { NodeType.Root }, new string[] { "SharedFile.Size", "SharedFileUpSideDown.Size" })]
54-
[InlineData(NodeType.Inbox, 0, 0)]
55-
[InlineData(NodeType.Trash, 0, 0)]
56-
public void GetFoldersize_FromNodeType_Succeeds(NodeType nodeType, long sharedFileSize, long sharedFileUpSideDownSize)
53+
[JsonInputsData(new object[] { NodeType.Root }, new string[] { "SharedFile.Size", "SharedFileUpSideDown.Size", "SampleZipFile.Size" })]
54+
[InlineData(NodeType.Inbox, 0, 0, 0)]
55+
[InlineData(NodeType.Trash, 0, 0, 0)]
56+
public void GetFoldersize_FromNodeType_Succeeds(NodeType nodeType, long sharedFileSize, long sharedFileUpSideDownSize, long sampleZipFile)
5757
{
5858
var node = GetNode(nodeType);
59-
var expectedSize = sharedFileSize + sharedFileUpSideDownSize;
59+
var expectedSize = sharedFileSize + sharedFileUpSideDownSize + sampleZipFile;
6060
Assert.Equal(expectedSize, node.GetFolderSize(Context.Client));
6161
Assert.Equal(expectedSize, node.GetFolderSizeAsync(Context.Client).Result);
6262
Assert.Equal(expectedSize, node.GetFolderSize(Context.Client.GetNodes()));

MegaApiClient/Node.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ internal Node(string id, DownloadUrlResponse downloadResponse, byte[] key)
3030
Attributes = Crypto.DecryptAttributes(downloadResponse.SerializedAttributes.FromBase64(), key);
3131
Size = downloadResponse.Size;
3232
Type = NodeType.File;
33-
34-
if (downloadResponse.SerializedAttributes != null)
35-
{
36-
FileAttributes = DeserializeFileAttributes(downloadResponse.SerializedFileAttributes);
37-
}
33+
FileAttributes = DeserializeFileAttributes(downloadResponse.SerializedFileAttributes);
3834
}
3935

4036
#region Public properties
@@ -185,11 +181,7 @@ public void OnDeserialized(StreamingContext ctx)
185181
}
186182

187183
Attributes = Crypto.DecryptAttributes(SerializedAttributes.FromBase64(), Key);
188-
189-
if (SerializedFileAttributes != null)
190-
{
191-
FileAttributes = DeserializeFileAttributes(SerializedFileAttributes);
192-
}
184+
FileAttributes = DeserializeFileAttributes(SerializedFileAttributes);
193185
}
194186
}
195187

@@ -216,6 +208,11 @@ public override bool Equals(object obj)
216208

217209
private static IFileAttribute[] DeserializeFileAttributes(string serializedFileAttributes)
218210
{
211+
if (serializedFileAttributes == null)
212+
{
213+
return new IFileAttribute[0];
214+
}
215+
219216
var attributes = serializedFileAttributes.Split('/');
220217

221218
return attributes

0 commit comments

Comments
 (0)