...

Source file src/github.com/google/go-github/v45/github/apps_installation_test.go

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

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

View as plain text