diff options
Diffstat (limited to 'src/testing/testing_api_cmd_post_orders.c')
-rw-r--r-- | src/testing/testing_api_cmd_post_orders.c | 169 |
1 files changed, 167 insertions, 2 deletions
diff --git a/src/testing/testing_api_cmd_post_orders.c b/src/testing/testing_api_cmd_post_orders.c index d5cfdddc..8f7bd46d 100644 --- a/src/testing/testing_api_cmd_post_orders.c +++ b/src/testing/testing_api_cmd_post_orders.c @@ -24,6 +24,10 @@ */ #include "platform.h" +#include <gnunet/gnunet_common.h> +#include <gnunet/gnunet_time_lib.h> +#include <jansson.h> +#include <stdint.h> #include <taler/taler_exchange_service.h> #include <taler/taler_testing_lib.h> #include "taler_merchant_service.h" @@ -56,6 +60,11 @@ struct OrdersState const char *expected_order_id; /** + * Reference to a POST /tokenfamilies command. Can be NULL. + */ + const char *token_family_reference; + + /** * Contract terms obtained from the backend. */ json_t *contract_terms; @@ -66,6 +75,11 @@ struct OrdersState json_t *order_terms; /** + * Choices array with inputs and outputs for v1 order. + */ + json_t *choices; + + /** * Contract terms hash code. */ struct TALER_PrivateContractHashP h_contract_terms; @@ -563,6 +577,118 @@ orders_run2 (void *cls, /** + * Constructs the json for a the choices of an order request. + * + * @param slug the name of the order to add, can be NULL. + * @param valid_after valid_after date for the input and output token. + * @param[out] choices where to write the json string. + */ +static void +make_choices_json ( + const char *input_slug, + const char *output_slug, + uint16_t input_count, + uint16_t output_count, + struct GNUNET_TIME_Timestamp input_valid_after, + struct GNUNET_TIME_Timestamp output_valid_after, + json_t **choices) +{ + json_t *c; + + c = json_pack("[{s:o, s:o}]", + "inputs", json_pack("[{s:s, s:i, s:s, s:o}]", + "kind", "token", + "count", input_count, + "token_family_slug", input_slug, + "valid_after", GNUNET_JSON_from_timestamp(input_valid_after)), + "outputs", json_pack("[{s:s, s:i, s:s, s:o}]", + "kind", "token", + "count", output_count, + "token_family_slug", output_slug, + "valid_after", GNUNET_JSON_from_timestamp(output_valid_after))); + + *choices = c; +} + + +/** + * Run a "orders" CMD. + * + * @param cls closure. + * @param cmd command currently being run. + * @param is interpreter state. + */ +static void +orders_run3 (void *cls, + const struct TALER_TESTING_Command *cmd, + struct TALER_TESTING_Interpreter *is) +{ + struct OrdersState *ps = cls; + struct GNUNET_TIME_Absolute now; + const char *slug; + + ps->is = is; + now = GNUNET_TIME_absolute_get_monotonic (ps->cfg); + if (NULL == json_object_get (ps->order_terms, + "order_id")) + { + char *order_id; + + order_id = GNUNET_STRINGS_data_to_string_alloc ( + &now, + sizeof (now)); + GNUNET_assert (0 == + json_object_set_new (ps->order_terms, + "order_id", + json_string (order_id))); + GNUNET_free (order_id); + } + + { + const struct TALER_TESTING_Command *token_family_cmd; + token_family_cmd = + TALER_TESTING_interpreter_lookup_command (is, + ps->token_family_reference); + if (NULL == token_family_cmd) + TALER_TESTING_FAIL (is); + if (GNUNET_OK != + TALER_TESTING_get_trait_token_family_slug (token_family_cmd, + &slug)) + TALER_TESTING_FAIL (is); + } + make_choices_json (slug, slug, + 1, 1, + GNUNET_TIME_absolute_to_timestamp(now), + GNUNET_TIME_absolute_to_timestamp(now), + &ps->choices); + + GNUNET_assert (0 == + json_object_set_new (ps->order_terms, + "choices", + ps->choices) + ); + GNUNET_assert (0 == + json_object_set_new (ps->order_terms, + "version", + json_string ("1")) + ); + + + GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, + &ps->nonce, + sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)); + ps->po = TALER_MERCHANT_orders_post (TALER_TESTING_interpreter_get_context ( + is), + ps->merchant_url, + ps->order_terms, + GNUNET_TIME_UNIT_ZERO, + &order_cb, + ps); + GNUNET_assert (NULL != ps->po); +} + + +/** * Free the state of a "orders" CMD, and possibly * cancel it if it did not complete. * @@ -651,8 +777,7 @@ make_order_json (const char *order_id, "dummy_array", /* For testing forgetting parts of arrays */ "item", "speakers", "item", "headphones", - "item", "earbuds" - ); + "item", "earbuds"); GNUNET_assert (GNUNET_OK == TALER_JSON_expand_path (contract_terms, "$.dummy_obj", @@ -828,3 +953,43 @@ TALER_TESTING_cmd_merchant_post_orders3 ( return cmd; } } + + +struct TALER_TESTING_Command +TALER_TESTING_cmd_merchant_post_orders_choices ( + const char *label, + const struct GNUNET_CONFIGURATION_Handle *cfg, + const char *merchant_url, + unsigned int http_status, + const char *token_family_reference, + const char *order_id, + struct GNUNET_TIME_Timestamp refund_deadline, + struct GNUNET_TIME_Timestamp pay_deadline, + const char *amount) +{ + struct OrdersState *ps; + + ps = GNUNET_new (struct OrdersState); + ps->cfg = cfg; + make_order_json (order_id, + refund_deadline, + pay_deadline, + amount, + &ps->order_terms); + ps->http_status = http_status; + ps->token_family_reference = token_family_reference; + ps->expected_order_id = order_id; + ps->merchant_url = merchant_url; + ps->with_claim = true; + { + struct TALER_TESTING_Command cmd = { + .cls = ps, + .label = label, + .run = &orders_run3, + .cleanup = &orders_cleanup, + .traits = &orders_traits + }; + + return cmd; + } +}
\ No newline at end of file |