From 756babd6af56bb92626b7ba885f4672d10bc4d07 Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Thu, 23 May 2024 17:06:32 +0100 Subject: [PATCH] Fix user seeding not creating user groups (#1048) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://eaflood.atlassian.net/browse/WATER-3981 The [water-abstraction-service](https://gitub.com/DEFRA/water-abstraction-service) has a mechanism for seeding data for testing that relies on loading in YAML fixture files, sort of. Some content comes from these, but other stuff is hard-coded into the 'loaders'. Long story short, we're going to replace it with something simpler yet more flexible. We initially were going to use [Knex's seeding functionality] (https://knexjs.org/guide/migrations.html#seed-files) to generate the data. But we've had a change of mind (look out for `/data/load` coming in a future change!) We still need users though and we can simplify things in our acceptance tests if they can assume the different types of users have been created. That's when we spotted this isn't actually working! 🤦 It is creating the users but not linking them to the groups that give them the permissions needed. This change fixes the user seed. --- db/seeds/01-users.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/db/seeds/01-users.js b/db/seeds/01-users.js index 0ad8b2a90f..b197e38b1f 100644 --- a/db/seeds/01-users.js +++ b/db/seeds/01-users.js @@ -52,10 +52,17 @@ const seedUsers = [ ] async function seed (knex) { + // The following applies to all the seed users listed above. + // + // First create the user record if it doesn't exist await _insertUsersWhereNotExists(knex) + // Then if the seed user has a group property check if a `user_group` record exists. (This is only expected to be the + // case if we have just created the user) await _updateSeedUsersWithUserIdAndGroupId(knex) + // Finally, if the seed user has a group property and we've not found a `user_group` record, insert one. It's this + // that gives, for example, the billing.data@wrls.gov.uk user the permissions it needs to access billing features await _insertUserGroupsWhereNotExists(knex) } @@ -69,7 +76,8 @@ function _generateHashedPassword () { } async function _groups (knex) { - return knex('idm.groups') + return knex('groups') + .withSchema('idm') .select('groupId', 'group') } @@ -77,13 +85,15 @@ async function _insertUsersWhereNotExists (knex) { const password = _generateHashedPassword() for (const seedUser of seedUsers) { - const existingUser = await knex('idm.users') + const existingUser = await knex('users') + .withSchema('idm') .first('userId') .where('userName', seedUser.userName) .andWhere('application', seedUser.application) if (!existingUser) { - await knex('idm.users') + await knex('users') + .withSchema('idm') .insert({ userName: seedUser.userName, application: seedUser.application, @@ -102,8 +112,9 @@ async function _insertUserGroupsWhereNotExists (knex) { }) for (const seedUser of seedUsersWithGroups) { - const existingUserGroup = await knex('idm.userGroups') - .select('userGroupId') + const existingUserGroup = await knex('userGroups') + .withSchema('idm') + .first('userGroupId') .where('userId', seedUser.userId) .andWhere('groupId', seedUser.groupId) @@ -138,9 +149,9 @@ async function _updateSeedUsersWithUserIdAndGroupId (knex) { } async function _users (knex) { - return knex('idm.users') + return knex('users') + .withSchema('idm') .select('userId', 'userName') - .whereJsonPath('userData', '$.source', '=', 'Seeded') } module.exports = {