mirror of
https://github.com/sochix/TLSharp.git
synced 2026-04-05 14:37:04 +00:00
Merge branch 'pr/198'
Conflicts: TLSharp.Tests/TLSharpTests.cs
This commit is contained in:
commit
148828edc1
5 changed files with 217 additions and 36 deletions
|
|
@ -1,6 +1,8 @@
|
|||
using System.Configuration;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using TLSharp.Core;
|
||||
|
|
@ -226,17 +228,17 @@ namespace TLSharp.Tests
|
|||
Assert.AreEqual(1, hist.Count);
|
||||
|
||||
var message = (MessageConstructor) hist[0];
|
||||
Assert.AreEqual(typeof(MessageMediaPhotoConstructor), message.media.GetType());
|
||||
Assert.AreEqual(typeof (MessageMediaPhotoConstructor), message.media.GetType());
|
||||
|
||||
var media = (MessageMediaPhotoConstructor) message.media;
|
||||
Assert.AreEqual(typeof(PhotoConstructor), media.photo.GetType());
|
||||
Assert.AreEqual(typeof (PhotoConstructor), media.photo.GetType());
|
||||
|
||||
var photo = (PhotoConstructor) media.photo;
|
||||
Assert.AreEqual(3, photo.sizes.Count);
|
||||
Assert.AreEqual(typeof(PhotoSizeConstructor), photo.sizes[2].GetType());
|
||||
Assert.AreEqual(typeof (PhotoSizeConstructor), photo.sizes[2].GetType());
|
||||
|
||||
var photoSize = (PhotoSizeConstructor) photo.sizes[2];
|
||||
Assert.AreEqual(typeof(FileLocationConstructor), photoSize.location.GetType());
|
||||
Assert.AreEqual(typeof (FileLocationConstructor), photoSize.location.GetType());
|
||||
|
||||
var fileLocation = (FileLocationConstructor) photoSize.location;
|
||||
var file = await client.GetFile(fileLocation.volume_id, fileLocation.local_id, fileLocation.secret, 0, photoSize.size + 1024);
|
||||
|
|
@ -244,11 +246,11 @@ namespace TLSharp.Tests
|
|||
byte[] bytes = file.Item2;
|
||||
|
||||
string name = "../../data/get_file.";
|
||||
if (type.GetType() == typeof(Storage_fileJpegConstructor))
|
||||
if (type.GetType() == typeof (Storage_fileJpegConstructor))
|
||||
name += "jpg";
|
||||
else if (type.GetType() == typeof(Storage_fileGifConstructor))
|
||||
else if (type.GetType() == typeof (Storage_fileGifConstructor))
|
||||
name += "gif";
|
||||
else if (type.GetType() == typeof(Storage_filePngConstructor))
|
||||
else if (type.GetType() == typeof (Storage_filePngConstructor))
|
||||
name += "png";
|
||||
|
||||
using (var fileStream = new FileStream(name, FileMode.Create, FileAccess.Write))
|
||||
|
|
@ -294,5 +296,85 @@ namespace TLSharp.Tests
|
|||
|
||||
Assert.IsNotNull(userFull);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task CreateChatRequest()
|
||||
{
|
||||
var client = await InitializeClient();
|
||||
|
||||
var chatName = Guid.NewGuid().ToString();
|
||||
var statedMessage = await client.CreateChat(chatName, new List<string> {NumberToSendMessage});
|
||||
|
||||
var createdChat = GetChatFromStatedMessage(statedMessage);
|
||||
|
||||
Assert.AreEqual(chatName, createdChat.title);
|
||||
Assert.AreEqual(2, createdChat.participants_count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task AddChatUserRequest()
|
||||
{
|
||||
var client = await InitializeClient();
|
||||
|
||||
var chatName = Guid.NewGuid().ToString();
|
||||
var statedMessageAfterCreation = await client.CreateChat(chatName, new List<string> { NumberToSendMessage });
|
||||
|
||||
var createdChat = GetChatFromStatedMessage(statedMessageAfterCreation);
|
||||
|
||||
var addUserId = await client.ImportContactByPhoneNumber("380685004559");
|
||||
|
||||
var statedMessageAfterAddUser = await client.AddChatUser(createdChat.id, addUserId.Value);
|
||||
var modifiedChat = GetChatFromStatedMessage(statedMessageAfterAddUser);
|
||||
|
||||
Assert.AreEqual(createdChat.id, modifiedChat.id);
|
||||
Assert.AreEqual(3, modifiedChat.participants_count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task LeaveChatRequest()
|
||||
{
|
||||
var client = await InitializeClient();
|
||||
|
||||
var chatName = Guid.NewGuid().ToString();
|
||||
var statedMessageAfterCreation = await client.CreateChat(chatName, new List<string> { NumberToSendMessage });
|
||||
|
||||
var createdChat = GetChatFromStatedMessage(statedMessageAfterCreation);
|
||||
|
||||
var statedMessageAfterLeave = await client.LeaveChat(createdChat.id);
|
||||
var modifiedChat = GetChatFromStatedMessage(statedMessageAfterLeave);
|
||||
|
||||
Assert.AreEqual(createdChat.id, modifiedChat.id);
|
||||
Assert.AreEqual(1, modifiedChat.participants_count);
|
||||
}
|
||||
|
||||
private ChatConstructor GetChatFromStatedMessage(Messages_statedMessageConstructor message)
|
||||
{
|
||||
var serviceMessage = message.message as MessageServiceConstructor;
|
||||
var peerChat = serviceMessage.to_id as PeerChatConstructor;
|
||||
|
||||
var createdChatId = peerChat.chat_id;
|
||||
return message.chats.OfType<ChatConstructor>().Single(c => c.id == createdChatId);
|
||||
}
|
||||
|
||||
private async Task<TelegramClient> InitializeClient()
|
||||
{
|
||||
var store = new FileSessionStore();
|
||||
var client = new TelegramClient(store, "session", apiId, apiHash);
|
||||
await client.Connect();
|
||||
|
||||
if (!client.IsUserAuthorized())
|
||||
{
|
||||
var hash = await client.SendCodeRequest(NumberToAuthenticate);
|
||||
|
||||
var code = ""; // you can change code in debugger
|
||||
Debugger.Break();
|
||||
|
||||
await client.MakeAuth(NumberToAuthenticate, hash, code);
|
||||
}
|
||||
|
||||
Assert.IsTrue(client.IsUserAuthorized());
|
||||
|
||||
return client;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue