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

begin;

select plan(11);

set search_path to camper, public;

select has_function('camper', 'redsys_sign_request', array['integer', 'redsys_request']);
select function_lang_is('camper', 'redsys_sign_request', array['integer', 'redsys_request'], 'sql');
select function_returns('camper', 'redsys_sign_request', array['integer', 'redsys_request'], 'redsys_signed_request');
select isnt_definer('camper', 'redsys_sign_request', array['integer', 'redsys_request']);
select volatility_is('camper', 'redsys_sign_request', array['integer', 'redsys_request'], 'stable');
select function_privs_are('camper', 'redsys_sign_request', array ['integer', 'redsys_request'], 'guest', array['EXECUTE']);
select function_privs_are('camper', 'redsys_sign_request', array ['integer', 'redsys_request'], 'employee', array['EXECUTE']);
select function_privs_are('camper', 'redsys_sign_request', array ['integer', 'redsys_request'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'redsys_sign_request', array ['integer', 'redsys_request'], '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_sign_request(2, row(0, '12.34', '00000001', 'Product #2', 'My Name', 'https://example.com/ok', 'https://example.com/ko', 'https://example.com/notification', 'ca')),
	row('eyJEc19NZXJjaGFudF9NZXJjaGFudENvZGUiIDogIjIyMjIyMjIyMiIsICJEc19NZXJjaGFudF9UZXJtaW5hbCIgOiAiMiIsICJEc19NZXJjaGFudF9NZXJjaGFudE5hbWUiIDogIkNvbXBhbnkgMiIsICJEc19NZXJjaGFudF9UcmFuc2FjdGlvblR5cGUiIDogIjAiLCAiRHNfTWVyY2hhbnRfQW1vdW50IiA6ICIxMjM0IiwgIkRzX01lcmNoYW50X0N1cnJlbmN5IiA6ICI5NzgiLCAiRHNfTWVyY2hhbnRfT3JkZXIiIDogIjAwMDAwMDAxIiwgIkRzX01lcmNoYW50X1Byb2R1Y3RkZXNjcmlwdGlvbiIgOiAiUHJvZHVjdCAjMiIsICJEc19NZXJjaGFudF9UaXR1bGFyIiA6ICJNeSBOYW1lIiwgIkRzX01lcmNoYW50X1VybE9LIiA6ICJodHRwczovL2V4YW1wbGUuY29tL29rIiwgIkRzX01lcmNoYW50X1VybEtPIiA6ICJodHRwczovL2V4YW1wbGUuY29tL2tvIiwgIkRzX01lcmNoYW50X01lcmNoYW50VVJMIiA6ICJodHRwczovL2V4YW1wbGUuY29tL25vdGlmaWNhdGlvbiIsICJEc19NZXJjaGFudF9Db25zdW1lckxhbmd1YWdlIiA6ICIzIn0=', 'aELGpY6jRl48YXNFkRKuIhhddroJpdeFigsRPDsB3pQ=', 'HMAC_SHA256_V1')::redsys_signed_request
);

select is(
	redsys_sign_request(4, row(1, '2144.00', '0000ABCDE', 'Booking ABCDE', 'Customer Full Name', 'http://example.cat/success', 'http://example.cat/failure', 'http://example.cat/notify', 'en')),
	row('eyJEc19NZXJjaGFudF9NZXJjaGFudENvZGUiIDogIjk5OTAwODg4MSIsICJEc19NZXJjaGFudF9UZXJtaW5hbCIgOiAiNCIsICJEc19NZXJjaGFudF9NZXJjaGFudE5hbWUiIDogIkNvbXBhbnkgNCIsICJEc19NZXJjaGFudF9UcmFuc2FjdGlvblR5cGUiIDogIjEiLCAiRHNfTWVyY2hhbnRfQW1vdW50IiA6ICIyMTQ0MDAiLCAiRHNfTWVyY2hhbnRfQ3VycmVuY3kiIDogIjg0MCIsICJEc19NZXJjaGFudF9PcmRlciIgOiAiMDAwMEFCQ0RFIiwgIkRzX01lcmNoYW50X1Byb2R1Y3RkZXNjcmlwdGlvbiIgOiAiQm9va2luZyBBQkNERSIsICJEc19NZXJjaGFudF9UaXR1bGFyIiA6ICJDdXN0b21lciBGdWxsIE5hbWUiLCAiRHNfTWVyY2hhbnRfVXJsT0siIDogImh0dHA6Ly9leGFtcGxlLmNhdC9zdWNjZXNzIiwgIkRzX01lcmNoYW50X1VybEtPIiA6ICJodHRwOi8vZXhhbXBsZS5jYXQvZmFpbHVyZSIsICJEc19NZXJjaGFudF9NZXJjaGFudFVSTCIgOiAiaHR0cDovL2V4YW1wbGUuY2F0L25vdGlmeSIsICJEc19NZXJjaGFudF9Db25zdW1lckxhbmd1YWdlIiA6ICIyIn0=', '5FybTC6lOiTUTeFCGiCZ7LCKb60Jt2CgCBz6QbfB/o0=', 'HMAC_SHA256_V1')::redsys_signed_request
);

select *
from finish();

rollback;
