...

Source file src/github.com/google/go-github/v47/github/interactions_repos_test.go

Documentation: github.com/google/go-github/v47/github

     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  	"testing"
    14  
    15  	"github.com/google/go-cmp/cmp"
    16  )
    17  
    18  func TestInteractionsService_GetRestrictionsForRepo(t *testing.T) {
    19  	client, mux, _, teardown := setup()
    20  	defer teardown()
    21  
    22  	mux.HandleFunc("/repos/o/r/interaction-limits", func(w http.ResponseWriter, r *http.Request) {
    23  		testMethod(t, r, "GET")
    24  		testHeader(t, r, "Accept", mediaTypeInteractionRestrictionsPreview)
    25  		fmt.Fprint(w, `{"origin":"repository"}`)
    26  	})
    27  
    28  	ctx := context.Background()
    29  	repoInteractions, _, err := client.Interactions.GetRestrictionsForRepo(ctx, "o", "r")
    30  	if err != nil {
    31  		t.Errorf("Interactions.GetRestrictionsForRepo returned error: %v", err)
    32  	}
    33  
    34  	want := &InteractionRestriction{Origin: String("repository")}
    35  	if !cmp.Equal(repoInteractions, want) {
    36  		t.Errorf("Interactions.GetRestrictionsForRepo returned %+v, want %+v", repoInteractions, want)
    37  	}
    38  
    39  	const methodName = "GetRestrictionsForRepo"
    40  	testBadOptions(t, methodName, func() (err error) {
    41  		_, _, err = client.Interactions.GetRestrictionsForRepo(ctx, "\n", "\n")
    42  		return err
    43  	})
    44  
    45  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    46  		got, resp, err := client.Interactions.GetRestrictionsForRepo(ctx, "o", "r")
    47  		if got != nil {
    48  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    49  		}
    50  		return resp, err
    51  	})
    52  }
    53  
    54  func TestInteractionsService_UpdateRestrictionsForRepo(t *testing.T) {
    55  	client, mux, _, teardown := setup()
    56  	defer teardown()
    57  
    58  	input := &InteractionRestriction{Limit: String("existing_users")}
    59  
    60  	mux.HandleFunc("/repos/o/r/interaction-limits", func(w http.ResponseWriter, r *http.Request) {
    61  		v := new(InteractionRestriction)
    62  		json.NewDecoder(r.Body).Decode(v)
    63  
    64  		testMethod(t, r, "PUT")
    65  		testHeader(t, r, "Accept", mediaTypeInteractionRestrictionsPreview)
    66  		if !cmp.Equal(v, input) {
    67  			t.Errorf("Request body = %+v, want %+v", v, input)
    68  		}
    69  		fmt.Fprint(w, `{"origin":"repository"}`)
    70  	})
    71  
    72  	ctx := context.Background()
    73  	repoInteractions, _, err := client.Interactions.UpdateRestrictionsForRepo(ctx, "o", "r", input.GetLimit())
    74  	if err != nil {
    75  		t.Errorf("Interactions.UpdateRestrictionsForRepo returned error: %v", err)
    76  	}
    77  
    78  	want := &InteractionRestriction{Origin: String("repository")}
    79  	if !cmp.Equal(repoInteractions, want) {
    80  		t.Errorf("Interactions.UpdateRestrictionsForRepo returned %+v, want %+v", repoInteractions, want)
    81  	}
    82  
    83  	const methodName = "UpdateRestrictionsForRepo"
    84  	testBadOptions(t, methodName, func() (err error) {
    85  		_, _, err = client.Interactions.UpdateRestrictionsForRepo(ctx, "\n", "\n", input.GetLimit())
    86  		return err
    87  	})
    88  
    89  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    90  		got, resp, err := client.Interactions.UpdateRestrictionsForRepo(ctx, "o", "r", input.GetLimit())
    91  		if got != nil {
    92  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    93  		}
    94  		return resp, err
    95  	})
    96  }
    97  
    98  func TestInteractionsService_RemoveRestrictionsFromRepo(t *testing.T) {
    99  	client, mux, _, teardown := setup()
   100  	defer teardown()
   101  
   102  	mux.HandleFunc("/repos/o/r/interaction-limits", func(w http.ResponseWriter, r *http.Request) {
   103  		testMethod(t, r, "DELETE")
   104  		testHeader(t, r, "Accept", mediaTypeInteractionRestrictionsPreview)
   105  	})
   106  
   107  	ctx := context.Background()
   108  	_, err := client.Interactions.RemoveRestrictionsFromRepo(ctx, "o", "r")
   109  	if err != nil {
   110  		t.Errorf("Interactions.RemoveRestrictionsFromRepo returned error: %v", err)
   111  	}
   112  
   113  	const methodName = "RemoveRestrictionsFromRepo"
   114  	testBadOptions(t, methodName, func() (err error) {
   115  		_, err = client.Interactions.RemoveRestrictionsFromRepo(ctx, "\n", "\n")
   116  		return err
   117  	})
   118  
   119  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   120  		return client.Interactions.RemoveRestrictionsFromRepo(ctx, "o", "r")
   121  	})
   122  }
   123  

View as plain text