github.com/google/go-github/v33@v33.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  	"reflect"
    13  	"testing"
    14  )
    15  
    16  func TestAppsService_ListRepos(t *testing.T) {
    17  	client, mux, _, teardown := setup()
    18  	defer teardown()
    19  
    20  	mux.HandleFunc("/installation/repositories", func(w http.ResponseWriter, r *http.Request) {
    21  		testMethod(t, r, "GET")
    22  		testFormValues(t, r, values{
    23  			"page":     "1",
    24  			"per_page": "2",
    25  		})
    26  		fmt.Fprint(w, `{"repositories": [{"id":1}]}`)
    27  	})
    28  
    29  	opt := &ListOptions{Page: 1, PerPage: 2}
    30  	repositories, _, err := client.Apps.ListRepos(context.Background(), opt)
    31  	if err != nil {
    32  		t.Errorf("Apps.ListRepos returned error: %v", err)
    33  	}
    34  
    35  	want := []*Repository{{ID: Int64(1)}}
    36  	if !reflect.DeepEqual(repositories, want) {
    37  		t.Errorf("Apps.ListRepos returned %+v, want %+v", repositories, want)
    38  	}
    39  }
    40  
    41  func TestAppsService_ListUserRepos(t *testing.T) {
    42  	client, mux, _, teardown := setup()
    43  	defer teardown()
    44  
    45  	mux.HandleFunc("/user/installations/1/repositories", func(w http.ResponseWriter, r *http.Request) {
    46  		testMethod(t, r, "GET")
    47  		testFormValues(t, r, values{
    48  			"page":     "1",
    49  			"per_page": "2",
    50  		})
    51  		fmt.Fprint(w, `{"repositories": [{"id":1}]}`)
    52  	})
    53  
    54  	opt := &ListOptions{Page: 1, PerPage: 2}
    55  	repositories, _, err := client.Apps.ListUserRepos(context.Background(), 1, opt)
    56  	if err != nil {
    57  		t.Errorf("Apps.ListUserRepos returned error: %v", err)
    58  	}
    59  
    60  	want := []*Repository{{ID: Int64(1)}}
    61  	if !reflect.DeepEqual(repositories, want) {
    62  		t.Errorf("Apps.ListUserRepos returned %+v, want %+v", repositories, want)
    63  	}
    64  }
    65  
    66  func TestAppsService_AddRepository(t *testing.T) {
    67  	client, mux, _, teardown := setup()
    68  	defer teardown()
    69  
    70  	mux.HandleFunc("/user/installations/1/repositories/1", func(w http.ResponseWriter, r *http.Request) {
    71  		testMethod(t, r, "PUT")
    72  		fmt.Fprint(w, `{"id":1,"name":"n","description":"d","owner":{"login":"l"},"license":{"key":"mit"}}`)
    73  	})
    74  
    75  	repo, _, err := client.Apps.AddRepository(context.Background(), 1, 1)
    76  	if err != nil {
    77  		t.Errorf("Apps.AddRepository returned error: %v", err)
    78  	}
    79  
    80  	want := &Repository{ID: Int64(1), Name: String("n"), Description: String("d"), Owner: &User{Login: String("l")}, License: &License{Key: String("mit")}}
    81  	if !reflect.DeepEqual(repo, want) {
    82  		t.Errorf("AddRepository returned %+v, want %+v", repo, want)
    83  	}
    84  }
    85  
    86  func TestAppsService_RemoveRepository(t *testing.T) {
    87  	client, mux, _, teardown := setup()
    88  	defer teardown()
    89  
    90  	mux.HandleFunc("/user/installations/1/repositories/1", func(w http.ResponseWriter, r *http.Request) {
    91  		testMethod(t, r, "DELETE")
    92  		w.WriteHeader(http.StatusNoContent)
    93  	})
    94  
    95  	_, err := client.Apps.RemoveRepository(context.Background(), 1, 1)
    96  	if err != nil {
    97  		t.Errorf("Apps.RemoveRepository returned error: %v", err)
    98  	}
    99  }
   100  
   101  func TestAppsService_RevokeInstallationToken(t *testing.T) {
   102  	client, mux, _, teardown := setup()
   103  	defer teardown()
   104  
   105  	mux.HandleFunc("/installation/token", func(w http.ResponseWriter, r *http.Request) {
   106  		testMethod(t, r, "DELETE")
   107  		w.WriteHeader(http.StatusNoContent)
   108  	})
   109  
   110  	_, err := client.Apps.RevokeInstallationToken(context.Background())
   111  	if err != nil {
   112  		t.Errorf("Apps.RevokeInstallationToken returned error: %v", err)
   113  	}
   114  }