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_0300_LoginWithToken_OK() { Kii.Initialize("appId", "appKey", Kii.Site.US); MockHttpClientFactory factory = new MockHttpClientFactory(); Kii.HttpClientFactory = factory; // set response MockHttpClient client = factory.Client; client.AddResponse(200, "{\"userID\":\"cdef\"}"); // LogIn KiiUser user = KiiUser.LoginWithToken("tokenABCD"); Assert.AreEqual("cdef", user.ID); // check AccessTokenDictionary Dictionary <string, object> dict = user.GetAccessTokenDictionary(); string token = (string)dict["access_token"]; Assert.AreEqual("tokenABCD", token); DateTime expiresAt = (DateTime)dict["expires_at"]; Assert.AreEqual(DateTime.MaxValue, expiresAt); }
public void Test_GetAccessTokenDictionary_WhenUserIsNotLoggedIn() { Kii.Initialize(APP_ID, APP_KEY, Kii.Site.US); KiiUser user = KiiUser.BuilderWithName("dummyName").Build(); Assert.IsNull(KiiUser.CurrentUser); IDictionary dict = user.GetAccessTokenDictionary(); Assert.IsNull(dict); }
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"]); }
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"]); }
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()); }
public void Test_0300_LoginWithToken_OK() { // set response this.mockClient.AddResponse(200, "{\"userID\":\"cdef\"}"); KiiUser target = null; Exception exp = null; // LogIn KiiUser.LoginWithToken("tokenABCD", (KiiUser user, Exception e) => { target = user; exp = e; }); Assert.AreEqual("cdef", target.ID); Assert.IsNull(exp); // check AccessTokenDictionary Dictionary <string, object> dict = target.GetAccessTokenDictionary(); string token = (string)dict["access_token"]; Assert.AreEqual("tokenABCD", token); DateTime expiresAt = (DateTime)dict["expires_at"]; Assert.AreEqual(DateTime.MaxValue, expiresAt); }
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); }
public void Test_GetAccessTokenDictionary__BeforeInitialization() { KiiUser user = KiiUser.BuilderWithName("dummyName").Build(); IDictionary dict = user.GetAccessTokenDictionary(); }