mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	
							parent
							
								
									105747293e
								
							
						
					
					
						commit
						80bc4e05a2
					
				@ -2,6 +2,7 @@ import getLogger from '../../test/fixtures/no-logger';
 | 
			
		||||
import patMiddleware from './pat-middleware';
 | 
			
		||||
import User from '../types/user';
 | 
			
		||||
import NotFoundError from '../error/notfound-error';
 | 
			
		||||
import { AccountService } from '../services/account-service';
 | 
			
		||||
 | 
			
		||||
let config: any;
 | 
			
		||||
 | 
			
		||||
@ -15,10 +16,11 @@ beforeEach(() => {
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
test('should not set user if unknown token', async () => {
 | 
			
		||||
    // @ts-expect-error wrong type
 | 
			
		||||
    const accountService = {
 | 
			
		||||
        getAccountByPersonalAccessToken: jest.fn(),
 | 
			
		||||
        addPATSeen: jest.fn(),
 | 
			
		||||
    };
 | 
			
		||||
    } as AccountService;
 | 
			
		||||
 | 
			
		||||
    const func = patMiddleware(config, { accountService });
 | 
			
		||||
 | 
			
		||||
@ -37,9 +39,10 @@ test('should not set user if unknown token', async () => {
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
test('should not set user if token wrong format', async () => {
 | 
			
		||||
    // @ts-expect-error wrong type
 | 
			
		||||
    const accountService = {
 | 
			
		||||
        getAccountByPersonalAccessToken: jest.fn(),
 | 
			
		||||
    };
 | 
			
		||||
    } as AccountService;
 | 
			
		||||
 | 
			
		||||
    const func = patMiddleware(config, { accountService });
 | 
			
		||||
 | 
			
		||||
@ -65,10 +68,11 @@ test('should add user if known token', async () => {
 | 
			
		||||
        id: 44,
 | 
			
		||||
        username: 'my-user',
 | 
			
		||||
    });
 | 
			
		||||
    // @ts-expect-error wrong type
 | 
			
		||||
    const accountService = {
 | 
			
		||||
        getAccountByPersonalAccessToken: jest.fn().mockReturnValue(apiUser),
 | 
			
		||||
        addPATSeen: jest.fn(),
 | 
			
		||||
    };
 | 
			
		||||
    } as AccountService;
 | 
			
		||||
 | 
			
		||||
    const func = patMiddleware(config, { accountService });
 | 
			
		||||
 | 
			
		||||
@ -89,11 +93,12 @@ test('should add user if known token', async () => {
 | 
			
		||||
 | 
			
		||||
test('should call next if accountService throws exception', async () => {
 | 
			
		||||
    getLogger.setMuteError(true);
 | 
			
		||||
    // @ts-expect-error wrong types
 | 
			
		||||
    const accountService = {
 | 
			
		||||
        getAccountByPersonalAccessToken: () => {
 | 
			
		||||
            throw new Error('Error occurred');
 | 
			
		||||
        },
 | 
			
		||||
    };
 | 
			
		||||
    } as AccountService;
 | 
			
		||||
 | 
			
		||||
    const func = patMiddleware(config, { accountService });
 | 
			
		||||
 | 
			
		||||
@ -126,11 +131,12 @@ test('Should not log at error level if user not found', async () => {
 | 
			
		||||
            isEnabled: jest.fn().mockReturnValue(true),
 | 
			
		||||
        },
 | 
			
		||||
    };
 | 
			
		||||
    // @ts-expect-error wrong type
 | 
			
		||||
    const accountService = {
 | 
			
		||||
        getAccountByPersonalAccessToken: jest.fn().mockImplementation(() => {
 | 
			
		||||
            throw new NotFoundError('Could not find pat');
 | 
			
		||||
        }),
 | 
			
		||||
    };
 | 
			
		||||
    } as AccountService;
 | 
			
		||||
    const mw = patMiddleware(conf, { accountService });
 | 
			
		||||
    const cb = jest.fn();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,11 +1,11 @@
 | 
			
		||||
import { IUnleashConfig } from '../types';
 | 
			
		||||
import { IAuthRequest } from '../routes/unleash-types';
 | 
			
		||||
import NotFoundError from '../error/notfound-error';
 | 
			
		||||
import { AccountService } from '../services/account-service';
 | 
			
		||||
 | 
			
		||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
 | 
			
		||||
const patMiddleware = (
 | 
			
		||||
    { getLogger }: Pick<IUnleashConfig, 'getLogger'>,
 | 
			
		||||
    { accountService }: any,
 | 
			
		||||
    { accountService }: { accountService: AccountService },
 | 
			
		||||
): any => {
 | 
			
		||||
    const logger = getLogger('/middleware/pat-middleware.ts');
 | 
			
		||||
    logger.debug('Enabling PAT middleware');
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user