-- Test redsys_decode_response
set client_min_messages to warning;
create extension if not exists pgtap;
reset client_min_messages;

begin;

select plan(12);

set search_path to camper, public;

select has_function('camper', 'redsys_decode_response', array['integer', 'text', 'text', 'text']);
select function_lang_is('camper', 'redsys_decode_response', array['integer', 'text', 'text', 'text'], 'sql');
select function_returns('camper', 'redsys_decode_response', array['integer', 'text', 'text', 'text'], 'redsys_response');
select isnt_definer('camper', 'redsys_decode_response', array['integer', 'text', 'text', 'text']);
select volatility_is('camper', 'redsys_decode_response', array['integer', 'text', 'text', 'text'], 'stable');
select function_privs_are('camper', 'redsys_decode_response', array ['integer', 'text', 'text', 'text'], 'guest', array['EXECUTE']);
select function_privs_are('camper', 'redsys_decode_response', array ['integer', 'text', 'text', 'text'], 'employee', array['EXECUTE']);
select function_privs_are('camper', 'redsys_decode_response', array ['integer', 'text', 'text', 'text'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'redsys_decode_response', array ['integer', 'text', 'text', 'text'], 'authenticator', array[]::text[]);


set client_min_messages to warning;
truncate redsys cascade;
truncate company cascade;
reset client_min_messages;

insert into company (company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, rtc_number, tourist_tax, tourist_tax_max_days, country_code, currency_code, default_lang_tag)
values (2, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '', 60, 7, 'ES', 'EUR', 'ca')
     , (4, 'Company 4', 'XX234', '', '666-666-666', 'b@b', '', '', '', '', '', '', 60, 7, 'FR', 'USD', 'ca')
;

insert into redsys (company_id, merchant_code, terminal_number, environment, integration, encrypt_key)
values (2, '222222222', 2, 'live', 'insite', decode('x8RS+UkDkAN3i5hGU92U2c5BhJlffg8w', 'base64'))
     , (4, '999008881', 4, 'test', 'insite', decode('sq7HjrUOBfKmC576ILgskD5srU870gJ7', 'base64'))
;

select is(
	redsys_decode_response(4, 'eyJEc19NZXJjaGFudENvZGUiOiIzNjE3MTY5NjIiLCJEc19UZXJtaW5hbCI6IjAwMSIsIkRzX09yZGVyIjoiNjk5ZmQ2YWRjYTE4IiwiRHNfQW1vdW50IjoiMTAwMCIsIkRzX0N1cnJlbmN5IjoiOTc4IiwiRHNfRGF0ZSI6IjEyXC8wMlwvMjAyNCIsIkRzX0hvdXIiOiIxODowMCIsIkRzX1NlY3VyZVBheW1lbnQiOiIxIiwiRHNfQ2FyZF9Db3VudHJ5IjoiNzI0IiwiRHNfUmVzcG9uc2UiOiIwMDAwIiwiRHNfTWVyY2hhbnREYXRhIjoiIiwiRHNfVHJhbnNhY3Rpb25UeXBlIjoiMCIsIkRzX0NvbnN1bWVyTGFuZ3VhZ2UiOiIyIiwiRHNfQXV0aG9yaXNhdGlvbkNvZGUiOiIyOTM3MzYiLCJEc19DYXJkX0JyYW5kIjoiMSIsIkRzX1Byb2Nlc3NlZFBheU1ldGhvZCI6IjgwIn0=', 'uOR3HLlSMH3zrHINAoDv1-MK9tSj5Vo4Og9oAh6Uj08=', 'HMAC_SHA256_V1'),
	row('361716962', 1, 0, '2024-02-12 18:00:00.000000 +00:00'::timestamp, true, 0, '10.00', 'EUR', '699fd6adca18', '293736', '')::redsys_response
);

select is(
	redsys_decode_response(4, 'eyJEc19NZXJjaGFudF9NZXJjaGFudENvZGUiIDogIjk5OTAwODg4MSIsICJEc19NZXJjaGFudF9UZXJtaW5hbCIgOiAiNCIsICJEc19NZXJjaGFudF9NZXJjaGFudE5hbWUiIDogIkNvbXBhbnkgNCIsICJEc19NZXJjaGFudF9UcmFuc2FjdGlvblR5cGUiIDogIjEiLCAiRHNfTWVyY2hhbnRfQW1vdW50IiA6ICIyMTQ0MDAiLCAiRHNfTWVyY2hhbnRfQ3VycmVuY3kiIDogIjg0MCIsICJEc19NZXJjaGFudF9PcmRlciIgOiAiMDAwMEFCQ0RFIiwgIkRzX01lcmNoYW50X1Byb2R1Y3RkZXNjcmlwdGlvbiIgOiAiQm9va2luZyBBQkNERSIsICJEc19NZXJjaGFudF9UaXR1bGFyIiA6ICJDdXN0b21lciBGdWxsIE5hbWUiLCAiRHNfTWVyY2hhbnRfVXJsT0siIDogImh0dHA6Ly9leGFtcGxlLmNhdC9zdWNjZXNzIiwgIkRzX01lcmNoYW50X1VybEtPIiA6ICJodHRwOi8vZXhhbXBsZS5jYXQvZmFpbHVyZSIsICJEc19NZXJjaGFudF9NZXJjaGFudFVSTCIgOiAiaHR0cDovL2V4YW1wbGUuY2F0L25vdGlmeSIsICJEc19NZXJjaGFudF9Db25zdW1lckxhbmd1YWdlIiA6ICIyIn0=', 'uOR3HLlSMH3zrHINAoDv1-MK9tSj5Vo4Og9oAh6Uj08=', 'HMAC_SHA256_V1'),
	null::redsys_response
);

select is(
	redsys_decode_response(2, 'eyJEc19NZXJjaGFudENvZGUiOiIzNjE3MTY5NjIiLCJEc19UZXJtaW5hbCI6IjAwMSIsIkRzX09yZGVyIjoiNjk5ZmQ2YWRjYTE4IiwiRHNfQW1vdW50IjoiMTAwMCIsIkRzX0N1cnJlbmN5IjoiOTc4IiwiRHNfRGF0ZSI6IjEyXC8wMlwvMjAyNCIsIkRzX0hvdXIiOiIxODowMCIsIkRzX1NlY3VyZVBheW1lbnQiOiIxIiwiRHNfQ2FyZF9Db3VudHJ5IjoiNzI0IiwiRHNfUmVzcG9uc2UiOiIwMDAwIiwiRHNfTWVyY2hhbnREYXRhIjoiIiwiRHNfVHJhbnNhY3Rpb25UeXBlIjoiMCIsIkRzX0NvbnN1bWVyTGFuZ3VhZ2UiOiIyIiwiRHNfQXV0aG9yaXNhdGlvbkNvZGUiOiIyOTM3MzYiLCJEc19DYXJkX0JyYW5kIjoiMSIsIkRzX1Byb2Nlc3NlZFBheU1ldGhvZCI6IjgwIn0=', 'uOR3HLlSMH3zrHINAoDv1-MK9tSj5Vo4Og9oAh6Uj08=', 'HMAC_SHA256_V1'),
	null::redsys_response
);

select *
from finish();

rollback;
