@@ -98,6 +98,15 @@ func TestSubroutineProcess(t *testing.T) {
9898 user ["id" ] = "1234"
9999 users = append (users , user )
100100 })
101+ mux .HandleFunc ("GET /admin/realms/acme/clients" , func (w http.ResponseWriter , r * http.Request ) {
102+ w .Header ().Set ("Content-Type" , "application/json" )
103+ w .WriteHeader (http .StatusOK )
104+ clients := []map [string ]any {
105+ {"id" : "client-uuid" , "clientId" : "acme" },
106+ }
107+ err := json .NewEncoder (w ).Encode (& clients )
108+ assert .NoError (t , err )
109+ })
101110 mux .HandleFunc ("PUT /admin/realms/acme/users/{id}/execute-actions-email" , func (w http.ResponseWriter , r * http.Request ) {
102111 w .Header ().Set ("Content-Type" , "application/json" )
103112 w .WriteHeader (http .StatusNoContent )
@@ -186,11 +195,130 @@ func TestSubroutineProcess(t *testing.T) {
186195 },
187196 expectErr : true ,
188197 setupK8sMocks : func (m * mocks.MockClient ) {
189- // Simulate k8s Get failure (AccountInfo missing)
190198 m .EXPECT ().Get (mock .Anything , mock .Anything , mock .Anything , mock .Anything ).Return (fmt .Errorf ("accountinfo not found" ))
191199 },
192200 setupKeycloakMocks : func (mux * http.ServeMux ) {
193- // No Keycloak calls expected when AccountInfo fetch fails.
201+ },
202+ },
203+ {
204+ desc : "error verifying client (500 from Keycloak)" ,
205+ obj : & v1alpha1.Invite {
206+ Spec : v1alpha1.InviteSpec {
207+ 208+ },
209+ },
210+ expectErr : true ,
211+ setupK8sMocks : func (m * mocks.MockClient ) {
212+ m .EXPECT ().Get (mock .Anything , mock .Anything , mock .Anything , mock .Anything ).RunAndReturn (
213+ func (ctx context.Context , nn types.NamespacedName , o client.Object , opts ... client.GetOption ) error {
214+ accountInfo := & accountsv1alpha1.AccountInfo {
215+ Spec : accountsv1alpha1.AccountInfoSpec {
216+ Organization : accountsv1alpha1.AccountLocation {
217+ Name : "acme" ,
218+ },
219+ },
220+ }
221+ * o .(* accountsv1alpha1.AccountInfo ) = * accountInfo
222+ return nil
223+ },
224+ )
225+ },
226+ setupKeycloakMocks : func (mux * http.ServeMux ) {
227+ users := []map [string ]any {}
228+ mux .HandleFunc ("GET /admin/realms/acme/users" , func (w http.ResponseWriter , r * http.Request ) {
229+ w .Header ().Set ("Content-Type" , "application/json" )
230+ w .WriteHeader (http .StatusOK )
231+ err := json .NewEncoder (w ).Encode (& users )
232+ assert .NoError (t , err )
233+ })
234+ mux .HandleFunc ("POST /admin/realms/acme/users" , func (w http.ResponseWriter , r * http.Request ) {
235+ w .Header ().Set ("Content-Type" , "application/json" )
236+ w .WriteHeader (http .StatusCreated )
237+ user := map [string ]any {}
238+ err := json .NewDecoder (r .Body ).Decode (& user )
239+ assert .NoError (t , err )
240+ user ["id" ] = "1234"
241+ users = append (users , user )
242+ })
243+ mux .HandleFunc ("GET /admin/realms/acme/clients" , func (w http.ResponseWriter , r * http.Request ) {
244+ w .WriteHeader (http .StatusInternalServerError )
245+ _ , _ = w .Write ([]byte (`{"error":"boom"}` ))
246+ })
247+ },
248+ },
249+ {
250+ desc : "client does not exist yet, should requeue" ,
251+ obj : & v1alpha1.Invite {
252+ Spec : v1alpha1.InviteSpec {
253+ 254+ },
255+ },
256+ expectErr : true ,
257+ setupK8sMocks : func (m * mocks.MockClient ) {
258+ m .EXPECT ().Get (mock .Anything , mock .Anything , mock .Anything , mock .Anything ).RunAndReturn (
259+ func (ctx context.Context , nn types.NamespacedName , o client.Object , opts ... client.GetOption ) error {
260+ accountInfo := & accountsv1alpha1.AccountInfo {
261+ Spec : accountsv1alpha1.AccountInfoSpec {
262+ Organization : accountsv1alpha1.AccountLocation {
263+ Name : "acme" ,
264+ },
265+ },
266+ }
267+ * o .(* accountsv1alpha1.AccountInfo ) = * accountInfo
268+ return nil
269+ },
270+ )
271+ },
272+ setupKeycloakMocks : func (mux * http.ServeMux ) {
273+ users := []map [string ]any {}
274+ mux .HandleFunc ("GET /admin/realms/acme/users" , func (w http.ResponseWriter , r * http.Request ) {
275+ w .Header ().Set ("Content-Type" , "application/json" )
276+ w .WriteHeader (http .StatusOK )
277+ err := json .NewEncoder (w ).Encode (& users )
278+ assert .NoError (t , err )
279+ })
280+ mux .HandleFunc ("POST /admin/realms/acme/users" , func (w http.ResponseWriter , r * http.Request ) {
281+ w .Header ().Set ("Content-Type" , "application/json" )
282+ w .WriteHeader (http .StatusCreated )
283+ user := map [string ]any {}
284+ err := json .NewDecoder (r .Body ).Decode (& user )
285+ assert .NoError (t , err )
286+ user ["id" ] = "1234"
287+ users = append (users , user )
288+ })
289+ mux .HandleFunc ("GET /admin/realms/acme/clients" , func (w http.ResponseWriter , r * http.Request ) {
290+ w .Header ().Set ("Content-Type" , "application/json" )
291+ w .WriteHeader (http .StatusOK )
292+ clients := []map [string ]any {}
293+ err := json .NewEncoder (w ).Encode (& clients )
294+ assert .NoError (t , err )
295+ })
296+ },
297+ },
298+ {
299+ desc : "organization name is empty in AccountInfo" ,
300+ obj : & v1alpha1.Invite {
301+ Spec : v1alpha1.InviteSpec {
302+ 303+ },
304+ },
305+ expectErr : true ,
306+ setupK8sMocks : func (m * mocks.MockClient ) {
307+ m .EXPECT ().Get (mock .Anything , mock .Anything , mock .Anything , mock .Anything ).RunAndReturn (
308+ func (ctx context.Context , nn types.NamespacedName , o client.Object , opts ... client.GetOption ) error {
309+ accountInfo := & accountsv1alpha1.AccountInfo {
310+ Spec : accountsv1alpha1.AccountInfoSpec {
311+ Organization : accountsv1alpha1.AccountLocation {
312+ Name : "" ,
313+ },
314+ },
315+ }
316+ * o .(* accountsv1alpha1.AccountInfo ) = * accountInfo
317+ return nil
318+ },
319+ )
320+ },
321+ setupKeycloakMocks : func (mux * http.ServeMux ) {
194322 },
195323 },
196324 }
0 commit comments