-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(e2e): Add test coverage for new trigger behaviour
- Loading branch information
1 parent
22c8d64
commit b63b6e3
Showing
4 changed files
with
92 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,48 @@ | ||
import { TEST_EMAIL } from "../../../ui-driven/src/helpers/globalHelpers"; | ||
import { $admin } from "../client"; | ||
import { safely } from "../globalHelpers"; | ||
import gql from "graphql-tag"; | ||
|
||
export const cleanup = async () => { | ||
await $admin.user._destroyAll(); | ||
await $admin.team._destroyAll(); | ||
}; | ||
|
||
export async function createDemoUser(demoTeamId: number) { | ||
const variables = { | ||
firstName: "Test", | ||
lastName: "Test", | ||
email: TEST_EMAIL, | ||
teamId: demoTeamId, | ||
role: "demoUser", | ||
}; | ||
|
||
const response = await safely(() => | ||
$admin.client.request<{ insertUsersOne: { id: number } }>( | ||
gql` | ||
mutation CreateAndAddUserToTeam( | ||
$email: String! | ||
$firstName: String! | ||
$lastName: String! | ||
$teamId: Int! | ||
$role: user_roles_enum! | ||
) { | ||
insertUsersOne: insert_users_one( | ||
object: { | ||
email: $email | ||
first_name: $firstName | ||
last_name: $lastName | ||
teams: { data: { role: $role, team_id: $teamId } } | ||
} | ||
) { | ||
id | ||
} | ||
} | ||
`, | ||
variables, | ||
), | ||
); | ||
|
||
const userId = response.insertUsersOne.id; | ||
return userId; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 22 additions & 29 deletions
51
hasura.planx.uk/migrations/1730277860890_run_sql_migration/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,22 @@ | ||
-- Could not auto-generate a down migration. | ||
-- Please write an appropriate down migration for the SQL below: | ||
-- CREATE OR REPLACE FUNCTION grant_new_user_template_access() RETURNS trigger AS $$ | ||
-- DECLARE | ||
-- templates_team_id INT; | ||
-- is_demo_user BOOLEAN; | ||
-- BEGIN | ||
-- SELECT EXISTS ( | ||
-- SELECT 1 | ||
-- FROM team_members | ||
-- WHERE user_id = NEW.id | ||
-- AND role = 'demoUser' | ||
-- ) INTO is_demo_user; | ||
-- | ||
-- -- Demo user should not get access as a teamEditor for the templates team... | ||
-- IF is_demo_user THEN | ||
-- RETURN NULL; | ||
-- END IF; | ||
-- | ||
-- -- ...but all other users should | ||
-- SELECT id INTO templates_team_id FROM teams WHERE slug = 'templates'; | ||
-- IF templates_team_id IS NOT NULL THEN | ||
-- INSERT INTO team_members (user_id, team_id, role) | ||
-- VALUES (NEW.id, templates_team_id, 'teamEditor'); | ||
-- END IF; | ||
-- | ||
-- RETURN NULL; | ||
-- END; | ||
-- $$ LANGUAGE plpgsql; | ||
-- Previous definition from 1696884217237_grant_new_user_template_team_access/up.sql | ||
|
||
CREATE OR REPLACE FUNCTION grant_new_user_template_team_access() RETURNS trigger AS $$ | ||
DECLARE | ||
templates_team_id INT; | ||
BEGIN | ||
SELECT id INTO templates_team_id FROM teams WHERE slug = 'templates'; | ||
IF templates_team_id IS NOT NULL THEN | ||
INSERT INTO team_members (user_id, team_id, role) VALUES (NEW.id, templates_team_id, 'teamEditor'); | ||
END IF; | ||
|
||
RETURN NULL; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER grant_new_user_template_team_access ON users; | ||
|
||
CREATE TRIGGER grant_new_user_template_team_access AFTER INSERT ON users | ||
FOR EACH ROW EXECUTE PROCEDURE grant_new_user_template_team_access(); | ||
|
||
COMMENT ON TRIGGER grant_new_user_template_team_access ON users | ||
IS 'Automatically grant all new users teamEditor access to the shared Templates team'; |