| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| 'use strict'; | ||
|
|
||
| const load = jest.fn(() => mock); | ||
| const resetPasswordByEmail = jest.fn(); | ||
| const admin = { | ||
| services: { | ||
| user: { | ||
| resetPasswordByEmail, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const mock = { | ||
| load, | ||
| admin, | ||
| }; | ||
|
|
||
| jest.mock('../../index', () => { | ||
| return jest.fn(() => mock); | ||
| }); | ||
|
|
||
| const inquirer = require('inquirer'); | ||
| const resetAdminPasswordCommand = require('../admin-reset'); | ||
|
|
||
| describe('admin:reset-password command', () => { | ||
| beforeEach(() => { | ||
| load.mockClear(); | ||
| resetPasswordByEmail.mockClear(); | ||
| }); | ||
|
|
||
| test('resetAdminPasswordCommand accepts direct input', async () => { | ||
| const email = 'email@email.fr'; | ||
| const password = 'testPasword1234'; | ||
|
|
||
| const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {}); | ||
| const consoleLog = jest.spyOn(console, 'log').mockImplementation(() => {}); | ||
|
|
||
| await resetAdminPasswordCommand({ email, password }); | ||
|
|
||
| expect(mockExit).toHaveBeenCalledWith(0); | ||
| expect(consoleLog).toHaveBeenCalled(); | ||
| expect(load).toHaveBeenCalled(); | ||
| expect(resetPasswordByEmail).toHaveBeenCalledWith(email, password); | ||
|
|
||
| mockExit.mockRestore(); | ||
| consoleLog.mockRestore(); | ||
| }); | ||
|
|
||
| describe('Handles prompt input', () => { | ||
| test('Only prompt on TTY', async () => { | ||
| const tmpTTY = process.stdin.isTTY; | ||
| process.stdin.isTTY = false; | ||
|
|
||
| // throw so the code will stop executing | ||
| const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => { | ||
| throw new Error('exit'); | ||
| }); | ||
|
|
||
| const consoleError = jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
|
|
||
| await resetAdminPasswordCommand().catch(err => { | ||
| expect(err).toEqual(new Error('exit')); | ||
| }); | ||
|
|
||
| expect(consoleError).toBeCalledWith('Missing required options `email` or `password`'); | ||
| expect(mockExit).toHaveBeenCalledWith(1); | ||
| expect(load).not.toHaveBeenCalled(); | ||
| expect(resetPasswordByEmail).not.toHaveBeenCalled(); | ||
|
|
||
| process.stdin.isTTY = tmpTTY; | ||
| mockExit.mockRestore(); | ||
| consoleError.mockRestore(); | ||
| }); | ||
|
|
||
| test('Stops if not confirmed', async () => { | ||
| process.stdin.isTTY = true; | ||
| const email = 'email@email.fr'; | ||
| const password = 'testPasword1234'; | ||
|
|
||
| const mockInquiry = jest | ||
| .spyOn(inquirer, 'prompt') | ||
| .mockImplementationOnce(async () => ({ email, password, confirm: false })); | ||
|
|
||
| // throw so the code will stop executing | ||
| const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => { | ||
| throw new Error('exit'); | ||
| }); | ||
|
|
||
| await resetAdminPasswordCommand().catch(err => { | ||
| expect(err).toEqual(new Error('exit')); | ||
| }); | ||
|
|
||
| expect(mockInquiry).toHaveBeenLastCalledWith([ | ||
| expect.objectContaining({ | ||
| message: expect.any(String), | ||
| name: 'email', | ||
| type: 'input', | ||
| }), | ||
| expect.objectContaining({ | ||
| message: expect.any(String), | ||
| name: 'password', | ||
| type: 'password', | ||
| }), | ||
| expect.objectContaining({ | ||
| message: expect.any(String), | ||
| name: 'confirm', | ||
| type: 'confirm', | ||
| }), | ||
| ]); | ||
| expect(mockExit).toHaveBeenCalledWith(0); | ||
| expect(load).not.toHaveBeenCalled(); | ||
| expect(resetPasswordByEmail).not.toHaveBeenCalled(); | ||
|
|
||
| mockExit.mockRestore(); | ||
| mockInquiry.mockRestore(); | ||
| }); | ||
|
|
||
| test('Calls the reset method with user input', async () => { | ||
| const email = 'email@email.fr'; | ||
| const password = 'testPasword1234'; | ||
|
|
||
| const mockInquiry = jest | ||
| .spyOn(inquirer, 'prompt') | ||
| .mockImplementationOnce(async () => ({ email, password, confirm: true })); | ||
|
|
||
| const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {}); | ||
| const consoleLog = jest.spyOn(console, 'log').mockImplementation(() => {}); | ||
|
|
||
| await resetAdminPasswordCommand(); | ||
|
|
||
| expect(mockExit).toHaveBeenCalledWith(0); | ||
| expect(consoleLog).toHaveBeenCalled(); | ||
| expect(load).toHaveBeenCalled(); | ||
| expect(resetPasswordByEmail).toHaveBeenCalledWith(email, password); | ||
|
|
||
| mockInquiry.mockRestore(); | ||
| mockExit.mockRestore(); | ||
| consoleLog.mockRestore(); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| 'use strict'; | ||
|
|
||
| const _ = require('lodash'); | ||
| const inquirer = require('inquirer'); | ||
| const strapi = require('../index'); | ||
|
|
||
| const promptQuestions = [ | ||
| { type: 'input', name: 'email', message: 'User email?' }, | ||
| { type: 'password', name: 'password', message: 'New password?' }, | ||
| { | ||
| type: 'confirm', | ||
| name: 'confirm', | ||
| message: "Do you really want to reset this user's password?", | ||
| }, | ||
| ]; | ||
|
|
||
| /** | ||
| * Reset user's password | ||
| * @param {Object} cmdOptions - command options | ||
| * @param {string} cmdOptions.email - user's email | ||
| * @param {string} cmdOptions.password - user's new password | ||
| */ | ||
| module.exports = async function(cmdOptions = {}) { | ||
| const { email, password } = cmdOptions; | ||
|
|
||
| if (_.isEmpty(email) && _.isEmpty(password) && process.stdin.isTTY) { | ||
| const inquiry = await inquirer.prompt(promptQuestions); | ||
|
|
||
| if (!inquiry.confirm) { | ||
| process.exit(0); | ||
alexandrebodin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return changePassword(inquiry); | ||
| } | ||
|
|
||
| if (_.isEmpty(email) || _.isEmpty(password)) { | ||
alexandrebodin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| console.error('Missing required options `email` or `password`'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| return changePassword({ email, password }); | ||
| }; | ||
|
|
||
| async function changePassword({ email, password }) { | ||
| const app = await strapi().load(); | ||
|
|
||
| await app.admin.services.user.resetPasswordByEmail(email, password); | ||
|
|
||
| console.log(`Successfully reset user's password`); | ||
alexandrebodin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| process.exit(0); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.