From daf099b4715471b40a03cd915957848629a2e917 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D0=B5=D0=BC?= Date: Fri, 21 Oct 2022 14:52:07 +0400 Subject: [PATCH] TD-427: Add plan id wrap with hash (#16) * added plan id wrap with hash * removed used mock * fixed * encoded --- apps/limiter/src/lim_accounting.erl | 18 ++++++++++++++---- apps/limiter/test/lim_turnover_SUITE.erl | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/apps/limiter/src/lim_accounting.erl b/apps/limiter/src/lim_accounting.erl index 39366c9..397d7ee 100644 --- a/apps/limiter/src/lim_accounting.erl +++ b/apps/limiter/src/lim_accounting.erl @@ -57,19 +57,19 @@ plan(PlanID, Batches, LimitContext) -> -spec hold(plan_id(), batch(), lim_context()) -> ok | {error, invalid_request_error()}. hold(PlanID, Batch, LimitContext) -> - do('Hold', construct_plan_change(PlanID, Batch), LimitContext). + do('Hold', construct_plan_change(wrap_plan_id(PlanID), Batch), LimitContext). -spec commit(plan_id(), [batch()], lim_context()) -> ok | {error, invalid_request_error()}. commit(PlanID, Batches, LimitContext) -> - do('CommitPlan', construct_plan(PlanID, Batches), LimitContext). + do('CommitPlan', construct_plan(wrap_plan_id(PlanID), Batches), LimitContext). -spec rollback(plan_id(), [batch()], lim_context()) -> ok | {error, invalid_request_error()}. rollback(PlanID, Batches, LimitContext) -> - do('RollbackPlan', construct_plan(PlanID, Batches), LimitContext). + do('RollbackPlan', construct_plan(wrap_plan_id(PlanID), Batches), LimitContext). -spec get_plan(plan_id(), lim_context()) -> {ok, [batch()]} | {error, notfound}. get_plan(PlanID, LimitContext) -> - case call_accounter('GetPlan', {PlanID}, LimitContext) of + case call_accounter('GetPlan', {wrap_plan_id(PlanID)}, LimitContext) of {ok, #accounter_PostingPlan{batch_list = BatchList}} -> {ok, decode_batch_list(BatchList)}; {exception, #accounter_PlanNotFound{}} -> @@ -157,6 +157,16 @@ construct_prototype(CurrencyCode, Description) -> %% +wrap_plan_id(PlanID) -> + %% Accounter requires max 64 byte plan id + case byte_size(PlanID) < 64 of + true -> + %% For backward compatibility + PlanID; + false -> + base64:encode(crypto:hash(sha384, PlanID)) + end. + call_accounter(Function, Args, LimitContext) -> WoodyContext = lim_context:woody_context(LimitContext), lim_client_woody:call(accounter, Function, Args, WoodyContext). diff --git a/apps/limiter/test/lim_turnover_SUITE.erl b/apps/limiter/test/lim_turnover_SUITE.erl index 5c58b18..85893bc 100644 --- a/apps/limiter/test/lim_turnover_SUITE.erl +++ b/apps/limiter/test/lim_turnover_SUITE.erl @@ -13,6 +13,7 @@ -export([init_per_testcase/2]). -export([end_per_testcase/2]). +-export([commit_with_long_change_id/1]). -export([commit_with_default_exchange/1]). -export([partial_commit_with_exchange/1]). -export([commit_with_exchange/1]). @@ -66,6 +67,7 @@ all() -> groups() -> [ {default, [], [ + commit_with_long_change_id, commit_with_default_exchange, partial_commit_with_exchange, commit_with_exchange, @@ -163,6 +165,19 @@ end_per_testcase(_Name, C) -> -define(LIMIT_CHANGE(ID), ?LIMIT_CHANGE(ID, ?CHANGE_ID)). -define(LIMIT_CHANGE(ID, ChangeID), #limiter_LimitChange{id = ID, change_id = gen_change_id(ID, ChangeID)}). +-spec commit_with_long_change_id(config()) -> _. +commit_with_long_change_id(C) -> + ID = configure_limit(?time_range_month(), ?global(), C), + Context = ?payproc_ctx_invoice(?cash(10, <<"RUB">>)), + LongBinary = + << + "LongBinaryLongBinaryLongBinaryLongBinaryLongBinaryLong\n" + " BinaryLongBinaryLongBinaryLongBinaryLongBinaryLongBinary" + >>, + ChangeID = <>, + {ok, {vector, _}} = hold_and_commit(?LIMIT_CHANGE(ID, ChangeID), Context, ?config(client, C)), + {ok, #limiter_Limit{amount = 10}} = lim_client:get(ID, Context, ?config(client, C)). + -spec commit_with_default_exchange(config()) -> _. commit_with_default_exchange(C) -> Rational = #base_Rational{p = 1000000, q = 100},