From 659da6acc8747d53dde75448a22a6a0a0783c5fc Mon Sep 17 00:00:00 2001 From: oneRain Date: Fri, 24 Apr 2020 17:42:45 +0800 Subject: [PATCH] * Client.cs: * Conversation.cs: * ConversationQuery.cs: * LocalSignatureFactory.cs: * Message.cs: test --- Test/Realtime.Test/Client.cs | 132 +++++++++++++++++++- Test/Realtime.Test/Conversation.cs | 131 ++++++++----------- Test/Realtime.Test/ConversationQuery.cs | 46 ++++++- Test/Realtime.Test/LocalSignatureFactory.cs | 94 +++++++++++++- Test/Realtime.Test/Message.cs | 61 ++++++--- 5 files changed, 362 insertions(+), 102 deletions(-) diff --git a/Test/Realtime.Test/Client.cs b/Test/Realtime.Test/Client.cs index 07823d2..9669bea 100644 --- a/Test/Realtime.Test/Client.cs +++ b/Test/Realtime.Test/Client.cs @@ -1,7 +1,135 @@ -using System; +using NUnit.Framework; +using System; +using System.Threading.Tasks; +using System.Collections.ObjectModel; +using LeanCloud; +using LeanCloud.Common; +using LeanCloud.Realtime; +using LeanCloud.Storage; + +using static NUnit.Framework.TestContext; + namespace Realtime.Test { public class Client { - public Client() { + [SetUp] + public void SetUp() { + LCLogger.LogDelegate += Utils.Print; + LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com"); + } + + [TearDown] + public void TearDown() { + LCLogger.LogDelegate -= Utils.Print; + } + + [Test] + public async Task OpenAndClose() { + LCIMClient client = new LCIMClient("c1"); + await client.Open(); + await client.Close(); + } + + [Test] + public async Task OpenAndCloseByLCUser() { + LCUser user = await LCUser.Login("hello", "world"); + LCIMClient client = new LCIMClient(user); + await client.Open(); + await client.Close(); + } + + [Test] + public async Task CreateConversation() { + TaskCompletionSource tcs = new TaskCompletionSource(); + + string clientId = Guid.NewGuid().ToString(); + LCIMClient client = new LCIMClient(clientId); + + await client.Open(); + + client.OnInvited = (conv, initBy) => { + WriteLine($"on invited: {initBy}"); + WriteLine(conv.CreatorId); + }; + + client.OnMembersJoined = (conv, memberList, initBy) => { + WriteLine($"on members joined: {initBy}"); + foreach (string memberId in conv.MemberIds) { + WriteLine(memberId); + } + tcs.SetResult(null); + }; + + string name = Guid.NewGuid().ToString(); + LCIMConversation conversation = await client.CreateConversation(new string[] { "world" }, name: name, unique: false); + + await tcs.Task; + } + + [Test] + public async Task CreateChatRoom() { + TaskCompletionSource tcs = new TaskCompletionSource(); + + string clientId = Guid.NewGuid().ToString(); + LCIMClient client = new LCIMClient(clientId); + + await client.Open(); + + client.OnInvited = (conv, initBy) => { + WriteLine($"on invited: {initBy}"); + }; + + string name = Guid.NewGuid().ToString(); + LCIMConversation conversation = await client.CreateChatRoom(name); + + string visitorId = Guid.NewGuid().ToString(); + LCIMClient visitor = new LCIMClient(visitorId); + + await visitor.Open(); + visitor.OnInvited = async (conv, initBy) => { + WriteLine($"on invited: {visitor.Id}"); + LCIMTextMessage textMessage = new LCIMTextMessage("hello, world"); + await conversation.Send(textMessage); + tcs.SetResult(null); + }; + + LCIMChatRoom chatRoom = await visitor.GetConversation(conversation.Id) as LCIMChatRoom; + await chatRoom.Join(); + + int count = await chatRoom.GetMembersCount(); + + ReadOnlyCollection onlineMembers = await chatRoom.GetOnlineMembers(); + Assert.GreaterOrEqual(onlineMembers.Count, 1); + foreach (string memberId in onlineMembers) { + WriteLine($"{memberId} online"); + } + + await client.Close(); + await visitor.Close(); + + await tcs.Task; + } + + [Test] + public async Task CreateTemporaryConversation() { + TaskCompletionSource tcs = new TaskCompletionSource(); + + string clientId = Guid.NewGuid().ToString(); + LCIMClient client = new LCIMClient(clientId); + + await client.Open(); + + client.OnInvited = (conv, initBy) => { + WriteLine($"on invited: {initBy}"); + }; + + client.OnMembersJoined = (conv, memberList, initBy) => { + WriteLine($"on members joined: {initBy}"); + tcs.SetResult(null); + }; + + await client.CreateTemporaryConversation(new string[] { "world" }); + + await tcs.Task; } } } diff --git a/Test/Realtime.Test/Conversation.cs b/Test/Realtime.Test/Conversation.cs index 03c0c90..e2b6067 100644 --- a/Test/Realtime.Test/Conversation.cs +++ b/Test/Realtime.Test/Conversation.cs @@ -8,115 +8,88 @@ using LeanCloud.Realtime; namespace Realtime.Test { public class Conversation { + private LCIMClient c1; + private LCIMClient c2; + private LCIMConversation conversation; + [SetUp] - public void SetUp() { + public async Task SetUp() { LCLogger.LogDelegate += Utils.Print; LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com"); + c1 = new LCIMClient(Guid.NewGuid().ToString()); + await c1.Open(); + c2 = new LCIMClient(Guid.NewGuid().ToString()); + await c2.Open(); + conversation = await c1.CreateConversation(new string[] { "lean", "cloud" }); } [TearDown] - public void TearDown() { + public async Task TearDown() { + await c1.Close(); + await c2.Close(); LCLogger.LogDelegate -= Utils.Print; } [Test] - public async Task CreateConversation() { - TaskCompletionSource tcs = new TaskCompletionSource(); - - string clientId = Guid.NewGuid().ToString(); - LCIMClient client = new LCIMClient(clientId); - - await client.Open(); - - client.OnInvited = (conv, initBy) => { - TestContext.WriteLine($"on invited: {initBy}"); - TestContext.WriteLine(conv.CreatorId); - }; - - client.OnMembersJoined = (conv, memberList, initBy) => { - TestContext.WriteLine($"on members joined: {initBy}"); - foreach (string memberId in conv.MemberIds) { - TestContext.WriteLine(memberId); - } - tcs.SetResult(null); - }; - - List memberIdList = new List { "world" }; - string name = Guid.NewGuid().ToString(); - await client.CreateConversation(memberIdList, name: name, unique: false); - - await tcs.Task; + [Order(0)] + public async Task AddMember() { + await conversation.AddMembers(new string[] { c2.Id }); + Assert.AreEqual(conversation.MemberIds.Count, 4); } [Test] - public async Task CreateChatRoom() { - TaskCompletionSource tcs = new TaskCompletionSource(); - - string clientId = Guid.NewGuid().ToString(); - LCIMClient client = new LCIMClient(clientId); - - await client.Open(); - - client.OnInvited = (conv, initBy) => { - TestContext.WriteLine($"on invited: {initBy}"); - tcs.SetResult(null); - }; - - string name = Guid.NewGuid().ToString(); - await client.CreateChatRoom(name); - - await tcs.Task; + [Order(1)] + public async Task MuteMembers() { + await conversation.MuteMembers(new string[] { "lean" }); + Assert.True(conversation.MutedMemberIds.Contains("lean")); } [Test] - public async Task CreateTemporaryConversation() { - TaskCompletionSource tcs = new TaskCompletionSource(); - - string clientId = Guid.NewGuid().ToString(); - LCIMClient client = new LCIMClient(clientId); - - await client.Open(); - - client.OnInvited = (conv, initBy) => { - TestContext.WriteLine($"on invited: {initBy}"); - }; - - client.OnMembersJoined = (conv, memberList, initBy) => { - TestContext.WriteLine($"on members joined: {initBy}"); - tcs.SetResult(null); - }; - - List memberIdList = new List { "world" }; - await client.CreateTemporaryConversation(memberIdList); - - await tcs.Task; + [Order(2)] + public async Task UnmuteMembers() { + await conversation.UnmuteMembers(new string[] { "lean" }); + Assert.False(conversation.MutedMemberIds.Contains("lean")); } [Test] - public async Task Query() { - LCIMClient client = new LCIMClient("hello123"); - await client.Open(); - - LCIMConversationQuery query = new LCIMConversationQuery(client); - await query.Find(); + [Order(3)] + public async Task BlockMembers() { + await conversation.BlockMembers(new string[] { "lean" }); + LCIMPageResult result = await conversation.QueryBlockedMembers(); + Assert.True(result.Results.Contains("lean")); } [Test] - public async Task Save() { - string clientId = Guid.NewGuid().ToString(); - LCIMClient client = new LCIMClient(clientId); + [Order(4)] + public async Task UnblockMembers() { + await conversation.UnblockMembers(new string[] { "lean" }); + LCIMPageResult result = await conversation.QueryBlockedMembers(); + Assert.False(result.Results.Contains("lean")); + } - await client.Open(); + [Test] + [Order(5)] + public async Task UpdateRole() { + await conversation.UpdateMemberRole("cloud", LCIMConversationMemberInfo.Manager); + LCIMConversationMemberInfo memberInfo = await conversation.GetMemberInfo("cloud"); + Assert.True(memberInfo.IsManager); + } - string otherId = Guid.NewGuid().ToString(); - LCIMConversation conversation = await client.CreateConversation(new List { otherId }); + [Test] + [Order(6)] + public async Task RemoveMember() { + await conversation.RemoveMembers(new string[] { c2.Id }); + Assert.AreEqual(conversation.MemberIds.Count, 3); + } + [Test] + [Order(7)] + public async Task UpdateInfo() { await conversation.UpdateInfo(new Dictionary { { "name", "leancloud" }, { "k1", "v1" }, { "k2", "v2" } }); - Assert.AreEqual(conversation.Name, "leancloud"); Assert.AreEqual(conversation["k1"], "v1"); Assert.AreEqual(conversation["k2"], "v2"); diff --git a/Test/Realtime.Test/ConversationQuery.cs b/Test/Realtime.Test/ConversationQuery.cs index f30a23b..2b2b849 100644 --- a/Test/Realtime.Test/ConversationQuery.cs +++ b/Test/Realtime.Test/ConversationQuery.cs @@ -1,7 +1,49 @@ -using System; +using NUnit.Framework; +using System.Collections.ObjectModel; +using System.Threading.Tasks; +using LeanCloud; +using LeanCloud.Common; +using LeanCloud.Realtime; + namespace Realtime.Test { public class ConversationQuery { - public ConversationQuery() { + private string clientId = "hello123"; + private LCIMClient client; + + [SetUp] + public async Task SetUp() { + LCLogger.LogDelegate += Utils.Print; + LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com"); + client = new LCIMClient(clientId); + await client.Open(); + } + + [TearDown] + public async Task TearDown() { + LCLogger.LogDelegate -= Utils.Print; + await client.Close(); + } + + [Test] + public async Task QueryMyConversation() { + LCIMConversationQuery query = new LCIMConversationQuery(client); + ReadOnlyCollection conversations = await query.Find(); + Assert.Greater(conversations.Count, 0); + foreach (LCIMConversation conversation in conversations) { + Assert.True(conversation.MemberIds.Contains(clientId)); + } + } + + [Test] + public async Task QueryMemberConversation() { + string memberId = "cc1"; + LCIMConversationQuery query = new LCIMConversationQuery(client); + query.WhereContains("m", memberId); + ReadOnlyCollection conversations = await query.Find(); + Assert.Greater(conversations.Count, 0); + foreach (LCIMConversation conversation in conversations) { + Assert.True(conversation.MemberIds.Contains(memberId)); + } } } } diff --git a/Test/Realtime.Test/LocalSignatureFactory.cs b/Test/Realtime.Test/LocalSignatureFactory.cs index 0db73d8..db59c8e 100644 --- a/Test/Realtime.Test/LocalSignatureFactory.cs +++ b/Test/Realtime.Test/LocalSignatureFactory.cs @@ -1,7 +1,97 @@ using System; +using System.Text; +using System.Linq; +using System.Threading.Tasks; +using System.Collections.Generic; +using System.Security.Cryptography; +using LeanCloud.Realtime; +using LeanCloud; + namespace Realtime.Test { - public class LocalSignatureFactory { - public LocalSignatureFactory() { + public class LocalSignatureFactory : ILCIMSignatureFactory { + const string MasterKey = "pyvbNSh5jXsuFQ3C8EgnIdhw"; + + public Task CreateConnectSignature(string clientId) { + long timestamp = DateTimeOffset.Now.ToUnixTimeSeconds(); + string nonce = NewNonce(); + string signature = GenerateSignature(LCApplication.AppId, clientId, string.Empty, timestamp.ToString(), nonce); + return Task.FromResult(new LCIMSignature { + Signature = signature, + Timestamp = timestamp, + Nonce = nonce + }); + } + + public Task CreateStartConversationSignature(string clientId, IEnumerable memberIds) { + string sortedMemberIds = string.Empty; + if (memberIds != null) { + List sortedMemberList = memberIds.ToList(); + sortedMemberList.Sort(); + sortedMemberIds = string.Join(":", sortedMemberList); + } + long timestamp = DateTimeOffset.Now.ToUnixTimeSeconds(); + string nonce = NewNonce(); + string signature = GenerateSignature(LCApplication.AppId, clientId, sortedMemberIds, timestamp.ToString(), nonce); + return Task.FromResult(new LCIMSignature { + Signature = signature, + Timestamp = timestamp, + Nonce = nonce + }); + } + + public Task CreateConversationSignature(string conversationId, string clientId, IEnumerable memberIds, string action) { + string sortedMemberIds = string.Empty; + if (memberIds != null) { + List sortedMemberList = memberIds.ToList(); + sortedMemberList.Sort(); + sortedMemberIds = string.Join(":", sortedMemberList); + } + long timestamp = DateTimeOffset.Now.ToUnixTimeSeconds(); + string nonce = NewNonce(); + string signature = GenerateSignature(LCApplication.AppId, clientId, conversationId, sortedMemberIds, timestamp.ToString(), nonce, action); + return Task.FromResult(new LCIMSignature { + Signature = signature, + Timestamp = timestamp, + Nonce = nonce + }); + } + + public Task CreateBlacklistSignature(string conversationId, string clientId, IEnumerable memberIds, string action) { + string sortedMemberIds = string.Empty; + if (memberIds != null) { + List sortedMemberList = memberIds.ToList(); + sortedMemberList.Sort(); + sortedMemberIds = string.Join(":", sortedMemberList); + } + long timestamp = DateTimeOffset.Now.ToUnixTimeSeconds(); + string nonce = NewNonce(); + string signature = GenerateSignature(LCApplication.AppId, clientId, conversationId, sortedMemberIds, timestamp.ToString(), nonce, action); + return Task.FromResult(new LCIMSignature { + Signature = signature, + Timestamp = timestamp, + Nonce = nonce + }); + } + + private static string SignSHA1(string key, string text) { + HMACSHA1 hmac = new HMACSHA1(Encoding.UTF8.GetBytes(key)); + byte[] bytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(text)); + string signature = BitConverter.ToString(bytes).Replace("-", string.Empty); + return signature; + } + + private static string NewNonce() { + byte[] bytes = new byte[10]; + using (RandomNumberGenerator generator = RandomNumberGenerator.Create()) { + generator.GetBytes(bytes); + } + return Convert.ToBase64String(bytes); + } + + private static string GenerateSignature(params string[] args) { + string text = string.Join(":", args); + string signature = SignSHA1(MasterKey, text); + return signature; } } } diff --git a/Test/Realtime.Test/Message.cs b/Test/Realtime.Test/Message.cs index e27dc54..c721a41 100644 --- a/Test/Realtime.Test/Message.cs +++ b/Test/Realtime.Test/Message.cs @@ -1,42 +1,69 @@ using NUnit.Framework; using System; +using System.Threading; using System.Threading.Tasks; using System.Collections.Generic; using LeanCloud; using LeanCloud.Common; +using LeanCloud.Storage; using LeanCloud.Realtime; +using static System.Console; + namespace Realtime.Test { public class Message { + private LCIMClient m1; + private LCIMClient m2; + + private LCIMConversation conversation; + [SetUp] - public void SetUp() { + public async Task SetUp() { LCLogger.LogDelegate += Utils.Print; LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com"); + m1 = new LCIMClient("m1"); + m2 = new LCIMClient("m2"); + await m1.Open(); + await m2.Open(); + conversation = await m1.CreateConversation(new string[] { "m2" }); } [TearDown] - public void TearDown() { + public async Task TearDown() { + await m1.Close(); + await m2.Close(); LCLogger.LogDelegate -= Utils.Print; } [Test] public async Task Send() { - try { - string clientId = Guid.NewGuid().ToString(); - LCIMClient client = new LCIMClient(clientId); - await client.Open(); - List memberIdList = new List { "world" }; - string name = Guid.NewGuid().ToString(); - LCIMConversation conversation = await client.CreateConversation(memberIdList, name: name, unique: false); - LCIMTextMessage textMessage = new LCIMTextMessage("hello, world"); - await conversation.Send(textMessage); + AutoResetEvent are = new AutoResetEvent(false); + m2.OnMessage = (conv, msg) => { + WriteLine(msg.Id); + if (msg is LCIMImageMessage imageMsg) { + WriteLine($"-------- url: {imageMsg.Url}"); + } else if (msg is LCIMFileMessage fileMsg) { + WriteLine($"-------- name: {fileMsg.Format}"); + } else if (msg is LCIMTextMessage textMsg) { + WriteLine($"-------- text: {textMsg.Text}"); + } + }; - TestContext.WriteLine(textMessage.Id); - TestContext.WriteLine(textMessage.DeliveredAt); - Assert.NotNull(textMessage.Id); - } catch (Exception e) { - LCLogger.Error(e.Message); - } + LCIMTextMessage textMessage = new LCIMTextMessage("hello, world"); + await conversation.Send(textMessage); + Assert.NotNull(textMessage.Id); + + LCFile image = new LCFile("hello", "../../../../assets/hello.png"); + await image.Save(); + LCIMImageMessage imageMessage = new LCIMImageMessage(image); + await conversation.Send(imageMessage); + Assert.NotNull(imageMessage.Id); + + LCFile file = new LCFile("apk", "../../../../assets/test.apk"); + await file.Save(); + LCIMFileMessage fileMessage = new LCIMFileMessage(file); + await conversation.Send(fileMessage); + Assert.NotNull(fileMessage.Id); } } }