* ACLTest.cs: chore: 完善 ACL 功能和测试

* LCACL.cs:
* JsonTest.cs:
* JustTest.cs:
* HelloTest.cs:
oneRain 2020-02-27 12:42:17 +08:00
parent 83ad15a8ec
commit f8798b67e8
5 changed files with 49 additions and 180 deletions

View File

@ -30,25 +30,48 @@ namespace LeanCloud.Test {
[Test]
public async Task UserReadAndWrite() {
//await LCUser.Login('hello', 'world');
//LCObject account = new LCObject('Account');
//LCUser currentUser = await LCUser.getCurrent();
//LCACL acl = LCACL.createWithOwner(currentUser);
//account.acl = acl;
//account['balance'] = 512;
//await account.save();
await LCUser.Login("hello", "world");
LCObject account = new LCObject("Account");
LCUser currentUser = await LCUser.GetCurrent();
LCACL acl = LCACL.CreateWithOwner(currentUser);
account.ACL = acl;
account["balance"] = 512;
await account.Save();
//assert(acl.getUserReadAccess(currentUser) == true);
//assert(acl.getUserWriteAccess(currentUser) == true);
Assert.IsTrue(acl.GetUserReadAccess(currentUser));
Assert.IsTrue(acl.GetUserWriteAccess(currentUser));
//LCQuery<LCObject> query = new LCQuery('Account');
//LCObject result = await query.get(account.objectId);
//print(result.objectId);
//assert(result.objectId != null);
LCQuery<LCObject> query = new LCQuery<LCObject>("Account");
LCObject result = await query.Get(account.ObjectId);
TestContext.WriteLine(result.ObjectId);
Assert.NotNull(result.ObjectId);
//LCUser.logout();
//result = await query.get(account.objectId);
//assert(result == null);
await LCUser.Logout();
result = await query.Get(account.ObjectId);
Assert.IsNull(result);
}
[Test]
public async Task RoleReadAndWrite() {
LCQuery<LCRole> query = LCRole.GetQuery();
LCRole owner = await query.Get("5e1440cbfc36ed006add1b8d");
LCObject account = new LCObject("Account");
LCACL acl = new LCACL();
acl.SetRoleReadAccess(owner, true);
acl.SetRoleWriteAccess(owner, true);
account.ACL = acl;
await account.Save();
Assert.IsTrue(acl.GetRoleReadAccess(owner));
Assert.IsTrue(acl.GetRoleWriteAccess(owner));
}
[Test]
public async Task Query() {
await LCUser.Login("game", "play");
LCQuery<LCObject> query = new LCQuery<LCObject>("Account");
LCObject account = await query.Get("5e144525dd3c13006a8f8de2");
TestContext.WriteLine(account.ObjectId);
Assert.NotNull(account.ObjectId);
}
}
}

View File

@ -1,52 +0,0 @@
using NUnit.Framework;
using System.Collections.Generic;
using System.Threading.Tasks;
using LeanCloud.Storage;
namespace LeanCloud.Test {
public class HelloTest {
[SetUp]
public void SetUp() {
LeanCloud.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
Logger.LogDelegate += Utils.Print;
}
[Test]
public async Task Run() {
Dictionary<string, object> parameters = new Dictionary<string, object> {
{ "name", "world" }
};
Dictionary<string, object> response = await LCCloud.Run("hello", parameters);
string ret = response["result"] as string;
TestContext.WriteLine($"ret: {ret}");
Assert.AreEqual(ret, "hello, world");
}
[Test]
public async Task Query() {
LCQuery<LCObject> query = new LCQuery<LCObject>("Hello");
query.Limit(30);
List<LCObject> results = await query.Find();
TestContext.WriteLine(results.Count);
foreach (LCObject obj in results) {
TestContext.WriteLine(obj.ObjectId);
Assert.NotNull(obj.ObjectId);
}
}
[Test]
public void InitByNull() {
List<string> sl = new List<string> { "a", "a", "b" };
HashSet<string> ss = new HashSet<string>(sl);
TestContext.WriteLine(ss.Count);
}
[Test]
public async Task Save() {
LCObject hello = new LCObject("Hello");
await hello.Save();
TestContext.WriteLine($"object id: {hello.ObjectId}");
Assert.NotNull(hello.ObjectId);
}
}
}

View File

@ -1,61 +0,0 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using LeanCloud.Storage.Internal;
namespace LeanCloud.Test {
public class JsonTest {
[Test]
public void Deserialize() {
// 对象类型
var obj = JsonConvert.DeserializeObject("{\"id\": 123}");
TestContext.Out.WriteLine(obj.GetType());
Assert.AreEqual(obj.GetType(), typeof(JObject));
// 数组类型
var arr = JsonConvert.DeserializeObject("[1, 2, 3]");
TestContext.Out.WriteLine(arr.GetType());
Assert.AreEqual(arr.GetType(), typeof(JArray));
try {
// null
var na = JsonConvert.DeserializeObject(null);
TestContext.Out.WriteLine(na.GetType());
} catch (ArgumentNullException) {
}
Assert.Pass();
}
[Test]
public void DeserializeDictionary() {
//var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>("{\"id\": 123, \"nest\": { \"count\": 1 }}",
// new DictionaryConverter());
var json = "{\"id\": 123, \"nest\": { \"count\": 1 }, \"arr\": [1, 2, 3], \"na\": null}";
TestContext.Out.WriteLine(JsonConvert.DeserializeObject(json).GetType());
var obj = JsonConvert.DeserializeObject<object>(json, new LeanCloudJsonConverter());
if (obj is IDictionary<string, object>) {
var dict = obj as Dictionary<string, object>;
TestContext.Out.WriteLine(dict.GetType());
TestContext.Out.WriteLine(dict["id"]);
TestContext.Out.WriteLine(dict["nest"].GetType());
TestContext.Out.WriteLine(dict["arr"].GetType());
}
}
[Test]
public void DeserializeList() {
var json = "[1, \"hello\", [2, 3, 4], { \"count\": 22 }]";
TestContext.Out.WriteLine(JsonConvert.DeserializeObject(json).GetType());
var obj = JsonConvert.DeserializeObject<IList<object>>(json, new LeanCloudJsonConverter());
if (obj is IList<object>) {
var arr = obj as List<object>;
TestContext.Out.WriteLine(arr.GetType());
TestContext.Out.WriteLine(arr[0]);
TestContext.Out.WriteLine(arr[1].GetType());
TestContext.Out.WriteLine(arr[2].GetType());
TestContext.Out.WriteLine(arr[3].GetType());
}
}
}
}

View File

@ -1,51 +0,0 @@
using NUnit.Framework;
using System;
using System.Linq;
using System.Collections.Generic;
namespace LeanCloud.Test {
public class JustTest {
[Test]
public void Concat() {
Dictionary<string, string> d1 = new Dictionary<string, string> {
{ "aaa", "111" }
};
Dictionary<string, string> d2 = new Dictionary<string, string> {
{ "aaa", "222" },
{ "ccc", "333" }
};
IEnumerable<KeyValuePair<string, string>> d = d1.Concat(d2);
foreach (var e in d) {
TestContext.Out.WriteLine($"{e.Key} : {e.Value}");
}
List<string> l1 = new List<string> { "aaa" };
List<string> l2 = new List<string> { "aaa", "bbb" };
IEnumerable<string> l = l1.Concat(l2);
foreach (var e in l) {
TestContext.Out.WriteLine($"{e}");
}
}
[Test]
public void Zip() {
List<int> l1 = new List<int> { 1, 2, 3, 4 };
List<int> l2 = new List<int> { 1, 1, 2 };
var l3 = l1.Zip(l2, (e1, e2) => $"{e1}-{e2}");
foreach (var e in l3) {
TestContext.Out.WriteLine($"{e}");
}
}
[Test]
public void GenericType() {
List<int> list = new List<int> { 1, 1, 2, 3, 5, 8 };
Type type = list.GetType();
TestContext.Out.WriteLine(type);
Type genericType = type.GetGenericTypeDefinition();
TestContext.Out.WriteLine(genericType);
TestContext.Out.WriteLine(typeof(IList<>));
TestContext.Out.WriteLine(typeof(List<>));
}
}
}

View File

@ -122,6 +122,16 @@ namespace LeanCloud.Storage {
writers = new HashSet<string>();
}
public static LCACL CreateWithOwner(LCUser owner) {
if (owner == null) {
throw new ArgumentNullException(nameof(owner));
}
LCACL acl = new LCACL();
acl.SetUserReadAccess(owner, true);
acl.SetUserWriteAccess(owner, true);
return acl;
}
bool GetAccess(HashSet<string> set, string key) {
return set.Contains(key);
}