[gnome-software] trivial: Add self tests for the new authentication functionality



commit ae529906fd599edb6c81ca3e6838ff5f113ad3a3
Author: Richard Hughes <richard hughsie com>
Date:   Fri Jun 10 15:48:31 2016 +0100

    trivial: Add self tests for the new authentication functionality

 src/gs-self-test.c            |   65 +++++++++++++++++++++
 src/plugins/gs-plugin-dummy.c |  126 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 190 insertions(+), 1 deletions(-)
---
diff --git a/src/gs-self-test.c b/src/gs-self-test.c
index f8c1f6e..fefd69f 100644
--- a/src/gs-self-test.c
+++ b/src/gs-self-test.c
@@ -845,6 +845,68 @@ gs_plugin_loader_plugin_cache_func (GsPluginLoader *plugin_loader)
        g_assert (app1 == app2);
 }
 
+static void
+gs_plugin_loader_authentication_func (GsPluginLoader *plugin_loader)
+{
+       GsAuth *auth;
+       gboolean ret;
+       g_autoptr(GError) error = NULL;
+       g_autoptr(GsApp) app = NULL;
+       g_autoptr(GsReview) review = NULL;
+
+       /* check initial state */
+       auth = gs_plugin_loader_get_auth_by_id (plugin_loader, "dummy");
+       g_assert (GS_IS_AUTH (auth));
+       g_assert_cmpint (gs_auth_get_flags (auth), ==, 0);
+
+       /* do an action that returns a URL */
+       review = gs_review_new ();
+       ret = gs_plugin_loader_auth_action (plugin_loader, auth,
+                                           GS_AUTH_ACTION_REGISTER,
+                                           NULL, &error);
+       g_assert_error (error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_AUTH_INVALID);
+       g_assert (!ret);
+       g_clear_error (&error);
+       g_assert (!gs_auth_has_flag (auth, GS_AUTH_FLAG_VALID));
+
+       /* do an action that requires a login */
+       app = gs_app_new (NULL);
+       review = gs_review_new ();
+       ret = gs_plugin_loader_review_action (plugin_loader, app, review,
+                                             GS_REVIEW_ACTION_REMOVE,
+                                             NULL, &error);
+       g_assert_error (error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_AUTH_REQUIRED);
+       g_assert (!ret);
+       g_clear_error (&error);
+
+       /* pretend to auth with no credentials */
+       ret = gs_plugin_loader_auth_action (plugin_loader, auth,
+                                           GS_AUTH_ACTION_LOGIN,
+                                           NULL, &error);
+       g_assert_error (error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_AUTH_INVALID);
+       g_assert (!ret);
+       g_clear_error (&error);
+       g_assert (!gs_auth_has_flag (auth, GS_AUTH_FLAG_VALID));
+
+       /* auth again with correct credentials */
+       gs_auth_set_username (auth, "dummy");
+       gs_auth_set_password (auth, "dummy");
+       ret = gs_plugin_loader_auth_action (plugin_loader, auth,
+                                           GS_AUTH_ACTION_LOGIN,
+                                           NULL, &error);
+       g_assert_no_error (error);
+       g_assert (ret);
+       g_assert (gs_auth_has_flag (auth, GS_AUTH_FLAG_VALID));
+
+       /* do the action that requires a login */
+       review = gs_review_new ();
+       ret = gs_plugin_loader_review_action (plugin_loader, app, review,
+                                             GS_REVIEW_ACTION_REMOVE,
+                                             NULL, &error);
+       g_assert_no_error (error);
+       g_assert (ret);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -984,6 +1046,9 @@ main (int argc, char **argv)
        g_assert (gs_plugin_loader_get_enabled (plugin_loader, "dummy"));
 
        /* plugin tests go here */
+       g_test_add_data_func ("/gnome-software/plugin-loader{authentication}",
+                             plugin_loader,
+                             (GTestDataFunc) gs_plugin_loader_authentication_func);
        g_test_add_data_func ("/gnome-software/plugin-loader{plugin-cache}",
                              plugin_loader,
                              (GTestDataFunc) gs_plugin_loader_plugin_cache_func);
diff --git a/src/plugins/gs-plugin-dummy.c b/src/plugins/gs-plugin-dummy.c
index 7b407fb..bcdbd68 100644
--- a/src/plugins/gs-plugin-dummy.c
+++ b/src/plugins/gs-plugin-dummy.c
@@ -30,12 +30,14 @@
 
 struct GsPluginData {
        guint                    quirk_id;
+       guint                    has_auth;
+       GsAuth                  *auth;
 };
 
 void
 gs_plugin_initialize (GsPlugin *plugin)
 {
-       gs_plugin_alloc_data (plugin, sizeof(GsPluginData));
+       GsPluginData *priv = gs_plugin_alloc_data (plugin, sizeof(GsPluginData));
        if (g_getenv ("GS_SELF_TEST_DUMMY_ENABLE") == NULL) {
                g_debug ("disabling '%s' as not in self test",
                         gs_plugin_get_name (plugin));
@@ -43,6 +45,16 @@ gs_plugin_initialize (GsPlugin *plugin)
                return;
        }
 
+       /* set up a dummy authentication provider */
+       priv->auth = gs_auth_new (gs_plugin_get_name (plugin));
+       gs_auth_set_provider_name (priv->auth, "GNOME SSO");
+       gs_auth_set_provider_logo (priv->auth, "/usr/share/pixmaps/gnome-about-logo.png");
+       gs_auth_set_provider_uri (priv->auth, "http://www.gnome.org/sso";);
+       gs_plugin_add_auth (plugin, priv->auth);
+
+       /* lets assume we read this from disk somewhere */
+       gs_auth_set_username (priv->auth, "dummy");
+
        /* need help from appstream */
        gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "appstream");
        gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_CONFLICTS, "odrs");
@@ -614,6 +626,118 @@ gs_plugin_review_remove (GsPlugin *plugin,
                         GCancellable *cancellable,
                         GError **error)
 {
+       GsPluginData *priv = gs_plugin_get_data (plugin);
+
+       /* simulate an auth check */
+       if (!priv->has_auth) {
+               g_set_error (error,
+                            GS_PLUGIN_ERROR,
+                            GS_PLUGIN_ERROR_AUTH_REQUIRED,
+                            "authentication is required using @%s",
+                            gs_plugin_get_name (plugin));
+               return FALSE;
+       }
+
+       /* all okay */
        g_debug ("Removing dummy self-review");
        return TRUE;
 }
+
+gboolean
+gs_plugin_auth_login (GsPlugin *plugin, GsAuth *auth,
+                     GCancellable *cancellable, GError **error)
+{
+       GsPluginData *priv = gs_plugin_get_data (plugin);
+
+       /* not us */
+       if (g_strcmp0 (gs_auth_get_provider_id (auth),
+                      gs_auth_get_provider_id (priv->auth)) != 0)
+               return TRUE;
+
+       /* already done */
+       if (priv->has_auth) {
+               g_set_error (error,
+                            GS_PLUGIN_ERROR,
+                            GS_PLUGIN_ERROR_FAILED,
+                            "authentication already done");
+               return FALSE;
+       }
+
+       /* check username and password */
+       if (g_strcmp0 (gs_auth_get_username (priv->auth), "dummy") != 0 ||
+           g_strcmp0 (gs_auth_get_password (priv->auth), "dummy") != 0) {
+               g_set_error (error,
+                            GS_PLUGIN_ERROR,
+                            GS_PLUGIN_ERROR_AUTH_INVALID,
+                            "The password was not correct.");
+               return FALSE;
+       }
+
+       priv->has_auth = TRUE;
+       gs_auth_add_flags (priv->auth, GS_AUTH_FLAG_VALID);
+       g_debug ("dummy now authenticated");
+       return TRUE;
+}
+
+gboolean
+gs_plugin_auth_logout (GsPlugin *plugin, GsAuth *auth,
+                      GCancellable *cancellable, GError **error)
+{
+       GsPluginData *priv = gs_plugin_get_data (plugin);
+
+       /* not us */
+       if (g_strcmp0 (gs_auth_get_provider_id (auth),
+                      gs_auth_get_provider_id (priv->auth)) != 0)
+               return TRUE;
+
+       /* not done */
+       if (!priv->has_auth) {
+               g_set_error (error,
+                            GS_PLUGIN_ERROR,
+                            GS_PLUGIN_ERROR_FAILED,
+                            "authentication not already done");
+               return FALSE;
+       }
+       priv->has_auth = FALSE;
+       gs_auth_set_flags (priv->auth, 0);
+       g_debug ("dummy now not authenticated");
+       return TRUE;
+}
+
+gboolean
+gs_plugin_auth_lost_password (GsPlugin *plugin, GsAuth *auth,
+                             GCancellable *cancellable, GError **error)
+{
+       GsPluginData *priv = gs_plugin_get_data (plugin);
+
+       /* not us */
+       if (g_strcmp0 (gs_auth_get_provider_id (auth),
+                      gs_auth_get_provider_id (priv->auth)) != 0)
+               return TRUE;
+
+       /* return with data */
+       g_set_error_literal (error,
+                            GS_PLUGIN_ERROR,
+                            GS_PLUGIN_ERROR_AUTH_INVALID,
+                            "do online using @http://www.gnome.org/lost-password/";);
+       return FALSE;
+}
+
+gboolean
+gs_plugin_auth_register (GsPlugin *plugin, GsAuth *auth,
+                        GCancellable *cancellable, GError **error)
+{
+       GsPluginData *priv = gs_plugin_get_data (plugin);
+
+       /* not us */
+       if (g_strcmp0 (gs_auth_get_provider_id (auth),
+                      gs_auth_get_provider_id (priv->auth)) != 0)
+               return TRUE;
+
+       /* return with data */
+       g_set_error_literal (error,
+                            GS_PLUGIN_ERROR,
+                            GS_PLUGIN_ERROR_AUTH_INVALID,
+                            "do online using @http://www.gnome.org/register/";);
+       return FALSE;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]