diff --git a/internal/codespaces/api/api_test.go b/internal/codespaces/api/api_test.go index 35616b42d..ebbbe5209 100644 --- a/internal/codespaces/api/api_test.go +++ b/internal/codespaces/api/api_test.go @@ -301,6 +301,34 @@ func TestCodespace_ExportData(t *testing.T) { "name": "test", }, }, + { + name: "just owner", + fields: fields{ + Owner: User{ + Login: "test", + }, + }, + args: args{ + fields: []string{"owner"}, + }, + want: map[string]interface{}{ + "owner": "test", + }, + }, + { + name: "just machine", + fields: fields{ + Machine: CodespaceMachine{ + Name: "test", + }, + }, + args: args{ + fields: []string{"machineName"}, + }, + want: map[string]interface{}{ + "machineName": "test", + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -322,3 +350,87 @@ func TestCodespace_ExportData(t *testing.T) { }) } } + +func createFakeEditServer(t *testing.T, codespaceName string) *httptest.Server { + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + checkPath := "/user/codespaces/" + codespaceName + + if r.URL.Path != checkPath { + t.Fatal("Incorrect path") + } + + if r.Method != http.MethodPatch { + t.Fatal("Incorrect method") + } + + body := r.Body + if body == nil { + t.Fatal("No body") + } + defer body.Close() + + var data map[string]interface{} + err := json.NewDecoder(body).Decode(&data) + + if err != nil { + t.Fatal(err) + } + + if data["display_name"] != "changeTo" { + t.Fatal("Incorrect display name") + } + + response := Codespace{ + DisplayName: "changeTo", + } + + responseData, _ := json.Marshal(response) + fmt.Fprint(w, string(responseData)) + })) +} +func TestAPI_EditCodespace(t *testing.T) { + type args struct { + ctx context.Context + codespaceName string + params *EditCodespaceParams + } + tests := []struct { + name string + args args + want *Codespace + wantErr bool + }{ + { + name: "success", + args: args{ + ctx: context.Background(), + codespaceName: "test", + params: &EditCodespaceParams{ + DisplayName: "changeTo", + }, + }, + want: &Codespace{ + DisplayName: "changeTo", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + svr := createFakeEditServer(t, tt.args.codespaceName) + defer svr.Close() + + a := &API{ + client: &http.Client{}, + githubAPI: svr.URL, + } + got, err := a.EditCodespace(tt.args.ctx, tt.args.codespaceName, tt.args.params) + if (err != nil) != tt.wantErr { + t.Errorf("API.EditCodespace() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("API.EditCodespace() = %v, want %v", got.DisplayName, tt.want.DisplayName) + } + }) + } +}