@@ -682,3 +682,180 @@ def test_set_org_accepted_tou_org_dne(dictcursor):
682682 dictcursor .callproc ('set_org_accepted_tou' , (str (bin_to_uuid (orgid )),))
683683 assert e .value .args [0 ] == 1305
684684 assert e .value .args [1 ] == "Organization does not exist"
685+
686+
687+ def test_store_token (dictcursor , new_user ):
688+ user = new_user ()
689+ dictcursor .callproc ('store_token' , (user ['auth0_id' ], 'testtoken' ))
690+ dictcursor .execute ('SELECT * FROM job_tokens' )
691+ out = dictcursor .fetchall ()
692+ assert len (out ) == 1
693+ assert out [0 ]['id' ] == user ['id' ]
694+ assert out [0 ]['token' ] == 'testtoken'
695+
696+ dictcursor .callproc ('store_token' , (user ['auth0_id' ], 'newtoken' ))
697+ dictcursor .execute ('SELECT * FROM job_tokens' )
698+ out = dictcursor .fetchall ()
699+ assert len (out ) == 1
700+ assert out [0 ]['id' ] == user ['id' ]
701+ assert out [0 ]['token' ] == 'newtoken'
702+
703+
704+ def test_create_job_user (cursor , new_organization ):
705+ org = new_organization ()
706+ orgid = bin_to_uuid (org ['id' ])
707+ auth0_id = 'auth0|testid'
708+ cursor .callproc ('create_job_user' , (auth0_id , orgid ))
709+ user_id = cursor .fetchone ()[0 ]
710+ cursor .execute (
711+ 'SELECT 1 as one FROM users WHERE auth0_id = %s '
712+ 'AND organization_id = UUID_TO_BIN(%s, 1)' ,
713+ (auth0_id , orgid ))
714+ assert cursor .fetchall ()[0 ]
715+ cursor .execute (
716+ 'SELECT name FROM roles WHERE id IN'
717+ '(select role_id from user_role_mapping where '
718+ 'user_id = UUID_TO_BIN(%s, 1))' ,
719+ (user_id ,)
720+ )
721+ roles = [r [0 ] for r in cursor .fetchall ()]
722+ assert len (roles ) == 2
723+ assert 'Read Reference Data' in roles
724+ assert f'DEFAULT User role { user_id } ' in roles
725+
726+
727+ def test_create_report_creation_role (dictcursor ):
728+ dictcursor .callproc ('create_organization' , ('test_org' ,))
729+ dictcursor .execute ('SELECT * FROM arbiter_data.organizations '
730+ 'WHERE name = "test_org"' )
731+ org = dictcursor .fetchone ()
732+ orgid = org ['id' ]
733+
734+ dictcursor .callproc ('create_report_creation_role' , (orgid ,))
735+ dictcursor .execute (
736+ 'SELECT * FROM roles WHERE name = "Create reports" and organization_id = %s' , # NOQA
737+ orgid )
738+ create_roles = dictcursor .fetchall ()
739+ assert len (create_roles ) == 1
740+ create_role = create_roles [0 ]
741+ assert 'Create reports' in create_role ['description' ]
742+ assert create_role ['organization_id' ] == orgid
743+ role_id = create_role ['id' ]
744+ dictcursor .execute (
745+ 'SELECT permission_id FROM role_permission_mapping WHERE role_id = %s' ,
746+ role_id )
747+ permission_ids = dictcursor .fetchall ()
748+ assert len (permission_ids ) == 10
749+ perm_objects = []
750+ for permid in [p ['permission_id' ] for p in permission_ids ]:
751+ dictcursor .execute ('SELECT * FROM permissions WHERE id = %s' , permid )
752+ perm = dictcursor .fetchone ()
753+ perm_objects .append (perm )
754+ perms = {p ['description' ]: p for p in perm_objects }
755+ for perm in perms .values ():
756+ assert perm ['applies_to_all' ] == 1
757+ assert perm ['organization_id' ] == orgid
758+ assert {'Read all sites' , 'Read all observations' ,
759+ 'Read all observation values' , 'Read all forecasts' ,
760+ 'Read all forecast values' , 'Read all probabilistic forecasts' ,
761+ 'Read all probabilistic forecast values' ,
762+ 'Read all aggregates' , 'Read all aggregate values' ,
763+ 'Create reports' } == set (perms .keys ())
764+
765+
766+ def test_create_data_validation_role (dictcursor ):
767+ dictcursor .callproc ('create_organization' , ('test_org' ,))
768+ dictcursor .execute ('SELECT * FROM arbiter_data.organizations '
769+ 'WHERE name = "test_org"' )
770+ org = dictcursor .fetchone ()
771+ orgid = org ['id' ]
772+
773+ dictcursor .callproc ('create_data_validation_role' , (orgid ,))
774+ dictcursor .execute (
775+ 'SELECT * FROM roles WHERE name = "Validate observations" and organization_id = %s' , # NOQA
776+ orgid )
777+ create_roles = dictcursor .fetchall ()
778+ assert len (create_roles ) == 1
779+ create_role = create_roles [0 ]
780+ assert 'Enable observation data validation' in create_role ['description' ]
781+ assert create_role ['organization_id' ] == orgid
782+ role_id = create_role ['id' ]
783+ dictcursor .execute (
784+ 'SELECT permission_id FROM role_permission_mapping WHERE role_id = %s' ,
785+ role_id )
786+ permission_ids = dictcursor .fetchall ()
787+ assert len (permission_ids ) == 4
788+ perm_objects = []
789+ for permid in [p ['permission_id' ] for p in permission_ids ]:
790+ dictcursor .execute ('SELECT * FROM permissions WHERE id = %s' , permid )
791+ perm = dictcursor .fetchone ()
792+ perm_objects .append (perm )
793+ perms = {p ['description' ]: p for p in perm_objects }
794+ for perm in perms .values ():
795+ assert perm ['applies_to_all' ] == 1
796+ assert perm ['organization_id' ] == orgid
797+ assert {'Read all sites' , 'Read all observations' , 'Read all observation values' ,
798+ 'Submit values to all observations' } == set (perms .keys ())
799+
800+
801+ def test_create_forecast_generation_role (dictcursor ):
802+ dictcursor .callproc ('create_organization' , ('test_org' ,))
803+ dictcursor .execute ('SELECT * FROM arbiter_data.organizations '
804+ 'WHERE name = "test_org"' )
805+ org = dictcursor .fetchone ()
806+ orgid = org ['id' ]
807+
808+ dictcursor .callproc ('create_forecast_generation_role' , (orgid ,))
809+ dictcursor .execute (
810+ 'SELECT * FROM roles WHERE name = "Generate reference forecasts" and organization_id = %s' , # NOQA
811+ orgid )
812+ create_roles = dictcursor .fetchall ()
813+ assert len (create_roles ) == 1
814+ create_role = create_roles [0 ]
815+ assert 'Enable writing forecast values for ' in create_role ['description' ]
816+ assert create_role ['organization_id' ] == orgid
817+ role_id = create_role ['id' ]
818+ dictcursor .execute (
819+ 'SELECT permission_id FROM role_permission_mapping WHERE role_id = %s' ,
820+ role_id )
821+ permission_ids = dictcursor .fetchall ()
822+ assert len (permission_ids ) == 9
823+ perm_objects = []
824+ for permid in [p ['permission_id' ] for p in permission_ids ]:
825+ dictcursor .execute ('SELECT * FROM permissions WHERE id = %s' , permid )
826+ perm = dictcursor .fetchone ()
827+ perm_objects .append (perm )
828+ perms = {p ['description' ]: p for p in perm_objects }
829+ for perm in perms .values ():
830+ assert perm ['applies_to_all' ] == 1
831+ assert perm ['organization_id' ] == orgid
832+
833+ assert {'Read all sites' , 'Read all forecasts' ,
834+ 'Read all probabilistic forecasts' , 'Read all aggregates' ,
835+ 'Read all observations' , 'Read all observation values' ,
836+ 'Read all aggregate values' ,
837+ 'Submit values to all forecasts' ,
838+ 'Submit values to all probabilistic forecasts' } == set (perms .keys ())
839+
840+
841+ @pytest .mark .parametrize ('role,precreate' , [
842+ ('Create reports' , None ),
843+ ('Validate observations' , None ),
844+ ('Generate reference forecasts' , None ),
845+ pytest .param ('Read all' , None , marks = pytest .mark .xfail ),
846+ ('Create reports' , 'create_report_creation_role' ),
847+ ('Validate observations' , 'create_report_creation_role' ),
848+ ('Validate observations' , 'create_data_validation_role' )
849+ ])
850+ def test_grant_job_role (cursor , new_user , role , precreate ):
851+ user = new_user ()
852+ struserid = str (bin_to_uuid (user ['id' ]))
853+ if precreate is not None :
854+ cursor .callproc (precreate , (user ['organization_id' ],))
855+
856+ cursor .callproc ('grant_job_role' , (struserid , role ))
857+ cursor .execute (
858+ 'SELECT 1 FROM user_role_mapping WHERE user_id = %s AND role_id = '
859+ '(SELECT id FROM roles WHERE name = %s AND organization_id = %s)' ,
860+ (user ['id' ], role , user ['organization_id' ]))
861+ assert cursor .fetchone ()[0 ]
0 commit comments