11import { beforeEach , describe , expect , it , vi } from 'vitest' ;
22import { AuthMiddleware } from '../../../src/webdav/middewares/auth.middleware' ;
33import { createWebDavRequestFixture , createWebDavResponseFixture } from '../../fixtures/webdav.fixture' ;
4- import { ConfigService } from '../../../src/services/config.service' ;
54import { UserCredentialsFixture } from '../../fixtures/login.fixture' ;
5+ import { AuthService } from '../../../src/services/auth.service' ;
6+ import { MissingCredentialsError } from '../../../src/types/command.types' ;
67
78describe ( 'Auth middleware' , ( ) => {
89 beforeEach ( ( ) => {
@@ -18,14 +19,16 @@ describe('Auth middleware', () => {
1819 } ) ;
1920 const next = vi . fn ( ) ;
2021
21- const configServiceStub = vi . spyOn ( ConfigService . instance , 'readUser' ) . mockResolvedValue ( undefined ) ;
22+ const authServiceStub = vi
23+ . spyOn ( AuthService . instance , 'getAuthDetails' )
24+ . mockRejectedValue ( new MissingCredentialsError ( ) ) ;
2225
23- await AuthMiddleware ( ConfigService . instance ) ( req , res , next ) ;
26+ await AuthMiddleware ( AuthService . instance ) ( req , res , next ) ;
2427
25- expect ( configServiceStub ) . toHaveBeenCalledOnce ( ) ;
28+ expect ( authServiceStub ) . toHaveBeenCalledOnce ( ) ;
2629 expect ( next ) . not . toHaveBeenCalled ( ) ;
2730 expect ( res . status ) . toHaveBeenCalledWith ( 401 ) ;
28- expect ( res . send ) . toHaveBeenCalledWith ( { error : 'Unauthorized' } ) ;
31+ expect ( res . send ) . toHaveBeenCalledWith ( { error : new MissingCredentialsError ( ) . message } ) ;
2932 } ) ;
3033
3134 it ( 'When the user is authenticated, then it should add the user to the request' , async ( ) => {
@@ -34,13 +37,13 @@ describe('Auth middleware', () => {
3437 } ) ;
3538 const res = createWebDavResponseFixture ( { } ) ;
3639 const next = vi . fn ( ) ;
37- const configServiceStub = vi . spyOn ( ConfigService . instance , 'readUser ' ) . mockResolvedValue ( UserCredentialsFixture ) ;
40+ const authServiceStub = vi . spyOn ( AuthService . instance , 'getAuthDetails ' ) . mockResolvedValue ( UserCredentialsFixture ) ;
3841
39- await AuthMiddleware ( ConfigService . instance ) ( req , res , next ) ;
42+ await AuthMiddleware ( AuthService . instance ) ( req , res , next ) ;
4043
4144 // @ts -expect-error - User is added to the request, but TS is not picking it as we specified null before
4245 expect ( req . user . rootFolderId ) . to . be . equal ( UserCredentialsFixture . user . root_folder_id ) ;
43- expect ( configServiceStub ) . toHaveBeenCalledOnce ( ) ;
46+ expect ( authServiceStub ) . toHaveBeenCalledOnce ( ) ;
4447 expect ( next ) . toHaveBeenCalledOnce ( ) ;
4548 expect ( res . status ) . not . toHaveBeenCalled ( ) ;
4649 expect ( res . send ) . not . toHaveBeenCalled ( ) ;
0 commit comments