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

305 lines
11 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;
2020-04-28 17:04:46 +08:00
using LeanCloud;
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)]
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;
await user.SignUp();
2021-04-29 15:58:22 +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)]
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);
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)]
public async Task LoginByEmail() {
2021-04-29 15:58:22 +08:00
await LCUser.LoginByEmail(GetTestEmail(), TestPhone);
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)]
public async Task LoginBySessionToken() {
2021-04-29 15:58:22 +08:00
LCUser user = await LCUser.Login(TestPhone, TestPhone);
string sessionToken = user.SessionToken;
await LCUser.Logout();
2021-04-29 15:58:22 +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)]
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;
await account.Save();
2019-08-21 16:30:34 +08:00
}
[Test]
2021-04-29 15:58:22 +08:00
[Order(5)]
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
}
[Test]
2021-04-29 15:58:22 +08:00
[Order(6)]
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]
2021-04-29 15:58:22 +08:00
[Order(7)]
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]
2021-04-29 15:58:22 +08:00
[Order(8)]
public async Task DisassociateAuthData() {
LCUser currentUser = await LCUser.Login("hello", "world");
await currentUser.DisassociateWithAuthData("weixin");
}
[Test]
2021-04-29 15:58:22 +08:00
[Order(9)]
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)]
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)]
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)]
public async Task AssociateAuthDataWithUnionId() {
2021-04-29 15:58:22 +08:00
LCUser currentUser = await LCUser.Login(TestPhone, TestPhone);
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);
}
// 手动测试
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);
}
//[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);
}
//[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);
}
//[Test]
//public async Task RequestResetPasswordBySMSCode() {
// await LCUser.RequestPasswordRestBySmsCode("15101006007");
//}
//[Test]
//public async Task ResetPasswordBySMSCode() {
// await LCUser.ResetPasswordBySmsCode("15101006007", "732552", "112358");
//}
2021-04-29 15:58:22 +08:00
//[Test]
//public async Task RequestSMSCodeForUpdatingPhoneNumber() {
// await LCUser.Login(TestPhone, TestPhone);
// await LCUser.RequestSMSCodeForUpdatingPhoneNumber(TestPhone);
//}
[Test]
2021-04-29 15:58:22 +08:00
[Order(16)]
public async Task VerifyCodeForUpdatingPhoneNumber() {
2021-04-29 15:58:22 +08:00
await LCUser.Login(TestPhone, TestPhone);
await LCUser.VerifyCodeForUpdatingPhoneNumber(TestPhone, TestSMSCode);
}
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
}
}