11
22
33using System ;
4+ using System . Collections . Generic ;
5+ using System . IO ;
6+ using System . Text ;
7+ using Moq ;
8+ using Newtonsoft . Json ;
49using NUnit . Framework ;
510
611namespace 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}
0 commit comments