From 2c19f6a6fb180f059fcaeb193dd387c4d6af21b1 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 29 Dec 2022 14:47:17 +0100 Subject: -towards fixing the template tests --- src/backend/taler-merchant-httpd_helper.c | 21 ++- .../taler-merchant-httpd_post-using-templates.c | 191 ++++++++++----------- ...taler-merchant-httpd_private-get-templates-ID.c | 15 +- .../taler-merchant-httpd_private-post-templates.c | 43 +++-- 4 files changed, 128 insertions(+), 142 deletions(-) (limited to 'src/backend') diff --git a/src/backend/taler-merchant-httpd_helper.c b/src/backend/taler-merchant-httpd_helper.c index a5de1781..86ff37c3 100644 --- a/src/backend/taler-merchant-httpd_helper.c +++ b/src/backend/taler-merchant-httpd_helper.c @@ -88,6 +88,7 @@ TMH_payto_uri_array_valid (const json_t *payto_uris) return payto_ok; } + bool TMH_location_object_valid (const json_t *location) { @@ -270,8 +271,8 @@ TMH_products_array_valid (const json_t *products) if ( (NULL != taxes) && (! TMH_taxes_array_valid (taxes)) ) { - GNUNET_break_op (0); - valid = false; + GNUNET_break_op (0); + valid = false; } if ( (NULL != description_i18n) && (! TALER_JSON_check_i18n (description_i18n)) ) @@ -315,13 +316,14 @@ TMH_image_data_url_valid (const char *image_data_url) return true; } + bool TMH_template_contract_valid (const json_t *template_contract) { const char *summary; struct TALER_Amount amount = { .value = 0}; uint32_t minimum_age = 0; - struct GNUNET_TIME_Relative pay_duration= { 0 }; + struct GNUNET_TIME_Relative pay_duration = { 0 }; struct GNUNET_JSON_Specification spec[] = { GNUNET_JSON_spec_mark_optional ( GNUNET_JSON_spec_string ("summary", @@ -332,14 +334,10 @@ TMH_template_contract_valid (const json_t *template_contract) TMH_currency, &amount), NULL), - GNUNET_JSON_spec_mark_optional ( - GNUNET_JSON_spec_uint32 ("minimum_age", - &minimum_age), - NULL), - GNUNET_JSON_spec_mark_optional ( - GNUNET_JSON_spec_relative_time ("pay_duration", - &pay_duration), - NULL), + GNUNET_JSON_spec_uint32 ("minimum_age", + &minimum_age), + GNUNET_JSON_spec_relative_time ("pay_duration", + &pay_duration), GNUNET_JSON_spec_end () }; const char *ename; @@ -359,6 +357,7 @@ TMH_template_contract_valid (const json_t *template_contract) return true; } + bool TMH_taxes_array_valid (const json_t *taxes) { diff --git a/src/backend/taler-merchant-httpd_post-using-templates.c b/src/backend/taler-merchant-httpd_post-using-templates.c index 75f489dd..9252afc8 100644 --- a/src/backend/taler-merchant-httpd_post-using-templates.c +++ b/src/backend/taler-merchant-httpd_post-using-templates.c @@ -1,6 +1,6 @@ /* This file is part of TALER - (C) 2020 Taler Systems SA + (C) 2022 Taler Systems SA TALER is free software; you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as @@ -28,11 +28,6 @@ #include "taler-merchant-httpd_helper.h" #include -/** - * How often do we retry the simple INSERT database transaction? - */ -#define MAX_RETRIES 3 - MHD_RESULT TMH_post_using_templates_ID (const struct TMH_RequestHandler *rh, @@ -48,7 +43,7 @@ TMH_post_using_templates_ID (const struct TMH_RequestHandler *rh, struct TALER_Amount amount; bool no_amount; json_t *fake_body; - bool no_summary; + bool no_summary; struct TALER_MERCHANTDB_TemplateDetails etp; struct GNUNET_JSON_Specification spec[] = { @@ -64,7 +59,7 @@ TMH_post_using_templates_ID (const struct TMH_RequestHandler *rh, GNUNET_JSON_spec_end () }; - { + { enum GNUNET_GenericReturnValue res; res = TALER_MHD_parse_json_data (connection, @@ -79,13 +74,13 @@ TMH_post_using_templates_ID (const struct TMH_RequestHandler *rh, } } - { - enum GNUNET_DB_QueryStatus qs; + { + enum GNUNET_DB_QueryStatus qs; - qs = TMH_db->lookup_template (TMH_db->cls, - mi->settings.id, - template_id, - &etp); + qs = TMH_db->lookup_template (TMH_db->cls, + mi->settings.id, + template_id, + &etp); switch (qs) { case GNUNET_DB_STATUS_HARD_ERROR: @@ -115,15 +110,15 @@ TMH_post_using_templates_ID (const struct TMH_RequestHandler *rh, /* all good */ break; } /* End of the switch */ - } + } - const char *tsummary; - uint32_t min_age; - struct GNUNET_TIME_Relative pay_duration; - struct TALER_Amount tamount; - bool no_tamount; + const char *tsummary; + uint32_t min_age; + struct GNUNET_TIME_Relative pay_duration; + struct TALER_Amount tamount; + bool no_tamount; - struct GNUNET_JSON_Specification tspec[] = { + struct GNUNET_JSON_Specification tspec[] = { GNUNET_JSON_spec_mark_optional ( GNUNET_JSON_spec_string ("summary", &tsummary), @@ -135,14 +130,12 @@ TMH_post_using_templates_ID (const struct TMH_RequestHandler *rh, &no_tamount), GNUNET_JSON_spec_uint32 ("minimum_age", &min_age), - GNUNET_JSON_spec_mark_optional ( - GNUNET_JSON_spec_relative_time ("pay_duration", - &pay_duration), - NULL), + GNUNET_JSON_spec_relative_time ("pay_duration", + &pay_duration), GNUNET_JSON_spec_end () }; - { + { enum GNUNET_GenericReturnValue res; const char *err_name; unsigned int err_line; @@ -154,6 +147,9 @@ TMH_post_using_templates_ID (const struct TMH_RequestHandler *rh, if (GNUNET_OK != res) { GNUNET_break (0); + json_dumpf (etp.template_contract, + stderr, + JSON_INDENT (2)); GNUNET_JSON_parse_free (spec); return TALER_MHD_reply_with_error (connection, MHD_HTTP_INTERNAL_SERVER_ERROR, @@ -162,85 +158,78 @@ TMH_post_using_templates_ID (const struct TMH_RequestHandler *rh, } } - // if amount given in template AND request - if ((!no_amount) && (!no_tamount)) - { - GNUNET_JSON_parse_free (spec); - return TALER_MHD_reply_with_error (connection, - MHD_HTTP_CONFLICT, - TALER_EC_MERCHANT_POST_USING_TEMPLATES_AMOUNT_CONFLICT_TEMPLATES_CONTRACT_AMOUNT, - NULL); - } - - // if there is no amount in template and request - if (no_amount && no_tamount) - { - GNUNET_JSON_parse_free (spec); - return TALER_MHD_reply_with_error (connection, - MHD_HTTP_CONFLICT, - TALER_EC_MERCHANT_POST_USING_TEMPLATES_NO_AMOUNT, - NULL); - } - - // if subject given in both - if ( (NULL!=summary) && (NULL != tsummary)) - { - - GNUNET_JSON_parse_free (spec); - return TALER_MHD_reply_with_error (connection, - MHD_HTTP_CONFLICT, - TALER_EC_MERCHANT_POST_USING_TEMPLATES_SUMMARY_CONFLICT_TEMPLATES_CONTRACT_SUBJECT, - NULL); - } - - //if there is no summary in template and request - if ( (NULL == summary) && (NULL == tsummary) ) - { - GNUNET_JSON_parse_free (spec); - return TALER_MHD_reply_with_error (connection, - MHD_HTTP_CONFLICT, - TALER_EC_MERCHANT_POST_USING_TEMPLATES_NO_SUMMARY, - NULL); - } - - else - { - if ( (NULL == summary) ) - { - no_summary = true; - } - else - { - no_summary = false; - } - } - - fake_body = GNUNET_JSON_PACK ( + // if amount given in template AND request + if ((! no_amount) && (! no_tamount)) + { + GNUNET_JSON_parse_free (spec); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_CONFLICT, + TALER_EC_MERCHANT_POST_USING_TEMPLATES_AMOUNT_CONFLICT_TEMPLATES_CONTRACT_AMOUNT, + NULL); + } + + // if there is no amount in template and request + if (no_amount && no_tamount) + { + GNUNET_JSON_parse_free (spec); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_CONFLICT, + TALER_EC_MERCHANT_POST_USING_TEMPLATES_NO_AMOUNT, + NULL); + } + + if ( (NULL!=summary) && (NULL != tsummary)) + { + + GNUNET_JSON_parse_free (spec); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_CONFLICT, + TALER_EC_MERCHANT_POST_USING_TEMPLATES_SUMMARY_CONFLICT_TEMPLATES_CONTRACT_SUBJECT, + NULL); + } + + // if there is no summary in template and request + if ( (NULL == summary) && (NULL == tsummary) ) + { + GNUNET_JSON_parse_free (spec); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_CONFLICT, + TALER_EC_MERCHANT_POST_USING_TEMPLATES_NO_SUMMARY, + NULL); + } + no_summary = (NULL == summary); + fake_body = GNUNET_JSON_PACK ( GNUNET_JSON_pack_object_steal ("order", GNUNET_JSON_PACK ( - TALER_JSON_pack_amount ("amount", - no_amount ? &tamount : &amount), - GNUNET_JSON_pack_string ("summary", - no_summary ? tsummary : summary), - GNUNET_JSON_pack_allow_null ( - GNUNET_JSON_pack_string ("fulfillment_url", - fulfillment_url)), - GNUNET_JSON_pack_allow_null ( - GNUNET_JSON_pack_string ("fulfillment_message", - fulfillment_message)) - )) + TALER_JSON_pack_amount ("amount", + no_amount ? + &tamount : + &amount), + GNUNET_JSON_pack_string ("summary", + no_summary ? + tsummary : + summary), + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_string ( + "fulfillment_url", + fulfillment_url)), + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_string ( + "fulfillment_message", + fulfillment_message)) + )) ); - - TALER_MERCHANTDB_template_details_free (&etp); - - struct TMH_HandlerContext fake_hc = { - .request_body = fake_body, - .instance = hc->instance - }; - mret = TMH_private_post_orders (NULL, /* not even used */ - connection, - &fake_hc); + { + struct TMH_HandlerContext fake_hc = { + .request_body = fake_body, + .instance = hc->instance + }; + + mret = TMH_private_post_orders (NULL, /* not even used */ + connection, + &fake_hc); + } json_decref (fake_body); return mret; } diff --git a/src/backend/taler-merchant-httpd_private-get-templates-ID.c b/src/backend/taler-merchant-httpd_private-get-templates-ID.c index 76e28fac..be5359f8 100644 --- a/src/backend/taler-merchant-httpd_private-get-templates-ID.c +++ b/src/backend/taler-merchant-httpd_private-get-templates-ID.c @@ -33,8 +33,8 @@ */ MHD_RESULT TMH_private_get_templates_ID (const struct TMH_RequestHandler *rh, - struct MHD_Connection *connection, - struct TMH_HandlerContext *hc) + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc) { struct TMH_MerchantInstance *mi = hc->instance; struct TALER_MERCHANTDB_TemplateDetails tp = { 0 }; @@ -42,9 +42,9 @@ TMH_private_get_templates_ID (const struct TMH_RequestHandler *rh, GNUNET_assert (NULL != mi); qs = TMH_db->lookup_template (TMH_db->cls, - mi->settings.id, - hc->infix, - &tp); + mi->settings.id, + hc->infix, + &tp); if (0 > qs) { GNUNET_break (0); @@ -68,8 +68,9 @@ TMH_private_get_templates_ID (const struct TMH_RequestHandler *rh, MHD_HTTP_OK, GNUNET_JSON_pack_string ("template_description", tp.template_description), - GNUNET_JSON_pack_string ("image", - tp.image), + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_string ("image", + tp.image)), GNUNET_JSON_pack_object_steal ("template_contract", tp.template_contract)); GNUNET_free (tp.template_description); diff --git a/src/backend/taler-merchant-httpd_private-post-templates.c b/src/backend/taler-merchant-httpd_private-post-templates.c index 38daf664..136f0891 100644 --- a/src/backend/taler-merchant-httpd_private-post-templates.c +++ b/src/backend/taler-merchant-httpd_private-post-templates.c @@ -43,21 +43,23 @@ */ static bool templates_equal (const struct TALER_MERCHANTDB_TemplateDetails *t1, - const struct TALER_MERCHANTDB_TemplateDetails *t2) + const struct TALER_MERCHANTDB_TemplateDetails *t2) { return ( (0 == strcmp (t1->template_description, t2->template_description)) && - (0 == strcmp (t1->image, - t2->image)) && + ( ( (NULL == t1->image) && (NULL == t2->image) ) || + ( (NULL != t1->image) && (NULL != t2->image) && + (0 == strcmp (t1->image, + t2->image)) ) ) && (1 == json_equal (t1->template_contract, - t2->template_contract))); + t2->template_contract)) ); } MHD_RESULT TMH_private_post_templates (const struct TMH_RequestHandler *rh, - struct MHD_Connection *connection, - struct TMH_HandlerContext *hc) + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc) { struct TMH_MerchantInstance *mi = hc->instance; struct TALER_MERCHANTDB_TemplateDetails tp = { 0 }; @@ -92,15 +94,12 @@ TMH_private_post_templates (const struct TMH_RequestHandler *rh, : MHD_NO; } } - - - if (NULL == tp.template_contract) - tp.template_contract = json_object ();; - - if (! TMH_template_contract_valid (tp.template_contract)) { GNUNET_break_op (0); + json_dumpf (tp.template_contract, + stderr, + JSON_INDENT (2)); GNUNET_JSON_parse_free (spec); return TALER_MHD_reply_with_error (connection, MHD_HTTP_BAD_REQUEST, @@ -108,10 +107,8 @@ TMH_private_post_templates (const struct TMH_RequestHandler *rh, "template_contract"); } - - if (NULL == tp.image) - tp.image = ""; - if (! TMH_image_data_url_valid (tp.image)) + if ( (NULL != tp.image) && + (! TMH_image_data_url_valid (tp.image)) ) { GNUNET_break_op (0); GNUNET_JSON_parse_free (spec); @@ -139,9 +136,9 @@ TMH_private_post_templates (const struct TMH_RequestHandler *rh, NULL); } qs = TMH_db->lookup_template (TMH_db->cls, - mi->settings.id, - template_id, - &etp); + mi->settings.id, + template_id, + &etp); switch (qs) { case GNUNET_DB_STATUS_HARD_ERROR: @@ -165,7 +162,7 @@ TMH_private_post_templates (const struct TMH_RequestHandler *rh, bool eq; eq = templates_equal (&tp, - &etp); + &etp); TALER_MERCHANTDB_template_details_free (&etp); TMH_db->rollback (TMH_db->cls); GNUNET_JSON_parse_free (spec); @@ -183,9 +180,9 @@ TMH_private_post_templates (const struct TMH_RequestHandler *rh, } /* end switch (qs) */ qs = TMH_db->insert_template (TMH_db->cls, - mi->settings.id, - template_id, - &tp); + mi->settings.id, + template_id, + &tp); if (GNUNET_DB_STATUS_HARD_ERROR == qs) { TMH_db->rollback (TMH_db->cls); -- cgit v1.2.3