public void Test_AppScope()
        {
            // app 'd92535a1' has over 50 app scope topics.
            CountDownLatch           cd     = new CountDownLatch(1);
            KiiListResult <KiiTopic> topics = null;
            Exception exception             = null;

            Kii.ListTopics((KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 20)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNull(exception);
            Assert.IsTrue(topics.HasNext);
            Assert.IsNotNull(topics.PaginationKey);
            Assert.AreEqual(50, topics.Result.Count);

            Kii.ListTopics(topics.PaginationKey, (KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 20)))
            {
                Assert.Fail("Callback not fired.");
            }

            Assert.IsNull(exception);
            Assert.IsTrue(topics.Result.Count > 0);
        }
Exemple #2
0
        public void Test_1_1_CountAllWhenObjectNotExists()
        {
            string    bucketName = "TestBucket" + CurrentTimeMillis();
            KiiBucket bucket     = Kii.Bucket(bucketName);

            KiiBucket      callbackBucket = null;
            KiiQuery       callbackQuery  = null;
            int            count          = -1;
            Exception      exp            = null;
            CountDownLatch cd             = new CountDownLatch(1);

            bucket.Count((KiiBucket b, KiiQuery q, int c, Exception e) => {
                callbackBucket = b;
                callbackQuery  = q;
                count          = c;
                exp            = e;
                cd.Signal();
            });

            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNotNull(callbackBucket);
            Assert.AreEqual(bucket.Uri, callbackBucket.Uri);
            Assert.IsNotNull(callbackQuery);
            Assert.AreEqual(new KiiQuery(null).ToString(), callbackQuery.ToString());
            Assert.IsNull(exp);
            Assert.AreEqual(0, count);
        }
        public void UpdateWithoutIdentityDataByPseudoUserTest_Async()
        {
            CountDownLatch cd = new CountDownLatch(1);

            KiiUser actual = null;

            KiiUser.RegisterAsPseudoUser(null, (KiiUser registeredUser, Exception e1) => {
                UserFields userFields  = new UserFields();
                userFields.Displayname = "disp";
                userFields.Country     = "JP";
                userFields["age"]      = 30;

                registeredUser.Update(null, userFields, (KiiUser updatedUser, Exception e2) => {
                    actual = updatedUser;
                    cd.Signal();
                });
            });

            if (!cd.Wait(new TimeSpan(0, 0, 0, 10)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsTrue(actual.IsPseudoUser);
            Assert.IsNull(actual.Username);
            Assert.IsNull(actual.Email);
            Assert.IsNull(actual.Phone);
            Assert.AreEqual("disp", actual.Displayname);
            Assert.AreEqual("JP", actual.Country);
            Assert.AreEqual(30, actual.GetInt("age"));
        }
        public void UpdateByDeletedPseudoUserTest_Async()
        {
            CountDownLatch cd = new CountDownLatch(1);
            Exception      e  = null;

            KiiUser.RegisterAsPseudoUser(null, (KiiUser registeredUser, Exception e1) => {
                string currentUserId          = Kii.CurrentUser.ID;
                string currentUserAccessToken = KiiUser.AccessToken;
                KiiUser.CurrentUser.Delete();
                Kii.CurrentUser = KiiUser.GetById(currentUserId);
                KiiCloudEngine.UpdateAccessToken(currentUserAccessToken);

                UserFields userFields  = new UserFields();
                userFields.Displayname = "disp";
                userFields.Country     = "JP";
                userFields["age"]      = 30;
                userFields.RemoveFromServer("birthday");

                Kii.CurrentUser.Update(null, userFields, (KiiUser updatedUser, Exception e2) => {
                    e = e2;
                    cd.Signal();
                });
            });

            if (!cd.Wait(new TimeSpan(0, 0, 0, 10)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOfType(typeof(ForbiddenException), e);
        }
        public void UpdateWithExistingPhoneTest_Async()
        {
            string  unixTime = CurrentTimeMillis().ToString();
            string  username = "******" + unixTime;
            KiiUser newUser  = KiiUser.BuilderWithName(username).Build();

            newUser.Register("123ABC");

            CountDownLatch cd = new CountDownLatch(1);
            Exception      e  = null;

            IdentityData.Builder builder      = IdentityData.Builder.CreateWithPhone(this.existingUser.Phone);
            IdentityData         identityData = builder.Build();

            newUser.Update(identityData, null, (KiiUser user, Exception e2) => {
                e = e2;
                cd.Signal();
            });

            if (!cd.Wait(new TimeSpan(0, 0, 0, 10)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOfType(typeof(ConflictException), e);
        }
        public void Test_GroupScopeTrueASync()
        {
            LogIn("test-user-00001");
            client.AddResponse(204, null);
            string         groupID   = "test_group";
            string         topicName = "test_topic";
            KiiTopic       topic     = KiiGroup.GroupWithID(groupID).Topic(topicName);
            CountDownLatch cd        = new CountDownLatch(1);
            bool?          existence = null;
            Exception      exception = null;

            topic.Exists((bool?b, Exception e) => {
                existence = b;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsTrue(existence.Value);
            Assert.IsNull(exception);
            Assert.AreEqual(KiiHttpMethod.HEAD, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "groups", groupID, "topics", topicName), client.RequestUrl [0]);
        }
        public void Test_UserScopeFalseASync()
        {
            LogIn("test-user-00001");
            client.AddResponse(new NotFoundException("", null, "", NotFoundException.Reason.TOPIC_NOT_FOUND));
            string         userID    = "test_user";
            string         topicName = "test_topic";
            KiiTopic       topic     = KiiUser.UserWithID(userID).Topic(topicName);
            CountDownLatch cd        = new CountDownLatch(1);
            bool?          existence = null;
            Exception      exception = null;

            topic.Exists((bool?b, Exception e) => {
                existence = b;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsFalse(existence.Value);
            Assert.IsNull(exception);
            Assert.AreEqual(KiiHttpMethod.HEAD, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "users", userID, "topics", topicName), client.RequestUrl [0]);
        }
        public void Test_CompleteResetPasswordWithEmail_Async()
        {
            CountDownLatch cd = new CountDownLatch(1);
            Exception      ex = null;

            string identifier  = "*****@*****.**";
            string newPassword = "******";
            string pinCode     = "1234";

            client.AddResponse(204, null);
            KiiUser.CompleteResetPassword(identifier, pinCode, newPassword, (Exception e) => {
                ex = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNull(ex);

            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod [0]);
            Assert.AreEqual("https://api.kii.com/api/apps/appId/users/EMAIL:" + identifier + "/password/complete-reset", client.RequestUrl [0]);
            Assert.AreEqual(String.Format("{{\"newPassword\":\"{0}\",\"pinCode\":\"{1}\"}}", newPassword, pinCode), client.RequestBody[0]);
            Assert.AreEqual("application/vnd.kii.CompletePasswordResetRequest+json", client.RequestHeader[0]["content-type"]);
        }
        public void Test_GroupScopeASync()
        {
            LogIn("test-user-00001");
            AddMockResponse(200, new string[] { "Topic1", "Topic2" }, null);
            string                   groupID   = "test_group";
            CountDownLatch           cd        = new CountDownLatch(1);
            KiiListResult <KiiTopic> topics    = null;
            Exception                exception = null;

            KiiGroup.GroupWithID(groupID).ListTopics((KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }

            Assert.IsNull(exception);
            Assert.IsFalse(topics.HasNext);
            Assert.IsNull(topics.PaginationKey);
            Assert.AreEqual(2, topics.Result.Count);
            Assert.AreEqual("Topic1", topics.Result[0].Name);
            Assert.AreEqual("kiicloud://" + Utils.Path("groups", groupID, "topics", "Topic1"), topics.Result[0].Uri.ToString());
            Assert.AreEqual("Topic2", topics.Result[1].Name);
            Assert.AreEqual("kiicloud://" + Utils.Path("groups", groupID, "topics", "Topic2"), topics.Result[1].Uri.ToString());

            Assert.AreEqual(KiiHttpMethod.GET, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "groups", groupID, "topics"), client.RequestUrl [0]);
            Assert.AreEqual("Bearer " + KiiUser.AccessToken, client.RequestHeader [0]["Authorization"]);
        }
Exemple #10
0
        public void Test()
        {
            string          groupID   = GetGroupID();
            string          groupName = "group-" + DateTime.Now.Ticks.ToString();
            IList <KiiUser> members   = new List <KiiUser>();

            members.Add(this.member1);
            members.Add(this.member2);

            CountDownLatch cd        = new CountDownLatch(1);
            KiiGroup       group     = null;
            Exception      exception = null;

            KiiGroup.RegisterGroupWithID(groupID, groupName, members, (KiiGroup result, Exception e) => {
                group     = result;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNull(exception);

            Assert.AreEqual(groupID, group.ID);
            Assert.AreEqual(groupName, group.Name);
            Assert.AreEqual(KiiUser.CurrentUser.ID, group.Owner.ID);

            members = group.ListMembers();
            Assert.AreEqual(3, members.Count); // 2 members and owner
        }
        public void Test_UserScopeWithEmptyResultASync()
        {
            LogIn("test-user-00001");
            AddMockResponse(200, new string[] {}, null);
            string                   userID    = "test_user";
            CountDownLatch           cd        = new CountDownLatch(1);
            KiiListResult <KiiTopic> topics    = null;
            Exception                exception = null;

            KiiUser.UserWithID(userID).ListTopics((KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }

            Assert.IsNull(exception);
            Assert.IsFalse(topics.HasNext);
            Assert.IsNull(topics.PaginationKey);
            Assert.AreEqual(0, topics.Result.Count);

            Assert.AreEqual(KiiHttpMethod.GET, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "users", userID, "topics"), client.RequestUrl [0]);
            Assert.AreEqual("Bearer " + KiiUser.AccessToken, client.RequestHeader [0]["Authorization"]);
        }
        public void Test_UserScope404ASync()
        {
            LogIn("test-user-00001");
            client.AddResponse(new NotFoundException("", null, "", NotFoundException.Reason.ACL_NOT_FOUND));
            string         userID    = "test_user";
            string         topicName = "test_topic";
            KiiTopic       topic     = KiiUser.UserWithID(userID).Topic(topicName);
            CountDownLatch cd        = new CountDownLatch(1);
            IList <KiiACLEntry <KiiTopic, TopicAction> > entries = null;
            Exception exception = null;

            topic.ListAclEntries((IList <KiiACLEntry <KiiTopic, TopicAction> > result, Exception e) => {
                entries   = result;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }

            Assert.IsNull(entries);
            Assert.IsInstanceOfType(typeof(NotFoundException), exception);
            Assert.AreEqual(KiiHttpMethod.GET, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "users", userID, "topics", topicName, "acl"), client.RequestUrl [0]);
        }
        public void PutIdentityByUnnoticedNormalUserTest_Async()
        {
            CountDownLatch cd = new CountDownLatch(1);

            string    unixTime = CurrentTimeMillis().ToString();
            string    username = "******" + unixTime;
            string    email    = username + "@kii.com";
            string    phone    = "+874" + unixTime.Substring(unixTime.Length - 9, 9);
            Exception e        = null;

            KiiUser user = KiiUser.BuilderWithName(username).WithEmail(email).WithPhone(phone).Build();

            user.Register("password");

            KiiUser unnoticedNormalUser = KiiUser.CreateByUri(user.Uri);

            IdentityData.Builder builder = IdentityData.Builder.CreateWithName(username);
            builder.WithEmail(email);
            builder.WithPhone(phone);
            IdentityData identityData = builder.Build();

            unnoticedNormalUser.PutIdentity(identityData, null, "123ABC", (KiiUser u, Exception e2) => {
                e = e2;
                cd.Signal();
            });

            if (!cd.Wait(new TimeSpan(0, 0, 0, 10)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOfType(typeof(AlreadyHasIdentityException), e);
        }
        public void Test_Status403ASync()
        {
            LogIn("test-user-00001");
            client.AddResponse(new ForbiddenException("", null, ""));
            string         topicName = "test_topic";
            KiiTopic       topic     = Kii.Topic(topicName);
            CountDownLatch cd        = new CountDownLatch(1);
            bool?          existence = null;
            Exception      exception = null;

            topic.Exists((bool?b, Exception e) => {
                existence = b;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsFalse(existence.HasValue);
            Assert.IsNotNull(exception);
            Assert.IsInstanceOfType(typeof(ForbiddenException), exception);
            Assert.AreEqual(KiiHttpMethod.HEAD, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "topics", topicName), client.RequestUrl [0]);
        }
        public void PutIdentityByDeletedUserTest_Async()
        {
            CountDownLatch cd = new CountDownLatch(1);

            string    unixTime = CurrentTimeMillis().ToString();
            string    username = "******" + unixTime;
            string    email    = username + "@kii.com";
            string    phone    = "+874" + unixTime.Substring(unixTime.Length - 9, 9);
            Exception e        = null;

            KiiUser.RegisterAsPseudoUser(null, (KiiUser registeredUser, Exception e1) => {
                IdentityData.Builder builder = IdentityData.Builder.CreateWithName(username);
                builder.WithEmail(email);
                builder.WithPhone(phone);
                IdentityData identityData = builder.Build();

                string currentUserId          = Kii.CurrentUser.ID;
                string currentUserAccessToken = KiiUser.AccessToken;
                KiiUser.CurrentUser.Delete();
                Kii.CurrentUser = KiiUser.GetById(currentUserId);
                KiiCloudEngine.UpdateAccessToken(currentUserAccessToken);

                Kii.CurrentUser.PutIdentity(identityData, null, "123ABC", (KiiUser user, Exception e2) => {
                    e = e2;
                    cd.Signal();
                });
            });

            if (!cd.Wait(new TimeSpan(0, 0, 0, 10)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOfType(typeof(ForbiddenException), e);
        }
Exemple #16
0
        public void TestReadObjectInBucket()
        {
            CountDownLatch cd     = new CountDownLatch(1);
            KiiUser        user   = KiiUser.CurrentUser;
            KiiBucket      bucket = user.Bucket("aclTest");
            KiiBucketAcl   acl    = bucket.Acl(BucketAction.READ_OBJECTS_IN_BUCKET);

            KiiACLEntry <KiiBucket, BucketAction> entry = acl.Subject(KiiAnyAuthenticatedUser.Get());

            entry.Save(ACLOperation.GRANT, (KiiACLEntry <KiiBucket, BucketAction> savedEntry, Exception e) => {
                Assert.IsNull(e);
                Assert.AreEqual(BucketAction.READ_OBJECTS_IN_BUCKET, savedEntry.Action);
                bucket.ListAclEntries((IList <KiiACLEntry <KiiBucket, BucketAction> > list, Exception e2) => {
                    Assert.IsNull(e2);
                    CheckEntriesAfterAdd(user, list);

                    // Remove ACL Entry
                    entry.Save(ACLOperation.REVOKE, (KiiACLEntry <KiiBucket, BucketAction> savedEntry2, Exception e3) => {
                        Assert.IsNull(e3);
                        Assert.AreEqual(BucketAction.READ_OBJECTS_IN_BUCKET, savedEntry2.Action);

                        bucket.ListAclEntries((IList <KiiACLEntry <KiiBucket, BucketAction> > list2, Exception e4) => {
                            Assert.IsNull(e4);
                            CheckEntriesAfterRemove(user, list2);
                            cd.Signal();
                        });
                    });
                });
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 10)))
            {
                Assert.Fail("Callback not fired.");
            }
        }
        public void PutIdentityTest_Async()
        {
            CountDownLatch cd = new CountDownLatch(1);

            string  unixTime = CurrentTimeMillis().ToString();
            string  username = "******" + unixTime;
            string  email    = username + "@kii.com";
            string  phone    = "+874" + unixTime.Substring(unixTime.Length - 9, 9);
            KiiUser actual   = null;

            KiiUser.RegisterAsPseudoUser(null, (KiiUser registeredUser, Exception e1) => {
                IdentityData.Builder builder = IdentityData.Builder.CreateWithName(username);
                builder.WithEmail(email);
                builder.WithPhone(phone);
                IdentityData identityData = builder.Build();

                registeredUser.PutIdentity(identityData, null, "123ABC", (KiiUser user, Exception e2) => {
                    actual = user;
                    cd.Signal();
                });
            });

            if (!cd.Wait(new TimeSpan(0, 0, 0, 10)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsFalse(actual.IsPseudoUser);
            Assert.AreEqual(username, actual.Username);
            Assert.AreEqual(email, actual.Email);
            Assert.AreEqual(phone, actual.Phone);
            Assert.IsNull(actual.Displayname);
            Assert.IsNull(actual.Country);
            Assert.IsFalse(actual.Has("age"));
        }
        public void Test_GroupScopeAsync()
        {
            KiiGroup group = Kii.Group("test_group");

            group.Save();
            group.Topic("group_topic").Save();

            CountDownLatch cd = new CountDownLatch(1);
            IList <KiiACLEntry <KiiTopic, TopicAction> > entries = null;
            Exception exception = null;

            KiiTopic topic = group.Topic("group_topic");

            topic.ListAclEntries((IList <KiiACLEntry <KiiTopic, TopicAction> > result, Exception e) => {
                entries   = result;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 20)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNull(exception);
            Assert.AreEqual(4, entries.Count);
        }
        public void UpdateByUnnoticedNormalUserTest_Async()
        {
            CountDownLatch cd = new CountDownLatch(1);

            string unixTime = CurrentTimeMillis().ToString();
            string username = "******" + unixTime;
            string email    = username + "@kii.com";
            string phone    = "+874" + unixTime.Substring(unixTime.Length - 9, 9);

            KiiUser user = KiiUser.BuilderWithName(username).Build();

            user.Register("password");

            KiiUser unnoticedNormalUser = KiiUser.CreateByUri(user.Uri);

            IdentityData.Builder builder = IdentityData.Builder.CreateWithName(username);
            builder.WithEmail(email);
            builder.WithPhone(phone);
            IdentityData identityData = builder.Build();

            KiiUser updatedUser = null;

            unnoticedNormalUser.Update(identityData, null, (KiiUser u, Exception e2) => {
                updatedUser = u;
                cd.Signal();
            });

            if (!cd.Wait(new TimeSpan(0, 0, 0, 10)))
            {
                Assert.Fail("Callback not fired.");
            }

            Assert.AreEqual(email, updatedUser.Email);
            Assert.AreEqual(phone, updatedUser.Phone);
        }
Exemple #20
0
        public void Test_LoginWithFacebookToken_Async()
        {
            // mock http client
            MockHttpClientFactory factory = new MockHttpClientFactory();

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

            // set refresh response
            this.setFBLoginResponse(client);
            this.setRefreshResponse(client);

            // perform login.
            Assert.IsNull(KiiUser.CurrentUser);
            CountDownLatch cd   = new CountDownLatch(1);
            KiiUser        user = null;
            Exception      exp  = null;

            KiiUser.LoginWithFacebookToken("fbtoken-dummy", (KiiUser usr, Exception e) => {
                user = usr;
                exp  = e;
                cd.Signal();
            });

            if (!cd.Wait())
            {
                Assert.Fail("Callback has not called.");
            }

            Assert.IsNull(exp);
            Assert.IsNotNull(user);

            // verify properties for return logged in user.
            Assert.AreEqual("user22222", user.ID);
            Assert.AreEqual("dummyAccessToken2", user.GetAccessTokenDictionary() ["access_token"]);
            Assert.AreEqual("User 2", user.Displayname);

            // verify social access token dictionary.
            Dictionary <string, object> dict = user.GetSocialAccessTokenDictionary();

            Assert.AreEqual(true, dict["kii_new_user"]);
            Assert.AreEqual("100000188475423", dict["provider_user_id"]);
            Assert.AreEqual(KiiCorp.Cloud.Storage.Connector.Provider.FACEBOOK, dict["provider"]);
            Assert.AreEqual("fbtoken-dummy", dict["oauth_token"]);

            // verify properties for current user.
            user = KiiUser.CurrentUser;
            Assert.AreEqual("user22222", user.ID);
            Assert.AreEqual("dummyAccessToken2", user.GetAccessTokenDictionary() ["access_token"]);
            Assert.AreEqual("User 2", user.Displayname);

            // verify social access token dictionary.
            dict = user.GetSocialAccessTokenDictionary();
            Assert.AreEqual(true, dict["kii_new_user"]);
            Assert.AreEqual("100000188475423", dict["provider_user_id"]);
            Assert.AreEqual(KiiCorp.Cloud.Storage.Connector.Provider.FACEBOOK, dict["provider"]);
            Assert.AreEqual("fbtoken-dummy", dict["oauth_token"]);
        }
        public void Test_UserScopeWithPaginationASync()
        {
            LogIn("test-user-00001");
            AddMockResponse(200, new string[] { "Topic1", "Topic2" }, "ab=12/12+");
            AddMockResponse(200, new string[] { "Topic3" }, null);
            string                   userID    = "test_user";
            CountDownLatch           cd        = new CountDownLatch(1);
            KiiListResult <KiiTopic> topics    = null;
            Exception                exception = null;

            KiiUser.UserWithID(userID).ListTopics((KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }

            Assert.IsNull(exception);
            Assert.IsTrue(topics.HasNext);
            Assert.AreEqual("ab=12/12+", topics.PaginationKey);
            Assert.AreEqual(2, topics.Result.Count);
            Assert.AreEqual("Topic1", topics.Result[0].Name);
            Assert.AreEqual("kiicloud://" + Utils.Path("users", userID, "topics", "Topic1"), topics.Result[0].Uri.ToString());
            Assert.AreEqual("Topic2", topics.Result[1].Name);
            Assert.AreEqual("kiicloud://" + Utils.Path("users", userID, "topics", "Topic2"), topics.Result[1].Uri.ToString());

            Assert.AreEqual(KiiHttpMethod.GET, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "users", userID, "topics"), client.RequestUrl [0]);
            Assert.AreEqual("Bearer " + KiiUser.AccessToken, client.RequestHeader [0]["Authorization"]);

            String paginationKey = topics.PaginationKey;

            cd        = new CountDownLatch(1);
            topics    = null;
            exception = null;

            KiiUser.UserWithID(userID).ListTopics(paginationKey, (KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsFalse(topics.HasNext);
            Assert.IsNull(topics.PaginationKey);
            Assert.AreEqual(1, topics.Result.Count);
            Assert.AreEqual("Topic3", topics.Result[0].Name);
            Assert.AreEqual("kiicloud://" + Utils.Path("users", userID, "topics", "Topic3"), topics.Result[0].Uri.ToString());

            Assert.AreEqual(KiiHttpMethod.GET, client.RequestMethod [1]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "users", userID, "topics") + "?paginationKey=" + Uri.EscapeUriString("ab=12/12+"), client.RequestUrl [1]);
            Assert.AreEqual("Bearer " + KiiUser.AccessToken, client.RequestHeader [1]["Authorization"]);
        }
        public void Test_LoginWithTokenAndExpiresAt_Async()
        {
            Kii.Initialize(APP_ID, APP_KEY, Kii.Site.US);

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

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

            // set refresh response
            client.AddResponse(200,
                               "{" +
                               "\"userID\" : \"dummyID\"," +
                               "\"loginName\" : \"dummyUser\"" +
                               "}");

            // perform login.
            Assert.IsNull(KiiUser.CurrentUser);
            DateTime       expiresAt    = DateTime.UtcNow;
            CountDownLatch cd           = new CountDownLatch(1);
            KiiUser        loggedInUser = null;
            Exception      exp          = null;

            KiiUser.LoginWithToken("dummyToken", expiresAt, (KiiUser user, Exception e) => {
                loggedInUser = user;
                exp          = e;
                cd.Signal();
            });

            if (!cd.Wait())
            {
                Assert.Fail("Callback has not called.");
            }

            Assert.IsNull(exp);
            Assert.IsNotNull(loggedInUser);
            Assert.IsNotNull(KiiUser.CurrentUser);
            Assert.AreEqual(loggedInUser, KiiUser.CurrentUser);

            // verify token dictionary
            Dictionary <string, object> tokenBundle = KiiUser.CurrentUser.GetAccessTokenDictionary();

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

            // verify refresh request
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", APP_ID, "users", "me");

            Assert.AreEqual(url, client.RequestUrl[0]);
            Assert.AreEqual(KiiHttpMethod.GET, 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="));
        }
        public void Test_GroupScope()
        {
            KiiGroup group = Kii.Group("test_group");

            group.Save();
            for (int i = 0; i < 51; i++)
            {
                group.Topic("user_topic" + i).Save();
            }

            CountDownLatch           cd     = new CountDownLatch(1);
            KiiListResult <KiiTopic> topics = null;
            Exception exception             = null;

            group.ListTopics((KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 20)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNull(exception);
            Assert.IsTrue(topics.HasNext);
            Assert.IsNotNull(topics.PaginationKey);
            Assert.AreEqual(50, topics.Result.Count);

            string paginationKey = topics.PaginationKey;

            cd        = new CountDownLatch(1);
            topics    = null;
            exception = null;

            group.ListTopics(paginationKey, (KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 20)))
            {
                Assert.Fail("Callback not fired.");
            }

            Assert.IsNull(exception);
            Assert.IsFalse(topics.HasNext);
            Assert.IsNull(topics.PaginationKey);
            Assert.AreEqual(1, topics.Result.Count);
        }
        public void ResetPasswordViaEmailTest()
        {
            CountDownLatch cd = new CountDownLatch(1);
            Exception      ex = null;

            KiiUser.ResetPassword(this.phone, KiiUser.NotificationMethod.EMAIL, (Exception e) => {
                ex = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNull(ex);
        }
        public void Test_AppScope()
        {
            CountDownLatch cd        = new CountDownLatch(1);
            bool?          existence = null;
            Exception      exception = null;

            Kii.ListTopics((KiiListResult <KiiTopic> t, Exception e1) => {
                if (e1 != null)
                {
                    exception = e1;
                    cd.Signal();
                    return;
                }
                if (t.Result.Count == 0)
                {
                    exception = new Exception("There are no app scope topic in App");
                    cd.Signal();
                    return;
                }
                foreach (KiiTopic topic in t.Result)
                {
                    topic.Exists((bool?result, Exception e2) => {
                        exception = e2;
                        existence = result;
                        cd.Signal();
                        return;
                    });
                }
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 60)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNull(exception);
            Assert.IsTrue(existence.Value);
        }
        public void PutIdentityWithRemoveFieldsTest_Async()
        {
            CountDownLatch cd = new CountDownLatch(1);

            string  unixTime = CurrentTimeMillis().ToString();
            string  username = "******" + unixTime;
            string  email    = username + "@kii.com";
            string  phone    = "+874" + unixTime.Substring(unixTime.Length - 9, 9);
            KiiUser actual   = null;

            UserFields userFields = new UserFields();

            userFields.Country     = "US";
            userFields.Displayname = "disp";
            userFields["birthday"] = "1978/7/22";
            userFields["age"]      = 30;

            KiiUser.RegisterAsPseudoUser(userFields, (KiiUser registeredUser, Exception e1) => {
                IdentityData.Builder builder = IdentityData.Builder.CreateWithName(username);
                builder.WithEmail(email);
                builder.WithPhone(phone);
                IdentityData identityData = builder.Build();

                UserFields removeFields = new UserFields();
                removeFields.RemoveDisplayname(); // remove displayname only local.
                removeFields.RemoveFromServer("birthday");
                removeFields["newFields"] = "new!!";

                registeredUser.PutIdentity(identityData, removeFields, "123ABC", (KiiUser user, Exception e2) => {
                    actual = user;
                    cd.Signal();
                });
            });

            if (!cd.Wait(new TimeSpan(0, 0, 0, 10)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsFalse(actual.IsPseudoUser);
            Assert.AreEqual(username, actual.Username);
            Assert.AreEqual(email, actual.Email);
            Assert.AreEqual(phone, actual.Phone);
            Assert.AreEqual("disp", actual.Displayname);
            Assert.AreEqual("US", actual.Country);
            Assert.IsFalse(actual.Has("birthday"));
            Assert.AreEqual(30, actual.GetInt("age"));
        }
        public void Test_IdentifyIsNull_Async()
        {
            CountDownLatch cd = new CountDownLatch(1);

            client.AddResponse(204, null);
            Exception ex = null;

            KiiUser.ResetPassword(null, KiiUser.NotificationMethod.SMS, (Exception e) => {
                ex = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNotNull(ex);
            Assert.IsTrue(ex is ArgumentException);
        }
        public void RegisterAsPseudoUserTest_Async()
        {
            CountDownLatch cd = new CountDownLatch(1);

            KiiUser.RegisterAsPseudoUser(null, (KiiUser user, Exception e) => {
                cd.Signal();
            });

            if (!cd.Wait(new TimeSpan(0, 0, 0, 10)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNotNull(KiiUser.AccessToken);
            Assert.IsNull(KiiUser.CurrentUser.Displayname);
            Assert.IsNull(KiiUser.CurrentUser.Country);
            Assert.IsTrue(KiiUser.AccessToken.Length > 10);
            Assert.IsTrue(KiiUser.CurrentUser.IsPseudoUser);
        }
Exemple #29
0
        public void RegisterGroupWithIDASyncTest()
        {
            string         groupID   = GetGroupID();
            string         groupName = "group-" + DateTime.Now.Ticks.ToString();
            List <KiiUser> members   = new List <KiiUser>();

            members.Add(KiiUser.CreateByUri(new Uri("kiicloud://users/user-member-0001")));
            members.Add(KiiUser.CreateByUri(new Uri("kiicloud://users/user-member-0002")));

            client.AddResponse(200, "{\"groupID\":\"" + groupID + "\"}");

            CountDownLatch cd        = new CountDownLatch(1);
            KiiGroup       group     = null;
            Exception      exception = null;

            KiiGroup.RegisterGroupWithID(groupID, groupName, members, (KiiGroup result, Exception e) => {
                group     = result;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNull(exception);

            Assert.AreEqual(groupID, group.ID);
            Assert.AreEqual(groupName, group.Name);
            Assert.AreEqual(KiiUser.CurrentUser.ID, group.Owner.ID);

            JsonObject expectedRequestBody = new JsonObject();
            JsonArray  expectedMembers     = new JsonArray();

            expectedMembers.Put("user-member-0001");
            expectedMembers.Put("user-member-0002");
            expectedRequestBody.Put("owner", KiiUser.CurrentUser.ID);
            expectedRequestBody.Put("name", groupName);
            expectedRequestBody.Put("members", expectedMembers);

            Assert.AreEqual(KiiHttpMethod.PUT, client.RequestMethod [0]);
            Assert.AreEqual("https://api.kii.com/api/apps/" + AppID + "/groups/" + groupID, client.RequestUrl [0]);
            KiiAssertion.AssertJson(expectedRequestBody, new JsonObject(client.RequestBody [0]));
            Assert.AreEqual("application/vnd.kii.GroupCreationRequest+json", client.RequestHeader[0]["content-type"]);
        }
        public void Test_CompleteResetPasswordWithoutPinCode_Async()
        {
            CountDownLatch cd = new CountDownLatch(1);
            Exception      ex = null;

            string identifier  = Guid.NewGuid().ToString();
            string newPassword = "******";

            KiiUser.CompleteResetPassword(identifier, null, newPassword, (Exception e) => {
                ex = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNotNull(ex);
            Assert.IsTrue(ex is ArgumentException);
        }