test_valid_group_names and test_not_valid_group_names

This commit is contained in:
Juan Ignacio Sánchez Lara 2015-08-11 14:49:12 +02:00
parent 1915e28a0f
commit e2dd1e014e
2 changed files with 24 additions and 4 deletions

View File

@ -102,13 +102,17 @@ $$ LANGUAGE PLPGSQL;
----------------------- -----------------------
-- Private functions -- Private functions
----------------------- -----------------------
-- Given a group name returns a role -- Given a group name returns a role. group_name must be a valid PostgreSQL idenfifier. See http://www.postgresql.org/docs/9.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
CREATE OR REPLACE CREATE OR REPLACE
FUNCTION cartodb._CDB_Group_GroupRole(group_name text) FUNCTION cartodb._CDB_Group_GroupRole(group_name text)
RETURNS TEXT AS $$ RETURNS TEXT AS $$
DECLARE DECLARE
group_role TEXT; group_role TEXT;
BEGIN BEGIN
IF group_name !~ '^[a-zA-Z_][a-zA-Z0-9_]*$'
THEN
RAISE EXCEPTION 'Group name (%) must be a valid identifier. See http://www.postgresql.org/docs/9.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS', group_name;
END IF;
RETURN 'g_' || md5(current_database()) || '_' || group_name; RETURN 'g_' || md5(current_database()) || '_' || group_name;
END END
$$ LANGUAGE PLPGSQL; $$ LANGUAGE PLPGSQL;

View File

@ -69,10 +69,10 @@ function sql() {
;; ;;
*) ;; *) ;;
esac esac
echo -n "; Code result after warnings: "
echo -n ${CODERESULT}
fi fi
echo
echo -n "; New code result: "
echo ${CODERESULT}
if [[ ${CODERESULT} -ne 0 ]] if [[ ${CODERESULT} -ne 0 ]]
then then
@ -238,6 +238,7 @@ function run_tests() {
echo "####################################################################" echo "####################################################################"
clear_partial_result clear_partial_result
setup setup
log_info "############################# TESTS #############################"
eval ${t} eval ${t}
if [[ ${PARTIALOK} -ne 0 ]] if [[ ${PARTIALOK} -ne 0 ]]
then then
@ -484,6 +485,21 @@ function test_group_management_functions_cant_be_used_by_normal_members() {
sql cdb_testmember_2 'DROP TABLE cdb_testmember_2.shared_with_group;' sql cdb_testmember_2 'DROP TABLE cdb_testmember_2.shared_with_group;'
} }
function test_valid_group_names() {
sql "select cartodb._CDB_Group_GroupRole('group_1$_a');"
sql "select cartodb._CDB_Group_GroupRole('GROUP_1$_A');"
sql "select cartodb._CDB_Group_GroupRole('_group_1$_a');"
}
function test_not_valid_group_names() {
sql postgres "select cartodb._CDB_Group_GroupRole('1$_a');" fails
sql postgres "select cartodb._CDB_Group_GroupRole(' group_1$_a');" fails
sql postgres "select cartodb._CDB_Group_GroupRole('group_1$_a ');" fails
sql postgres "select cartodb._CDB_Group_GroupRole(' group_1$_a ');" fails
sql postgres "select cartodb._CDB_Group_GroupRole('group _1$_a');" fails
sql postgres "select cartodb._CDB_Group_GroupRole('groupña');" fails
}
#################################################### TESTS END HERE #################################################### #################################################### TESTS END HERE ####################################################