Skip to content

Commit

Permalink
Fix user seeding not creating user groups (#1048)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Cruikshanks authored May 23, 2024
1 parent a8dfbe2 commit 756babd
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions db/seeds/01-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected] user the permissions it needs to access billing features
await _insertUserGroupsWhereNotExists(knex)
}

Expand All @@ -69,21 +76,24 @@ function _generateHashedPassword () {
}

async function _groups (knex) {
return knex('idm.groups')
return knex('groups')
.withSchema('idm')
.select('groupId', 'group')
}

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,
Expand All @@ -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)

Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 756babd

Please sign in to comment.