-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #259 from adhocteam/main
Bootstrap account admin
- Loading branch information
Showing
7 changed files
with
97 additions
and
9 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { User, Permission } from '../models'; | ||
import logger from '../logger'; | ||
import SCOPES from '../middleware/scopeConstants'; | ||
|
||
const { ADMIN } = SCOPES; | ||
|
||
export const ADMIN_EMAIL = 'ryan.ahearn@gsa.gov'; | ||
|
||
const bootstrapAdmin = async () => { | ||
const user = await User.findOne({ where: { email: ADMIN_EMAIL } }); | ||
if (user === null) { | ||
throw new Error(`User ${ADMIN_EMAIL} could not be found to bootstrap admin`); | ||
} | ||
|
||
const [permission, created] = await Permission.findOrCreate({ | ||
where: { | ||
userId: user.id, | ||
scopeId: ADMIN, | ||
regionId: 14, | ||
}, | ||
}); | ||
if (created) { | ||
logger.warn(`SECURITY ALERT: Setting ${ADMIN_EMAIL} as an ADMIN`); | ||
} | ||
return permission; | ||
}; | ||
|
||
export default bootstrapAdmin; |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import bootstrapAdmin, { ADMIN_EMAIL } from './bootstrapAdmin'; | ||
import db, { User } from '../models'; | ||
|
||
describe('Bootstrap the first Admin user', () => { | ||
afterAll(() => { | ||
db.sequelize.close(); | ||
}); | ||
|
||
describe('when user already exists', () => { | ||
beforeAll(async () => { | ||
await User.findOrCreate({ where: { email: ADMIN_EMAIL } }); | ||
}); | ||
afterAll(async () => { | ||
await User.destroy({ where: { email: ADMIN_EMAIL } }); | ||
}); | ||
|
||
it('should create an admin permission for the user', async () => { | ||
const user = await User.findOne({ where: { email: ADMIN_EMAIL } }); | ||
expect(user).toBeDefined(); | ||
expect((await user.getPermissions()).length).toBe(0); | ||
const newPermission = await bootstrapAdmin(); | ||
expect(newPermission).toBeDefined(); | ||
expect((await user.getPermissions()).length).toBe(1); | ||
await newPermission.destroy(); | ||
}); | ||
|
||
it('is idempotent', async () => { | ||
const permission = await bootstrapAdmin(); | ||
const secondRun = await bootstrapAdmin(); | ||
expect(secondRun.id).toBe(permission.id); | ||
await permission.destroy(); | ||
}); | ||
}); | ||
|
||
describe('when user does not exist', () => { | ||
it('should loudly exit', async () => { | ||
await expect(bootstrapAdmin()).rejects.toThrow(`User ${ADMIN_EMAIL} could not be found to bootstrap admin`); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import bootstrapAdmin from './bootstrapAdmin'; | ||
|
||
/** | ||
* bootstrapAdminCLI is responsible for setting the first ADMIN for TTA Smart Hub. | ||
* It should be run via `cf run-task tta-smarthub-prod "yarn db:bootstrap:admin"` | ||
* | ||
* All other admins and permissions should be set via the admin UI. | ||
* | ||
* To change the initial admin (for instance, after the previous one has left the project) | ||
* Open a new issue and PR to update the ADMIN_EMAIL constant within bootstrapAdmin.js | ||
*/ | ||
bootstrapAdmin().catch((e) => { | ||
// eslint-disable-next-line no-console | ||
console.log(e); | ||
return process.exit(1); | ||
}); |
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
4 changes: 2 additions & 2 deletions
4
tools/importPlanGoals.test.js → src/tools/importPlanGoals.test.js
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
File renamed without changes.