github.com/google/go-github/v33@v33.0.0/github/interactions_orgs_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  	"reflect"
    14  	"testing"
    15  )
    16  
    17  func TestInteractionsService_GetRestrictionsForOrgs(t *testing.T) {
    18  	client, mux, _, teardown := setup()
    19  	defer teardown()
    20  
    21  	mux.HandleFunc("/orgs/o/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":"organization"}`)
    25  	})
    26  
    27  	organizationInteractions, _, err := client.Interactions.GetRestrictionsForOrg(context.Background(), "o")
    28  	if err != nil {
    29  		t.Errorf("Interactions.GetRestrictionsForOrg returned error: %v", err)
    30  	}
    31  
    32  	want := &InteractionRestriction{Origin: String("organization")}
    33  	if !reflect.DeepEqual(organizationInteractions, want) {
    34  		t.Errorf("Interactions.GetRestrictionsForOrg returned %+v, want %+v", organizationInteractions, want)
    35  	}
    36  }
    37  
    38  func TestInteractionsService_UpdateRestrictionsForOrg(t *testing.T) {
    39  	client, mux, _, teardown := setup()
    40  	defer teardown()
    41  
    42  	input := &InteractionRestriction{Limit: String("existing_users")}
    43  
    44  	mux.HandleFunc("/orgs/o/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":"organization"}`)
    54  	})
    55  
    56  	organizationInteractions, _, err := client.Interactions.UpdateRestrictionsForOrg(context.Background(), "o", input.GetLimit())
    57  	if err != nil {
    58  		t.Errorf("Interactions.UpdateRestrictionsForOrg returned error: %v", err)
    59  	}
    60  
    61  	want := &InteractionRestriction{Origin: String("organization")}
    62  	if !reflect.DeepEqual(organizationInteractions, want) {
    63  		t.Errorf("Interactions.UpdateRestrictionsForOrg returned %+v, want %+v", organizationInteractions, want)
    64  	}
    65  }
    66  
    67  func TestInteractionsService_RemoveRestrictionsFromOrg(t *testing.T) {
    68  	client, mux, _, teardown := setup()
    69  	defer teardown()
    70  
    71  	mux.HandleFunc("/orgs/o/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.RemoveRestrictionsFromOrg(context.Background(), "o")
    77  	if err != nil {
    78  		t.Errorf("Interactions.RemoveRestrictionsFromOrg returned error: %v", err)
    79  	}
    80  }