@@ -428,3 +428,74 @@ func TestParseRepo(t *testing.T) {
428428 })
429429 }
430430}
431+
432+ func TestUpdateSettings (t * testing.T ) {
433+ mockKvStore , mockAPI , mockLogger , mockLoggerWith , _ := GetTestSetup (t )
434+ p := getPluginTest (mockAPI , mockKvStore )
435+ mockGHContext , err := GetMockUserContext (p , mockLogger )
436+ assert .NoError (t , err )
437+
438+ tests := []struct {
439+ name string
440+ requestBody string
441+ setup func ()
442+ expectedStatusCode int
443+ assertions func (t * testing.T , rec * httptest.ResponseRecorder )
444+ }{
445+ {
446+ name : "Invalid JSON Request Body" ,
447+ requestBody : "invalid-json" ,
448+ setup : func () {
449+ mockLogger .EXPECT ().WithError (gomock .Any ()).Return (mockLoggerWith ).Times (1 )
450+ mockLoggerWith .EXPECT ().Warnf ("Error decoding settings from JSON body" ).Times (1 )
451+ },
452+ expectedStatusCode : http .StatusBadRequest ,
453+ assertions : func (t * testing.T , rec * httptest.ResponseRecorder ) {
454+ assert .Equal (t , http .StatusBadRequest , rec .Result ().StatusCode )
455+ },
456+ },
457+ {
458+ name : "Error Storing User Info" ,
459+ requestBody : `{"access_token": "mockAccessToken"}` ,
460+ setup : func () {
461+ p .setConfiguration (& Configuration {
462+ EncryptionKey : "dummyEncryptKey1" ,
463+ })
464+ mockKvStore .EXPECT ().Set (gomock .Any (), gomock .Any ()).Return (false , errors .New ("store error" )).Times (1 )
465+ mockLogger .EXPECT ().WithError (gomock .Any ()).Return (mockLoggerWith ).Times (1 )
466+ mockLoggerWith .EXPECT ().Warnf ("Failed to store GitHub user info" ).Times (1 )
467+ },
468+ expectedStatusCode : http .StatusInternalServerError ,
469+ assertions : func (t * testing.T , rec * httptest.ResponseRecorder ) {
470+ assert .Equal (t , http .StatusInternalServerError , rec .Result ().StatusCode )
471+ },
472+ },
473+ {
474+ name : "Successful Update" ,
475+ requestBody : `{"access_token": "mockAccessToken"}` ,
476+ setup : func () {
477+ mockKvStore .EXPECT ().Set (gomock .Any (), gomock .Any ()).Return (true , nil ).Times (1 )
478+ },
479+ expectedStatusCode : http .StatusOK ,
480+ assertions : func (t * testing.T , rec * httptest.ResponseRecorder ) {
481+ assert .Equal (t , http .StatusOK , rec .Result ().StatusCode )
482+ var settings UserSettings
483+ err := json .NewDecoder (rec .Body ).Decode (& settings )
484+ assert .NoError (t , err )
485+ assert .Equal (t , mockGHContext .GHInfo .Settings , & settings )
486+ },
487+ },
488+ }
489+ for _ , tc := range tests {
490+ t .Run (tc .name , func (t * testing.T ) {
491+ tc .setup ()
492+
493+ req := httptest .NewRequest (http .MethodPost , "/update/settings" , strings .NewReader (tc .requestBody ))
494+ rec := httptest .NewRecorder ()
495+
496+ p .updateSettings (mockGHContext , rec , req )
497+
498+ tc .assertions (t , rec )
499+ })
500+ }
501+ }
0 commit comments