graphql-engine/server/tests-py/queries/v1/select/permissions/setup.yaml
Swann Moreau 5bc0355bdd [server] coalesce multiple run_sql calls in tests (#270)
GitOrigin-RevId: abd7303aaf8e7a8739fd10574249aec450082ef8
2021-01-06 16:07:22 +00:00

370 lines
7.9 KiB
YAML

type: bulk
args:
#Author table
- type: run_sql
args:
sql: |
create table author(
id serial primary key,
name text unique,
is_registered boolean not null default false,
remarks_internal text
);
CREATE TABLE article (
id SERIAL PRIMARY KEY,
title TEXT,
content TEXT,
author_id INTEGER NOT NULL REFERENCES author(id),
is_published BOOLEAN NOT NULL default FALSE,
published_on TIMESTAMP
);
CREATE EXTENSION IF NOT EXISTS postgis;
DO $$
BEGIN
IF PostGIS_lib_version() ~ '^3.*' THEN
CREATE EXTENSION IF NOT EXISTS postgis_raster;
END IF;
END$$;
CREATE EXTENSION IF NOT EXISTS postgis_topology;
CREATE TABLE geom_table(
id SERIAL PRIMARY KEY,
type TEXT NOT NULL,
geom_col geometry NOT NULL
);
INSERT INTO geom_table (type, geom_col)
VALUES
('point', ST_GeomFromEWKT('SRID=4326;POINT(1 2)')),
('linestring', ST_GeomFromEWKT('SRID=4326;LINESTRING(0 0, 0.5 1, 1 2, 1.5 3)')),
('linestring', ST_GeomFromEWKT('SRID=4326;LINESTRING(1 0, 0.5 0.5, 0 1)')),
('polygon', ST_GeomFromEWKT('SRID=4326;POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))')),
('polygon', ST_GeomFromEWKT('SRID=4326;POLYGON((2 0, 2 1, 3 1, 3 0, 2 0))'))
;
CREATE TABLE geog_table(
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
geog_col geography NOT NULL
);
INSERT INTO geog_table (name, geog_col)
VALUES
('London', ST_GeographyFromText('SRID=4326;POINT(0.1278 51.5074)') ),
('Paris', ST_GeographyFromText('SRID=4326;POINT(2.3522 48.8566)') ),
('Moscow', ST_GeographyFromText('SRID=4326;POINT(37.6173 55.7558)') ),
('New York', ST_GeographyFromText('SRID=4326;POINT(-74.0060 40.7128)') ),
('Linestring', ST_GeographyFromText('SRID=4326;LINESTRING(0.0 0.0,0.0 1.0)')),
('Point', ST_GeographyFromText('SRID=4326;POINT(0.0 0.5)'));
CREATE TABLE jsonb_table(
id SERIAL PRIMARY KEY,
jsonb_col jsonb NOT NULL
);
CREATE TABLE auction (
id serial primary key,
price integer not null DEFAULT 250,
bid_price integer
);
INSERT INTO auction
(bid_price)
VALUES
(100), (120), (300), (260)
;
- type: track_table
args:
schema: public
name: author
#Article table
- type: track_table
args:
schema: public
name: article
#Object relationship
- type: create_object_relationship
args:
name: author
table: article
using:
foreign_key_constraint_on: author_id
#Array relationship
- type: create_array_relationship
args:
table: author
name: articles
using:
foreign_key_constraint_on:
table: article
column: author_id
#Article select permission for user
- type: create_select_permission
args:
table: article
role: user
permission:
columns:
- id
- title
- content
- is_published
filter:
$or:
- author_id: X-HASURA-USER-ID
- is_published: true
#Article select permission for anonymous (only published articles)
- type: create_select_permission
args:
table: article
role: anonymous
permission:
columns:
- id
- title
- content
- is_published
filter:
is_published: true
#Article insert permission for user
- type: create_insert_permission
args:
table: article
role: user
permission:
check:
author_id: X-Hasura-User-Id
#Author select permission for user
- type: create_select_permission
args:
table: author
role: user
permission:
columns:
- id
- name
- is_registered
filter:
_or:
- id: X-HASURA-USER-ID
- articles:
is_published:
_eq: true
#Author select permission for anonymous users
#Only authors with atleast one article will be shown
- type: create_select_permission
args:
table: author
role: anonymous
permission:
columns:
- id
- name
filter:
articles:
is_published:
_eq: true
#Author insert permission for user
- type: create_insert_permission
args:
table: author
role: user
permission:
check:
id: X-HASURA-USER-ID
#Insert Author values
- type: insert
args:
table: author
objects:
- name: Author 1
remarks_internal: remark 1
- name: Author 2
remarks_internal: remark 2
- name: Author 3
remarks_internal: remark 3
#Insert Article values
- type: insert
args:
table: article
objects:
- title: Article 1
content: Sample article content 1
author_id: 1
is_published: false
- title: Article 2
content: Sample article content 2
author_id: 1
is_published: true
- title: Article 3
content: Sample article content 3
author_id: 2
is_published: true
- title: Article 4
content: Sample article content 4
author_id: 3
is_published: false
#Permission based on PostGIS operators
- type: track_table
args:
name: geom_table
schema: public
#Create select permission using postgis operator
- type: create_select_permission
args:
table: geom_table
role: user1
permission:
columns:
- id
- type
- geom_col
filter:
geom_col:
$st_d_within:
distance: 1
from:
type: Polygon
crs:
type: name
properties:
name: urn:ogc:def:crs:EPSG::4326
coordinates:
- - - 2
- 0
- - 2
- 1
- - 3
- 1
- - 3
- 0
- - 2
- 0
#Create select permission using postgis operator and session variables
- type: create_select_permission
args:
table: geom_table
role: user2
permission:
columns:
- id
- type
- geom_col
filter:
geom_col:
$st_d_within:
distance: X-Hasura-Geom-Dist
from: X-Hasura-Geom-Val
#Insert data
- type: track_table
args:
name: geog_table
schema: public
- type: create_select_permission
args:
table: geog_table
role: postgis_user1
permission:
columns:
- id
- name
- geog_col
filter:
geog_col:
$st_intersects:
type: Point
coordinates:
- 0.0
- 0.5
crs:
type: name
properties:
name: urn:ogc:def:crs:EPSG::4326
- type: create_select_permission
args:
table: geog_table
role: postgis_user2
permission:
columns:
- id
- name
- geog_col
filter:
geog_col:
$st_d_within:
distance: X-Hasura-Geog-Dist
from: X-Hasura-Geog-From
use_spheroid: X-Hasura-Geog-Sph
- type: track_table
args:
name: jsonb_table
schema: public
#Create select permission using jsonb operator
- type: create_select_permission
args:
table: jsonb_table
role: user1
permission:
columns:
- id
- jsonb_col
filter:
jsonb_col:
$has_key: age
- type: create_select_permission
args:
table: jsonb_table
role: user2
permission:
columns:
- id
- jsonb_col
filter:
jsonb_col:
$has_key: X-Hasura-Has-Key
#Insert data
- type: insert
args:
table: jsonb_table
objects:
- jsonb_col:
name: Hasura
age: 7
- jsonb_col:
name: Cross
#Auction table
- type: track_table
args:
name: auction
schema: public
- type: create_select_permission
args:
role: user
table: auction
permission:
filter:
bid_price:
$cgt: price
columns:
- id
- bid_price