view tap_tests.sql @ 104:bc1c7fa3a939

Add tests for authorisation of templates.
author Tom Gottfried <tom@intevation.de>
date Fri, 15 Jun 2018 18:15:04 +0200
parents 7f934f77831a
children ae5bb7a979ff
line wrap: on
line source

--
-- pgTAP test script
--
BEGIN;

CREATE EXTENSION pgtap;

SELECT plan(7); -- Give number of tests that have to be run

SET search_path TO public, wamos, wamos_waterway, wamos_fairway;

--
-- Add test data
--
INSERT INTO countries VALUES ('AT');

INSERT INTO user_profiles (username, country, email_adress)
       VALUES
       ('waterway_user', 'AT', 'xxx'),
       ('waterway_admin', 'AT', 'yyy'),
       ('sys_admin', 'AT', 'zzz');

INSERT INTO limiting_factors VALUES ('depth'), ('width');
INSERT INTO gauges (
       location, function_code, objname, geom, zero_point, source_organization)
       VALUES
       (('AT', 'XXX', '00001', '00000', 1)::isrs,
        'xxx',
        'testgauge',
        ST_geomfromtext('POINT(0 0)', 4326),
        0,
        'testorganization');
INSERT INTO bottlenecks (
       bottleneck_id, fk_g_fid, stretch, area, rb, lb, responsible_country,
       revisiting_time, limiting, source_organization)
       VALUES
       ('testbottleneck1',
        ('AT', 'XXX', '00001', '00000', 1)::isrs,
        isrsrange(('AT', 'XXX', '00001', '00000', 0)::isrs,
                  ('AT', 'XXX', '00001', '00000', 2)::isrs),
        ST_geomfromtext('POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))', 4326),
        'AT', 'AT', 'AT',
        1, 'depth', 'testorganization');
INSERT INTO bottlenecks (
       bottleneck_id, fk_g_fid, stretch, area, rb, lb, responsible_country,
       revisiting_time, limiting, source_organization, staging_done)
       VALUES
       ('testbottleneck2',
        ('AT', 'XXX', '00001', '00000', 1)::isrs,
        isrsrange(('AT', 'XXX', '00001', '00000', 0)::isrs,
                  ('AT', 'XXX', '00001', '00000', 2)::isrs),
        ST_geomfromtext('POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))', 4326),
        'AT', 'AT', 'AT',
        1, 'depth', 'testorganization', true);

INSERT INTO templates (dummy_attrib)
       VALUES ('usertemplate'), ('othertemplate');
INSERT INTO user_templates
       SELECT 'waterway_user', id FROM templates
              WHERE dummy_attrib = 'usertemplate';

--
-- Run tests as unprivileged user
--
SET SESSION AUTHORIZATION waterway_user;

SELECT throws_ok('CREATE TABLE test()', 42501, NULL,
                 'No objects can be created');

SELECT isnt_empty('SELECT * FROM bottlenecks',
                  'Staged data should be visible');
SELECT is_empty('SELECT * FROM bottlenecks WHERE NOT staging_done',
                'Only staged data should be visible');

SELECT set_eq('SELECT count(*) FROM wamos.user_profiles', ARRAY[1],
              'User should only see his own profile');
SELECT results_eq('SELECT username FROM user_profiles',
                  'SELECT CAST(current_user AS varchar)',
                  'User should only see his own profile');

SELECT isnt_empty('SELECT * FROM templates',
                  'User should see templates associated to him');
SELECT is_empty('SELECT * FROM templates t
                 JOIN user_templates ut ON t.id = template_id
                 WHERE username <> current_user',
                'User should only see templates associated to him');

--
-- finish tests
--
SELECT * FROM finish();

ROLLBACK;