-- Test setup_redsys
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', 'setup_redsys', array['integer', 'text', 'integer', 'redsys_environment', 'redsys_integration', 'text']);
select function_lang_is('camper', 'setup_redsys', array['integer', 'text', 'integer', 'redsys_environment', 'redsys_integration', 'text'], 'plpgsql');
select function_returns('camper', 'setup_redsys', array['integer', 'text', 'integer', 'redsys_environment', 'redsys_integration', 'text'], 'void');
select isnt_definer('camper', 'setup_redsys', array['integer', 'text', 'integer', 'redsys_environment', 'redsys_integration', 'text']);
select volatility_is('camper', 'setup_redsys', array['integer', 'text', 'integer', 'redsys_environment', 'redsys_integration', 'text'], 'volatile');
select function_privs_are('camper', 'setup_redsys', array ['integer', 'text', 'integer', 'redsys_environment', 'redsys_integration', 'text'], 'guest', array[]::text[]);
select function_privs_are('camper', 'setup_redsys', array ['integer', 'text', 'integer', 'redsys_environment', 'redsys_integration', 'text'], 'employee', array[]::text[]);
select function_privs_are('camper', 'setup_redsys', array ['integer', 'text', 'integer', 'redsys_environment', 'redsys_integration', 'text'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'setup_redsys', array ['integer', 'text', 'integer', 'redsys_environment', 'redsys_integration', '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 (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '', 60, 7, 'ES', 'EUR', 'ca')
     , (2, 'Company 4', 'XX234', '', '666-666-666', 'b@b', '', '', '', '', '', '', 60, 7, 'FR', 'USD', 'ca')
;

prepare redsys_data as
select company_id, merchant_code, terminal_number, environment::text, integration::text, encrypt_key
from redsys
;

select lives_ok(
	$$ select setup_redsys(1, '111111111', 1, 'live', 'insite', encode(E'\\x1111', 'base64')) $$,
	'Should be able to setup Redsys parameters for the first company'
);

select lives_ok(
	$$ select setup_redsys(2, '222222222', 2, 'test', 'redirect', encode(E'\\x1212', 'base64')) $$,
	'Should be able to setup Redsys parameters for the second company'
);

select bag_eq(
	'redsys_data',
	$$ values (1, '111111111', 1, 'live', 'insite', E'\\x1111'::bytea),
	          (2, '222222222', 2, 'test', 'redirect', E'\\x1212'::bytea)
	$$,
	'Should have inserted all Redsys parameters'
);

select lives_ok(
	$$ select setup_redsys(1, '999999999', 9, 'live', 'redirect', encode(E'\\x9119', 'base64')) $$,
	'Should be able to update Redsys parameters for the first company'
);

select lives_ok(
	$$ select setup_redsys(2, '123456789', 5, 'live', 'redirect', NULL) $$,
	'Should be able to update Redsys parameters for the second company, leaving the encrypt key alone'
);

select bag_eq(
	'redsys_data',
	$$ values (1, '999999999', 9, 'live', 'redirect', E'\\x9119'::bytea),
	          (2, '123456789', 5, 'live', 'redirect', E'\\x1212'::bytea)
	$$,
	'Should have updated all Redsys parameters'
);


reset client_min_messages;
select *
from finish();

rollback;
