github.com/google/go-github/v74@v74.0.0/github/apps_installation_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 TestAppsService_ListRepos(t *testing.T) { 18 t.Parallel() 19 client, mux, _ := setup(t) 20 21 mux.HandleFunc("/installation/repositories", func(w http.ResponseWriter, r *http.Request) { 22 testMethod(t, r, "GET") 23 testFormValues(t, r, values{ 24 "page": "1", 25 "per_page": "2", 26 }) 27 fmt.Fprint(w, `{"total_count": 1,"repositories": [{"id": 1}]}`) 28 }) 29 30 opt := &ListOptions{Page: 1, PerPage: 2} 31 ctx := context.Background() 32 repositories, _, err := client.Apps.ListRepos(ctx, opt) 33 if err != nil { 34 t.Errorf("Apps.ListRepos returned error: %v", err) 35 } 36 37 want := &ListRepositories{TotalCount: Ptr(1), Repositories: []*Repository{{ID: Ptr(int64(1))}}} 38 if !cmp.Equal(repositories, want) { 39 t.Errorf("Apps.ListRepos returned %+v, want %+v", repositories, want) 40 } 41 42 const methodName = "ListRepos" 43 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 44 got, resp, err := client.Apps.ListRepos(ctx, nil) 45 if got != nil { 46 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 47 } 48 return resp, err 49 }) 50 } 51 52 func TestAppsService_ListUserRepos(t *testing.T) { 53 t.Parallel() 54 client, mux, _ := setup(t) 55 56 mux.HandleFunc("/user/installations/1/repositories", func(w http.ResponseWriter, r *http.Request) { 57 testMethod(t, r, "GET") 58 testFormValues(t, r, values{ 59 "page": "1", 60 "per_page": "2", 61 }) 62 fmt.Fprint(w, `{"total_count":1,"repositories": [{"id":1}]}`) 63 }) 64 65 opt := &ListOptions{Page: 1, PerPage: 2} 66 ctx := context.Background() 67 repositories, _, err := client.Apps.ListUserRepos(ctx, 1, opt) 68 if err != nil { 69 t.Errorf("Apps.ListUserRepos returned error: %v", err) 70 } 71 72 want := &ListRepositories{TotalCount: Ptr(1), Repositories: []*Repository{{ID: Ptr(int64(1))}}} 73 if !cmp.Equal(repositories, want) { 74 t.Errorf("Apps.ListUserRepos returned %+v, want %+v", repositories, want) 75 } 76 77 const methodName = "ListUserRepos" 78 testBadOptions(t, methodName, func() (err error) { 79 _, _, err = client.Apps.ListUserRepos(ctx, -1, &ListOptions{}) 80 return err 81 }) 82 83 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 84 got, resp, err := client.Apps.ListUserRepos(ctx, 1, nil) 85 if got != nil { 86 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 87 } 88 return resp, err 89 }) 90 } 91 92 func TestAppsService_AddRepository(t *testing.T) { 93 t.Parallel() 94 client, mux, _ := setup(t) 95 96 mux.HandleFunc("/user/installations/1/repositories/1", func(w http.ResponseWriter, r *http.Request) { 97 testMethod(t, r, "PUT") 98 fmt.Fprint(w, `{"id":1,"name":"n","description":"d","owner":{"login":"l"},"license":{"key":"mit"}}`) 99 }) 100 101 ctx := context.Background() 102 repo, _, err := client.Apps.AddRepository(ctx, 1, 1) 103 if err != nil { 104 t.Errorf("Apps.AddRepository returned error: %v", err) 105 } 106 107 want := &Repository{ID: Ptr(int64(1)), Name: Ptr("n"), Description: Ptr("d"), Owner: &User{Login: Ptr("l")}, License: &License{Key: Ptr("mit")}} 108 if !cmp.Equal(repo, want) { 109 t.Errorf("AddRepository returned %+v, want %+v", repo, want) 110 } 111 112 const methodName = "AddRepository" 113 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 114 got, resp, err := client.Apps.AddRepository(ctx, 1, 1) 115 if got != nil { 116 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 117 } 118 return resp, err 119 }) 120 } 121 122 func TestAppsService_RemoveRepository(t *testing.T) { 123 t.Parallel() 124 client, mux, _ := setup(t) 125 126 mux.HandleFunc("/user/installations/1/repositories/1", func(w http.ResponseWriter, r *http.Request) { 127 testMethod(t, r, "DELETE") 128 w.WriteHeader(http.StatusNoContent) 129 }) 130 131 ctx := context.Background() 132 _, err := client.Apps.RemoveRepository(ctx, 1, 1) 133 if err != nil { 134 t.Errorf("Apps.RemoveRepository returned error: %v", err) 135 } 136 137 const methodName = "RemoveRepository" 138 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 139 return client.Apps.RemoveRepository(ctx, 1, 1) 140 }) 141 } 142 143 func TestAppsService_RevokeInstallationToken(t *testing.T) { 144 t.Parallel() 145 client, mux, _ := setup(t) 146 147 mux.HandleFunc("/installation/token", func(w http.ResponseWriter, r *http.Request) { 148 testMethod(t, r, "DELETE") 149 w.WriteHeader(http.StatusNoContent) 150 }) 151 152 ctx := context.Background() 153 _, err := client.Apps.RevokeInstallationToken(ctx) 154 if err != nil { 155 t.Errorf("Apps.RevokeInstallationToken returned error: %v", err) 156 } 157 158 const methodName = "RevokeInstallationToken" 159 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 160 return client.Apps.RevokeInstallationToken(ctx) 161 }) 162 } 163 164 func TestListRepositories_Marshal(t *testing.T) { 165 t.Parallel() 166 testJSONMarshal(t, &ListRepositories{}, "{}") 167 168 u := &ListRepositories{ 169 TotalCount: Ptr(1), 170 Repositories: []*Repository{ 171 { 172 ID: Ptr(int64(1)), 173 URL: Ptr("u"), 174 Name: Ptr("n"), 175 }, 176 }, 177 } 178 179 want := `{ 180 "total_count": 1, 181 "repositories": [{ 182 "id":1, 183 "name":"n", 184 "url":"u" 185 }] 186 }` 187 188 testJSONMarshal(t, u, want) 189 }