Example #1
0
        public void Test_RenRenkSync()
        {
            AddMockLoginAndRefreshResponse(200, "user-0000-1111-2222", "token-aaaa-bbbb-cccc", 3600, true);
            Dictionary <string, string> accessCredential = new Dictionary <string, string>();

            accessCredential.Add("accessToken", "renren-9999-8888-7777");
            KiiUser user = KiiUser.LoginWithSocialNetwork(accessCredential, Provider.RENREN);

            Assert.IsNotNull(user);
            Assert.AreEqual(user.ID, "user-0000-1111-2222");
            Assert.AreEqual(user.Username, "username_user-0000-1111-2222");
            Assert.AreEqual(user.Displayname, "display_user-0000-1111-2222");
            Assert.AreEqual(KiiUser.CurrentUser.ID, "user-0000-1111-2222");
            Assert.AreEqual(KiiUser.AccessToken, "token-aaaa-bbbb-cccc");
            Assert.AreEqual("renren-9999-8888-7777", user.GetSocialAccessTokenDictionary() ["oauth_token"]);
            Assert.AreEqual(true, user.GetSocialAccessTokenDictionary() ["kii_new_user"]);

            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod [0]);
            Assert.AreEqual("appId", client.RequestHeader[0]["X-Kii-AppID"]);
            Assert.AreEqual("appKey", client.RequestHeader[0]["X-Kii-AppKey"]);
            Assert.AreEqual("application/vnd.kii.AuthTokenRenRenRequest+json", client.RequestHeader[0]["content-type"]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "integration", "renren"), client.RequestUrl[0]);
            Assert.AreEqual(1, new JsonObject(client.RequestBody[0]).Length());
            Assert.AreEqual("renren-9999-8888-7777", new JsonObject(client.RequestBody[0]).Get("accessToken"));
        }
Example #2
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"]);
        }
Example #3
0
        public void Test_GoogleSync()
        {
            LogIn("user-0000-1111-2222");
            Dictionary <string, object> socialAccessTokenDictionary = new Dictionary <string, object>();

            socialAccessTokenDictionary.Add("accessToken", "dummy");
            KiiUser.CurrentUser.SetSocialAccessTokenDictionary(socialAccessTokenDictionary);

            AddMockUnLinkAndRefreshResponse(200, "user-0000-1111-2222");
            KiiUser user = KiiUser.CurrentUser.UnLinkWithSocialNetwork(Provider.GOOGLE);

            Assert.IsNotNull(user);
            Assert.AreEqual(user.ID, "user-0000-1111-2222");
            Assert.AreEqual(user.Username, "username_user-0000-1111-2222");
            Assert.AreEqual(user.Displayname, "display_user-0000-1111-2222");
            Assert.AreEqual(KiiUser.CurrentUser.ID, "user-0000-1111-2222");
            Assert.AreEqual(KiiUser.AccessToken, "token-aaaa-bbbb-cccc");
            Assert.AreEqual(0, user.GetSocialAccessTokenDictionary().Count);

            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod [0]);
            Assert.AreEqual("appId", client.RequestHeader[0]["X-Kii-AppID"]);
            Assert.AreEqual("appKey", client.RequestHeader[0]["X-Kii-AppKey"]);
            Assert.IsFalse(client.RequestHeader[0].values.ContainsKey("content-type"));
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "users", "me", "google", "unlink"), client.RequestUrl[0]);
            Assert.IsNull(client.RequestBody[0]);
        }
Example #4
0
        public void Test_QQASync()
        {
            AddMockLoginAndRefreshResponse(200, "user-0000-1111-2222", "token-aaaa-bbbb-cccc", 3600, false);
            Dictionary <string, string> accessCredential = new Dictionary <string, string>();

            accessCredential.Add("accessToken", "qq-9999-8888-7777");
            accessCredential.Add("openID", "qq-openid-6666-5555");

            CountDownLatch cd        = new CountDownLatch(1);
            KiiUser        user      = null;
            Exception      exception = null;

            KiiUser.LoginWithSocialNetwork(accessCredential, Provider.QQ, (KiiUser u, Exception e) => {
                user      = u;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNull(exception);
            Assert.IsNotNull(user);
            Assert.AreEqual(user.ID, "user-0000-1111-2222");
            Assert.AreEqual(user.Username, "username_user-0000-1111-2222");
            Assert.AreEqual(user.Displayname, "display_user-0000-1111-2222");
            Assert.AreEqual(KiiUser.CurrentUser.ID, "user-0000-1111-2222");
            Assert.AreEqual(KiiUser.AccessToken, "token-aaaa-bbbb-cccc");
            Assert.AreEqual("qq-9999-8888-7777", user.GetSocialAccessTokenDictionary() ["oauth_token"]);
            Assert.AreEqual(false, user.GetSocialAccessTokenDictionary() ["kii_new_user"]);
            Assert.AreEqual("qq-openid-6666-5555", user.GetSocialAccessTokenDictionary() ["openID"]);

            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod [0]);
            Assert.AreEqual("appId", client.RequestHeader[0]["X-Kii-AppID"]);
            Assert.AreEqual("appKey", client.RequestHeader[0]["X-Kii-AppKey"]);
            Assert.AreEqual("application/vnd.kii.AuthTokenQQRequest+json", client.RequestHeader[0]["content-type"]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "integration", "qq"), client.RequestUrl[0]);
            Assert.AreEqual(2, new JsonObject(client.RequestBody[0]).Length());
            Assert.AreEqual("qq-9999-8888-7777", new JsonObject(client.RequestBody[0]).Get("accessToken"));
            Assert.AreEqual("qq-openid-6666-5555", new JsonObject(client.RequestBody[0]).Get("openID"));
        }
Example #5
0
        public void Test_LoginWithFacebookToken()
        {
            // setup mock http client
            MockHttpClientFactory factory = new MockHttpClientFactory();

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

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

            // verify properties for return logged in user.
            KiiUser user = KiiUser.LoginWithFacebookToken("fbtoken-dummy");

            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"]);
        }
Example #6
0
    static string KiiUserToString(KiiUser user)
    {
        StringBuilder builder = new StringBuilder();

        if (user == null)
        {
            builder.Append("No user logged in!");
            return(builder.ToString());
        }

        builder.AppendLine("[Kii UserID]");
        builder.Append("* ");
        builder.AppendLine(user.ID);
        Dictionary <string, object> userToken = user.GetAccessTokenDictionary();

        builder.AppendLine("[KiiUser AccessToken]");
        builder.AppendLine(ExpandDictionary(userToken));

        // Add linked social account information.
        builder.AppendLine("[Linked social accounts]");
        Dictionary <Provider, SocialAccountInfo> socialAccounts = user.LinkedSocialAccounts;

        foreach (KeyValuePair <Provider, SocialAccountInfo> account in socialAccounts)
        {
            builder.Append("* ");
            builder.Append(account.Key.ToString());
            builder.Append(" = { ");
            builder.Append("id :" + account.Value.SocialAccountId + " createdAt: " + account.Value.CreatedAt);
            builder.AppendLine(" }");
        }

        // Social tokens
        var socialToken = user.GetSocialAccessTokenDictionary();

        builder.AppendLine("[Social AccessToken]");
        builder.Append(ExpandDictionary(socialToken));

        return(builder.ToString());
    }
Example #7
0
        public void Test_QQASync()
        {
            LogIn("user-0000-1111-2222");
            AddMockUnLinkAndRefreshResponse(200, "user-0000-1111-2222");

            CountDownLatch cd        = new CountDownLatch(1);
            KiiUser        user      = null;
            Exception      exception = null;

            Dictionary <string, object> socialAccessTokenDictionary = new Dictionary <string, object>();

            socialAccessTokenDictionary.Add("accessToken", "dummy");
            KiiUser.CurrentUser.SetSocialAccessTokenDictionary(socialAccessTokenDictionary);

            KiiUser.CurrentUser.UnLinkWithSocialNetwork(Provider.QQ, (KiiUser u, Exception e) => {
                user      = u;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNull(exception);
            Assert.IsNotNull(user);
            Assert.AreEqual(user.ID, "user-0000-1111-2222");
            Assert.AreEqual(user.Username, "username_user-0000-1111-2222");
            Assert.AreEqual(user.Displayname, "display_user-0000-1111-2222");
            Assert.AreEqual(KiiUser.CurrentUser.ID, "user-0000-1111-2222");
            Assert.AreEqual(KiiUser.AccessToken, "token-aaaa-bbbb-cccc");
            Assert.AreEqual(0, user.GetSocialAccessTokenDictionary().Count);

            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod [0]);
            Assert.AreEqual("appId", client.RequestHeader[0]["X-Kii-AppID"]);
            Assert.AreEqual("appKey", client.RequestHeader[0]["X-Kii-AppKey"]);
            Assert.IsFalse(client.RequestHeader[0].values.ContainsKey("content-type"));
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "users", "me", "qq", "unlink"), client.RequestUrl[0]);
            Assert.IsNull(client.RequestBody[0]);
        }
Example #8
0
        public void Test_FBLogin_After_PseudoUserCreation()
        {
            // setup mock http client
            MockHttpClientFactory factory = new MockHttpClientFactory();

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

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

            KiiUser user = KiiUser.RegisterAsPseudoUser(null);

            Assert.AreEqual("user11111", user.ID);
            Assert.AreEqual("dummyAccessToken1", user.GetAccessTokenDictionary() ["access_token"]);

            KiiUser.LoginWithFacebookToken("fbtoken-dummy");
            System.Collections.Generic.IList <MockHttpHeaderList> l = client.RequestHeader;
            string auth = l[2]["Authorization"];

            Assert.AreEqual("Bearer " + "dummyAccessToken2", auth);

            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.
            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"]);
        }
Example #9
0
    void OnGUI()
    {
        gui.Label(10, 10, 500, 50, (current == APP_US ? "US:" : "JP:") + current.AppID);
        if (gui.Button(10, 55, 245, 100, "Switch APP to US"))
        {
            this.current = APP_US;
            KiiInitializeBehaviour.Instance.SwitchApp(this.current.AppID, this.current.AppKey, this.current.Site);
            this.message = string.Format("Switch APP to {0} : {1}\n", this.current.Site, this.current.AppID);
        }
        if (gui.Button(265, 55, 245, 100, "Switch APP to JP"))
        {
            this.current = APP_JP;
            KiiInitializeBehaviour.Instance.SwitchApp(this.current.AppID, this.current.AppKey, this.current.Site);
            this.message = string.Format("Switch APP to {0} : {1}\n", this.current.Site, this.current.AppID);
        }
        if (gui.Button(10, 160, 160, 100, "Create Random User"))
        {
            this.message = "";
            string  username = "******" + DateTime.Now.Ticks.ToString();
            KiiUser user     = KiiUser.BuilderWithName(username).Build();
            user.Register("pa$$sword", (KiiUser u, Exception e1) => {
                if (e1 == null)
                {
                    this.message = "SUCCESS:\nuser="******"Failed to register user", e1);
                }
            });
        }
        if (gui.Button(180, 160, 160, 100, "Facebook Login"))
        {
            this.message = "";
            var connector = this.gameObject.AddComponent <KiiSocialNetworkConnector> ();
            connector.LogIn(Provider.FACEBOOK,
                            (KiiUser user, Provider provider, Exception exception) => {
                if (exception == null)
                {
                    this.message = "SUCCESS:\nuser="******"Failed to login with social connector", exception);
                }
                // Destroy connector if required.
                Destroy(connector);
            });
        }
        if (gui.Button(350, 160, 160, 100, "Show user info"))
        {
            this.message = "";
            KiiUser u = KiiUser.CurrentUser;
            if (u == null)
            {
                this.message = "User is not logged in :P";
                return;
            }
            this.message  = "*** User tokens ***\n";
            this.message += GetDictionaryContents(u.GetAccessTokenDictionary());
            this.message += "\n*** Social tokens ***\n";
            this.message += GetDictionaryContents(u.GetSocialAccessTokenDictionary());
        }
        if (gui.Button(10, 265, 160, 100, "Create 5-Object\n(1 Object/sec)"))
        {
            this.message = "";
            KiiUser u = KiiUser.CurrentUser;
            if (u == null)
            {
                this.message = "User is not logged in :P";
                return;
            }
            StartCoroutine(CreateNewObjects(5));
        }
        if (gui.Button(10, 370, 160, 100, string.Format("Create topic\n({0})", topicname)))
        {
            this.message = "";
            KiiUser u = KiiUser.CurrentUser;
            if (u == null)
            {
                this.message = "User is not logged in :P";
                return;
            }
            KiiTopic topic = u.Topic(topicname);
            topic.Save((KiiTopic retTopic, Exception retException) => {
                if (retException == null)
                {
                    this.message = "SUCCESS:\ntopic=" + retTopic.Uri.ToString();
                }
                else
                {
                    this.ShowException("Failed to create topic", retException);
                }
            });
        }
        if (gui.Button(180, 370, 160, 100, "Subscribe topic"))
        {
            this.message = "";
            KiiUser u = KiiUser.CurrentUser;
            if (u == null)
            {
                this.message = "User is not logged in :P";
                return;
            }
            KiiTopic topic = u.Topic(topicname);
            u.PushSubscription.Subscribe(topic, (KiiSubscribable retSub, Exception retException) => {
                if (retException == null)
                {
                    this.message = "SUCCESS:\nsubscribed topic=" + ((KiiTopic)retSub).Uri.ToString();
                }
                else
                {
                    this.ShowException("Failed to subscribe topic", retException);
                }
            });
        }
        if (gui.Button(350, 370, 160, 100, "Check topic existence"))
        {
            this.message = "";
            KiiUser u = KiiUser.CurrentUser;
            if (u == null)
            {
                this.message = "User is not logged in :P";
                return;
            }
            KiiTopic topic = u.Topic(topicname);
            try
            {
                bool retExists = topic.Exists();
                this.message = "SUCCESS:\ntopic existence=" + retExists;
            }
            catch (Exception e)
            {
                this.ShowException("Failed to check topic existence", e);
            }
        }
        if (gui.Button(10, 480, 160, 100, "Install Push"))
        {
            this.message = "";
            KiiUser u = KiiUser.CurrentUser;
            if (u == null)
            {
                this.message = "User is not logged in :P";
                return;
            }
                        #if UNITY_IPHONE
            bool development = true;
            KiiPushInstallation.DeviceType deviceType = KiiPushInstallation.DeviceType.IOS;
                        #elif UNITY_ANDROID
            bool development = true;
            KiiPushInstallation.DeviceType deviceType = KiiPushInstallation.DeviceType.ANDROID;
                        #else
            this.message = "Push feature does not support on this platform :P";
            return;
                        #endif

            this.kiiPushPlugin.RegisterPush((string pushToken, Exception e0) => {
                if (e0 != null)
                {
                    this.ShowException("Failed to register push : " + this.kiiPushPlugin.SenderID, e0);
                    return;
                }
                this.message = "Push token : " + pushToken;
                KiiUser.PushInstallation(development).Install(pushToken, deviceType, (Exception e1) => {
                    if (e1 != null)
                    {
                        this.ShowException("Failed to install push", e1);
                    }
                    else
                    {
                        this.message += "SUCCESS:\ninstall push=" + pushToken;
                        SavePushInformation(deviceType, pushToken);
                    }
                });
            });
        }
        if (gui.Button(180, 480, 160, 100, "Uninstall Push"))
        {
            this.message = "";
            KiiUser u = KiiUser.CurrentUser;
            if (u == null)
            {
                this.message = "User is not logged in :P";
                return;
            }
                        #if UNITY_IPHONE
            bool development = true;
            KiiPushInstallation.DeviceType deviceType = KiiPushInstallation.DeviceType.IOS;
                        #elif UNITY_ANDROID
            bool development = true;
            KiiPushInstallation.DeviceType deviceType = KiiPushInstallation.DeviceType.ANDROID;
                        #else
            this.message = "Push feature does not support on this platform :P";
            return;
                        #endif

            string pushToken = LoadPushInformation(deviceType);
            KiiUser.PushInstallation(development).Uninstall(pushToken, deviceType, (Exception e0) => {
                if (e0 != null)
                {
                    this.ShowException("Failed to uninstall push", e0);
                    return;
                }
                this.kiiPushPlugin.UnregisterPush((Exception e1) => {
                    if (e1 != null)
                    {
                        this.ShowException("Failed to unregister push", e1);
                    }
                    else
                    {
                        this.message = "SUCCESS:\nuninstall push=" + pushToken;
                        ClearPushInformation(deviceType);
                    }
                });
            });
        }
        if (gui.Button(350, 480, 160, 100, "Send Push"))
        {
            this.message = "";
            KiiUser u = KiiUser.CurrentUser;
            if (u == null)
            {
                this.message = "User is not logged in :P";
                return;
            }

            KiiTopic topic = u.Topic(topicname);

            // Build a push message.
            KiiPushMessageData data = new KiiPushMessageData()
                                      .Put("message", "Hi, switch app :)");
            KiiPushMessage message = KiiPushMessage.BuildWith(data).SendAppID(true).Build();

            // Send the push message.
            topic.SendMessage(message, (KiiPushMessage retMessage, Exception retException) => {
                if (retException == null)
                {
                    this.message = "SUCCESS:\nsend message=" + retMessage.ToString();
                }
                else
                {
                    this.ShowException("Failed to send message", retException);
                }
            });
        }
        int messageHeight = 590;
        gui.Label(10, messageHeight, 500, screenHeight - messageHeight, this.message);
    }