TLSharp/TLSharp.Tests/TLSharpTests.cs
Andres G. Aragoneses bbc84f0748 Tests: put API settings also in app.config
For these tests to pass you not only needed to change
the app.config file but the .cs file as well with your
API settings (Hash & ID).

So let's put everything of them in the app.config so
it's easier to discover.
2016-10-23 18:11:26 +08:00

153 lines
5.4 KiB
C#

using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TeleSharp.TL;
using TLSharp.Core;
namespace TLSharp.Tests
{
[TestClass]
public class TLSharpTests
{
private string NumberToSendMessage { get; set; }
private string NumberToAuthenticate { get; set; }
private string NotRegisteredNumberToSignUp { get; set; }
private string UserNameToSendMessage { get; set; }
private string NumberToGetUserFull { get; set; }
private string NumberToAddToChat { get; set; }
private string ApiHash { get; set; }
private int ApiId { get; set; }
[TestInitialize]
public void Init()
{
// Setup your API settings and phone numbers in app.config
ApiHash = ConfigurationManager.AppSettings[nameof(ApiHash)];
if (string.IsNullOrEmpty(ApiHash))
Debug.WriteLine("ApiHash not configured in app.config! Some tests may fail.");
var apiId = ConfigurationManager.AppSettings[nameof(ApiId)];
if (string.IsNullOrEmpty(apiId))
Debug.WriteLine("ApiId not configured in app.config! Some tests may fail.");
ApiId = int.Parse(apiId);
NumberToAuthenticate = ConfigurationManager.AppSettings[nameof(NumberToAuthenticate)];
if (string.IsNullOrEmpty(NumberToAuthenticate))
Debug.WriteLine("NumberToAuthenticate 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.");
NumberToSendMessage = ConfigurationManager.AppSettings[nameof(NumberToSendMessage)];
if (string.IsNullOrEmpty(NumberToSendMessage))
Debug.WriteLine("NumberToSendMessage not configured in app.config! Some tests may fail.");
UserNameToSendMessage = ConfigurationManager.AppSettings[nameof(UserNameToSendMessage)];
if (string.IsNullOrEmpty(UserNameToSendMessage))
Debug.WriteLine("UserNameToSendMessage not configured in app.config! Some tests may fail.");
NumberToGetUserFull = ConfigurationManager.AppSettings[nameof(NumberToGetUserFull)];
if (string.IsNullOrEmpty(NumberToGetUserFull))
Debug.WriteLine("NumberToGetUserFull not configured in app.config! Some tests may fail.");
NumberToAddToChat = ConfigurationManager.AppSettings[nameof(NumberToAddToChat)];
if (string.IsNullOrEmpty(NumberToAddToChat))
Debug.WriteLine("NumberToAddToChat not configured in app.config! Some tests may fail.");
}
[TestMethod]
public async Task AuthUser()
{
var client = new TelegramClient(ApiId, ApiHash);
await client.ConnectAsync();
var hash = await client.SendCodeRequestAsync(NumberToAuthenticate);
var code = "93463"; // you can change code in debugger
var user = await client.MakeAuthAsync(NumberToAuthenticate, hash, code);
Assert.IsNotNull(user);
Assert.IsTrue(client.IsUserAuthorized());
}
[TestMethod]
public async Task SendMessageTest()
{
var client = new TelegramClient(ApiId, ApiHash);
await client.ConnectAsync();
var result = await client.GetContactsAsync();
var user = result.users.lists
.Where(x => x.GetType() == typeof (TLUser))
.Cast<TLUser>()
.FirstOrDefault(x => x.phone == NumberToSendMessage);
await client.SendTypingAsync(new TLInputPeerUser() {user_id = user.id});
Thread.Sleep(3000);
await client.SendMessageAsync(new TLInputPeerUser() {user_id = user.id}, "TEST");
}
[TestMethod]
public async Task SendMessageToChannelTest()
{
var client = new TelegramClient(ApiId, ApiHash);
await client.ConnectAsync();
var dialogs = await client.GetUserDialogsAsync();
var chat = dialogs.chats.lists
.Where(c => c.GetType() == typeof(TLChannel))
.Cast<TLChannel>()
.FirstOrDefault(c => c.title == "TestGroup");
await client.SendMessageAsync(new TLInputPeerChannel() { channel_id = chat.id, access_hash = chat.access_hash.Value }, "TEST MSG");
}
[TestMethod]
public async Task SignUpNewUser()
{
var client = new TelegramClient(ApiId, ApiHash);
await client.ConnectAsync();
var hash = await client.SendCodeRequestAsync(NotRegisteredNumberToSignUp);
var code = "";
var registeredUser = await client.SignUpAsync(NotRegisteredNumberToSignUp, hash, code, "TLSharp", "User");
Assert.IsNotNull(registeredUser);
Assert.IsTrue(client.IsUserAuthorized());
var loggedInUser = await client.MakeAuthAsync(NotRegisteredNumberToSignUp, hash, code);
Assert.IsNotNull(loggedInUser);
}
[TestMethod]
public async Task CheckPhones()
{
var client = new TelegramClient(ApiId, ApiHash);
await client.ConnectAsync();
var result = await client.IsPhoneRegisteredAsync(NumberToAuthenticate);
Assert.IsTrue(result);
}
}
}