Skip to content

Commit 6ff8b06

Browse files
committed
Added tests for nodes operations
1 parent 49d2376 commit 6ff8b06

File tree

11 files changed

+491
-29
lines changed

11 files changed

+491
-29
lines changed

MegaApiClient.Tests/Extensions.cs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
using NUnit.Framework;
4+
using NUnit.Framework.Constraints;
5+
6+
namespace CG.Web.MegaApiClient.Tests
7+
{
8+
public static class Extensions
9+
{
10+
public static void DoIt(this Has has)
11+
{
12+
13+
}
14+
// http://blog.drorhelper.com/2012/11/making-string-based-method-strongly.html?m=1
15+
public static ResolvableConstraintExpression Property<T>(this ConstraintExpression expression, Expression<Func<T, object>> lambda)
16+
{
17+
MemberExpression memberExpression = null;
18+
switch (lambda.Body.NodeType)
19+
{
20+
case ExpressionType.Convert:
21+
// lambda is obj => Convert(obj.Prop) - the operand of conversion is our member expression
22+
var unary = lambda.Body as UnaryExpression;
23+
if (unary == null)
24+
{
25+
Assert.Fail("Cannot parse expression");
26+
}
27+
memberExpression = unary.Operand as MemberExpression;
28+
break;
29+
30+
case ExpressionType.MemberAccess:
31+
// lambda is (obj => obj.Prop) - the body is the member expression
32+
memberExpression = lambda.Body as MemberExpression;
33+
break;
34+
35+
default:
36+
Assert.Fail("Cannot parse expression");
37+
break;
38+
}
39+
40+
if (memberExpression == null || string.IsNullOrEmpty(memberExpression.Member.Name))
41+
{
42+
Assert.Fail("Labda body is not MemberExpression - use only properties");
43+
}
44+
45+
var propertyName = memberExpression.Member.Name;
46+
47+
return expression.Property(propertyName);
48+
}
49+
}
50+
}

MegaApiClient.Tests/Login.cs

+139-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11

22

33
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Text;
7+
using Moq;
8+
using Newtonsoft.Json;
49
using NUnit.Framework;
510

611
namespace CG.Web.MegaApiClient.Tests
@@ -13,45 +18,52 @@ public Login()
1318
{
1419
}
1520

16-
[TestCase(null, null)]
17-
[TestCase(null, "")]
18-
[TestCase("", null)]
19-
[TestCase("", "")]
20-
[TestCase(null, "password")]
21-
[TestCase("username", null)]
22-
public void Login_UnsupportedCredentials_Throws(string username, string password)
21+
[Test]
22+
public void ClientCtor_NullWebClient_Throws()
23+
{
24+
Assert.That(
25+
() => this.Client = new MegaApiClient(null),
26+
Throws.TypeOf<ArgumentNullException>()
27+
.With.Property<ArgumentNullException>(x => x.ParamName).EqualTo("webClient"));
28+
}
29+
30+
[TestCaseSource("GetInvalidCredentials")]
31+
public void Login_UnsupportedCredentials_Throws(string email, string password)
2332
{
2433
Assert.That(
25-
() => this.Client.Login(username, password),
26-
Throws.TypeOf<ArgumentNullException>());
34+
() => this.Client.Login(email, password),
35+
Throws.TypeOf<ArgumentNullException>()
36+
.With.Property<ArgumentNullException>(x => x.ParamName).EqualTo("email")
37+
.Or.With.Property<ArgumentNullException>(x => x.ParamName).EqualTo("password"));
2738
}
2839

2940
[TestCase("username", "password", ApiResultCode.BadArguments)]
3041
[TestCase("[email protected]", "password", ApiResultCode.ResourceNotExists)]
31-
public void Login_InvalidCredentials_Throws(string username, string password, ApiResultCode expectedErrorCode)
42+
public void Login_InvalidCredentials_Throws(string email, string password, ApiResultCode expectedErrorCode)
3243
{
3344
Assert.That(
34-
() => this.Client.Login(username, password),
45+
() => this.Client.Login(email, password),
3546
Throws.TypeOf<ApiException>()
36-
.With.Property("ApiResultCode").EqualTo(expectedErrorCode));
47+
.With.Property<ApiException>(x => x.ApiResultCode).EqualTo(expectedErrorCode));
3748
}
3849

3950
[TestCaseSource("GetCredentials")]
40-
public void Login_ValidCredentials_Succeeds(string username, string password)
51+
public void Login_ValidCredentials_Succeeds(string email, string password)
4152
{
4253
Assert.That(
43-
() => this.Client.Login(username, password),
54+
() => this.Client.Login(email, password),
4455
Throws.Nothing);
4556
}
4657

4758
[TestCaseSource("GetCredentials")]
48-
public void LoginTwice_ValidCredentials_Throws(string username, string password)
59+
public void LoginTwice_ValidCredentials_Throws(string email, string password)
4960
{
50-
this.Client.Login(username, password);
61+
this.Client.Login(email, password);
5162

5263
Assert.That(
53-
() => this.Client.Login(username, password),
54-
Throws.TypeOf<NotSupportedException>());
64+
() => this.Client.Login(email, password),
65+
Throws.TypeOf<NotSupportedException>()
66+
.With.Message.EqualTo("Already logged in"));
5567
}
5668

5769
[Test]
@@ -69,25 +81,129 @@ public void LoginAnonymousTwice_Throws()
6981

7082
Assert.That(
7183
() => this.Client.LoginAnonymous(),
72-
Throws.TypeOf<NotSupportedException>());
84+
Throws.TypeOf<NotSupportedException>()
85+
.With.Message.EqualTo("Already logged in"));
7386
}
7487

7588
[TestCaseSource("GetCredentials")]
76-
public void LogoutAfterLogin_Succeeds(string username, string password)
89+
public void LogoutAfterLogin_Succeeds(string email, string password)
7790
{
78-
this.Client.Login(username, password);
79-
91+
this.Client.Login(email, password);
92+
8093
Assert.That(
8194
() => this.Client.Logout(),
8295
Throws.Nothing);
8396
}
8497

98+
[TestCaseSource("GetCredentials")]
99+
public void LogoutTwiceAfterLogin_Throws(string email, string password)
100+
{
101+
this.Client.Login(email, password);
102+
103+
this.Client.Logout();
104+
105+
Assert.That(
106+
() => this.Client.Logout(),
107+
Throws.TypeOf<NotSupportedException>()
108+
.With.Message.EqualTo("Not logged in"));
109+
}
110+
85111
[Test]
86112
public void LogoutWithoutLogin_Throws()
87113
{
88114
Assert.That(
89115
() => this.Client.Logout(),
90-
Throws.TypeOf<NotSupportedException>());
116+
Throws.TypeOf<NotSupportedException>()
117+
.With.Message.EqualTo("Not logged in"));
118+
}
119+
120+
[TestCase(null)]
121+
public void Login_NullAuthInfos_Throws(MegaApiClient.AuthInfos authInfos)
122+
{
123+
Assert.That(
124+
() => this.Client.Login(authInfos),
125+
Throws.TypeOf<ArgumentNullException>()
126+
.With.Property<ArgumentNullException>(x => x.ParamName).EqualTo("authInfos"));
127+
}
128+
129+
[TestCaseSource("GetCredentials")]
130+
public void Login_DeserializedAuthInfos_Succeeds(string email, string password)
131+
{
132+
var authInfos = MegaApiClient.GenerateAuthInfos(email, password);
133+
var serializedAuthInfos = JsonConvert.SerializeObject(authInfos, Formatting.None).Replace('\"', '\'');
134+
var deserializedAuthInfos = JsonConvert.DeserializeObject<MegaApiClient.AuthInfos>(serializedAuthInfos);
135+
136+
Assert.That(
137+
() => this.Client.Login(deserializedAuthInfos),
138+
Throws.Nothing);
139+
}
140+
141+
[TestCaseSource("GetInvalidCredentials")]
142+
public void GenerateAuthInfos_InvalidCredentials_Throws(string email, string password)
143+
{
144+
Assert.That(() =>
145+
MegaApiClient.GenerateAuthInfos(email, password),
146+
Throws.TypeOf<ArgumentNullException>()
147+
.With.Property<ArgumentNullException>(x => x.ParamName).EqualTo("email")
148+
.Or.With.Property<ArgumentNullException>(x => x.ParamName).EqualTo("password"));
149+
}
150+
151+
[TestCase("[email protected]", "password", Result = "{'Email':'[email protected]','Hash':'ObELy57HULI','PasswordAesKey':'ZAM5cl5uvROiXwBSEp98sQ=='}")]
152+
public string GenerateAuthInfos_ValidCredentials_Succeeds(string email, string password)
153+
{
154+
var authInfos = MegaApiClient.GenerateAuthInfos(email, password);
155+
156+
return JsonConvert.SerializeObject(authInfos, Formatting.None).Replace('\"', '\'');
157+
}
158+
159+
[TestCaseSource("GetMethodsRequiredLogin")]
160+
public void Methods_LoginRequired_Throws(Action<MegaApiClient> testMethod)
161+
{
162+
Assert.That(
163+
() => testMethod(this.Client),
164+
Throws.TypeOf<NotSupportedException>()
165+
.With.Message.EqualTo("Not logged in"));
166+
}
167+
168+
private IEnumerable<ITestCaseData> GetInvalidCredentials()
169+
{
170+
yield return new TestCaseData(null, null);
171+
yield return new TestCaseData(null, "");
172+
yield return new TestCaseData("", null);
173+
yield return new TestCaseData("", "");
174+
yield return new TestCaseData(null, "password");
175+
yield return new TestCaseData("username", null);
176+
}
177+
178+
private IEnumerable<ITestCaseData> GetMethodsRequiredLogin()
179+
{
180+
Mock<INode> nodeDirectoryMock = new Mock<INode>();
181+
nodeDirectoryMock.SetupGet(x => x.Type).Returns(NodeType.Directory);
182+
nodeDirectoryMock.As<INodeCrypto>();
183+
INode nodeDirectory = nodeDirectoryMock.Object;
184+
185+
Mock<INode> nodeFileMock = new Mock<INode>();
186+
nodeFileMock.SetupGet(x => x.Type).Returns(NodeType.File);
187+
nodeFileMock.As<INodeCrypto>();
188+
INode nodeFile = nodeFileMock.Object;
189+
190+
Uri uri = new Uri("http://www.example.com");
191+
string tempFile = Path.GetTempFileName();
192+
193+
yield return new TestCaseData((Action<MegaApiClient>)(x => x.Delete(nodeDirectory)));
194+
yield return new TestCaseData((Action<MegaApiClient>)(x => x.Delete(nodeDirectory, false)));
195+
yield return new TestCaseData((Action<MegaApiClient>)(x => x.Delete(nodeDirectory, true)));
196+
yield return new TestCaseData((Action<MegaApiClient>)(x => x.DownloadFile(nodeFile, "outputFile")));
197+
yield return new TestCaseData((Action<MegaApiClient>)(x => x.DownloadFile(uri, "outputFile")));
198+
yield return new TestCaseData((Action<MegaApiClient>)(x => x.GetNodes()));
199+
yield return new TestCaseData((Action<MegaApiClient>)(x => x.GetNodes(nodeDirectory)));
200+
yield return new TestCaseData((Action<MegaApiClient>)(x => x.CreateFolder("name", nodeDirectory)));
201+
yield return new TestCaseData((Action<MegaApiClient>)(x => x.Download(nodeFile)));
202+
yield return new TestCaseData((Action<MegaApiClient>)(x => x.Download(uri)));
203+
yield return new TestCaseData((Action<MegaApiClient>)(x => x.GetDownloadLink(nodeDirectory)));
204+
yield return new TestCaseData((Action<MegaApiClient>)(x => x.Move(nodeDirectory, nodeDirectory)));
205+
yield return new TestCaseData((Action<MegaApiClient>)(x => x.Upload(new MemoryStream(new byte[0]), "name", nodeDirectory)));
206+
yield return new TestCaseData((Action<MegaApiClient>)(x => x.Upload(tempFile, nodeDirectory)));
91207
}
92208
}
93209
}

MegaApiClient.Tests/MegaApiClient.Tests.csproj

+6-3
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@
3737
<AssemblyOriginatorKeyFile>..\key.snk</AssemblyOriginatorKeyFile>
3838
</PropertyGroup>
3939
<ItemGroup>
40-
<Reference Include="Castle.Core">
41-
<HintPath>..\packages\Castle.Core.3.1.0\lib\net35\Castle.Core.dll</HintPath>
42-
</Reference>
4340
<Reference Include="Moq">
4441
<HintPath>..\packages\Moq.4.2.1409.1722\lib\net35\Moq.dll</HintPath>
4542
</Reference>
43+
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
44+
<SpecificVersion>False</SpecificVersion>
45+
<HintPath>..\packages\Newtonsoft.Json.6.0.8\lib\net35\Newtonsoft.Json.dll</HintPath>
46+
</Reference>
4647
<Reference Include="nunit.framework">
4748
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
4849
</Reference>
@@ -53,7 +54,9 @@
5354
</ItemGroup>
5455
<ItemGroup>
5556
<Compile Include="Login.cs" />
57+
<Compile Include="NodeOperations.cs" />
5658
<Compile Include="Properties\AssemblyInfo.cs" />
59+
<Compile Include="Extensions.cs" />
5760
<Compile Include="TestsBase.cs" />
5861
<Compile Include="Upload.cs" />
5962
</ItemGroup>

0 commit comments

Comments
 (0)