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

begin;

select plan(15);

set search_path to camper, public;

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

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

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', 1, 'live', 'insite', decode('x8RS+UkDkAN3i5hGU92U2c5BhJlffg8w', 'base64'))
     , (4, '999008881', 1, 'test', 'insite', decode('sq7HjrUOBfKmC576ILgskD5srU870gJ7', 'base64'))
;


select is(redsys_encrypt(2, '\x61626364656667'::bytea), '\x5df27f7580163beb'::bytea);
select is(redsys_encrypt(2, '\x3030303030303031'::bytea), '\x6cf019b21fc92a68'::bytea);
select is(redsys_encrypt(2, '\x414243444546474849'::bytea), '\x3aa8d51dd4dfa79be2fa13de2c636f06'::bytea);

select is(redsys_encrypt(4, '\x61626364656667'::bytea), '\x9b9cc420acfcd328'::bytea);
select is(redsys_encrypt(4, '\x3030303030303031'::bytea), '\x8ae3e9826d15ef02'::bytea);
select is(redsys_encrypt(4, '\x414243444546474849'::bytea), '\xce914bbfb35a70c38b7ea38cee8e7aea'::bytea);


select *
from finish();

rollback;
