@@ -17,6 +17,17 @@ const (
1717 HEADER_ACCEPT = "application/json"
1818)
1919
20+ // Helper functions for common HTTP responses
21+ func writeJSONResponse (w http.ResponseWriter , status int , body string ) {
22+ w .Header ().Set (HEADER_CONTENT_TYPE , HEADER_ACCEPT )
23+ w .WriteHeader (status )
24+ fmt .Fprint (w , body )
25+ }
26+
27+ func writeMethodNotAllowed (w http.ResponseWriter ) {
28+ w .WriteHeader (http .StatusMethodNotAllowed )
29+ }
30+
2031var (
2132 // TEST_PROJECT is a test project used in unit tests.
2233 TEST_PROJECT = & gitlab.Project {
@@ -379,6 +390,18 @@ func setupEmptyTestServer(t *testing.T, role string, instanceSize string) (*http
379390 server := httptest .NewServer (mux )
380391 t .Cleanup (server .Close )
381392
393+ // Add test handler for current user
394+ mux .HandleFunc ("/api/v4/user" , func (w http.ResponseWriter , r * http.Request ) {
395+ if r .Method != http .MethodGet {
396+ w .WriteHeader (http .StatusMethodNotAllowed )
397+ return
398+ }
399+ w .Header ().Set (HEADER_CONTENT_TYPE , HEADER_ACCEPT )
400+ w .WriteHeader (http .StatusOK )
401+ // Return mock current user with ID 1
402+ fmt .Fprint (w , `{"id": 1, "username": "testuser", "name": "Test User", "state": "active"}` )
403+ })
404+
382405 gitlabInstance , err := NewGitlabInstance (& GitlabInstanceOpts {
383406 GitlabURL : server .URL ,
384407 GitlabToken : "test-token" ,
@@ -405,30 +428,42 @@ func setupTestServer(t *testing.T, role string, instanceSize string) (*http.Serv
405428 server := httptest .NewServer (mux )
406429 t .Cleanup (server .Close )
407430
408- gitlabInstance , err := NewGitlabInstance (& GitlabInstanceOpts {
409- GitlabURL : server .URL ,
410- GitlabToken : "test-token" ,
411- Role : role ,
412- InstanceSize : instanceSize ,
413- MaxRetries : 0 ,
414- })
415-
416- if err != nil {
417- t .Fatalf ("Failed to create client: %v" , err )
418- }
419-
420431 // Add test handlers for the projects and groups endpoints.
421432 setupTestProjects (mux )
422433 setupTestGroups (mux )
423434
424435 // Add test handlers for the GraphQL endpoint.
425436 setupTestGraphQL (mux )
426437
438+ // Add test handler for current user
439+ mux .HandleFunc ("/api/v4/user" , func (w http.ResponseWriter , r * http.Request ) {
440+ if r .Method != http .MethodGet {
441+ w .WriteHeader (http .StatusMethodNotAllowed )
442+ return
443+ }
444+ w .Header ().Set (HEADER_CONTENT_TYPE , HEADER_ACCEPT )
445+ w .WriteHeader (http .StatusOK )
446+ // Return mock current user with ID 1
447+ fmt .Fprint (w , `{"id": 1, "username": "testuser", "name": "Test User", "state": "active"}` )
448+ })
449+
427450 // Catch-all handler for undefined routes
428451 mux .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
429452 http .Error (w , fmt .Sprintf ("Undefined route accessed: %s %s" , r .Method , r .URL .Path ), http .StatusNotFound )
430453 })
431454
455+ gitlabInstance , err := NewGitlabInstance (& GitlabInstanceOpts {
456+ GitlabURL : server .URL ,
457+ GitlabToken : "test-token" ,
458+ Role : role ,
459+ InstanceSize : instanceSize ,
460+ MaxRetries : 0 ,
461+ })
462+
463+ if err != nil {
464+ t .Fatalf ("Failed to create client: %v" , err )
465+ }
466+
432467 return mux , gitlabInstance
433468}
434469
@@ -476,15 +511,11 @@ func setupTestGroup(mux *http.ServeMux, group *gitlab.Group, stringResponse stri
476511 mux .HandleFunc (fmt .Sprintf ("/api/v4/groups/%d" , group .ID ), func (w http.ResponseWriter , r * http.Request ) {
477512 switch r .Method {
478513 case http .MethodGet :
479- w .Header ().Set (HEADER_CONTENT_TYPE , HEADER_ACCEPT )
480- w .WriteHeader (http .StatusOK )
481- fmt .Fprint (w , stringResponse )
514+ writeJSONResponse (w , http .StatusOK , stringResponse )
482515 case http .MethodPut :
483- w .Header ().Set (HEADER_CONTENT_TYPE , HEADER_ACCEPT )
484- w .WriteHeader (http .StatusOK )
485- fmt .Fprint (w , stringResponse )
516+ writeJSONResponse (w , http .StatusOK , stringResponse )
486517 default :
487- w . WriteHeader ( http . StatusMethodNotAllowed )
518+ writeMethodNotAllowed ( w )
488519 return
489520 }
490521 })
@@ -499,6 +530,17 @@ func setupTestGroup(mux *http.ServeMux, group *gitlab.Group, stringResponse stri
499530 w .WriteHeader (http .StatusOK )
500531 w .Write ([]byte {0x89 , 0x50 , 0x4E , 0x47 , 0x0D , 0x0A , 0x1A , 0x0A }) // PNG header
501532 })
533+ // Setup the add group member endpoint
534+ mux .HandleFunc (fmt .Sprintf ("/api/v4/groups/%d/members" , group .ID ), func (w http.ResponseWriter , r * http.Request ) {
535+ if r .Method != http .MethodPost {
536+ w .WriteHeader (http .StatusMethodNotAllowed )
537+ return
538+ }
539+ w .Header ().Set (HEADER_CONTENT_TYPE , HEADER_ACCEPT )
540+ w .WriteHeader (http .StatusCreated )
541+ // Return a mock member response
542+ fmt .Fprint (w , `{"id": 1, "username": "testuser", "name": "Test User", "state": "active", "access_level": 50}` )
543+ })
502544}
503545
504546func setupTestGraphQL (mux * http.ServeMux ) {
@@ -678,17 +720,24 @@ func setupTestProject(mux *http.ServeMux, project *gitlab.Project, stringRespons
678720 mux .HandleFunc (fmt .Sprintf ("/api/v4/projects/%d/issues/%d" , project .ID , TEST_ISSUE .IID ), func (w http.ResponseWriter , r * http.Request ) {
679721 switch r .Method {
680722 case http .MethodGet :
681- w .Header ().Set (HEADER_CONTENT_TYPE , HEADER_ACCEPT )
682- w .WriteHeader (http .StatusOK )
683- fmt .Fprint (w , TEST_ISSUE_STRING )
723+ writeJSONResponse (w , http .StatusOK , TEST_ISSUE_STRING )
684724 case http .MethodPut :
685- w .Header ().Set (HEADER_CONTENT_TYPE , HEADER_ACCEPT )
686- w .WriteHeader (http .StatusOK )
687- fmt .Fprint (w , TEST_ISSUE_STRING )
725+ writeJSONResponse (w , http .StatusOK , TEST_ISSUE_STRING )
688726 default :
727+ writeMethodNotAllowed (w )
728+ return
729+ }
730+ })
731+
732+ mux .HandleFunc (fmt .Sprintf ("/api/v4/projects/%d/members" , project .ID ), func (w http.ResponseWriter , r * http.Request ) {
733+ if r .Method != http .MethodPost {
689734 w .WriteHeader (http .StatusMethodNotAllowed )
690735 return
691736 }
737+ w .Header ().Set (HEADER_CONTENT_TYPE , HEADER_ACCEPT )
738+ w .WriteHeader (http .StatusCreated )
739+ // Return a mock member response
740+ fmt .Fprint (w , `{"id": 1, "username": "testuser", "name": "Test User", "state": "active", "access_level": 50}` )
692741 })
693742 // Setup the get project issue notes response from the project ID and issue IID
694743}
0 commit comments