@@ -190,6 +190,109 @@ describe('ObjectStackController', () => {
190190 } ) ;
191191 } ) ;
192192
193+ describe ( 'auth() via AuthPlugin service' , ( ) => {
194+ it ( 'uses kernel.getService("auth") when available' , async ( ) => {
195+ const mockHandleRequest = vi . fn ( ) . mockResolvedValue (
196+ new Response ( JSON . stringify ( { user : { id : '1' } } ) , {
197+ status : 200 ,
198+ headers : new Headers ( { 'Content-Type' : 'application/json' } ) ,
199+ } ) ,
200+ ) ;
201+ const kernelWithAuth = {
202+ ...createMockKernel ( ) ,
203+ getService : vi . fn ( ) . mockReturnValue ( { handleRequest : mockHandleRequest } ) ,
204+ } ;
205+ const svc = new ObjectStackService ( kernelWithAuth ) ;
206+ const ctrl = new ObjectStackController ( svc ) ;
207+ const r = createMockRes ( ) ;
208+ const req = {
209+ params : { 0 : 'sign-in/email' } ,
210+ url : '/api/auth/sign-in/email' ,
211+ method : 'POST' ,
212+ protocol : 'http' ,
213+ get : ( key : string ) => key === 'host' ? 'localhost' : undefined ,
214+ headers : { 'content-type' : 'application/json' } ,
215+ originalUrl : '/api/auth/sign-in/email' ,
216+ } ;
217+
218+ await ctrl . auth ( req , r , { email : 'a@b.com' , password : 'pass' } ) ;
219+
220+ expect ( kernelWithAuth . getService ) . toHaveBeenCalledWith ( 'auth' ) ;
221+ expect ( mockHandleRequest ) . toHaveBeenCalledWith ( expect . any ( Request ) ) ;
222+ expect ( r . _status ) . toBe ( 200 ) ;
223+ } ) ;
224+
225+ it ( 'falls back to dispatcher when auth service is not available' , async ( ) => {
226+ const kernelWithoutAuth = {
227+ ...createMockKernel ( ) ,
228+ getService : vi . fn ( ) . mockReturnValue ( null ) ,
229+ } ;
230+ const svc = new ObjectStackService ( kernelWithoutAuth ) ;
231+ const ctrl = new ObjectStackController ( svc ) ;
232+ const r = createMockRes ( ) ;
233+ const req = { params : { 0 : 'login' } , url : '/api/auth/login' , method : 'POST' } ;
234+
235+ await ctrl . auth ( req , r , { email : 'a@b.com' } ) ;
236+
237+ expect ( svc . dispatcher . handleAuth ) . toHaveBeenCalled ( ) ;
238+ } ) ;
239+
240+ it ( 'forwards GET requests to auth service' , async ( ) => {
241+ const mockHandleRequest = vi . fn ( ) . mockResolvedValue (
242+ new Response ( JSON . stringify ( { session : { token : 'abc' } } ) , {
243+ status : 200 ,
244+ headers : new Headers ( { 'Content-Type' : 'application/json' } ) ,
245+ } ) ,
246+ ) ;
247+ const kernelWithAuth = {
248+ ...createMockKernel ( ) ,
249+ getService : vi . fn ( ) . mockReturnValue ( { handleRequest : mockHandleRequest } ) ,
250+ } ;
251+ const svc = new ObjectStackService ( kernelWithAuth ) ;
252+ const ctrl = new ObjectStackController ( svc ) ;
253+ const r = createMockRes ( ) ;
254+ const req = {
255+ params : { 0 : 'get-session' } ,
256+ url : '/api/auth/get-session' ,
257+ method : 'GET' ,
258+ protocol : 'http' ,
259+ get : ( key : string ) => key === 'host' ? 'localhost' : undefined ,
260+ headers : { } ,
261+ originalUrl : '/api/auth/get-session' ,
262+ } ;
263+
264+ await ctrl . auth ( req , r , { } ) ;
265+
266+ expect ( mockHandleRequest ) . toHaveBeenCalled ( ) ;
267+ expect ( r . _status ) . toBe ( 200 ) ;
268+ } ) ;
269+
270+ it ( 'returns error when auth service throws' , async ( ) => {
271+ const mockHandleRequest = vi . fn ( ) . mockRejectedValue ( new Error ( 'Auth failed' ) ) ;
272+ const kernelWithAuth = {
273+ ...createMockKernel ( ) ,
274+ getService : vi . fn ( ) . mockReturnValue ( { handleRequest : mockHandleRequest } ) ,
275+ } ;
276+ const svc = new ObjectStackService ( kernelWithAuth ) ;
277+ const ctrl = new ObjectStackController ( svc ) ;
278+ const r = createMockRes ( ) ;
279+ const req = {
280+ params : { 0 : 'sign-in/email' } ,
281+ url : '/api/auth/sign-in/email' ,
282+ method : 'POST' ,
283+ protocol : 'http' ,
284+ get : ( key : string ) => key === 'host' ? 'localhost' : undefined ,
285+ headers : { } ,
286+ originalUrl : '/api/auth/sign-in/email' ,
287+ } ;
288+
289+ await ctrl . auth ( req , r , { } ) ;
290+
291+ expect ( r . _status ) . toBe ( 500 ) ;
292+ expect ( r . _body . success ) . toBe ( false ) ;
293+ } ) ;
294+ } ) ;
295+
193296 describe ( 'metadata()' , ( ) => {
194297 it ( 'dispatches to handleMetadata with extracted path' , async ( ) => {
195298 const req = { params : { 0 : '' } , url : '/api/meta/objects' , method : 'GET' } ;
0 commit comments