2019-08-07 17:41:36 +08:00
|
|
|
|
using NUnit.Framework;
|
2020-02-27 11:31:38 +08:00
|
|
|
|
using System;
|
2019-08-07 17:41:36 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-04-28 17:04:46 +08:00
|
|
|
|
using LeanCloud;
|
2020-02-27 11:31:38 +08:00
|
|
|
|
using LeanCloud.Storage;
|
2021-03-29 14:56:00 +08:00
|
|
|
|
using LC.Newtonsoft.Json;
|
2019-08-07 17:41:36 +08:00
|
|
|
|
|
2020-04-28 17:04:46 +08:00
|
|
|
|
namespace Storage.Test {
|
2021-04-29 15:58:22 +08:00
|
|
|
|
public class UserTest : BaseTest {
|
2019-08-07 17:41:36 +08:00
|
|
|
|
[Test]
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Order(0)]
|
2020-02-27 11:31:38 +08:00
|
|
|
|
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;
|
2021-04-29 15:58:22 +08:00
|
|
|
|
//Random random = new Random();
|
|
|
|
|
//string mobile = $"151{random.Next(10000000, 99999999)}";
|
|
|
|
|
//user.Mobile = mobile;
|
2020-02-27 11:31:38 +08:00
|
|
|
|
await user.SignUp();
|
2021-04-29 15:58:22 +08:00
|
|
|
|
|
2020-02-27 11:31:38 +08:00
|
|
|
|
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]
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Order(1)]
|
2020-02-27 11:31:38 +08:00
|
|
|
|
public async Task Login() {
|
2021-04-29 15:58:22 +08:00
|
|
|
|
try {
|
|
|
|
|
await LCUser.Login(TestPhone, TestPhone);
|
|
|
|
|
} catch (LCException e) {
|
|
|
|
|
if (e.Code == 211) {
|
|
|
|
|
LCUser user = new LCUser {
|
|
|
|
|
Username = TestPhone,
|
|
|
|
|
Password = TestPhone,
|
|
|
|
|
Mobile = TestPhone,
|
|
|
|
|
Email = GetTestEmail()
|
|
|
|
|
};
|
|
|
|
|
await user.SignUp();
|
|
|
|
|
} else {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await LCUser.Login(TestPhone, TestPhone);
|
2020-02-27 11:31:38 +08:00
|
|
|
|
LCUser current = await LCUser.GetCurrent();
|
|
|
|
|
Assert.NotNull(current.ObjectId);
|
|
|
|
|
Assert.IsFalse(current.EmailVerified);
|
2021-04-29 15:58:22 +08:00
|
|
|
|
Assert.IsTrue(current.MobileVerified);
|
|
|
|
|
Assert.AreEqual(current.Mobile, TestPhone);
|
2019-08-21 16:30:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Order(2)]
|
2020-02-27 11:31:38 +08:00
|
|
|
|
public async Task LoginByEmail() {
|
2021-04-29 15:58:22 +08:00
|
|
|
|
await LCUser.LoginByEmail(GetTestEmail(), TestPhone);
|
2020-02-27 11:31:38 +08:00
|
|
|
|
LCUser current = await LCUser.GetCurrent();
|
|
|
|
|
Assert.NotNull(current.ObjectId);
|
2019-08-21 16:30:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Order(3)]
|
2020-02-27 11:31:38 +08:00
|
|
|
|
public async Task LoginBySessionToken() {
|
2021-04-29 15:58:22 +08:00
|
|
|
|
LCUser user = await LCUser.Login(TestPhone, TestPhone);
|
|
|
|
|
string sessionToken = user.SessionToken;
|
2020-03-03 15:08:59 +08:00
|
|
|
|
await LCUser.Logout();
|
2021-04-29 15:58:22 +08:00
|
|
|
|
|
2020-02-27 11:31:38 +08:00
|
|
|
|
await LCUser.BecomeWithSessionToken(sessionToken);
|
|
|
|
|
LCUser current = await LCUser.GetCurrent();
|
|
|
|
|
Assert.NotNull(current.ObjectId);
|
2019-08-21 16:30:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Order(4)]
|
2020-02-27 11:31:38 +08:00
|
|
|
|
public async Task RelateObject() {
|
2021-04-29 15:58:22 +08:00
|
|
|
|
LCUser user = await LCUser.LoginByMobilePhoneNumber(TestPhone, TestPhone);
|
|
|
|
|
Account account = new Account();
|
|
|
|
|
account.User = user;
|
2020-02-27 11:31:38 +08:00
|
|
|
|
await account.Save();
|
2019-08-21 16:30:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Order(5)]
|
2020-02-27 11:31:38 +08:00
|
|
|
|
public async Task LoginAnonymous() {
|
|
|
|
|
LCUser user = await LCUser.LoginAnonymously();
|
|
|
|
|
Assert.NotNull(user.ObjectId);
|
2020-05-06 14:14:07 +08:00
|
|
|
|
Assert.IsTrue(user.IsAnonymous);
|
2019-08-07 17:41:36 +08:00
|
|
|
|
}
|
2019-08-23 16:16:18 +08:00
|
|
|
|
|
|
|
|
|
[Test]
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Order(6)]
|
2019-08-23 16:16:18 +08:00
|
|
|
|
public async Task LoginWithAuthData() {
|
2020-02-27 11:31:38 +08:00
|
|
|
|
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);
|
2019-08-23 16:16:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Order(7)]
|
2019-08-23 16:16:18 +08:00
|
|
|
|
public async Task AssociateAuthData() {
|
2020-02-27 11:31:38 +08:00
|
|
|
|
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"]);
|
2019-08-23 16:16:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Order(8)]
|
2020-02-27 11:31:38 +08:00
|
|
|
|
public async Task DisassociateAuthData() {
|
|
|
|
|
LCUser currentUser = await LCUser.Login("hello", "world");
|
|
|
|
|
await currentUser.DisassociateWithAuthData("weixin");
|
2019-08-23 16:16:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Order(9)]
|
2020-02-27 11:31:38 +08:00
|
|
|
|
public async Task IsAuthenticated() {
|
|
|
|
|
LCUser currentUser = await LCUser.Login("hello", "world");
|
|
|
|
|
bool isAuthenticated = await currentUser.IsAuthenticated();
|
|
|
|
|
TestContext.WriteLine(isAuthenticated);
|
|
|
|
|
Assert.IsTrue(isAuthenticated);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Order(10)]
|
2020-02-27 11:31:38 +08:00
|
|
|
|
public async Task UpdatePassword() {
|
|
|
|
|
LCUser currentUser = await LCUser.Login("hello", "world");
|
|
|
|
|
await currentUser.UpdatePassword("world", "newWorld");
|
|
|
|
|
await currentUser.UpdatePassword("newWorld", "world");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Order(11)]
|
2020-02-27 11:31:38 +08:00
|
|
|
|
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]
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Order(12)]
|
2020-02-27 11:31:38 +08:00
|
|
|
|
public async Task AssociateAuthDataWithUnionId() {
|
2021-04-29 15:58:22 +08:00
|
|
|
|
LCUser currentUser = await LCUser.Login(TestPhone, TestPhone);
|
2020-02-27 11:31:38 +08:00
|
|
|
|
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);
|
2019-08-23 16:16:18 +08:00
|
|
|
|
}
|
2020-03-03 15:53:18 +08:00
|
|
|
|
|
|
|
|
|
// 手动测试
|
|
|
|
|
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Test]
|
|
|
|
|
[Order(13)]
|
|
|
|
|
public async Task LoginByMobile() {
|
|
|
|
|
LCUser user = await LCUser.LoginByMobilePhoneNumber(TestPhone, TestPhone);
|
|
|
|
|
Assert.NotNull(user.ObjectId);
|
|
|
|
|
}
|
2020-03-03 15:53:18 +08:00
|
|
|
|
|
|
|
|
|
//[Test]
|
|
|
|
|
//public async Task RequestLoginSMSCode() {
|
|
|
|
|
// await LCUser.RequestLoginSMSCode("15101006007");
|
|
|
|
|
//}
|
|
|
|
|
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Test]
|
|
|
|
|
[Order(14)]
|
|
|
|
|
public async Task LoginBySMSCode() {
|
|
|
|
|
LCUser user = await LCUser.LoginBySMSCode(TestPhone, TestSMSCode);
|
|
|
|
|
Assert.NotNull(user.ObjectId);
|
|
|
|
|
}
|
2020-03-03 15:53:18 +08:00
|
|
|
|
|
|
|
|
|
//[Test]
|
|
|
|
|
//public async Task RequestEmailVerify() {
|
|
|
|
|
// await LCUser.RequestEmailVerify("171253484@qq.com");
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//[Test]
|
|
|
|
|
//public async Task RequestMobileVerify() {
|
|
|
|
|
// await LCUser.RequestMobilePhoneVerify("15101006007");
|
|
|
|
|
//}
|
|
|
|
|
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Test]
|
|
|
|
|
[Order(15)]
|
|
|
|
|
public async Task VerifyMobile() {
|
|
|
|
|
await LCUser.VerifyMobilePhone(TestPhone, TestSMSCode);
|
|
|
|
|
}
|
2020-03-03 15:53:18 +08:00
|
|
|
|
|
|
|
|
|
//[Test]
|
|
|
|
|
//public async Task RequestResetPasswordBySMSCode() {
|
|
|
|
|
// await LCUser.RequestPasswordRestBySmsCode("15101006007");
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//[Test]
|
|
|
|
|
//public async Task ResetPasswordBySMSCode() {
|
|
|
|
|
// await LCUser.ResetPasswordBySmsCode("15101006007", "732552", "112358");
|
|
|
|
|
//}
|
2020-07-14 11:51:30 +08:00
|
|
|
|
|
2021-04-29 15:58:22 +08:00
|
|
|
|
//[Test]
|
|
|
|
|
//public async Task RequestSMSCodeForUpdatingPhoneNumber() {
|
|
|
|
|
// await LCUser.Login(TestPhone, TestPhone);
|
|
|
|
|
// await LCUser.RequestSMSCodeForUpdatingPhoneNumber(TestPhone);
|
|
|
|
|
//}
|
2020-07-14 11:51:30 +08:00
|
|
|
|
|
|
|
|
|
[Test]
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Order(16)]
|
2020-07-14 11:51:30 +08:00
|
|
|
|
public async Task VerifyCodeForUpdatingPhoneNumber() {
|
2021-04-29 15:58:22 +08:00
|
|
|
|
await LCUser.Login(TestPhone, TestPhone);
|
|
|
|
|
await LCUser.VerifyCodeForUpdatingPhoneNumber(TestPhone, TestSMSCode);
|
2020-07-14 11:51:30 +08:00
|
|
|
|
}
|
2021-01-13 16:30:30 +08:00
|
|
|
|
|
|
|
|
|
[Test]
|
2021-04-29 15:58:22 +08:00
|
|
|
|
[Order(17)]
|
2021-01-13 16:30:30 +08:00
|
|
|
|
public async Task AuthData() {
|
|
|
|
|
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(JsonConvert.SerializeObject(currentUser.AuthData));
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
authData = new Dictionary<string, object> {
|
|
|
|
|
{ "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));
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 15:58:22 +08:00
|
|
|
|
|
|
|
|
|
private string GetTestEmail() {
|
|
|
|
|
return $"{TestPhone}@leancloud.rocks";
|
|
|
|
|
}
|
2019-08-07 17:41:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|