csharp-sdk-upm/Test/Storage.Test/UserTest.cs

233 lines
8.6 KiB
C#
Raw Normal View History

2019-08-07 17:41:36 +08:00
using NUnit.Framework;
using System;
2019-08-07 17:41:36 +08:00
using System.Collections.Generic;
using System.Threading.Tasks;
using LeanCloud.Storage;
2020-03-04 15:17:53 +08:00
using LeanCloud.Common;
2019-08-07 17:41:36 +08:00
namespace LeanCloud.Test {
2019-08-07 17:41:36 +08:00
public class UserTest {
[SetUp]
public void SetUp() {
2020-03-10 16:55:17 +08:00
LCLogger.LogDelegate += Utils.Print;
2020-03-10 16:26:21 +08:00
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
2019-08-07 17:41:36 +08:00
}
[TearDown]
public void TearDown() {
2020-03-10 16:55:17 +08:00
LCLogger.LogDelegate -= Utils.Print;
2019-08-07 17:41:36 +08:00
}
[Test]
public async Task SignUp() {
LCUser user = new LCUser();
long unixTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
user.Username = $"{unixTime}";
user.Password = "world";
string email = $"{unixTime}@qq.com";
user.Email = email;
string mobile = $"{unixTime / 100}";
user.Mobile = mobile;
await user.SignUp();
TestContext.WriteLine(user.Username);
TestContext.WriteLine(user.Password);
Assert.NotNull(user.ObjectId);
TestContext.WriteLine(user.ObjectId);
Assert.NotNull(user.SessionToken);
TestContext.WriteLine(user.SessionToken);
Assert.AreEqual(user.Email, email);
2019-08-21 16:30:34 +08:00
}
[Test]
public async Task Login() {
await LCUser.Login("hello", "world");
LCUser current = await LCUser.GetCurrent();
Assert.NotNull(current.ObjectId);
Assert.IsFalse(current.EmailVerified);
Assert.IsFalse(current.MobileVerified);
Assert.AreEqual(current.Mobile, "15101006008");
2019-08-21 16:30:34 +08:00
}
[Test]
public async Task LoginByEmail() {
await LCUser.LoginByEmail("171253484@qq.com", "world");
LCUser current = await LCUser.GetCurrent();
Assert.NotNull(current.ObjectId);
2019-08-21 16:30:34 +08:00
}
[Test]
public async Task LoginBySessionToken() {
await LCUser.Logout();
string sessionToken = "luo2fpl4qij2050e7enqfz173";
await LCUser.BecomeWithSessionToken(sessionToken);
LCUser current = await LCUser.GetCurrent();
Assert.NotNull(current.ObjectId);
2019-08-21 16:30:34 +08:00
}
[Test]
public async Task RelateObject() {
LCUser user = await LCUser.LoginByMobilePhoneNumber("15101006007", "112358");
LCObject account = new LCObject("Account");
account["user"] = user;
await account.Save();
Assert.AreEqual(user.ObjectId, "5e0d5c667d5774006a5c1177");
2019-08-21 16:30:34 +08:00
}
[Test]
public async Task LoginAnonymous() {
LCUser user = await LCUser.LoginAnonymously();
Assert.NotNull(user.ObjectId);
2019-08-07 17:41:36 +08:00
}
[Test]
public async Task LoginWithAuthData() {
string uuid = Guid.NewGuid().ToString();
Dictionary<string, object> authData = new Dictionary<string, object> {
{ "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(currentUser.AuthData);
await LCUser.Logout();
currentUser = await LCUser.GetCurrent();
Assert.IsNull(currentUser);
currentUser = await LCUser.LoginWithAuthData(authData, "weixin");
TestContext.WriteLine(currentUser.SessionToken);
Assert.NotNull(currentUser.SessionToken);
Assert.AreEqual(currentUser.ObjectId, userId);
TestContext.WriteLine(currentUser.AuthData);
}
[Test]
public async Task AssociateAuthData() {
string uuid = Guid.NewGuid().ToString();
LCUser currentUser = await LCUser.Login("hello", "world");
Dictionary<string, object> authData = new Dictionary<string, object> {
{ "expires_in", 7200 },
{ "openid", uuid },
{ "access_token", uuid }
};
await currentUser.AssociateAuthData(authData, "weixin");
TestContext.WriteLine(currentUser.AuthData);
TestContext.WriteLine(currentUser.AuthData["weixin"]);
}
[Test]
public async Task DisassociateAuthData() {
LCUser currentUser = await LCUser.Login("hello", "world");
await currentUser.DisassociateWithAuthData("weixin");
}
[Test]
public async Task IsAuthenticated() {
LCUser currentUser = await LCUser.Login("hello", "world");
bool isAuthenticated = await currentUser.IsAuthenticated();
TestContext.WriteLine(isAuthenticated);
Assert.IsTrue(isAuthenticated);
}
[Test]
public async Task UpdatePassword() {
LCUser currentUser = await LCUser.Login("hello", "world");
await currentUser.UpdatePassword("world", "newWorld");
await currentUser.UpdatePassword("newWorld", "world");
}
[Test]
public async Task LoginWithAuthDataWithUnionId() {
string uuid = Guid.NewGuid().ToString();
Dictionary<string, object> authData = new Dictionary<string, object> {
{ "expires_in", 7200 },
{ "openid", uuid },
{ "access_token", uuid }
};
string unionId = Guid.NewGuid().ToString();
LCUserAuthDataLoginOption option = new LCUserAuthDataLoginOption();
option.AsMainAccount = true;
LCUser currentUser = await LCUser.LoginWithAuthDataAndUnionId(authData, "weixin_app", unionId, option: option);
TestContext.WriteLine(currentUser.SessionToken);
Assert.NotNull(currentUser.SessionToken);
string userId = currentUser.ObjectId;
TestContext.WriteLine($"userId: {userId}");
TestContext.WriteLine(currentUser.AuthData);
await LCUser.Logout();
currentUser = await LCUser.GetCurrent();
Assert.IsNull(currentUser);
currentUser = await LCUser.LoginWithAuthDataAndUnionId(authData, "weixin_mini_app", unionId, option: option);
TestContext.WriteLine(currentUser.SessionToken);
Assert.NotNull(currentUser.SessionToken);
Assert.AreEqual(currentUser.ObjectId, userId);
TestContext.WriteLine(currentUser.AuthData);
}
[Test]
public async Task AssociateAuthDataWithUnionId() {
LCUser currentUser = await LCUser.Login("hello", "world");
string uuid = Guid.NewGuid().ToString();
Dictionary<string, object> authData = new Dictionary<string, object> {
{ "expires_in", 7200 },
{ "openid", uuid },
{ "access_token", uuid }
};
string unionId = Guid.NewGuid().ToString();
await currentUser.AssociateAuthDataAndUnionId(authData, "qq", unionId);
}
// 手动测试
//[Test]
//public async Task LoginByMobile() {
// LCUser user = await LCUser.LoginByMobilePhoneNumber("15101006007", "112358");
// Assert.NotNull(user.ObjectId);
//}
//[Test]
//public async Task RequestLoginSMSCode() {
// await LCUser.RequestLoginSMSCode("15101006007");
//}
//[Test]
//public async Task LoginBySMSCode() {
// LCUser user = await LCUser.LoginBySMSCode("15101006007", "194998");
// Assert.NotNull(user.ObjectId);
//}
//[Test]
//public async Task RequestEmailVerify() {
// await LCUser.RequestEmailVerify("171253484@qq.com");
//}
//[Test]
//public async Task RequestMobileVerify() {
// await LCUser.RequestMobilePhoneVerify("15101006007");
//}
//[Test]
//public async Task VerifyMobile() {
// await LCUser.VerifyMobilePhone("15101006007", "506993");
//}
//[Test]
//public async Task RequestResetPasswordBySMSCode() {
// await LCUser.RequestPasswordRestBySmsCode("15101006007");
//}
//[Test]
//public async Task ResetPasswordBySMSCode() {
// await LCUser.ResetPasswordBySmsCode("15101006007", "732552", "112358");
//}
2019-08-07 17:41:36 +08:00
}
}