gnunet-svn
[Top][All Lists]
Advanced

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

[taler-merchant] 221/277: test for GET /private/reserves/


From: gnunet
Subject: [taler-merchant] 221/277: test for GET /private/reserves/
Date: Sun, 05 Jul 2020 20:52:14 +0200

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository merchant.

commit 63c035bfffaec9b245da58828f7d6504278fc46e
Author: Jonathan Buchanan <jonathan.russ.buchanan@gmail.com>
AuthorDate: Mon Jun 15 21:08:33 2020 -0400

    test for GET /private/reserves/
---
 src/backend/taler-merchant-httpd.c                 |   2 +-
 .../taler-merchant-httpd_private-get-reserves-ID.c |  11 +-
 src/include/taler_merchant_testing_lib.h           |  16 ++
 src/lib/merchant_api_get_reserve.c                 |  17 +-
 src/testing/Makefile.am                            |   1 +
 src/testing/test_merchant_api.c                    |   9 +-
 src/testing/testing_api_cmd_get_reserve.c          | 197 +++++++++++++++++++++
 7 files changed, 237 insertions(+), 16 deletions(-)

diff --git a/src/backend/taler-merchant-httpd.c 
b/src/backend/taler-merchant-httpd.c
index c9b22dd..721861c 100644
--- a/src/backend/taler-merchant-httpd.c
+++ b/src/backend/taler-merchant-httpd.c
@@ -890,7 +890,7 @@ url_handler (void *cls,
     },
     /* GET /reserves: */
     {
-      .url_prefix = "/reserves",
+      .url_prefix = "/reserves/",
       .have_id_segment = true,
       .method = MHD_HTTP_METHOD_GET,
       .handler = &TMH_private_get_reserves_ID
diff --git a/src/backend/taler-merchant-httpd_private-get-reserves-ID.c 
b/src/backend/taler-merchant-httpd_private-get-reserves-ID.c
index d77a7bf..2d789d0 100644
--- a/src/backend/taler-merchant-httpd_private-get-reserves-ID.c
+++ b/src/backend/taler-merchant-httpd_private-get-reserves-ID.c
@@ -78,8 +78,13 @@ handle_reserve_details (void *cls,
 {
   struct GetReserveContext *ctx = cls;
   json_t *tips_json;
+  struct GNUNET_TIME_Absolute creation_time_round = creation_time;
+  struct GNUNET_TIME_Absolute expiration_time_round = expiration_time;
 
-  if (tips)
+  GNUNET_TIME_round_abs (&creation_time_round);
+  GNUNET_TIME_round_abs (&expiration_time_round);
+
+  if (NULL != tips)
   {
     tips_json = json_array ();
     GNUNET_assert (NULL != tips_json);
@@ -108,8 +113,8 @@ handle_reserve_details (void *cls,
     ctx->connection,
     MHD_HTTP_OK,
     "{s:o, s:o, s:o, s:o, s:o, s:o, s:o?}",
-    "creation_time", GNUNET_JSON_from_time_abs (creation_time),
-    "expiration_time", GNUNET_JSON_from_time_abs (expiration_time),
+    "creation_time", GNUNET_JSON_from_time_abs (creation_time_round),
+    "expiration_time", GNUNET_JSON_from_time_abs (expiration_time_round),
     "merchant_initial_amount", TALER_JSON_from_amount 
(merchant_initial_amount),
     "exchange_initial_amount", TALER_JSON_from_amount 
(exchange_initial_amount),
     "pickup_amount", TALER_JSON_from_amount (picked_up_amount),
diff --git a/src/include/taler_merchant_testing_lib.h 
b/src/include/taler_merchant_testing_lib.h
index 83de976..60846a9 100644
--- a/src/include/taler_merchant_testing_lib.h
+++ b/src/include/taler_merchant_testing_lib.h
@@ -582,6 +582,22 @@ TALER_TESTING_cmd_merchant_post_reserves (const char 
*label,
                                           const char *wire_method,
                                           unsigned int http_status);
 
+/**
+ * Define a "GET reserve" CMD.
+ *
+ * @param label command label.
+ * @param merchant_url base URL of the merchant serving the request.
+ * @param http_status expected HTTP response code.
+ * @param reserve_reference reference to a "POST /reserves" that provides the
+ *        information we are expecting.
+ * @return the command.
+ */
+struct TALER_TESTING_Command
+TALER_TESTING_cmd_merchant_get_reserve (const char *label,
+                                        const char *merchant_url,
+                                        unsigned int http_status,
+                                        const char *reserve_reference);
+
 
 /**
  * Define a "GET /reserves" CMD
diff --git a/src/lib/merchant_api_get_reserve.c 
b/src/lib/merchant_api_get_reserve.c
index a836b8b..d9f4a27 100644
--- a/src/lib/merchant_api_get_reserve.c
+++ b/src/lib/merchant_api_get_reserve.c
@@ -95,6 +95,10 @@ handle_reserve_get_finished (void *cls,
       struct TALER_MERCHANT_ReserveSummary rs;
       const json_t *tips;
       struct GNUNET_JSON_Specification spec[] = {
+        GNUNET_JSON_spec_absolute_time ("creation_time",
+                                        &rs.creation_time),
+        GNUNET_JSON_spec_absolute_time ("expiration_time",
+                                        &rs.expiration_time),
         TALER_JSON_spec_amount ("merchant_initial_amount",
                                 &rs.merchant_initial_amount),
         TALER_JSON_spec_amount ("exchange_initial_amount",
@@ -103,14 +107,6 @@ handle_reserve_get_finished (void *cls,
                                 &rs.pickup_amount),
         TALER_JSON_spec_amount ("committed_amount",
                                 &rs.committed_amount),
-        GNUNET_JSON_spec_fixed_auto ("reserve_pub",
-                                     &rs.reserve_pub),
-        GNUNET_JSON_spec_absolute_time ("creation_time",
-                                        &rs.creation_time),
-        GNUNET_JSON_spec_absolute_time ("expiration_time",
-                                        &rs.expiration_time),
-        GNUNET_JSON_spec_bool ("active",
-                               &rs.active),
         GNUNET_JSON_spec_end ()
       };
 
@@ -127,7 +123,8 @@ handle_reserve_get_finished (void *cls,
 
       tips = json_object_get (json,
                               "tips");
-      if (NULL == tips)
+      if ((NULL == tips) ||
+          json_is_null (tips))
       {
         rgh->cb (rgh->cb_cls,
                  &hr,
@@ -263,7 +260,7 @@ TALER_MERCHANT_reserve_get (struct GNUNET_CURL_Context *ctx,
     *end = '\0';
     GNUNET_snprintf (arg_str,
                      sizeof (arg_str),
-                     "/reserves/%s",
+                     "private/reserves/%s",
                      res_str);
     rgh->url = TALER_url_join (backend_url,
                                arg_str,
diff --git a/src/testing/Makefile.am b/src/testing/Makefile.am
index 8ffe415..6a3aeda 100644
--- a/src/testing/Makefile.am
+++ b/src/testing/Makefile.am
@@ -21,6 +21,7 @@ libtalermerchanttesting_la_SOURCES = \
   testing_api_cmd_get_instances.c \
   testing_api_cmd_get_product.c \
   testing_api_cmd_get_products.c \
+  testing_api_cmd_get_reserve.c \
   testing_api_cmd_get_reserves.c \
   testing_api_cmd_get_tips.c \
   testing_api_cmd_get_transfers.c \
diff --git a/src/testing/test_merchant_api.c b/src/testing/test_merchant_api.c
index bc51e4a..c2ed953 100644
--- a/src/testing/test_merchant_api.c
+++ b/src/testing/test_merchant_api.c
@@ -654,15 +654,20 @@ run (void *cls,
     TALER_TESTING_cmd_merchant_get_reserves ("get-reserves-1",
                                              merchant_url,
                                              MHD_HTTP_OK),
+    TALER_TESTING_cmd_merchant_get_reserve ("get-reserve-1",
+                                            merchant_url,
+                                            MHD_HTTP_OK,
+                                            "create-reserve-tip-1"),
+
     TALER_TESTING_cmd_merchant_delete_reserve ("delete-reserve-tip-1",
                                                merchant_url,
                                                "create-reserve-tip-1",
                                                MHD_HTTP_NO_CONTENT),
-    TALER_TESTING_cmd_merchant_purge_reserve ("delete-reserve-tip-1",
+    TALER_TESTING_cmd_merchant_purge_reserve ("delete-reserve-tip-2",
                                               merchant_url,
                                               "create-reserve-tip-1",
                                               MHD_HTTP_NO_CONTENT),
-    TALER_TESTING_cmd_merchant_purge_reserve ("delete-reserve-tip-1",
+    TALER_TESTING_cmd_merchant_purge_reserve ("delete-reserve-tip-3",
                                               merchant_url,
                                               "create-reserve-tip-1",
                                               MHD_HTTP_NOT_FOUND),
diff --git a/src/testing/testing_api_cmd_get_reserve.c 
b/src/testing/testing_api_cmd_get_reserve.c
new file mode 100644
index 0000000..1cae418
--- /dev/null
+++ b/src/testing/testing_api_cmd_get_reserve.c
@@ -0,0 +1,197 @@
+/*
+  This file is part of TALER
+  Copyright (C) 2020 Taler Systems SA
+
+  TALER is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as
+  published by the Free Software Foundation; either version 3, or
+  (at your option) any later version.
+
+  TALER is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public
+  License along with TALER; see the file COPYING.  If not, see
+  <http://www.gnu.org/licenses/>
+*/
+/**
+ * @file lib/testing_api_cmd_get_reserve.c
+ * @brief command to test GET /private/reserves/$RESERVE_PUB
+ * @author Jonathan Buchanan
+ */
+#include "platform.h"
+#include <taler/taler_exchange_service.h>
+#include <taler/taler_testing_lib.h>
+#include "taler_merchant_service.h"
+#include "taler_merchant_testing_lib.h"
+
+
+struct GetReserveState
+{
+
+  /**
+   * Handle for a "GET reserve" request.
+   */
+  struct TALER_MERCHANT_ReserveGetHandle *rgh;
+
+  /**
+   * The interpreter state.
+   */
+  struct TALER_TESTING_Interpreter *is;
+
+  /**
+   * Base URL of the merchant serving the request.
+   */
+  const char *merchant_url;
+
+  /**
+   * Label for a command that created a reserve.
+   */
+  const char *reserve_reference;
+
+  /**
+   * Expected HTTP response code.
+   */
+  unsigned int http_status;
+
+  /**
+   * Fetch tips
+   */
+  bool fetch_tips;
+};
+
+
+static void
+get_reserve_cb (void *cls,
+                const struct TALER_MERCHANT_HttpResponse *hr,
+                const struct TALER_MERCHANT_ReserveSummary *rs,
+                unsigned int tips_length,
+                const struct TALER_MERCHANT_TipDetails tips[])
+{
+  /* FIXME, deeper checks should be implemented here. */
+  struct GetReserveState *grs = cls;
+
+  grs->rgh = NULL;
+  if (grs->http_status != hr->http_status)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Unexpected response code %u (%d) to command %s\n",
+                hr->http_status,
+                (int) hr->ec,
+                TALER_TESTING_interpreter_get_current_label (grs->is));
+    TALER_TESTING_interpreter_fail (grs->is);
+    return;
+  }
+  switch (hr->http_status)
+  {
+  case MHD_HTTP_OK:
+    // FIXME: use grs->reserve_reference here to
+    // check if the data returned matches that from the POST / PATCH
+    break;
+  default:
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                "Unhandled HTTP status.\n");
+  }
+  TALER_TESTING_interpreter_next (grs->is);
+}
+
+
+/**
+ * Run the "GET /private/reserves/$RESERVE_PUB" CMD.
+ *
+ * @param cls closure.
+ * @param cmd command being run now.
+ * @param is interpreter state.
+ */
+static void
+get_reserve_run (void *cls,
+                 const struct TALER_TESTING_Command *cmd,
+                 struct TALER_TESTING_Interpreter *is)
+{
+  struct GetReserveState *grs = cls;
+  const struct TALER_TESTING_Command *reserve_cmd;
+  const struct TALER_ReservePublicKeyP *reserve_pub;
+
+  reserve_cmd = TALER_TESTING_interpreter_lookup_command (
+    is,
+    grs->reserve_reference);
+  if (GNUNET_OK !=
+      TALER_TESTING_get_trait_reserve_pub (reserve_cmd,
+                                           0,
+                                           &reserve_pub))
+    TALER_TESTING_FAIL (is);
+
+  grs->is = is;
+  grs->rgh = TALER_MERCHANT_reserve_get (is->ctx,
+                                         grs->merchant_url,
+                                         reserve_pub,
+                                         grs->fetch_tips,
+                                         &get_reserve_cb,
+                                         grs);
+
+  GNUNET_assert (NULL != grs->rgh);
+}
+
+
+/**
+ * Free the state of a "GET reserve" CMD, and possibly
+ * cancel a pending operation thereof.
+ *
+ * @param cls closure.
+ * @param cmd command being run.
+ */
+static void
+get_reserve_cleanup (void *cls,
+                     const struct TALER_TESTING_Command *cmd)
+{
+  struct GetReserveState *grs = cls;
+
+  if (NULL != grs->rgh)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "GET /private/reserve/$RESERVE_PUB operation did not 
complete\n");
+    TALER_MERCHANT_reserve_get_cancel (grs->rgh);
+  }
+  GNUNET_free (grs);
+}
+
+
+/**
+ * Define a "GET reserve" CMD.
+ *
+ * @param label command label.
+ * @param merchant_url base URL of the merchant serving the request.
+ * @param http_status expected HTTP response code.
+ * @param reserve_reference reference to a "POST /reserves" that provides the
+ *        information we are expecting.
+ * @return the command.
+ */
+struct TALER_TESTING_Command
+TALER_TESTING_cmd_merchant_get_reserve (const char *label,
+                                        const char *merchant_url,
+                                        unsigned int http_status,
+                                        const char *reserve_reference)
+{
+  struct GetReserveState *grs;
+
+  grs = GNUNET_new (struct GetReserveState);
+  grs->merchant_url = merchant_url;
+  grs->http_status = http_status;
+  grs->reserve_reference = reserve_reference;
+  grs->fetch_tips = false;
+  {
+    struct TALER_TESTING_Command cmd = {
+      .cls = grs,
+      .label = label,
+      .run = &get_reserve_run,
+      .cleanup = &get_reserve_cleanup
+    };
+
+    return cmd;
+  }
+}
+
+
+/* end of testing_api_cmd_get_reserve.c */

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]