@@ -95,4 +95,53 @@ describe('ActiveDirectory auth method', () => {
95
95
expect ( dbStub . updateUser . calledOnce ) . to . be . true ;
96
96
} ) ;
97
97
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
+
104
+ userPrincipalName :
'[email protected] ' ,
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
+
130
+ userPrincipalName :
'[email protected] ' ,
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
+ } ) ;
98
147
} ) ;
0 commit comments