1
1
2
2
3
3
using System ;
4
+ using System . Collections . Generic ;
5
+ using System . IO ;
6
+ using System . Text ;
7
+ using Moq ;
8
+ using Newtonsoft . Json ;
4
9
using NUnit . Framework ;
5
10
6
11
namespace CG . Web . MegaApiClient . Tests
@@ -13,45 +18,52 @@ public Login()
13
18
{
14
19
}
15
20
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 )
23
32
{
24
33
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" ) ) ;
27
38
}
28
39
29
40
[ TestCase ( "username" , "password" , ApiResultCode . BadArguments ) ]
30
41
[ 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 )
32
43
{
33
44
Assert . That (
34
- ( ) => this . Client . Login ( username , password ) ,
45
+ ( ) => this . Client . Login ( email , password ) ,
35
46
Throws . TypeOf < ApiException > ( )
36
- . With . Property ( " ApiResultCode" ) . EqualTo ( expectedErrorCode ) ) ;
47
+ . With . Property < ApiException > ( x => x . ApiResultCode ) . EqualTo ( expectedErrorCode ) ) ;
37
48
}
38
49
39
50
[ TestCaseSource ( "GetCredentials" ) ]
40
- public void Login_ValidCredentials_Succeeds ( string username , string password )
51
+ public void Login_ValidCredentials_Succeeds ( string email , string password )
41
52
{
42
53
Assert . That (
43
- ( ) => this . Client . Login ( username , password ) ,
54
+ ( ) => this . Client . Login ( email , password ) ,
44
55
Throws . Nothing ) ;
45
56
}
46
57
47
58
[ TestCaseSource ( "GetCredentials" ) ]
48
- public void LoginTwice_ValidCredentials_Throws ( string username , string password )
59
+ public void LoginTwice_ValidCredentials_Throws ( string email , string password )
49
60
{
50
- this . Client . Login ( username , password ) ;
61
+ this . Client . Login ( email , password ) ;
51
62
52
63
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" ) ) ;
55
67
}
56
68
57
69
[ Test ]
@@ -69,25 +81,129 @@ public void LoginAnonymousTwice_Throws()
69
81
70
82
Assert . That (
71
83
( ) => this . Client . LoginAnonymous ( ) ,
72
- Throws . TypeOf < NotSupportedException > ( ) ) ;
84
+ Throws . TypeOf < NotSupportedException > ( )
85
+ . With . Message . EqualTo ( "Already logged in" ) ) ;
73
86
}
74
87
75
88
[ TestCaseSource ( "GetCredentials" ) ]
76
- public void LogoutAfterLogin_Succeeds ( string username , string password )
89
+ public void LogoutAfterLogin_Succeeds ( string email , string password )
77
90
{
78
- this . Client . Login ( username , password ) ;
79
-
91
+ this . Client . Login ( email , password ) ;
92
+
80
93
Assert . That (
81
94
( ) => this . Client . Logout ( ) ,
82
95
Throws . Nothing ) ;
83
96
}
84
97
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
+
85
111
[ Test ]
86
112
public void LogoutWithoutLogin_Throws ( )
87
113
{
88
114
Assert . That (
89
115
( ) => 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 ) ) ) ;
91
207
}
92
208
}
93
209
}
0 commit comments