github.com/google/go-github/v74@v74.0.0/github/codespaces_test.go (about)

     1  // Copyright 2023 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  	"net/http"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/google/go-cmp/cmp"
    16  )
    17  
    18  func TestCodespacesService_ListInRepo(t *testing.T) {
    19  	t.Parallel()
    20  	client, mux, _ := setup(t)
    21  
    22  	mux.HandleFunc("/repos/owner/repo/codespaces", func(w http.ResponseWriter, r *http.Request) {
    23  		testMethod(t, r, "GET")
    24  		testFormValues(t, r, values{
    25  			"page":     "1",
    26  			"per_page": "2",
    27  		})
    28  		fmt.Fprint(w, `{"total_count":2,"codespaces":[{"id":1,"name":"monalisa-octocat-hello-world-g4wpq6h95q","environment_id":"26a7c758-7299-4a73-b978-5a92a7ae98a0","owner":{"login":"octocat"},"billable_owner":{"login":"octocat"},"repository":{"id":1296269},"machine":{"name":"standardLinux","display_name":"4 cores, 8 GB RAM, 64 GB storage","operating_system":"linux","storage_in_bytes":68719476736,"memory_in_bytes":8589934592,"cpus":4},"prebuild":false,"devcontainer_path":".devcontainer/devcontainer.json","created_at":"2021-10-14T00:53:30-06:00","updated_at":"2021-10-14T00:53:32-06:00","last_used_at":"2021-10-14T00:53:30-06:00","state":"Available","url":"https://api.github.com/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q","git_status":{"ahead":0,"behind":0,"has_unpushed_changes":false,"has_uncommitted_changes":false,"ref":"main"},"location":"WestUs2","idle_timeout_minutes":60,"web_url":"https://monalisa-octocat-hello-world-g4wpq6h95q.github.dev","machines_url":"https://api.github.com/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/machines","start_url":"https://api.github.com/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/start","stop_url":"https://api.github.com/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/stop","recent_folders":["testfolder1","testfolder2"]},{"id":2}]}`)
    29  	})
    30  
    31  	opt := &ListOptions{Page: 1, PerPage: 2}
    32  	ctx := context.Background()
    33  	codespaces, _, err := client.Codespaces.ListInRepo(ctx, "owner", "repo", opt)
    34  	if err != nil {
    35  		t.Errorf("Codespaces.ListInRepo returned error: %v", err)
    36  	}
    37  
    38  	want := &ListCodespaces{TotalCount: Ptr(2), Codespaces: []*Codespace{
    39  		{
    40  			ID:            Ptr(int64(1)),
    41  			Name:          Ptr("monalisa-octocat-hello-world-g4wpq6h95q"),
    42  			EnvironmentID: Ptr("26a7c758-7299-4a73-b978-5a92a7ae98a0"),
    43  			Owner: &User{
    44  				Login: Ptr("octocat"),
    45  			},
    46  			BillableOwner: &User{
    47  				Login: Ptr("octocat"),
    48  			},
    49  			Repository: &Repository{
    50  				ID: Ptr(int64(1296269)),
    51  			},
    52  			Machine: &CodespacesMachine{
    53  				Name:            Ptr("standardLinux"),
    54  				DisplayName:     Ptr("4 cores, 8 GB RAM, 64 GB storage"),
    55  				OperatingSystem: Ptr("linux"),
    56  				StorageInBytes:  Ptr(int64(68719476736)),
    57  				MemoryInBytes:   Ptr(int64(8589934592)),
    58  				CPUs:            Ptr(4),
    59  			},
    60  			Prebuild:         Ptr(false),
    61  			DevcontainerPath: Ptr(".devcontainer/devcontainer.json"),
    62  			CreatedAt:        &Timestamp{time.Date(2021, 10, 14, 0, 53, 30, 0, time.FixedZone("", -6*60*60))},
    63  			UpdatedAt:        &Timestamp{time.Date(2021, 10, 14, 0, 53, 32, 0, time.FixedZone("", -6*60*60))},
    64  			LastUsedAt:       &Timestamp{time.Date(2021, 10, 14, 0, 53, 30, 0, time.FixedZone("", -6*60*60))},
    65  			State:            Ptr("Available"),
    66  			URL:              Ptr("https://api.github.com/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q"),
    67  			GitStatus: &CodespacesGitStatus{
    68  				Ahead:                 Ptr(0),
    69  				Behind:                Ptr(0),
    70  				HasUnpushedChanges:    Ptr(false),
    71  				HasUncommittedChanges: Ptr(false),
    72  				Ref:                   Ptr("main"),
    73  			},
    74  			Location:           Ptr("WestUs2"),
    75  			IdleTimeoutMinutes: Ptr(60),
    76  			WebURL:             Ptr("https://monalisa-octocat-hello-world-g4wpq6h95q.github.dev"),
    77  			MachinesURL:        Ptr("https://api.github.com/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/machines"),
    78  			StartURL:           Ptr("https://api.github.com/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/start"),
    79  			StopURL:            Ptr("https://api.github.com/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/stop"),
    80  			RecentFolders: []string{
    81  				"testfolder1",
    82  				"testfolder2",
    83  			},
    84  		},
    85  		{
    86  			ID: Ptr(int64(2)),
    87  		},
    88  	}}
    89  	if !cmp.Equal(codespaces, want) {
    90  		t.Errorf("Codespaces.ListInRepo returned %+v, want %+v", codespaces, want)
    91  	}
    92  
    93  	const methodName = "ListInRepo"
    94  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    95  		got, resp, err := client.Codespaces.ListInRepo(ctx, "", "", nil)
    96  		if got != nil {
    97  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    98  		}
    99  		return resp, err
   100  	})
   101  }
   102  
   103  func TestCodespacesService_List(t *testing.T) {
   104  	t.Parallel()
   105  	client, mux, _ := setup(t)
   106  
   107  	mux.HandleFunc("/user/codespaces", func(w http.ResponseWriter, r *http.Request) {
   108  		testMethod(t, r, "GET")
   109  		testFormValues(t, r, values{
   110  			"page":          "1",
   111  			"per_page":      "2",
   112  			"repository_id": "1296269",
   113  		})
   114  		fmt.Fprint(w, `{"total_count":1,"codespaces":[{"id":1, "repository": {"id": 1296269}}]}`)
   115  	})
   116  
   117  	opt := &ListCodespacesOptions{ListOptions: ListOptions{Page: 1, PerPage: 2}, RepositoryID: 1296269}
   118  	ctx := context.Background()
   119  	codespaces, _, err := client.Codespaces.List(ctx, opt)
   120  	if err != nil {
   121  		t.Errorf("Codespaces.List returned error: %v", err)
   122  	}
   123  
   124  	want := &ListCodespaces{TotalCount: Ptr(1), Codespaces: []*Codespace{
   125  		{
   126  			ID: Ptr(int64(1)),
   127  			Repository: &Repository{
   128  				ID: Ptr(int64(1296269)),
   129  			},
   130  		},
   131  	}}
   132  	if !cmp.Equal(codespaces, want) {
   133  		t.Errorf("Codespaces.ListInRepo returned %+v, want %+v", codespaces, want)
   134  	}
   135  
   136  	const methodName = "List"
   137  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   138  		got, resp, err := client.Codespaces.List(ctx, nil)
   139  		if got != nil {
   140  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   141  		}
   142  		return resp, err
   143  	})
   144  }
   145  
   146  func TestCodespacesService_CreateInRepo(t *testing.T) {
   147  	t.Parallel()
   148  	client, mux, _ := setup(t)
   149  
   150  	mux.HandleFunc("/repos/owner/repo/codespaces", func(w http.ResponseWriter, r *http.Request) {
   151  		testMethod(t, r, "POST")
   152  		testHeader(t, r, "Content-Type", "application/json")
   153  		testBody(t, r, `{"ref":"main","geo":"WestUs2","machine":"standardLinux","idle_timeout_minutes":60}`+"\n")
   154  		fmt.Fprint(w, `{"id":1, "repository": {"id": 1296269}}`)
   155  	})
   156  	input := &CreateCodespaceOptions{
   157  		Ref:                Ptr("main"),
   158  		Geo:                Ptr("WestUs2"),
   159  		Machine:            Ptr("standardLinux"),
   160  		IdleTimeoutMinutes: Ptr(60),
   161  	}
   162  	ctx := context.Background()
   163  	codespace, _, err := client.Codespaces.CreateInRepo(ctx, "owner", "repo", input)
   164  	if err != nil {
   165  		t.Errorf("Codespaces.CreateInRepo returned error: %v", err)
   166  	}
   167  	want := &Codespace{
   168  		ID: Ptr(int64(1)),
   169  		Repository: &Repository{
   170  			ID: Ptr(int64(1296269)),
   171  		},
   172  	}
   173  
   174  	if !cmp.Equal(codespace, want) {
   175  		t.Errorf("Codespaces.CreateInRepo returned %+v, want %+v", codespace, want)
   176  	}
   177  
   178  	const methodName = "CreateInRepo"
   179  	testBadOptions(t, methodName, func() (err error) {
   180  		_, _, err = client.Codespaces.CreateInRepo(ctx, "\n", "", input)
   181  		return err
   182  	})
   183  
   184  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   185  		got, resp, err := client.Codespaces.CreateInRepo(ctx, "o", "r", input)
   186  		if got != nil {
   187  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   188  		}
   189  		return resp, err
   190  	})
   191  }
   192  
   193  func TestCodespacesService_Start(t *testing.T) {
   194  	t.Parallel()
   195  	client, mux, _ := setup(t)
   196  
   197  	mux.HandleFunc("/user/codespaces/codespace_1/start", func(w http.ResponseWriter, r *http.Request) {
   198  		testMethod(t, r, "POST")
   199  		fmt.Fprint(w, `{"id":1, "repository": {"id": 1296269}}`)
   200  	})
   201  	ctx := context.Background()
   202  	codespace, _, err := client.Codespaces.Start(ctx, "codespace_1")
   203  	if err != nil {
   204  		t.Errorf("Codespaces.Start returned error: %v", err)
   205  	}
   206  	want := &Codespace{
   207  		ID: Ptr(int64(1)),
   208  		Repository: &Repository{
   209  			ID: Ptr(int64(1296269)),
   210  		},
   211  	}
   212  
   213  	if !cmp.Equal(codespace, want) {
   214  		t.Errorf("Codespaces.Start returned %+v, want %+v", codespace, want)
   215  	}
   216  
   217  	const methodName = "Start"
   218  	testBadOptions(t, methodName, func() (err error) {
   219  		_, _, err = client.Codespaces.Start(ctx, "\n")
   220  		return err
   221  	})
   222  
   223  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   224  		got, resp, err := client.Codespaces.Start(ctx, "o")
   225  		if got != nil {
   226  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   227  		}
   228  		return resp, err
   229  	})
   230  }
   231  
   232  func TestCodespacesService_Stop(t *testing.T) {
   233  	t.Parallel()
   234  	client, mux, _ := setup(t)
   235  
   236  	mux.HandleFunc("/user/codespaces/codespace_1/stop", func(w http.ResponseWriter, r *http.Request) {
   237  		testMethod(t, r, "POST")
   238  		fmt.Fprint(w, `{"id":1, "repository": {"id": 1296269}}`)
   239  	})
   240  	ctx := context.Background()
   241  	codespace, _, err := client.Codespaces.Stop(ctx, "codespace_1")
   242  	if err != nil {
   243  		t.Errorf("Codespaces.Stop returned error: %v", err)
   244  	}
   245  	want := &Codespace{
   246  		ID: Ptr(int64(1)),
   247  		Repository: &Repository{
   248  			ID: Ptr(int64(1296269)),
   249  		},
   250  	}
   251  
   252  	if !cmp.Equal(codespace, want) {
   253  		t.Errorf("Codespaces.Stop returned %+v, want %+v", codespace, want)
   254  	}
   255  
   256  	const methodName = "Stop"
   257  	testBadOptions(t, methodName, func() (err error) {
   258  		_, _, err = client.Codespaces.Stop(ctx, "\n")
   259  		return err
   260  	})
   261  
   262  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   263  		got, resp, err := client.Codespaces.Stop(ctx, "o")
   264  		if got != nil {
   265  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   266  		}
   267  		return resp, err
   268  	})
   269  }
   270  
   271  func TestCodespacesService_Delete(t *testing.T) {
   272  	t.Parallel()
   273  	client, mux, _ := setup(t)
   274  
   275  	mux.HandleFunc("/user/codespaces/codespace_1", func(_ http.ResponseWriter, r *http.Request) {
   276  		testMethod(t, r, "DELETE")
   277  	})
   278  
   279  	ctx := context.Background()
   280  	_, err := client.Codespaces.Delete(ctx, "codespace_1")
   281  	if err != nil {
   282  		t.Errorf("Codespaces.Delete return error: %v", err)
   283  	}
   284  
   285  	const methodName = "Delete"
   286  	testBadOptions(t, methodName, func() (err error) {
   287  		_, err = client.Codespaces.Delete(ctx, "\n")
   288  		return err
   289  	})
   290  
   291  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   292  		return client.Codespaces.Delete(ctx, "codespace_1")
   293  	})
   294  }