github.com/google/go-github/v33@v33.0.0/github/interactions_repos_test.go (about) 1 // Copyright 2018 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 "reflect" 14 "testing" 15 ) 16 17 func TestInteractionsService_GetRestrictionsForRepo(t *testing.T) { 18 client, mux, _, teardown := setup() 19 defer teardown() 20 21 mux.HandleFunc("/repos/o/r/interaction-limits", func(w http.ResponseWriter, r *http.Request) { 22 testMethod(t, r, "GET") 23 testHeader(t, r, "Accept", mediaTypeInteractionRestrictionsPreview) 24 fmt.Fprint(w, `{"origin":"repository"}`) 25 }) 26 27 repoInteractions, _, err := client.Interactions.GetRestrictionsForRepo(context.Background(), "o", "r") 28 if err != nil { 29 t.Errorf("Interactions.GetRestrictionsForRepo returned error: %v", err) 30 } 31 32 want := &InteractionRestriction{Origin: String("repository")} 33 if !reflect.DeepEqual(repoInteractions, want) { 34 t.Errorf("Interactions.GetRestrictionsForRepo returned %+v, want %+v", repoInteractions, want) 35 } 36 } 37 38 func TestInteractionsService_UpdateRestrictionsForRepo(t *testing.T) { 39 client, mux, _, teardown := setup() 40 defer teardown() 41 42 input := &InteractionRestriction{Limit: String("existing_users")} 43 44 mux.HandleFunc("/repos/o/r/interaction-limits", func(w http.ResponseWriter, r *http.Request) { 45 v := new(InteractionRestriction) 46 json.NewDecoder(r.Body).Decode(v) 47 48 testMethod(t, r, "PUT") 49 testHeader(t, r, "Accept", mediaTypeInteractionRestrictionsPreview) 50 if !reflect.DeepEqual(v, input) { 51 t.Errorf("Request body = %+v, want %+v", v, input) 52 } 53 fmt.Fprint(w, `{"origin":"repository"}`) 54 }) 55 56 repoInteractions, _, err := client.Interactions.UpdateRestrictionsForRepo(context.Background(), "o", "r", input.GetLimit()) 57 if err != nil { 58 t.Errorf("Interactions.UpdateRestrictionsForRepo returned error: %v", err) 59 } 60 61 want := &InteractionRestriction{Origin: String("repository")} 62 if !reflect.DeepEqual(repoInteractions, want) { 63 t.Errorf("Interactions.UpdateRestrictionsForRepo returned %+v, want %+v", repoInteractions, want) 64 } 65 } 66 67 func TestInteractionsService_RemoveRestrictionsFromRepo(t *testing.T) { 68 client, mux, _, teardown := setup() 69 defer teardown() 70 71 mux.HandleFunc("/repos/o/r/interaction-limits", func(w http.ResponseWriter, r *http.Request) { 72 testMethod(t, r, "DELETE") 73 testHeader(t, r, "Accept", mediaTypeInteractionRestrictionsPreview) 74 }) 75 76 _, err := client.Interactions.RemoveRestrictionsFromRepo(context.Background(), "o", "r") 77 if err != nil { 78 t.Errorf("Interactions.RemoveRestrictionsFromRepo returned error: %v", err) 79 } 80 }