mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	feat: Adds sendEmail flag to body of create user request (#894)
This allows frontend to support a toggle for admins wanting to create users and passwords manually, without sending emails to users they create. Co-authored-by: Ivar Conradi Østhus <ivarconr@gmail.com>
This commit is contained in:
		
							parent
							
								
									2bcdb5ec31
								
							
						
					
					
						commit
						45f5d1fb1a
					
				@ -12,6 +12,14 @@ import { IUnleashServices } from '../../types/services';
 | 
			
		||||
import SessionService from '../../services/session-service';
 | 
			
		||||
import { IAuthRequest } from '../unleash-types';
 | 
			
		||||
 | 
			
		||||
interface ICreateUserBody {
 | 
			
		||||
    username: string;
 | 
			
		||||
    email: string;
 | 
			
		||||
    name: string;
 | 
			
		||||
    rootRole: number;
 | 
			
		||||
    sendEmail: boolean;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default class UserAdminController extends Controller {
 | 
			
		||||
    private userService: UserService;
 | 
			
		||||
 | 
			
		||||
@ -117,8 +125,11 @@ export default class UserAdminController extends Controller {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
 | 
			
		||||
    async createUser(req: IAuthRequest, res: Response): Promise<void> {
 | 
			
		||||
        const { username, email, name, rootRole } = req.body;
 | 
			
		||||
    async createUser(
 | 
			
		||||
        req: IAuthRequest<any, any, ICreateUserBody, any>,
 | 
			
		||||
        res: Response,
 | 
			
		||||
    ): Promise<void> {
 | 
			
		||||
        const { username, email, name, rootRole, sendEmail } = req.body;
 | 
			
		||||
        const { user } = req;
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
@ -131,7 +142,6 @@ export default class UserAdminController extends Controller {
 | 
			
		||||
                },
 | 
			
		||||
                user,
 | 
			
		||||
            );
 | 
			
		||||
 | 
			
		||||
            const inviteLink = await this.resetTokenService.createNewUserUrl(
 | 
			
		||||
                createdUser.id,
 | 
			
		||||
                user.email,
 | 
			
		||||
@ -139,7 +149,9 @@ export default class UserAdminController extends Controller {
 | 
			
		||||
 | 
			
		||||
            let emailSent = false;
 | 
			
		||||
            const emailConfigured = this.emailService.configured();
 | 
			
		||||
            if (emailConfigured) {
 | 
			
		||||
            const reallySendEmail =
 | 
			
		||||
                emailConfigured && (sendEmail !== undefined ? sendEmail : true);
 | 
			
		||||
            if (reallySendEmail) {
 | 
			
		||||
                try {
 | 
			
		||||
                    await this.emailService.sendGettingStartedMail(
 | 
			
		||||
                        createdUser.name,
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
import { setupApp } from '../../helpers/test-helper';
 | 
			
		||||
import { setupApp, setupAppWithCustomConfig } from '../../helpers/test-helper';
 | 
			
		||||
import dbInit from '../../helpers/database-init';
 | 
			
		||||
import getLogger from '../../../fixtures/no-logger';
 | 
			
		||||
import {
 | 
			
		||||
@ -244,6 +244,44 @@ test('Creates a user and includes inviteLink and emailConfigured', async () => {
 | 
			
		||||
        });
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
test('Creates a user but does not send email if sendEmail is set to false', async () => {
 | 
			
		||||
    const myAppConfig = await setupAppWithCustomConfig(stores, {
 | 
			
		||||
        email: {
 | 
			
		||||
            host: 'smtp.ethereal.email',
 | 
			
		||||
            smtpuser: 'rafaela.pouros@ethereal.email',
 | 
			
		||||
            smtppass: 'CuVPBSvUFBPuqXMFEe',
 | 
			
		||||
        },
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    await myAppConfig.request
 | 
			
		||||
        .post('/api/admin/user-admin')
 | 
			
		||||
        .send({
 | 
			
		||||
            email: 'some@getunelash.ai',
 | 
			
		||||
            name: 'Some Name',
 | 
			
		||||
            rootRole: editorRole.id,
 | 
			
		||||
            sendEmail: false,
 | 
			
		||||
        })
 | 
			
		||||
        .set('Content-Type', 'application/json')
 | 
			
		||||
        .expect(201)
 | 
			
		||||
        .expect((res) => {
 | 
			
		||||
            expect(res.body.emailSent).toBeFalsy();
 | 
			
		||||
        });
 | 
			
		||||
    await myAppConfig.request
 | 
			
		||||
        .post('/api/admin/user-admin')
 | 
			
		||||
        .send({
 | 
			
		||||
            email: 'some2@getunelash.ai',
 | 
			
		||||
            name: 'Some2 Name',
 | 
			
		||||
            rootRole: editorRole.id,
 | 
			
		||||
        })
 | 
			
		||||
        .set('Content-Type', 'application/json')
 | 
			
		||||
        .expect(201)
 | 
			
		||||
        .expect((res) => {
 | 
			
		||||
            expect(res.body.emailSent).toBeTruthy();
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
    await myAppConfig.destroy();
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
test('generates USER_CREATED event', async () => {
 | 
			
		||||
    expect.assertions(5);
 | 
			
		||||
    const email = 'some@getunelash.ai';
 | 
			
		||||
 | 
			
		||||
@ -60,6 +60,14 @@ export async function setupApp(stores: IUnleashStores): Promise<IUnleashTest> {
 | 
			
		||||
    return createApp(stores);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export async function setupAppWithCustomConfig(
 | 
			
		||||
    stores: IUnleashStores,
 | 
			
		||||
    // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
 | 
			
		||||
    customOptions: any,
 | 
			
		||||
): Promise<IUnleashTest> {
 | 
			
		||||
    return createApp(stores, undefined, undefined, customOptions);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export async function setupAppWithAuth(
 | 
			
		||||
    stores: IUnleashStores,
 | 
			
		||||
): Promise<IUnleashTest> {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user