Set ID before invoking the client callback
authorAlberto Mardegan <alberto.mardegan@canonical.com>
Thu, 22 Nov 2012 10:48:21 +0000 (12:48 +0200)
committerAlberto Mardegan <alberto.mardegan@canonical.com>
Thu, 22 Nov 2012 10:48:21 +0000 (12:48 +0200)
This change makes SignonIdentity process the results from
store_credentials() before invoking the client callback. This is
important because otherwise any SignonAuthSession created by the client
from within the callback would not be bound to the identity.

Also, add a unit test to catch any regressions.

Fixes: http://code.google.com/p/accounts-sso/issues/detail?id=132
libsignon-glib/signon-identity.c
tests/check_signon.c

index b30ca12e7994a45a99f6d575fba4bf1e6c199510..cdf7dcafd70d9e61173eb5ed5be5597eb408591b 100644 (file)
@@ -817,11 +817,6 @@ identity_store_credentials_reply (GObject *object, GAsyncResult *res,
     sso_identity_call_store_finish (proxy, &id, res, &error);
     SIGNON_RETURN_IF_CANCELLED (error);
 
-    if (cb_data->cb)
-    {
-        (cb_data->cb) (cb_data->self, id, error, cb_data->user_data);
-    }
-
     if (error == NULL)
     {
         g_return_if_fail (priv->identity_info == NULL);
@@ -845,6 +840,11 @@ identity_store_credentials_reply (GObject *object, GAsyncResult *res,
         priv->removed = FALSE;
     }
 
+    if (cb_data->cb)
+    {
+        (cb_data->cb) (cb_data->self, id, error, cb_data->user_data);
+    }
+
     g_clear_error(&error);
     g_slice_free (IdentityStoreCredentialsCbData, cb_data);
 }
index a8284ffa04b41599e41b2ed777b49a571821e502..957db3d4c53b54505ba5eb05c1c03fc4084e1190 100644 (file)
@@ -522,6 +522,104 @@ START_TEST(test_auth_session_process)
 }
 END_TEST
 
+static void
+test_auth_session_process_after_store_cb (SignonAuthSession *self,
+                                          GHashTable *reply,
+                                          const GError *error,
+                                          gpointer user_data)
+{
+    GValue *v_username;
+
+    if (error != NULL)
+    {
+        fail("Got error: %s", error->message);
+        g_main_loop_quit (main_loop);
+        return;
+    }
+
+    fail_unless (reply != NULL, "The result is empty");
+
+    v_username = g_hash_table_lookup(reply,
+                                     SIGNON_SESSION_DATA_USERNAME);
+
+    fail_unless (g_strcmp0 (g_value_get_string (v_username), "Nice user") == 0,
+                 "Wrong value of username");
+
+    g_hash_table_unref (reply);
+    g_object_unref (self);
+
+    g_main_loop_quit (main_loop);
+}
+
+static void
+test_auth_session_process_after_store_start_session(SignonIdentity *self,
+                                                    guint32 id,
+                                                    const GError *error,
+                                                    gpointer user_data)
+{
+    GError *err = NULL;
+
+    if (error != NULL)
+    {
+        g_warning ("%s %d: %s", G_STRFUNC, __LINE__, error->message);
+        fail();
+        g_main_loop_quit (main_loop);
+        return;
+    }
+
+    fail_unless (id > 0);
+
+    SignonAuthSession *auth_session =
+        signon_identity_create_session (self,
+                                        "ssotest",
+                                        &err);
+
+    fail_unless (auth_session != NULL, "Cannot create AuthSession object");
+    if (err != NULL)
+    {
+        fail ("Got error: %s", err->message);
+        g_clear_error (&err);
+    }
+
+    GHashTable *session_data = g_hash_table_new (g_str_hash,
+                                                 g_str_equal);
+
+    signon_auth_session_process (auth_session,
+                                 session_data,
+                                 "mech1",
+                                 test_auth_session_process_after_store_cb,
+                                 NULL);
+}
+
+START_TEST(test_auth_session_process_after_store)
+{
+    SignonIdentityInfo *info;
+    SignonIdentity *identity;
+
+    g_debug("%s", G_STRFUNC);
+
+    g_type_init();
+    main_loop = g_main_loop_new (NULL, FALSE);
+
+    identity = signon_identity_new ();
+    fail_unless (SIGNON_IS_IDENTITY (identity),
+                 "Failed to initialize the Identity.");
+
+    info = signon_identity_info_new ();
+    signon_identity_info_set_username (info, "Nice user");
+
+    signon_identity_store_credentials_with_info (identity,
+                                                 info,
+                                                 test_auth_session_process_after_store_start_session,
+                                                 NULL);
+    g_main_loop_run (main_loop);
+
+    g_object_unref (identity);
+
+    end_test ();
+}
+END_TEST
+
 static GHashTable *create_methods_hashtable()
 {
     gchar *mechanisms[] = {
@@ -1297,6 +1395,7 @@ signon_suite(void)
     tcase_add_test (tc_core, test_auth_session_query_mechanisms);
     tcase_add_test (tc_core, test_auth_session_query_mechanisms_nonexisting);
     tcase_add_test (tc_core, test_auth_session_process);
+    tcase_add_test (tc_core, test_auth_session_process_after_store);
     tcase_add_test (tc_core, test_store_credentials_identity);
     tcase_add_test (tc_core, test_verify_secret_identity);
     tcase_add_test (tc_core, test_remove_identity);