public void Test_2_15_CreateWithUri_InCloud_NoPatch_NotOverwrite_EtagMatch()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;
            string objId = "abcd-1234";

            // save object to cloud
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{\"createdAt\" : 1, \"modifiedAt\" : 1}", "1");

            KiiObject obj = KiiObject.CreateByUri(new Uri("kiicloud://buckets/test/objects/" + objId));

            obj["key"] = "value";
            obj.SaveAllFields(true);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);
            string etag = (string)SDKTestHack.GetField(obj, "mEtag");

            Assert.AreEqual("1", etag);

            // server send error response (assume object already updated in server side)
            string mockResponseBody = "{\"errorCode\" : \"OBJECT_VERSION_IN_STALE\"}";

            client.AddResponse(new CloudException(409, mockResponseBody));
            obj["key1"] = "value1";
            CloudException exp = null;

            try {
                obj.SaveAllFields(false);
                Assert.Fail("Exception not thrown");
            } catch (CloudException e) {
                exp = e;
            }
            Assert.IsNotNull(exp);
            Assert.AreEqual(409, exp.Status);
            Assert.AreEqual(mockResponseBody, exp.Body);


            // request contains if-match
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "test", "objects", objId);

            Assert.AreEqual(url, client.RequestUrl[1]);
            Assert.AreEqual(KiiHttpMethod.PUT, client.RequestMethod[1]);
            MockHttpHeaderList headerList = client.RequestHeader[1];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv="));
            Assert.AreEqual("1", headerList["If-Match"]);

            string     reqBody          = "{ \"key\" : \"value\", \"key1\" : \"value1\"}";
            JsonObject expectedBodyJson = new JsonObject(reqBody);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[1]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
        public void Test_3_19_CreateWithNoId_InCloud_Patch_NoOverwrite_EtagNone()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;

            // set response
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{\"objectID\" : \"abcd-1234\", \"createdAt\" : 1, \"modifiedAt\" : 1}", "1");

            KiiObject obj = Kii.Bucket("test").NewKiiObject();

            obj["key"] = "value";
            obj.SaveAllFields(false);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);

            client.AddResponse(201, "{\"createdAt\" : 1, \"modifiedAt\" : 1}", "1");
            // set etag to null
            SDKTestHack.SetField(obj, "mEtag", null);
            obj.SaveAllFields(false);
        }
        public void Test_0504_remove_member()
        {
            // Create test user for group owner
            string  userID = TextUtils.generateUUID();
            KiiUser user   = KiiUser.CreateByUri(new Uri("kiicloud://users/" + userID));

            SDKTestHack.SetField(Kii.Instance, "mLoginUser", user);
            string authToken = TextUtils.generateUUID();

            KiiCloudEngine.UpdateAccessToken(authToken);

            // Prepare group
            string   groupName = TextUtils.randomAlphaNumeric(10);
            string   groupID   = TextUtils.randomAlphaNumeric(26);
            KiiGroup group     = Kii.Group(groupName);

            SDKTestHack.SetField(group, "mId", groupID);
            SDKTestHack.SetField(group, "mOwnerId", userID);

            // Prepare KiiUser for member
            string  memberID = TextUtils.generateUUID();
            KiiUser member   = KiiUser.CreateByUri(new Uri("kiicloud://users/" + memberID));

            group.RemoveUser(member);

            // Check value
            HashSet <KiiUser> addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            HashSet <KiiUser> removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");

            Assert.AreEqual(0, addUsers.Count);
            Assert.AreEqual(1, removeUsers.Count);

            // Prepare mock response for adding user
            client.AddResponse(204, "");

            // Save group
            group.Save();

            // Verify request
            Assert.AreEqual(1, client.RequestUrl.Count);
            Assert.AreEqual(1, client.RequestHeader.Count);
            string requestUrl = AppUtil.getUrlOfSite(AppConst.APP_SITE, AppConst.APP_ID, "groups", groupID, "members", memberID);

            Assert.AreEqual(requestUrl, client.RequestUrl[0]);
            Assert.AreEqual("DELETE", client.RequestMethod[0].ToString());
            IList <MockHttpHeaderList> headers = client.RequestHeader;

            Assert.AreEqual(AppConst.APP_ID, client.RequestHeader[0]["X-Kii-AppID"]);
            Assert.AreEqual(AppConst.APP_KEY, client.RequestHeader[0]["X-Kii-AppKey"]);
            Assert.AreEqual("Bearer " + authToken, client.RequestHeader[0]["Authorization"]);
            Assert.AreEqual(null, client.RequestBody[0]);

            // Verify
            Assert.AreEqual(groupID, group.ID);

            addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");
            Assert.AreEqual(0, addUsers.Count);
            Assert.AreEqual(0, removeUsers.Count);
        }
        public void Test_2_19_CreateWithUri_InCloud_NoPatch_NoOverwrite_EtagNone()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;
            string objId = "abcd-1234";

            // prepare response
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{\"createdAt\" : 1, \"modifiedAt\" : 1}", "1");

            // save object to cloud
            KiiObject obj = KiiObject.CreateByUri(new Uri("kiicloud://buckets/test/objects/" + objId));

            obj["key"] = "value";
            obj.SaveAllFields(true);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);
            string etag = (string)SDKTestHack.GetField(obj, "mEtag");

            Assert.AreEqual("1", etag);

            // set etag to null
            SDKTestHack.SetField(obj, "mEtag", null);
            obj.SaveAllFields(false);
        }
        public void Test_0200_Delete()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;

            // set response
            MockHttpClient client = factory.Client;

            this.SetStandardSaveResponse(client);

            KiiObject obj = Kii.Bucket("test").NewKiiObject();

            obj["name"] = "Kii";
            obj["age"]  = 18;

            obj.Save();

            Assert.AreEqual("d8dc9f29-0fb9-48be-a80c-ec60fddedb54", obj.ID);
            Assert.AreEqual(1337039114613, obj.CreatedTime);
            Assert.AreEqual(1337039114613, obj.ModifedTime);
            // set body content-type
            SDKTestHack.SetField(obj, "mBodyContentType", "text/plain");

            // set Response
            client.AddResponse(204, "");

            obj.Delete();
            Assert.AreEqual(-1, obj.CreatedTime);
            Assert.AreEqual(-1, obj.ModifedTime);
            Assert.AreEqual(null, obj.Uri);
            Assert.IsNull(obj.BodyContentType);
        }
Example #6
0
        public void Test_Login_With_TokenExpiration_Negative()
        {
            Kii.Initialize(APP_ID, APP_KEY, Kii.Site.US);

            // mock http client
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;
            MockHttpClient client = factory.Client;

            // set login response
            client.AddResponse(200,
                               "{" +
                               "\"id\" : \"efgh\"," +
                               "\"access_token\" : \"dummyToken\"," +
                               "\"expires_in\" : -9223372036854775" +
                               "}");

            // set refresh response
            client.AddResponse(200,
                               "{" +
                               "\"id\" : \"efgh\"," +
                               "\"username\" : \"dummyUser\"" +
                               "}");

            // set access token expiration to negative
            FieldInfo fieldInfo = typeof(Kii).GetField("INSTANCE", BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic);
            Kii       kii       = (Kii)fieldInfo.GetValue(null);

            SDKTestHack.SetField(kii, "mAccessTokenExpiration", -1L);
            Assert.AreEqual(-1L, Kii.AccessTokenExpiration);

            // perform login.
            Assert.IsNull(KiiUser.CurrentUser);
            KiiUser.LogIn("dummyUser", "111111");
            Assert.IsNotNull(KiiUser.CurrentUser);
            IDictionary tokenBundle = KiiUser.CurrentUser.GetAccessTokenDictionary();

            Assert.AreEqual("dummyToken", tokenBundle["access_token"]);
            Assert.AreEqual(DateTime.MinValue, tokenBundle["expires_at"]);

            // verify login request
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "oauth2", "token");

            Assert.AreEqual(url, client.RequestUrl[0]);
            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[0]);
            MockHttpHeaderList headerList = client.RequestHeader[0];

            Assert.AreEqual(APP_ID, headerList["X-Kii-AppID"]);
            Assert.AreEqual(APP_KEY, headerList["X-Kii-AppKey"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv="));

            // verify body.
            JsonObject requestBody = new JsonObject(client.RequestBody[0]);

            Assert.AreEqual(2, requestBody.Length());
            Assert.AreEqual("dummyUser", requestBody.GetString("username"));
            Assert.AreEqual("111111", requestBody.GetString("password"));
        }
Example #7
0
        public void Test_1_3_GroupWithID_ExistsInCloud_Refresh_AddMembers()
        {
            // mock refresh response
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;
            MockHttpClient client = (MockHttpClient)factory.Client;

            client.AddResponse(200, "{" +
                               "\"groupID\" : \"dummyId\"," +
                               "\"name\" : \"MyGroup\"," +
                               "\"owner\" : \"user1234\"" +
                               "}");

            // mock response for adding user
            client.AddResponse(201, "{}");

            // create group
            string   groupId = "dummyId";
            KiiGroup group   = KiiGroup.GroupWithID(groupId);

            Assert.IsNull(group.Name);

            // refresh
            group.Refresh();
            Assert.AreEqual("MyGroup", group.Name);

            // add member
            string  memberID = TextUtils.generateUUID();
            KiiUser member   = KiiUser.UserWithID(memberID);

            group.AddUser(member);

            HashSet <KiiUser> addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            HashSet <KiiUser> removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");

            Assert.AreEqual(1, addUsers.Count);
            Assert.AreEqual(0, removeUsers.Count);

            // Save group
            group.Save();

            // Verify add member request
            string requestUrl = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "groups", "dummyId", "members", memberID);

            Assert.AreEqual(requestUrl, client.RequestUrl[1]);
            Assert.AreEqual("PUT", client.RequestMethod[1].ToString());
            Assert.AreEqual(AppConst.APP_ID, client.RequestHeader[1]["X-Kii-AppID"]);
            Assert.AreEqual(AppConst.APP_KEY, client.RequestHeader[1]["X-Kii-AppKey"]);
            Assert.AreEqual("Bearer token1234", client.RequestHeader[1]["Authorization"]);
            Assert.AreEqual(null, client.RequestBody[1]);

            // Verify local userList get empty.
            Assert.AreEqual(groupId, group.ID);
            addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");
            Assert.AreEqual(0, addUsers.Count);
            Assert.AreEqual(0, removeUsers.Count);
        }
Example #8
0
        public void SetUp()
        {
            Kii.Initialize(AppConst.APP_ID, AppConst.APP_KEY, AppConst.APP_SITE);
            KiiUser user = KiiUser.UserWithID("user1234");

            SDKTestHack.SetField(Kii.Instance, "mLoginUser", user);
            KiiCloudEngine.UpdateAccessToken("token1234");
        }
Example #9
0
        public void Test_4_20_CreateWithQuery_InCloud_Patch_NotOverwrite_EtagMatch()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;

            // set response
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{" +
                               "\"results\" : [" +
                               "{" +
                               "\"_created\" : 1," +
                               "\"_modified\" : 1," +
                               "\"key\" : \"value\"," +
                               "\"_id\" : \"abcd-1234\"," +
                               "\"_version\" : \"1\" " +
                               "}]" +
                               "}");

            KiiQueryResult <KiiObject> result = Kii.Bucket("test").Query(null);

            Assert.AreEqual(1, result.Count);
            KiiObject obj = result[0];

            client.AddResponse(201, "{\"_created\" : 1, \"_modified\" : 1, \"_id\" : \"abcd-1234\"}", "1");
            obj["key1"] = "value1";
            obj.Save(false);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);
            string etag = (string)SDKTestHack.GetField(obj, "mEtag");

            Assert.AreEqual("1", etag);

            // check request
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "test", "objects", "abcd-1234");

            Assert.AreEqual(url, client.RequestUrl[1]);
            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[1]);
            MockHttpHeaderList headerList = client.RequestHeader[1];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            Assert.AreEqual("1", headerList["If-Match"]);
            Assert.AreEqual("PATCH", headerList["X-HTTP-Method-Override"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv=")); Assert.AreEqual("1", headerList["If-Match"]);

            string     reqBody          = "{\"key1\" : \"value1\"}";
            JsonObject expectedBodyJson = new JsonObject(reqBody);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[1]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
        public void Test_0508_AddAndRemoveUser_null_KiiUser_instance()
        {
            // Prepare KiiUser for member
            KiiUser memberUser1 = KiiUser.CreateByUri(new Uri("kiicloud://users/" + TextUtils.generateUUID()));
            KiiUser memberUser2 = KiiUser.CreateByUri(new Uri("kiicloud://users/" + TextUtils.generateUUID()));
            KiiUser nullUser    = null;

            // Prepare group
            string   groupName = TextUtils.randomAlphaNumeric(10);
            string   groupID   = TextUtils.randomAlphaNumeric(26);
            KiiGroup group     = Kii.Group(groupName);

            SDKTestHack.SetField(group, "mId", groupID);
            SDKTestHack.SetField(group, "mOwnerId", TextUtils.generateUUID());
            group.AddUser(memberUser1);
            group.RemoveUser(memberUser2);
            // Check value
            HashSet <KiiUser> addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            HashSet <KiiUser> removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");

            Assert.AreEqual(1, addUsers.Count);
            Assert.AreEqual(1, removeUsers.Count);

            // Add user
            try
            {
                group.AddUser(nullUser);
                Assert.Fail("Should throw ArgumentException");
            }
            catch (ArgumentException)
            {
                // Pass
            }
            // Check value
            addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");
            Assert.AreEqual(1, addUsers.Count);
            Assert.AreEqual(1, removeUsers.Count);

            // Remove user
            try
            {
                group.RemoveUser(nullUser);
                Assert.Fail("Should throw ArgumentException");
            }
            catch (ArgumentException)
            {
                // Pass
            }
            // Check value
            addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");
            Assert.AreEqual(1, addUsers.Count);
            Assert.AreEqual(1, removeUsers.Count);
        }
        public void Test_2_22_CreateWithUri_InCloud_Patch_Overwrite_EtagNone()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;
            string objId = "abcd-1234";

            // prepare response
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{\"createdAt\" : 1, \"modifiedAt\" : 1}", "1");

            // save object to cloud
            KiiObject obj = KiiObject.CreateByUri(new Uri("kiicloud://buckets/test/objects/" + objId));

            obj["key"] = "value";
            obj.SaveAllFields(true);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);
            string etag = (string)SDKTestHack.GetField(obj, "mEtag");

            Assert.AreEqual("1", etag);

            // set etag to null
            SDKTestHack.SetField(obj, "mEtag", null);

            client.AddResponse(201, "{\"_created\" : 1, \"_modified\" : 1}");
            obj["key1"] = "value1";

            // object save successfully as Overwrite is true.
            obj.Save(true);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);

            // request contains x-http-method-override
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "test", "objects", objId);

            Assert.AreEqual(url, client.RequestUrl[1]);
            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[1]);
            MockHttpHeaderList headerList = client.RequestHeader[1];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv="));
            Assert.AreEqual("PATCH", headerList["X-HTTP-Method-Override"]);
            string     reqBody          = "{\"key1\" : \"value1\"}";
            JsonObject expectedBodyJson = new JsonObject(reqBody);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[1]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
        public void Test_3_20_CreateWithNoId_InCloud_Patch_NoOverwrite_EtagMatch()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;
            string objId = "abcd-1234";

            // prepare response
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{\"objectID\" : \"abcd-1234\", \"createdAt\" : 1, \"modifiedAt\" : 1}", "1");

            KiiObject obj = Kii.Bucket("test").NewKiiObject();

            obj["key"] = "value";
            obj.SaveAllFields(true);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);
            string etag = (string)SDKTestHack.GetField(obj, "mEtag");

            Assert.AreEqual("1", etag);


            client.AddResponse(201, "{\"_created\" : 1, \"_modified\" : 1}");

            obj["key1"] = "value1";
            obj.Save(false);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);

            // request contains x-http-method-override, if-match header
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "test", "objects", objId);

            Assert.AreEqual(url, client.RequestUrl[1]);
            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[1]);
            MockHttpHeaderList headerList = client.RequestHeader[1];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv="));
            Assert.AreEqual("PATCH", headerList["X-HTTP-Method-Override"]);
            Assert.AreEqual("1", headerList["If-Match"]);
            string     reqBody          = "{\"key1\" : \"value1\"}";
            JsonObject expectedBodyJson = new JsonObject(reqBody);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[1]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
        public void Test_1_16_CreateWithId_InCloud_NoPatch_Overwrite_EtagNone()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;
            string objId = "abcd-1234";

            // prepare response
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{\"createdAt\" : 1, \"modifiedAt\" : 1}", "1");

            // save object to cloud
            KiiObject obj = Kii.Bucket("test").NewKiiObject(objId);

            obj["key"] = "value";
            obj.SaveAllFields(true);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);
            string etag = (string)SDKTestHack.GetField(obj, "mEtag");

            Assert.AreEqual("1", etag);

            // Set Etag to null
            SDKTestHack.GetField(obj, "mEtag");
            client.AddResponse(201, "{\"createdAt\" : 1, \"modifiedAt\" : 1}", "1");

            // save successful as overwrite is true.
            obj.SaveAllFields(true);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);

            // check request
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "test", "objects", objId);

            Assert.AreEqual(url, client.RequestUrl[1]);
            Assert.AreEqual(KiiHttpMethod.PUT, client.RequestMethod[1]);
            MockHttpHeaderList headerList = client.RequestHeader[1];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv="));
            string     reqBody          = "{ \"key\" : \"value\"}";
            JsonObject expectedBodyJson = new JsonObject(reqBody);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[1]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
        public void Test_2_14_CreateWithUri_InCloud_NoPatch_NotOverwrite_EtagMatch()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;
            string objId = "abcd-1234";

            // save object to cloud
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{\"createdAt\" : 1, \"modifiedAt\" : 1}", "1");

            KiiObject obj = KiiObject.CreateByUri(new Uri("kiicloud://buckets/test/objects/" + objId));

            obj["key"] = "value";
            obj.SaveAllFields(true);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);
            string etag = (string)SDKTestHack.GetField(obj, "mEtag");

            Assert.AreEqual("1", etag);

            // update object
            client.AddResponse(201, "{\"createdAt\" : 1, \"modifiedAt\" : 1}", "1");
            obj["key1"] = "value1";
            obj.SaveAllFields(false);

            // check request
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "test", "objects", objId);

            Assert.AreEqual(url, client.RequestUrl[1]);
            Assert.AreEqual(KiiHttpMethod.PUT, client.RequestMethod[1]);
            MockHttpHeaderList headerList = client.RequestHeader[1];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv="));
            Assert.AreEqual("1", headerList["If-Match"]);

            string     reqBody          = "{ \"key\" : \"value\", \"key1\" : \"value1\"}";
            JsonObject expectedBodyJson = new JsonObject(reqBody);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[1]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
        public void Test_0509_AddAndRemoveUser_notsaved_group()
        {
            // Prepare KiiUser for member
            KiiUser memberUser1 = KiiUser.CreateByUri(new Uri("kiicloud://users/" + TextUtils.generateUUID()));
            KiiUser memberUser2 = KiiUser.CreateByUri(new Uri("kiicloud://users/" + TextUtils.generateUUID()));

            // Prepare group
            string   groupName = TextUtils.randomAlphaNumeric(10);
            string   groupID   = TextUtils.randomAlphaNumeric(26);
            KiiGroup group     = Kii.Group(groupName);
            // Check value
            HashSet <KiiUser> addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            HashSet <KiiUser> removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");

            Assert.AreEqual(0, addUsers.Count);
            Assert.AreEqual(0, removeUsers.Count);

            // Add user
            group.AddUser(memberUser1);
            // Check value
            addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");
            Assert.AreEqual(1, addUsers.Count);
            Assert.AreEqual(0, removeUsers.Count);

            // Remove user
            group.RemoveUser(memberUser2);
            // Check value
            addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");
            Assert.AreEqual(1, addUsers.Count);
            Assert.AreEqual(0, removeUsers.Count);

            // Remove user for user1
            group.RemoveUser(memberUser1);
            // Check value
            addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");
            Assert.AreEqual(0, addUsers.Count);
            Assert.AreEqual(0, removeUsers.Count);
        }
        public void Test_1_6_DeleteBody()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;

            // set response
            MockHttpClient client = factory.Client;

            this.SetStandardResponse(client);

            KiiObject obj = KiiObject.CreateByUri(new Uri("kiicloud://buckets/images/objects/object1234"));

            // set body content-type
            SDKTestHack.SetField(obj, "mBodyContentType", "text/plain");

            obj.DeleteBody();

            // check body content-type is cleared.
            Assert.IsNull(obj.BodyContentType);
        }
Example #17
0
        public void Test_0101_RemoveMyself_FromGroupMember()
        {
            // Create KiiUser for group member
            long    milliseconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
            string  memberName   = "kiiGroupMember-" + milliseconds;
            KiiUser member       = KiiUser.BuilderWithName(memberName).Build();

            member.Register("pass1234");
            string memberToken = KiiUser.AccessToken;

            // Create KiiUser for group member
            string  ownerName = "kiiGroupOwner-" + milliseconds;
            KiiUser owner     = KiiUser.BuilderWithName(ownerName).Build();

            owner.Register("pass1234");

            // Create group
            KiiGroup group = Kii.Group("testGroup");

            // Add member and save
            group.AddUser(member);
            group.Save();

            // Auth as member
            KiiUser.LogOut();
            KiiUser.LoginWithToken(memberToken);

            // Try to remove myself from the group
            group.RemoveUser(KiiUser.CurrentUser);
            group.Save();

            // Check value
            HashSet <KiiUser> addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            HashSet <KiiUser> removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");

            Assert.AreEqual(0, addUsers.Count);
            Assert.AreEqual(0, removeUsers.Count);
        }
Example #18
0
        private IEnumerator TestStep()
        {
            string  username = StringUtils.RandomAlphabetic(20);
            string  password = StringUtils.RandomAlphabetic(20);
            KiiUser user     = KiiUser.BuilderWithName(username).Build();
            var     task     = RegisterUser(user, password, (KiiUser u, Exception e) => {
                if (e != null)
                {
                    throw new TestFailException();
                }
                Debug.Log("Register user.");
            });

            yield return(StartCoroutine(task));

            // Create object
            string    bucketName = StringUtils.RandomAlphabetic(10);
            string    objectId   = StringUtils.RandomAlphabetic(10);
            KiiBucket bucket     = user.Bucket(bucketName);
            KiiObject obj        = bucket.NewKiiObject(objectId);
            string    objKey     = "KeyA";
            string    objValue   = "ValueA";

            obj [objKey] = objValue;

            Exception         exp      = null;
            bool              endFlag  = false;
            KiiObjectCallback callback = (KiiObject obj1, Exception e) => {
                exp     = e;
                endFlag = true;
            };

            obj.SaveAllFields(true, callback);
            while (!endFlag)
            {
                yield return(new WaitForSeconds(1));
            }
            if (exp != null)
            {
                throw new TestFailException();
            }
            Debug.Log("object created");

            // refresh
            endFlag = false;
            exp     = null;
            obj.Refresh(callback);
            while (!endFlag)
            {
                yield return(new WaitForSeconds(1));
            }
            if (exp != null)
            {
                throw new TestFailException();
            }
            Debug.Log("object refreshed");

            // Update with patch
            obj [objKey] = "UpdateA";
            SDKTestHack.SetField(obj, "mEtag", "999");
            endFlag = false;
            exp     = null;
            obj.SaveAllFields(false, callback);
            while (!endFlag)
            {
                yield return(new WaitForSeconds(1));
            }
            if (exp == null)
            {
                throw new TestFailException();
            }
            if (409 != ((CloudException)exp).Status)
            {
                throw new TestFailException();
            }
            Debug.Log("object updated has expected conflict response .");
            resultFlag = true;
        }
        public void Test_0502_CreateGroup_with_member()
        {
            // Create test user for group owner
            string  userID = TextUtils.generateUUID();
            KiiUser user   = KiiUser.CreateByUri(new Uri("kiicloud://users/" + userID));

            SDKTestHack.SetField(Kii.Instance, "mLoginUser", user);
            string authToken = TextUtils.generateUUID();

            KiiCloudEngine.UpdateAccessToken(authToken);

            // Prepare KiiUser for member
            string  memberID = TextUtils.generateUUID();
            KiiUser member   = KiiUser.CreateByUri(new Uri("kiicloud://users/" + memberID));

            // Prepare group
            string   groupName = TextUtils.randomAlphaNumeric(10);
            KiiGroup group     = Kii.Group(groupName, new List <KiiUser>()
            {
                member
            });

            // Check value
            HashSet <KiiUser> addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            HashSet <KiiUser> removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");

            Assert.AreEqual(1, addUsers.Count);
            Assert.AreEqual(0, removeUsers.Count);

            // Prepare mock response for creating group
            string     groupID   = TextUtils.randomAlphaNumeric(26);
            JsonObject resEntity = new JsonObject(new Dictionary <string, object>()
            {
                { "groupID", groupID },
                { "notFoundUsers", new JsonArray() }
            });

            client.AddResponse(201, resEntity.ToString());

            // Save group
            group.Save();

            // Verify request
            Assert.AreEqual(1, client.RequestUrl.Count);
            Assert.AreEqual(1, client.RequestHeader.Count);
            string requestUrl = AppUtil.getUrlOfSite(AppConst.APP_SITE, AppConst.APP_ID, "groups");

            Assert.AreEqual(requestUrl, client.RequestUrl[0]);
            Assert.AreEqual("POST", client.RequestMethod[0].ToString());
            IList <MockHttpHeaderList> headers = client.RequestHeader;

            Assert.AreEqual(AppConst.APP_ID, client.RequestHeader[0]["X-Kii-AppID"]);
            Assert.AreEqual(AppConst.APP_KEY, client.RequestHeader[0]["X-Kii-AppKey"]);
            Assert.AreEqual("Bearer " + authToken, client.RequestHeader[0]["Authorization"]);

            JsonObject requestBody = new JsonObject(client.RequestBody[0]);

            Assert.AreEqual(3, requestBody.Length());
            Assert.AreEqual(groupName, requestBody.GetString("name"));
            Assert.AreEqual(userID, requestBody.GetString("owner"));
            Assert.AreEqual(1, requestBody.GetJsonArray("members").Length());
            Assert.AreEqual(memberID, requestBody.GetJsonArray("members").GetString(0));

            // Verify
            Assert.AreEqual(groupID, group.ID);
            Assert.AreEqual(user.ID, group.Owner.ID);

            addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");
            Assert.AreEqual(0, addUsers.Count);
            Assert.AreEqual(0, removeUsers.Count);
        }
        public void Test_0506_AddAndRemoveUser_different_KiiUser_instance()
        {
            // Prepare KiiUser for member
            KiiUser memberUser1 = KiiUser.CreateByUri(new Uri("kiicloud://users/" + TextUtils.generateUUID()));
            KiiUser memberUser2 = KiiUser.CreateByUri(new Uri("kiicloud://users/" + TextUtils.generateUUID()));
            KiiUser memberUser3 = KiiUser.CreateByUri(new Uri("kiicloud://users/" + TextUtils.generateUUID()));

            // Prepare group
            string   groupName = TextUtils.randomAlphaNumeric(10);
            string   groupID   = TextUtils.randomAlphaNumeric(26);
            KiiGroup group     = Kii.Group(groupName);

            SDKTestHack.SetField(group, "mId", groupID);
            SDKTestHack.SetField(group, "mOwnerId", TextUtils.generateUUID());
            group.AddUser(memberUser1);
            group.RemoveUser(memberUser2);
            // Check value
            HashSet <KiiUser> addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            HashSet <KiiUser> removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");

            Assert.AreEqual(1, addUsers.Count);
            Assert.AreEqual(1, removeUsers.Count);

            // Add user
            group.AddUser(memberUser3);
            // Check value
            addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");
            Assert.AreEqual(2, addUsers.Count);
            Assert.AreEqual(1, removeUsers.Count);

            // Add user again
            KiiUser memberUser3a = KiiUser.CreateByUri(memberUser3.Uri);

            group.AddUser(memberUser3a);
            // Check value
            addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");
            Assert.AreEqual(2, addUsers.Count);
            Assert.AreEqual(1, removeUsers.Count);

            // Remove user
            group.RemoveUser(memberUser1);
            // Check value
            addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");
            Assert.AreEqual(1, addUsers.Count);
            Assert.AreEqual(2, removeUsers.Count);

            // Remove user again
            KiiUser memberUser1a = KiiUser.CreateByUri(memberUser1.Uri);

            group.RemoveUser(memberUser1a);
            // Check value
            addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");
            Assert.AreEqual(1, addUsers.Count);
            Assert.AreEqual(2, removeUsers.Count);

            // Add user
            KiiUser memberUser2a = KiiUser.CreateByUri(memberUser2.Uri);

            group.AddUser(memberUser2a);
            // Check value
            addUsers    = (HashSet <KiiUser>)SDKTestHack.GetField(group, "addUsers");
            removeUsers = (HashSet <KiiUser>)SDKTestHack.GetField(group, "removeUsers");
            Assert.AreEqual(2, addUsers.Count);
            Assert.AreEqual(1, removeUsers.Count);
        }
        public void Test_1_21_CreateWithId_InCloud_Patch_NotOverwrite_EtagNotMatch()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;
            string objId = "abcd-1234";

            // prepare response
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{\"createdAt\" : 1, \"modifiedAt\" : 1}", "1");

            // save object to cloud
            KiiObject obj = Kii.Bucket("test").NewKiiObject(objId);

            obj["key"] = "value";
            obj.SaveAllFields(true);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);
            string etag = (string)SDKTestHack.GetField(obj, "mEtag");

            Assert.AreEqual("1", etag);


            string mockResponseBody = "{" +
                                      "\"errorCode\" : \"OBJECT_VERSION_IN_STALE\"," +
                                      "\"message\" : \"object version did not matched\" " +
                                      "}";

            client.AddResponse(new CloudException(409, mockResponseBody));


            obj["key1"] = "value1";
            CloudException exp = null;

            try {
                obj.Save(false);
                Assert.Fail("Exception not thrown");
            } catch (CloudException e) {
                exp = e;
            }
            Assert.IsNotNull(exp);
            Assert.AreEqual(409, exp.Status);
            Assert.AreEqual(mockResponseBody, exp.Body);

            // request contains x-http-method-override, if-match header
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "test", "objects", objId);

            Assert.AreEqual(url, client.RequestUrl[1]);
            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[1]);
            MockHttpHeaderList headerList = client.RequestHeader[1];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            Assert.AreEqual("PATCH", headerList["X-HTTP-Method-Override"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv="));
            Assert.AreEqual("1", headerList["If-Match"]);
            string     reqBody          = "{\"key1\" : \"value1\"}";
            JsonObject expectedBodyJson = new JsonObject(reqBody);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[1]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }