Skip to content

Commit

Permalink
Ensure views with Layman users roles and relations
Browse files Browse the repository at this point in the history
  • Loading branch information
index-git committed Nov 30, 2023
1 parent 056af0f commit c0f2365
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/layman/upgrade/upgrade_v1_23.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,20 @@ def create_role_service_schema():
CONSTRAINT bussiness_user_roles_username_rolename_key UNIQUE (username,rolename)
);"""
db_util.run_statement(create_role_table)

create_layman_users_roles_view = f"""create view {ROLE_SERVICE_SCHEMA}.layman_users_roles
as
select concat('USER_', UPPER(w.name)) as name
from {DB_SCHEMA}.users u inner join
{DB_SCHEMA}.workspaces w on w.id = u.id_workspace
;"""
db_util.run_statement(create_layman_users_roles_view)

create_layman_users_user_roles_view = f"""create view {ROLE_SERVICE_SCHEMA}.layman_users_user_roles
as
select w.name as username,
concat('USER_', UPPER(w.name)) as rolename
from {DB_SCHEMA}.users u inner join
{DB_SCHEMA}.workspaces w on w.id = u.id_workspace
;"""
db_util.run_statement(create_layman_users_user_roles_view)
21 changes: 21 additions & 0 deletions src/layman/upgrade/upgrade_v1_23_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from db import util as db_util
from layman import app, settings
from layman.common.prime_db_schema import ensure_whole_user
from test_tools import process_client
from . import upgrade_v1_23

Expand Down Expand Up @@ -62,10 +63,26 @@ def test_adjust_db_for_roles():


def test_create_role_service_schema():
username = 'test_create_role_service_schema_username'
rolename = f'USER_{username.upper()}'
userinfo = {"issuer_id": 'mock_test_users_test',
"sub": '10',
"claims": {"email": "[email protected]",
"name": "test ensure user",
"preferred_username": 'test_preferred',
"given_name": "test",
"family_name": "user",
"middle_name": "ensure",
}
}
drop_statement = f'''DROP SCHEMA IF EXISTS {ROLE_SERVICE_SCHEMA} CASCADE;'''
schema_existence_query = f'''SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name = '{ROLE_SERVICE_SCHEMA}';'''
table_existence_query = f'''SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '{ROLE_SERVICE_SCHEMA}' and table_name = %s;'''
layman_users_roles_query = f'''select COUNT(*) from {ROLE_SERVICE_SCHEMA}.layman_users_roles where name = %s'''
layman_users_user_roles_query = f'''select COUNT(*) from {ROLE_SERVICE_SCHEMA}.layman_users_user_roles where username = %s and rolename = %s'''

with app.app_context():
ensure_whole_user(username, userinfo)
db_util.run_statement(drop_statement)
result = db_util.run_query(schema_existence_query)[0][0]
assert result == 0
Expand All @@ -78,3 +95,7 @@ def test_create_role_service_schema():
assert result == 1
result = db_util.run_query(table_existence_query, ('bussiness_user_roles',))[0][0]
assert result == 1
result = db_util.run_query(layman_users_roles_query, (rolename,))[0][0]
assert result == 1
result = db_util.run_query(layman_users_user_roles_query, (username, rolename,))[0][0]
assert result == 1

0 comments on commit c0f2365

Please sign in to comment.