From cf87916b8c7a0f71936aca33207779090d3ba29e Mon Sep 17 00:00:00 2001 From: oneRain Date: Wed, 13 Jan 2021 16:30:30 +0800 Subject: [PATCH] feat: friend test --- Storage/Storage.Test/FriendTest.cs | 103 +++++++++++++++++++++ Storage/Storage.Test/StatusTest.cs | 98 ++++++++++++++++++++ Storage/Storage.Test/UserTest.cs | 30 ++++++ Storage/Storage/LCApplication.cs | 1 + Storage/Storage/LCFollowersAndFollowees.cs | 2 +- Storage/Storage/LCUser.cs | 2 +- 6 files changed, 234 insertions(+), 2 deletions(-) create mode 100644 Storage/Storage.Test/FriendTest.cs create mode 100644 Storage/Storage.Test/StatusTest.cs diff --git a/Storage/Storage.Test/FriendTest.cs b/Storage/Storage.Test/FriendTest.cs new file mode 100644 index 0000000..80dff68 --- /dev/null +++ b/Storage/Storage.Test/FriendTest.cs @@ -0,0 +1,103 @@ +using NUnit.Framework; +using System; +using System.Linq; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Threading.Tasks; +using LeanCloud; +using LeanCloud.Storage; + +namespace Storage.Test { + public class FriendTest { + async Task SignUp() { + LCUser user = new LCUser { + Username = Guid.NewGuid().ToString(), + Password = "world" + }; + return await user.SignUp(); + } + + async Task GetRequest() { + LCUser user = await LCUser.GetCurrent(); + LCQuery query = new LCQuery("_FriendshipRequest") + .WhereEqualTo("friend", user) + .WhereEqualTo("status", "pending"); + return await query.First(); + } + + async Task> GetFriends() { + LCUser user = await LCUser.GetCurrent(); + LCQuery query = new LCQuery("_Followee") + .WhereEqualTo("user", user) + .WhereEqualTo("friendStatus", true); + return await query.Find(); + } + + private LCUser user1; + private LCUser user2; + + [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] + [Order(0)] + public async Task Init() { + user1 = await SignUp(); + user2 = await SignUp(); + Dictionary attrs = new Dictionary { + { "group", "sport" } + }; + await LCFriendship.Request(user1.ObjectId, attrs); + + await SignUp(); + await LCFriendship.Request(user1.ObjectId); + + await LCUser.BecomeWithSessionToken(user1.SessionToken); + } + + [Test] + [Order(1)] + public async Task Accept() { + // 查询好友请求 + LCFriendshipRequest request = await GetRequest(); + // 接受 + await LCFriendship.AcceptRequest(request); + // 查询好友 + Assert.Greater((await GetFriends()).Count, 0); + } + + [Test] + [Order(2)] + public async Task Decline() { + // 查询好友请求 + LCFriendshipRequest request = await GetRequest(); + // 拒绝 + await LCFriendship.DeclineRequest(request); + } + + [Test] + [Order(3)] + public async Task Attributes() { + LCObject followee = (await GetFriends()).First(); + followee["group"] = "friend"; + await followee.Save(); + } + + [Test] + [Order(4)] + public async Task Delete() { + await user1.Unfollow(user2.ObjectId); + // 查询好友 + Assert.AreEqual((await GetFriends()).Count, 0); + } + } +} diff --git a/Storage/Storage.Test/StatusTest.cs b/Storage/Storage.Test/StatusTest.cs new file mode 100644 index 0000000..002b653 --- /dev/null +++ b/Storage/Storage.Test/StatusTest.cs @@ -0,0 +1,98 @@ +using NUnit.Framework; +using System; +using System.Threading.Tasks; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using LeanCloud; +using LeanCloud.Storage; + +namespace Storage.Test { + public class StatusTest { + private LCUser user1; + private LCUser user2; + private LCUser user3; + + [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] + [Order(0)] + public async Task Init() { + user1 = new LCUser { + Username = Guid.NewGuid().ToString(), + Password = "world" + }; + await user1.SignUp(); + + user2 = new LCUser { + Username = Guid.NewGuid().ToString(), + Password = "world" + }; + await user2.SignUp(); + + user3 = new LCUser { + Username = Guid.NewGuid().ToString(), + Password = "world" + }; + await user3.SignUp(); + } + + [Test] + [Order(1)] + public async Task Follow() { + await LCUser.BecomeWithSessionToken(user2.SessionToken); + Dictionary attrs = new Dictionary { + { "score", 100 } + }; + await user2.Follow(user1.ObjectId, attrs); + + await LCUser.BecomeWithSessionToken(user3.SessionToken); + await user3.Follow(user2.ObjectId); + } + + [Test] + [Order(2)] + public async Task QueryFollowersAndFollowees() { + await LCUser.BecomeWithSessionToken(user2.SessionToken); + + LCQuery query = user2.FolloweeQuery(); + ReadOnlyCollection results = await query.Find(); + Assert.Greater(results.Count, 0); + foreach (LCObject item in results) { + Assert.IsTrue(item["followee"] is LCObject); + Assert.AreEqual(user1.ObjectId, (item["followee"] as LCObject).ObjectId); + } + + query = user2.FollowerQuery(); + results = await query.Find(); + Assert.Greater(results.Count, 0); + foreach (LCObject item in results) { + Assert.IsTrue(item["follower"] is LCObject); + Assert.AreEqual(user3.ObjectId, (item["follower"] as LCObject).ObjectId); + } + + LCFollowersAndFollowees followersAndFollowees = await user2.GetFollowersAndFollowees(true, true, true); + Assert.AreEqual(followersAndFollowees.FollowersCount, 1); + Assert.AreEqual(followersAndFollowees.FolloweesCount, 1); + } + + [Test] + [Order(5)] + public async Task Unfollow() { + await LCUser.BecomeWithSessionToken(user2.SessionToken); + await user2.Unfollow(user1.ObjectId); + + await LCUser.BecomeWithSessionToken(user3.SessionToken); + await user3.Unfollow(user1.ObjectId); + } + } +} diff --git a/Storage/Storage.Test/UserTest.cs b/Storage/Storage.Test/UserTest.cs index 56cfaca..06ea54a 100644 --- a/Storage/Storage.Test/UserTest.cs +++ b/Storage/Storage.Test/UserTest.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using LeanCloud; using LeanCloud.Storage; +using Newtonsoft.Json; namespace Storage.Test { public class UserTest { @@ -241,5 +242,34 @@ namespace Storage.Test { await LCUser.Login("hello", "world"); await LCUser.VerifyCodeForUpdatingPhoneNumber("15101006007", "055595"); } + + [Test] + public async Task AuthData() { + string uuid = Guid.NewGuid().ToString(); + Dictionary authData = new Dictionary { + { "expires_in", 7200 }, + { "openid", uuid }, + { "access_token", uuid } + }; + LCUser currentUser = await LCUser.LoginWithAuthData(authData, "weixin"); + TestContext.WriteLine(currentUser.SessionToken); + Assert.NotNull(currentUser.SessionToken); + string userId = currentUser.ObjectId; + TestContext.WriteLine($"userId: {userId}"); + TestContext.WriteLine(JsonConvert.SerializeObject(currentUser.AuthData)); + + try { + authData = new Dictionary { + { "expires_in", 7200 }, + { "openid", uuid }, + { "access_token", uuid } + }; + await currentUser.AssociateAuthData(authData, "qq"); + TestContext.WriteLine(JsonConvert.SerializeObject(currentUser.AuthData)); + } catch (LCException e) { + TestContext.WriteLine($"{e.Code} : {e.Message}"); + TestContext.WriteLine(JsonConvert.SerializeObject(currentUser.AuthData)); + } + } } } diff --git a/Storage/Storage/LCApplication.cs b/Storage/Storage/LCApplication.cs index 694e7cd..6256fde 100644 --- a/Storage/Storage/LCApplication.cs +++ b/Storage/Storage/LCApplication.cs @@ -61,6 +61,7 @@ namespace LeanCloud { LCObject.RegisterSubclass(LCUser.CLASS_NAME, () => new LCUser()); LCObject.RegisterSubclass(LCRole.CLASS_NAME, () => new LCRole()); LCObject.RegisterSubclass(LCFile.CLASS_NAME, () => new LCFile()); + LCObject.RegisterSubclass(LCFriendshipRequest.CLASS_NAME, () => new LCFriendshipRequest()); AppRouter = new LCAppRouter(appId, server); diff --git a/Storage/Storage/LCFollowersAndFollowees.cs b/Storage/Storage/LCFollowersAndFollowees.cs index 288b1b5..3677490 100644 --- a/Storage/Storage/LCFollowersAndFollowees.cs +++ b/Storage/Storage/LCFollowersAndFollowees.cs @@ -14,7 +14,7 @@ namespace LeanCloud.Storage { get; internal set; } - public int FolloweeCount { + public int FolloweesCount { get; internal set; } } diff --git a/Storage/Storage/LCUser.cs b/Storage/Storage/LCUser.cs index 46ad157..bb50f86 100644 --- a/Storage/Storage/LCUser.cs +++ b/Storage/Storage/LCUser.cs @@ -684,7 +684,7 @@ namespace LeanCloud.Storage { } if (response.TryGetValue("followees_count", out object followeesCountObj) && (followeesCountObj is int followeesCount)) { - result.FolloweeCount = followeesCount; + result.FolloweesCount = followeesCount; } return result; }