This repository has been archived by the owner on Dec 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 380
/
TLSharpTests.cs
381 lines (294 loc) · 13.6 KB
/
TLSharpTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using TeleSharp.TL;
using TeleSharp.TL.Messages;
using TLSharp.Core;
using TLSharp.Core.Exceptions;
using TLSharp.Core.Network;
using TLSharp.Core.Network.Exceptions;
using TLSharp.Core.Utils;
namespace TLSharp.Tests
{
public class TLSharpTests
{
private string NumberToSendMessage { get; set; }
private string NumberToAuthenticate { get; set; }
private string CodeToAuthenticate { get; set; }
private string PasswordToAuthenticate { 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; }
class Assert
{
static internal void IsNotNull(object obj)
{
IsNotNullHanlder(obj);
}
static internal void IsTrue(bool cond)
{
IsTrueHandler(cond);
}
}
internal static Action<object> IsNotNullHanlder;
internal static Action<bool> IsTrueHandler;
protected void Init(Action<object> notNullHandler, Action<bool> trueHandler)
{
IsNotNullHanlder = notNullHandler;
IsTrueHandler = trueHandler;
// Setup your API settings and phone numbers in app.config
GatherTestConfiguration();
}
private TelegramClient NewClient()
{
try
{
return new TelegramClient(ApiId, ApiHash);
}
catch (MissingApiConfigurationException ex)
{
throw new Exception($"Please add your API settings to the `app.config` file. (More info: {MissingApiConfigurationException.InfoUrl})",
ex);
}
}
private void GatherTestConfiguration()
{
string appConfigMsgWarning = "{0} not configured in app.config! Some tests may fail.";
ApiHash = ConfigurationManager.AppSettings[nameof(ApiHash)];
if (string.IsNullOrEmpty(ApiHash))
Debug.WriteLine(appConfigMsgWarning, nameof(ApiHash));
var apiId = ConfigurationManager.AppSettings[nameof(ApiId)];
if (string.IsNullOrEmpty(apiId))
Debug.WriteLine(appConfigMsgWarning, nameof(ApiId));
else
ApiId = int.Parse(apiId);
NumberToAuthenticate = ConfigurationManager.AppSettings[nameof(NumberToAuthenticate)];
if (string.IsNullOrEmpty(NumberToAuthenticate))
Debug.WriteLine(appConfigMsgWarning, nameof(NumberToAuthenticate));
CodeToAuthenticate = ConfigurationManager.AppSettings[nameof(CodeToAuthenticate)];
if (string.IsNullOrEmpty(CodeToAuthenticate))
Debug.WriteLine(appConfigMsgWarning, nameof(CodeToAuthenticate));
PasswordToAuthenticate = ConfigurationManager.AppSettings[nameof(PasswordToAuthenticate)];
if (string.IsNullOrEmpty(PasswordToAuthenticate))
Debug.WriteLine(appConfigMsgWarning, nameof(PasswordToAuthenticate));
NotRegisteredNumberToSignUp = ConfigurationManager.AppSettings[nameof(NotRegisteredNumberToSignUp)];
if (string.IsNullOrEmpty(NotRegisteredNumberToSignUp))
Debug.WriteLine(appConfigMsgWarning, nameof(NotRegisteredNumberToSignUp));
UserNameToSendMessage = ConfigurationManager.AppSettings[nameof(UserNameToSendMessage)];
if (string.IsNullOrEmpty(UserNameToSendMessage))
Debug.WriteLine(appConfigMsgWarning, nameof(UserNameToSendMessage));
NumberToGetUserFull = ConfigurationManager.AppSettings[nameof(NumberToGetUserFull)];
if (string.IsNullOrEmpty(NumberToGetUserFull))
Debug.WriteLine(appConfigMsgWarning, nameof(NumberToGetUserFull));
NumberToAddToChat = ConfigurationManager.AppSettings[nameof(NumberToAddToChat)];
if (string.IsNullOrEmpty(NumberToAddToChat))
Debug.WriteLine(appConfigMsgWarning, nameof(NumberToAddToChat));
}
public virtual async Task AuthUser()
{
var client = NewClient();
await client.ConnectAsync();
var hash = await client.SendCodeRequestAsync(NumberToAuthenticate);
var code = CodeToAuthenticate; // you can change code in debugger too
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 (CloudPasswordNeededException ex)
{
var passwordSetting = await client.GetPasswordSetting();
var password = PasswordToAuthenticate;
user = await client.MakeAuthWithPasswordAsync(passwordSetting, password);
}
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());
}
public virtual async Task SendMessageTest()
{
NumberToSendMessage = ConfigurationManager.AppSettings[nameof(NumberToSendMessage)];
if (string.IsNullOrWhiteSpace(NumberToSendMessage))
throw new Exception($"Please fill the '{nameof(NumberToSendMessage)}' setting in app.config file first");
// this is because the contacts in the address come without the "+" prefix
var normalizedNumber = NumberToSendMessage.StartsWith("+") ?
NumberToSendMessage.Substring(1, NumberToSendMessage.Length - 1) :
NumberToSendMessage;
var client = NewClient();
await client.ConnectAsync();
var result = await client.GetContactsAsync();
var user = result.Users
.OfType<TLUser>()
.FirstOrDefault(x => x.Phone == normalizedNumber);
if (user == null)
{
throw new System.Exception("Number was not found in Contacts List of user: " + NumberToSendMessage);
}
await client.SendTypingAsync(new TLInputPeerUser() { UserId = user.Id });
Thread.Sleep(3000);
await client.SendMessageAsync(new TLInputPeerUser() { UserId = user.Id }, "TEST");
}
public virtual async Task SendMessageToChannelTest()
{
var client = NewClient();
await client.ConnectAsync();
var dialogs = (TLDialogs) await client.GetUserDialogsAsync();
var chat = dialogs.Chats
.OfType<TLChannel>()
.FirstOrDefault(c => c.Title == "TestGroup");
await client.SendMessageAsync(new TLInputPeerChannel() { ChannelId = chat.Id, AccessHash = chat.AccessHash.Value }, "TEST MSG");
}
public virtual async Task SendPhotoToContactTest()
{
var client = NewClient();
await client.ConnectAsync();
var result = await client.GetContactsAsync();
var user = result.Users
.OfType<TLUser>()
.FirstOrDefault(x => x.Phone == NumberToSendMessage);
var fileResult = (TLInputFile)await client.UploadFile("cat.jpg", new StreamReader("data/cat.jpg"));
await client.SendUploadedPhoto(new TLInputPeerUser() { UserId = user.Id }, fileResult, "kitty");
}
public virtual async Task SendBigFileToContactTest()
{
var client = NewClient();
await client.ConnectAsync();
var result = await client.GetContactsAsync();
var user = result.Users
.OfType<TLUser>()
.FirstOrDefault(x => x.Phone == NumberToSendMessage);
var fileResult = (TLInputFileBig)await client.UploadFile("some.zip", new StreamReader("<some big file path>"));
await client.SendUploadedDocument(
new TLInputPeerUser() { UserId = user.Id },
fileResult,
"some zips",
"application/zip",
new TLVector<TLAbsDocumentAttribute>());
}
public virtual async Task DownloadFileFromContactTest()
{
var client = NewClient();
await client.ConnectAsync();
var result = await client.GetContactsAsync();
var user = result.Users
.OfType<TLUser>()
.FirstOrDefault(x => x.Phone == NumberToSendMessage);
var inputPeer = new TLInputPeerUser() { UserId = user.Id };
var res = await client.SendRequestAsync<TLMessagesSlice>(new TLRequestGetHistory() { Peer = inputPeer });
var document = res.Messages
.OfType<TLMessage>()
.Where(m => m.Media != null)
.Select(m => m.Media)
.OfType<TLMessageMediaDocument>()
.Select(md => md.Document)
.OfType<TLDocument>()
.First();
var resFile = await client.GetFile(
new TLInputDocumentFileLocation()
{
AccessHash = document.AccessHash,
Id = document.Id,
Version = document.Version
},
document.Size);
Assert.IsTrue(resFile.Bytes.Length > 0);
}
public virtual async Task DownloadFileFromWrongLocationTest()
{
var client = NewClient();
await client.ConnectAsync();
var result = await client.GetContactsAsync();
var user = result.Users
.OfType<TLUser>()
.FirstOrDefault(x => x.Id == 5880094);
var photo = ((TLUserProfilePhoto)user.Photo);
var photoLocation = (TLFileLocation) photo.PhotoBig;
var resFile = await client.GetFile(new TLInputFileLocation()
{
LocalId = photoLocation.LocalId,
Secret = photoLocation.Secret,
VolumeId = photoLocation.VolumeId
}, 1024);
var res = await client.GetUserDialogsAsync();
Assert.IsTrue(resFile.Bytes.Length > 0);
}
public virtual async Task SignUpNewUser()
{
var client = NewClient();
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);
}
public virtual async Task CheckPhones()
{
var client = NewClient();
await client.ConnectAsync();
var result = await client.IsPhoneRegisteredAsync(NumberToAuthenticate);
Assert.IsTrue(result);
}
public virtual async Task FloodExceptionShouldNotCauseCannotReadPackageLengthError()
{
for (int i = 0; i < 50; i++)
{
try
{
await CheckPhones();
}
catch (FloodException floodException)
{
Console.WriteLine($"FLOODEXCEPTION: {floodException}");
Thread.Sleep(floodException.TimeToWait);
}
}
}
public virtual async Task SendMessageByUserNameTest()
{
UserNameToSendMessage = ConfigurationManager.AppSettings[nameof(UserNameToSendMessage)];
if (string.IsNullOrWhiteSpace(UserNameToSendMessage))
throw new Exception($"Please fill the '{nameof(UserNameToSendMessage)}' setting in app.config file first");
var client = NewClient();
await client.ConnectAsync();
var result = await client.SearchUserAsync(UserNameToSendMessage);
var user = result.Users
.Where(x => x.GetType() == typeof(TLUser))
.OfType<TLUser>()
.FirstOrDefault(x => x.Username == UserNameToSendMessage.TrimStart('@'));
if (user == null)
{
var contacts = await client.GetContactsAsync();
user = contacts.Users
.Where(x => x.GetType() == typeof(TLUser))
.OfType<TLUser>()
.FirstOrDefault(x => x.Username == UserNameToSendMessage.TrimStart('@'));
}
if (user == null)
{
throw new System.Exception("Username was not found: " + UserNameToSendMessage);
}
await client.SendTypingAsync(new TLInputPeerUser() { UserId = user.Id });
Thread.Sleep(3000);
await client.SendMessageAsync(new TLInputPeerUser() { UserId = user.Id }, "TEST");
}
}
}