github.com/google/go-github/v49@v49.1.0/github/users_projects_test.go (about) 1 // Copyright 2019 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 "encoding/json" 11 "fmt" 12 "net/http" 13 "testing" 14 15 "github.com/google/go-cmp/cmp" 16 ) 17 18 func TestUsersService_ListProjects(t *testing.T) { 19 client, mux, _, teardown := setup() 20 defer teardown() 21 22 mux.HandleFunc("/users/u/projects", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 testHeader(t, r, "Accept", mediaTypeProjectsPreview) 25 testFormValues(t, r, values{"state": "open", "page": "2"}) 26 fmt.Fprint(w, `[{"id":1}]`) 27 }) 28 29 opt := &ProjectListOptions{State: "open", ListOptions: ListOptions{Page: 2}} 30 ctx := context.Background() 31 projects, _, err := client.Users.ListProjects(ctx, "u", opt) 32 if err != nil { 33 t.Errorf("Users.ListProjects returned error: %v", err) 34 } 35 36 want := []*Project{{ID: Int64(1)}} 37 if !cmp.Equal(projects, want) { 38 t.Errorf("Users.ListProjects returned %+v, want %+v", projects, want) 39 } 40 41 const methodName = "ListProjects" 42 testBadOptions(t, methodName, func() (err error) { 43 _, _, err = client.Users.ListProjects(ctx, "\n", opt) 44 return err 45 }) 46 47 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 48 got, resp, err := client.Users.ListProjects(ctx, "u", opt) 49 if got != nil { 50 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 51 } 52 return resp, err 53 }) 54 } 55 56 func TestUsersService_CreateProject(t *testing.T) { 57 client, mux, _, teardown := setup() 58 defer teardown() 59 60 input := &CreateUserProjectOptions{Name: "Project Name", Body: String("Project body.")} 61 62 mux.HandleFunc("/user/projects", func(w http.ResponseWriter, r *http.Request) { 63 testMethod(t, r, "POST") 64 testHeader(t, r, "Accept", mediaTypeProjectsPreview) 65 66 v := &CreateUserProjectOptions{} 67 json.NewDecoder(r.Body).Decode(v) 68 if !cmp.Equal(v, input) { 69 t.Errorf("Request body = %+v, want %+v", v, input) 70 } 71 72 fmt.Fprint(w, `{"id":1}`) 73 }) 74 75 ctx := context.Background() 76 project, _, err := client.Users.CreateProject(ctx, input) 77 if err != nil { 78 t.Errorf("Users.CreateProject returned error: %v", err) 79 } 80 81 want := &Project{ID: Int64(1)} 82 if !cmp.Equal(project, want) { 83 t.Errorf("Users.CreateProject returned %+v, want %+v", project, want) 84 } 85 86 const methodName = "CreateProject" 87 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 88 got, resp, err := client.Users.CreateProject(ctx, input) 89 if got != nil { 90 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 91 } 92 return resp, err 93 }) 94 } 95 96 func TestCreateUserProjectOptions_Marshal(t *testing.T) { 97 testJSONMarshal(t, &CreateUserProjectOptions{}, `{}`) 98 99 c := CreateUserProjectOptions{ 100 Name: "SomeProject", 101 Body: String("SomeProjectBody"), 102 } 103 104 want := `{ 105 "name": "SomeProject", 106 "body": "SomeProjectBody" 107 }` 108 109 testJSONMarshal(t, c, want) 110 }