Skip to content

Commit 49d2376

Browse files
committed
Added test for Login and Upload
1 parent babae20 commit 49d2376

File tree

6 files changed

+245
-107
lines changed

6 files changed

+245
-107
lines changed

MegaApiClient.Tests/Login.cs

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+

2+
3+
using System;
4+
using NUnit.Framework;
5+
6+
namespace CG.Web.MegaApiClient.Tests
7+
{
8+
[TestFixture]
9+
public class Login : TestsBase
10+
{
11+
public Login()
12+
: base(Options.None)
13+
{
14+
}
15+
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)
23+
{
24+
Assert.That(
25+
() => this.Client.Login(username, password),
26+
Throws.TypeOf<ArgumentNullException>());
27+
}
28+
29+
[TestCase("username", "password", ApiResultCode.BadArguments)]
30+
[TestCase("[email protected]", "password", ApiResultCode.ResourceNotExists)]
31+
public void Login_InvalidCredentials_Throws(string username, string password, ApiResultCode expectedErrorCode)
32+
{
33+
Assert.That(
34+
() => this.Client.Login(username, password),
35+
Throws.TypeOf<ApiException>()
36+
.With.Property("ApiResultCode").EqualTo(expectedErrorCode));
37+
}
38+
39+
[TestCaseSource("GetCredentials")]
40+
public void Login_ValidCredentials_Succeeds(string username, string password)
41+
{
42+
Assert.That(
43+
() => this.Client.Login(username, password),
44+
Throws.Nothing);
45+
}
46+
47+
[TestCaseSource("GetCredentials")]
48+
public void LoginTwice_ValidCredentials_Throws(string username, string password)
49+
{
50+
this.Client.Login(username, password);
51+
52+
Assert.That(
53+
() => this.Client.Login(username, password),
54+
Throws.TypeOf<NotSupportedException>());
55+
}
56+
57+
[Test]
58+
public void LoginAnonymous_Succeeds()
59+
{
60+
Assert.That(
61+
() => this.Client.LoginAnonymous(),
62+
Throws.Nothing);
63+
}
64+
65+
[Test]
66+
public void LoginAnonymousTwice_Throws()
67+
{
68+
this.Client.LoginAnonymous();
69+
70+
Assert.That(
71+
() => this.Client.LoginAnonymous(),
72+
Throws.TypeOf<NotSupportedException>());
73+
}
74+
75+
[TestCaseSource("GetCredentials")]
76+
public void LogoutAfterLogin_Succeeds(string username, string password)
77+
{
78+
this.Client.Login(username, password);
79+
80+
Assert.That(
81+
() => this.Client.Logout(),
82+
Throws.Nothing);
83+
}
84+
85+
[Test]
86+
public void LogoutWithoutLogin_Throws()
87+
{
88+
Assert.That(
89+
() => this.Client.Logout(),
90+
Throws.TypeOf<NotSupportedException>());
91+
}
92+
}
93+
}

MegaApiClient.Tests/LoginTests.cs

-106
This file was deleted.

MegaApiClient.Tests/MegaApiClient.Tests.csproj

+9-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
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>
43+
<Reference Include="Moq">
44+
<HintPath>..\packages\Moq.4.2.1409.1722\lib\net35\Moq.dll</HintPath>
45+
</Reference>
4046
<Reference Include="nunit.framework">
4147
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
4248
</Reference>
@@ -46,8 +52,10 @@
4652
<Reference Include="System.Xml" />
4753
</ItemGroup>
4854
<ItemGroup>
49-
<Compile Include="LoginTests.cs" />
55+
<Compile Include="Login.cs" />
5056
<Compile Include="Properties\AssemblyInfo.cs" />
57+
<Compile Include="TestsBase.cs" />
58+
<Compile Include="Upload.cs" />
5159
</ItemGroup>
5260
<ItemGroup>
5361
<None Include="..\key.snk">

MegaApiClient.Tests/TestsBase.cs

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using NUnit.Framework;
5+
6+
namespace CG.Web.MegaApiClient.Tests
7+
{
8+
public abstract class TestsBase
9+
{
10+
private const string Username = "[email protected]";
11+
private const string Password = "megaapiclient";
12+
13+
private readonly Options _options;
14+
15+
protected MegaApiClient Client;
16+
17+
[Flags]
18+
protected enum Options
19+
{
20+
None = 0,
21+
Login = 1 << 0,
22+
LoginAuthenticated = Login | 1 << 1,
23+
LoginAnonymous = Login | 1 << 2,
24+
Clean = LoginAuthenticated | 1 << 3
25+
}
26+
27+
protected TestsBase(Options options)
28+
{
29+
this._options = options;
30+
}
31+
32+
[SetUp]
33+
public void Setup()
34+
{
35+
this.Client = new MegaApiClient();
36+
if ((this._options & Options.LoginAuthenticated) == Options.LoginAuthenticated)
37+
{
38+
this.Client.Login(Username, Password);
39+
}
40+
41+
if ((this._options & Options.LoginAnonymous) == Options.LoginAnonymous)
42+
{
43+
this.Client.LoginAnonymous();
44+
}
45+
46+
if ((this._options & Options.Clean) == Options.Clean)
47+
{
48+
IEnumerable<INode> nodes = this.Client.GetNodes().ToArray();
49+
INode root = nodes.Single(x => x.Type == NodeType.Root);
50+
INode inbox = nodes.Single(x => x.Type == NodeType.Inbox);
51+
INode trash = nodes.Single(x => x.Type == NodeType.Trash);
52+
IEnumerable<INode> nodesToRemove = nodes.Where(x => x.ParentId == root.Id || x.ParentId == inbox.Id || x.ParentId == trash.Id);
53+
foreach (INode node in nodesToRemove)
54+
{
55+
this.Client.Delete(node, false);
56+
}
57+
}
58+
}
59+
60+
[TearDown]
61+
public void Teardown()
62+
{
63+
if ((this._options & Options.Login) == Options.Login)
64+
{
65+
this.Client.Logout();
66+
}
67+
}
68+
69+
protected IEnumerable<TestCaseData> GetCredentials()
70+
{
71+
yield return new TestCaseData(Username, Password);
72+
}
73+
}
74+
}

MegaApiClient.Tests/Upload.cs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using Moq;
6+
using NUnit.Framework;
7+
8+
namespace CG.Web.MegaApiClient.Tests
9+
{
10+
[TestFixture]
11+
public class Upload : TestsBase
12+
{
13+
public Upload() : base(Options.LoginAuthenticated | Options.Clean)
14+
{
15+
}
16+
17+
[TestCaseSource("GetInvalidUploadStreamParameters")]
18+
public void UploadStream_InvalidParameters_Throws(Stream stream, string name, INode parent)
19+
{
20+
Assert.That(
21+
() => this.Client.Upload(stream, name, parent),
22+
Throws.TypeOf<ArgumentNullException>());
23+
}
24+
[TestCase(NodeType.Root)]
25+
[TestCase(NodeType.Inbox)]
26+
[TestCase(NodeType.Trash)]
27+
public void UploadStream_Succeeds(NodeType parent)
28+
{
29+
byte[] data = new byte[123];
30+
Random r = new Random();
31+
r.NextBytes(data);
32+
33+
INode root = this.Client.GetNodes().First(x => x.Type == parent);
34+
35+
INode node;
36+
using (Stream stream = new MemoryStream(data))
37+
{
38+
node = this.Client.Upload(stream, "test", root);
39+
}
40+
41+
Assert.That(node, Is.Not.Null);
42+
Assert.That(node.Type, Is.EqualTo(NodeType.File));
43+
Assert.That(node.ParentId, Is.EqualTo(root.Id));
44+
Assert.That(node.Name, Is.EqualTo("test"));
45+
Assert.That(node.Size, Is.EqualTo(data.Length));
46+
Assert.That(node, Is.EqualTo(this.Client.GetNodes().Single(x => x.Id == node.Id)));
47+
}
48+
49+
private IEnumerable<TestCaseData> GetInvalidUploadStreamParameters()
50+
{
51+
INode node = Mock.Of<INode>(x => x.Type == NodeType.Root);
52+
Stream stream = new MemoryStream();
53+
54+
yield return new TestCaseData(null, null, null);
55+
yield return new TestCaseData(null, null, node);
56+
yield return new TestCaseData(null, "", null);
57+
yield return new TestCaseData(null, "", node);
58+
yield return new TestCaseData(null, "name", null);
59+
yield return new TestCaseData(null, "name", node);
60+
yield return new TestCaseData(stream, null, null);
61+
yield return new TestCaseData(stream, null, node);
62+
yield return new TestCaseData(stream, "", null);
63+
yield return new TestCaseData(stream, "", node);
64+
yield return new TestCaseData(stream, "name", null);
65+
}
66+
}
67+
}

MegaApiClient.Tests/packages.config

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Castle.Core" version="3.1.0" targetFramework="net35" />
4+
<package id="Moq" version="4.2.1409.1722" targetFramework="net35" />
35
<package id="NUnit" version="2.6.4" targetFramework="net35" />
46
</packages>

0 commit comments

Comments
 (0)