@@ -95,4 +95,53 @@ describe('ActiveDirectory auth method', () => {
9595 expect ( dbStub . updateUser . calledOnce ) . to . be . true ;
9696 } ) ;
9797
98+ it ( 'should fail if user is not in user group' , async ( ) => {
99+ const mockReq = { } ;
100+ const mockProfile = {
101+ _json : {
102+ sAMAccountName : 'bad-user' ,
103+ mail : 'bad@test.com' ,
104+ userPrincipalName : 'bad@test.com' ,
105+ title : 'Bad User'
106+ } ,
107+ displayName : 'Bad User'
108+ } ;
109+
110+ ldapStub . isUserInAdGroup . onCall ( 0 ) . resolves ( false ) ;
111+
112+ const done = sinon . spy ( ) ;
113+
114+ await strategyCallback ( mockReq , mockProfile , { } , done ) ;
115+
116+ expect ( done . calledOnce ) . to . be . true ;
117+ const [ err , user ] = done . firstCall . args ;
118+ expect ( err ) . to . include ( 'not a member' ) ;
119+ expect ( user ) . to . be . null ;
120+
121+ expect ( dbStub . updateUser . notCalled ) . to . be . true ;
122+ } ) ;
123+
124+ it ( 'should handle LDAP errors gracefully' , async ( ) => {
125+ const mockReq = { } ;
126+ const mockProfile = {
127+ _json : {
128+ sAMAccountName : 'error-user' ,
129+ mail : 'err@test.com' ,
130+ userPrincipalName : 'err@test.com' ,
131+ title : 'Whoops'
132+ } ,
133+ displayName : 'Error User'
134+ } ;
135+
136+ ldapStub . isUserInAdGroup . rejects ( new Error ( 'LDAP error' ) ) ;
137+
138+ const done = sinon . spy ( ) ;
139+
140+ await strategyCallback ( mockReq , mockProfile , { } , done ) ;
141+
142+ expect ( done . calledOnce ) . to . be . true ;
143+ const [ err , user ] = done . firstCall . args ;
144+ expect ( err . message ) . to . equal ( 'LDAP error' ) ;
145+ expect ( user ) . to . be . null ;
146+ } ) ;
98147} ) ;
0 commit comments