github.com/google/go-github/v74@v74.0.0/github/repos_invitations_test.go (about) 1 // Copyright 2016 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 14 "github.com/google/go-cmp/cmp" 15 ) 16 17 func TestRepositoriesService_ListInvitations(t *testing.T) { 18 t.Parallel() 19 client, mux, _ := setup(t) 20 21 mux.HandleFunc("/repos/o/r/invitations", func(w http.ResponseWriter, r *http.Request) { 22 testMethod(t, r, "GET") 23 testFormValues(t, r, values{"page": "2"}) 24 fmt.Fprint(w, `[{"id":1}, {"id":2}]`) 25 }) 26 27 opt := &ListOptions{Page: 2} 28 ctx := context.Background() 29 got, _, err := client.Repositories.ListInvitations(ctx, "o", "r", opt) 30 if err != nil { 31 t.Errorf("Repositories.ListInvitations returned error: %v", err) 32 } 33 34 want := []*RepositoryInvitation{{ID: Ptr(int64(1))}, {ID: Ptr(int64(2))}} 35 if !cmp.Equal(got, want) { 36 t.Errorf("Repositories.ListInvitations = %+v, want %+v", got, want) 37 } 38 39 const methodName = "ListInvitations" 40 testBadOptions(t, methodName, func() (err error) { 41 _, _, err = client.Repositories.ListInvitations(ctx, "\n", "\n", opt) 42 return err 43 }) 44 45 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 46 got, resp, err := client.Repositories.ListInvitations(ctx, "o", "r", opt) 47 if got != nil { 48 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 49 } 50 return resp, err 51 }) 52 } 53 54 func TestRepositoriesService_DeleteInvitation(t *testing.T) { 55 t.Parallel() 56 client, mux, _ := setup(t) 57 58 mux.HandleFunc("/repos/o/r/invitations/2", func(w http.ResponseWriter, r *http.Request) { 59 testMethod(t, r, "DELETE") 60 w.WriteHeader(http.StatusNoContent) 61 }) 62 63 ctx := context.Background() 64 _, err := client.Repositories.DeleteInvitation(ctx, "o", "r", 2) 65 if err != nil { 66 t.Errorf("Repositories.DeleteInvitation returned error: %v", err) 67 } 68 69 const methodName = "DeleteInvitation" 70 testBadOptions(t, methodName, func() (err error) { 71 _, err = client.Repositories.DeleteInvitation(ctx, "\n", "\n", 2) 72 return err 73 }) 74 75 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 76 return client.Repositories.DeleteInvitation(ctx, "o", "r", 2) 77 }) 78 } 79 80 func TestRepositoriesService_UpdateInvitation(t *testing.T) { 81 t.Parallel() 82 client, mux, _ := setup(t) 83 84 mux.HandleFunc("/repos/o/r/invitations/2", func(w http.ResponseWriter, r *http.Request) { 85 testMethod(t, r, "PATCH") 86 fmt.Fprint(w, `{"id":1}`) 87 }) 88 89 ctx := context.Background() 90 got, _, err := client.Repositories.UpdateInvitation(ctx, "o", "r", 2, "write") 91 if err != nil { 92 t.Errorf("Repositories.UpdateInvitation returned error: %v", err) 93 } 94 95 want := &RepositoryInvitation{ID: Ptr(int64(1))} 96 if !cmp.Equal(got, want) { 97 t.Errorf("Repositories.UpdateInvitation = %+v, want %+v", got, want) 98 } 99 100 const methodName = "UpdateInvitation" 101 testBadOptions(t, methodName, func() (err error) { 102 _, _, err = client.Repositories.UpdateInvitation(ctx, "\n", "\n", 2, "write") 103 return err 104 }) 105 106 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 107 got, resp, err := client.Repositories.UpdateInvitation(ctx, "o", "r", 2, "write") 108 if got != nil { 109 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 110 } 111 return resp, err 112 }) 113 } 114 115 func TestRepositoryInvitation_Marshal(t *testing.T) { 116 t.Parallel() 117 testJSONMarshal(t, &RepositoryInvitation{}, "{}") 118 119 r := &RepositoryInvitation{ 120 ID: Ptr(int64(1)), 121 Repo: &Repository{ 122 ID: Ptr(int64(1)), 123 Name: Ptr("n"), 124 URL: Ptr("u"), 125 }, 126 Invitee: &User{ 127 ID: Ptr(int64(1)), 128 Name: Ptr("n"), 129 URL: Ptr("u"), 130 }, 131 Inviter: &User{ 132 ID: Ptr(int64(1)), 133 Name: Ptr("n"), 134 URL: Ptr("u"), 135 }, 136 Permissions: Ptr("p"), 137 CreatedAt: &Timestamp{referenceTime}, 138 URL: Ptr("u"), 139 HTMLURL: Ptr("h"), 140 } 141 142 want := `{ 143 "id":1, 144 "repository":{ 145 "id":1, 146 "name":"n", 147 "url":"u" 148 }, 149 "invitee":{ 150 "id":1, 151 "name":"n", 152 "url":"u" 153 }, 154 "inviter":{ 155 "id":1, 156 "name":"n", 157 "url":"u" 158 }, 159 "permissions":"p", 160 "created_at":` + referenceTimeStr + `, 161 "url":"u", 162 "html_url":"h" 163 }` 164 165 testJSONMarshal(t, r, want) 166 }