Friendlier exceptions for Auth testing

This change also lets you write the auth code in the
app.config file without the need to recompile/debug.
This commit is contained in:
Andres G. Aragoneses 2016-10-31 00:15:10 +08:00
parent b0b58be25e
commit f062c0a7a1
4 changed files with 32 additions and 2 deletions

View file

@ -293,6 +293,10 @@ namespace TLSharp.Core.Network
var dcIdx = int.Parse(resultString);
throw new UserMigrationException(dcIdx);
}
else if (errorMessage == "PHONE_CODE_INVALID")
{
throw new InvalidPhoneCodeException("The numeric code used to authenticate does not match the numeric code sent by SMS/Telegram");
}
else
{
throw new InvalidOperationException(errorMessage);

View file

@ -294,4 +294,9 @@ namespace TLSharp.Core
{
}
}
public class InvalidPhoneCodeException : Exception
{
internal InvalidPhoneCodeException(string msg) : base(msg) { }
}
}

View file

@ -24,6 +24,8 @@ namespace TLSharp.Tests
private string NumberToAuthenticate { get; set; }
private string CodeToAuthenticate { get; set; }
private string NotRegisteredNumberToSignUp { get; set; }
private string UserNameToSendMessage { get; set; }
@ -72,6 +74,10 @@ namespace TLSharp.Tests
if (string.IsNullOrEmpty(NumberToAuthenticate))
Debug.WriteLine("NumberToAuthenticate not configured in app.config! Some tests may fail.");
CodeToAuthenticate = ConfigurationManager.AppSettings[nameof(CodeToAuthenticate)];
if (string.IsNullOrEmpty(CodeToAuthenticate))
Debug.WriteLine("CpdeToAuthenticate not configured in app.config! Some tests may fail.");
NotRegisteredNumberToSignUp = ConfigurationManager.AppSettings[nameof(NotRegisteredNumberToSignUp)];
if (string.IsNullOrEmpty(NotRegisteredNumberToSignUp))
Debug.WriteLine("NotRegisteredNumberToSignUp not configured in app.config! Some tests may fail.");
@ -101,9 +107,23 @@ namespace TLSharp.Tests
await client.ConnectAsync();
var hash = await client.SendCodeRequestAsync(NumberToAuthenticate);
var code = "93463"; // you can change code in debugger
var code = CodeToAuthenticate; // you can change code in debugger too
var user = await client.MakeAuthAsync(NumberToAuthenticate, hash, code);
if (String.IsNullOrWhiteSpace(code))
{
throw new Exception("CodeToAuthenticate is empty in the app.config file, fill it with the code you just got now by SMS/Telegram");
}
TLUser user = null;
try
{
user = await client.MakeAuthAsync(NumberToAuthenticate, hash, code);
}
catch (InvalidPhoneCodeException ex)
{
throw new Exception("CodeToAuthenticate is wrong in the app.config file, fill it with the code you just got now by SMS/Telegram",
ex);
}
Assert.IsNotNull(user);
Assert.IsTrue(client.IsUserAuthorized());

View file

@ -4,6 +4,7 @@
<add key="ApiHash" value="" />
<add key="ApiId" value="" />
<add key="NumberToAuthenticate" value="" />
<add key="CodeToAuthenticate" value="" />
<add key="NotRegisteredNumberToSignUp" value=""/>
<add key="NumberToSendMessage" value=""/>
<add key="UserNameToSendMessage" value=""/>