github.com/google/go-github/v33@v33.0.0/github/apps_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  	"bytes"
    10  	"context"
    11  	"encoding/json"
    12  	"fmt"
    13  	"io"
    14  	"io/ioutil"
    15  	"net/http"
    16  	"reflect"
    17  	"testing"
    18  	"time"
    19  )
    20  
    21  func TestAppsService_Get_authenticatedApp(t *testing.T) {
    22  	client, mux, _, teardown := setup()
    23  	defer teardown()
    24  
    25  	mux.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) {
    26  		testMethod(t, r, "GET")
    27  		fmt.Fprint(w, `{"id":1}`)
    28  	})
    29  
    30  	app, _, err := client.Apps.Get(context.Background(), "")
    31  	if err != nil {
    32  		t.Errorf("Apps.Get returned error: %v", err)
    33  	}
    34  
    35  	want := &App{ID: Int64(1)}
    36  	if !reflect.DeepEqual(app, want) {
    37  		t.Errorf("Apps.Get returned %+v, want %+v", app, want)
    38  	}
    39  }
    40  
    41  func TestAppsService_Get_specifiedApp(t *testing.T) {
    42  	client, mux, _, teardown := setup()
    43  	defer teardown()
    44  
    45  	mux.HandleFunc("/apps/a", func(w http.ResponseWriter, r *http.Request) {
    46  		testMethod(t, r, "GET")
    47  		fmt.Fprint(w, `{"html_url":"https://github.com/apps/a"}`)
    48  	})
    49  
    50  	app, _, err := client.Apps.Get(context.Background(), "a")
    51  	if err != nil {
    52  		t.Errorf("Apps.Get returned error: %v", err)
    53  	}
    54  
    55  	want := &App{HTMLURL: String("https://github.com/apps/a")}
    56  	if !reflect.DeepEqual(app, want) {
    57  		t.Errorf("Apps.Get returned %+v, want %+v", *app.HTMLURL, *want.HTMLURL)
    58  	}
    59  }
    60  
    61  func TestAppsService_ListInstallations(t *testing.T) {
    62  	client, mux, _, teardown := setup()
    63  	defer teardown()
    64  
    65  	mux.HandleFunc("/app/installations", func(w http.ResponseWriter, r *http.Request) {
    66  		testMethod(t, r, "GET")
    67  		testFormValues(t, r, values{
    68  			"page":     "1",
    69  			"per_page": "2",
    70  		})
    71  		fmt.Fprint(w, `[{
    72                                     "id":1,
    73                                     "app_id":1,
    74                                     "target_id":1,
    75                                     "target_type": "Organization",
    76                                     "permissions": {
    77                                         "administration": "read",
    78                                         "checks": "read",
    79                                         "contents": "read",
    80                                         "content_references": "read",
    81                                         "deployments": "read",
    82                                         "issues": "write",
    83                                         "metadata": "read",
    84                                         "members": "read",
    85                                         "organization_administration": "write",
    86                                         "organization_hooks": "write",
    87                                         "organization_plan": "read",
    88                                         "organization_pre_receive_hooks": "write",
    89                                         "organization_projects": "read",
    90                                         "organization_user_blocking": "write",
    91                                         "packages": "read",
    92                                         "pages": "read",
    93                                         "pull_requests": "write",
    94                                         "repository_hooks": "write",
    95                                         "repository_projects": "read",
    96                                         "repository_pre_receive_hooks": "read",
    97                                         "single_file": "write",
    98                                         "statuses": "write",
    99                                         "team_discussions": "read",
   100                                         "vulnerability_alerts": "read"
   101                                     },
   102                                    "events": [
   103                                        "push",
   104                                        "pull_request"
   105                                    ],
   106                                   "single_file_name": "config.yml",
   107                                   "repository_selection": "selected",
   108                                   "created_at": "2018-01-01T00:00:00Z",
   109                                   "updated_at": "2018-01-01T00:00:00Z"}]`,
   110  		)
   111  	})
   112  
   113  	opt := &ListOptions{Page: 1, PerPage: 2}
   114  	installations, _, err := client.Apps.ListInstallations(context.Background(), opt)
   115  	if err != nil {
   116  		t.Errorf("Apps.ListInstallations returned error: %v", err)
   117  	}
   118  
   119  	date := Timestamp{Time: time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)}
   120  	want := []*Installation{{
   121  		ID:                  Int64(1),
   122  		AppID:               Int64(1),
   123  		TargetID:            Int64(1),
   124  		TargetType:          String("Organization"),
   125  		SingleFileName:      String("config.yml"),
   126  		RepositorySelection: String("selected"),
   127  		Permissions: &InstallationPermissions{
   128  			Administration:              String("read"),
   129  			Checks:                      String("read"),
   130  			Contents:                    String("read"),
   131  			ContentReferences:           String("read"),
   132  			Deployments:                 String("read"),
   133  			Issues:                      String("write"),
   134  			Metadata:                    String("read"),
   135  			Members:                     String("read"),
   136  			OrganizationAdministration:  String("write"),
   137  			OrganizationHooks:           String("write"),
   138  			OrganizationPlan:            String("read"),
   139  			OrganizationPreReceiveHooks: String("write"),
   140  			OrganizationProjects:        String("read"),
   141  			OrganizationUserBlocking:    String("write"),
   142  			Packages:                    String("read"),
   143  			Pages:                       String("read"),
   144  			PullRequests:                String("write"),
   145  			RepositoryHooks:             String("write"),
   146  			RepositoryProjects:          String("read"),
   147  			RepositoryPreReceiveHooks:   String("read"),
   148  			SingleFile:                  String("write"),
   149  			Statuses:                    String("write"),
   150  			TeamDiscussions:             String("read"),
   151  			VulnerabilityAlerts:         String("read")},
   152  		Events:    []string{"push", "pull_request"},
   153  		CreatedAt: &date,
   154  		UpdatedAt: &date,
   155  	}}
   156  	if !reflect.DeepEqual(installations, want) {
   157  		t.Errorf("Apps.ListInstallations returned %+v, want %+v", installations, want)
   158  	}
   159  }
   160  
   161  func TestAppsService_GetInstallation(t *testing.T) {
   162  	client, mux, _, teardown := setup()
   163  	defer teardown()
   164  
   165  	mux.HandleFunc("/app/installations/1", func(w http.ResponseWriter, r *http.Request) {
   166  		testMethod(t, r, "GET")
   167  		fmt.Fprint(w, `{"id":1, "app_id":1, "target_id":1, "target_type": "Organization"}`)
   168  	})
   169  
   170  	installation, _, err := client.Apps.GetInstallation(context.Background(), 1)
   171  	if err != nil {
   172  		t.Errorf("Apps.GetInstallation returned error: %v", err)
   173  	}
   174  
   175  	want := &Installation{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("Organization")}
   176  	if !reflect.DeepEqual(installation, want) {
   177  		t.Errorf("Apps.GetInstallation returned %+v, want %+v", installation, want)
   178  	}
   179  }
   180  
   181  func TestAppsService_ListUserInstallations(t *testing.T) {
   182  	client, mux, _, teardown := setup()
   183  	defer teardown()
   184  
   185  	mux.HandleFunc("/user/installations", func(w http.ResponseWriter, r *http.Request) {
   186  		testMethod(t, r, "GET")
   187  		testFormValues(t, r, values{
   188  			"page":     "1",
   189  			"per_page": "2",
   190  		})
   191  		fmt.Fprint(w, `{"installations":[{"id":1, "app_id":1, "target_id":1, "target_type": "Organization"}]}`)
   192  	})
   193  
   194  	opt := &ListOptions{Page: 1, PerPage: 2}
   195  	installations, _, err := client.Apps.ListUserInstallations(context.Background(), opt)
   196  	if err != nil {
   197  		t.Errorf("Apps.ListUserInstallations returned error: %v", err)
   198  	}
   199  
   200  	want := []*Installation{{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("Organization")}}
   201  	if !reflect.DeepEqual(installations, want) {
   202  		t.Errorf("Apps.ListUserInstallations returned %+v, want %+v", installations, want)
   203  	}
   204  }
   205  
   206  func TestAppsService_SuspendInstallation(t *testing.T) {
   207  	client, mux, _, teardown := setup()
   208  	defer teardown()
   209  
   210  	mux.HandleFunc("/app/installations/1/suspended", func(w http.ResponseWriter, r *http.Request) {
   211  		testMethod(t, r, "PUT")
   212  
   213  		w.WriteHeader(http.StatusNoContent)
   214  	})
   215  
   216  	if _, err := client.Apps.SuspendInstallation(context.Background(), 1); err != nil {
   217  		t.Errorf("Apps.SuspendInstallation returned error: %v", err)
   218  	}
   219  }
   220  
   221  func TestAppsService_UnsuspendInstallation(t *testing.T) {
   222  	client, mux, _, teardown := setup()
   223  	defer teardown()
   224  
   225  	mux.HandleFunc("/app/installations/1/suspended", func(w http.ResponseWriter, r *http.Request) {
   226  		testMethod(t, r, "DELETE")
   227  
   228  		w.WriteHeader(http.StatusNoContent)
   229  	})
   230  
   231  	if _, err := client.Apps.UnsuspendInstallation(context.Background(), 1); err != nil {
   232  		t.Errorf("Apps.UnsuspendInstallation returned error: %v", err)
   233  	}
   234  }
   235  
   236  func TestAppsService_DeleteInstallation(t *testing.T) {
   237  	client, mux, _, teardown := setup()
   238  	defer teardown()
   239  
   240  	mux.HandleFunc("/app/installations/1", func(w http.ResponseWriter, r *http.Request) {
   241  		testMethod(t, r, "DELETE")
   242  		w.WriteHeader(http.StatusNoContent)
   243  	})
   244  
   245  	_, err := client.Apps.DeleteInstallation(context.Background(), 1)
   246  	if err != nil {
   247  		t.Errorf("Apps.DeleteInstallation returned error: %v", err)
   248  	}
   249  }
   250  
   251  func TestAppsService_CreateInstallationToken(t *testing.T) {
   252  	client, mux, _, teardown := setup()
   253  	defer teardown()
   254  
   255  	mux.HandleFunc("/app/installations/1/access_tokens", func(w http.ResponseWriter, r *http.Request) {
   256  		testMethod(t, r, "POST")
   257  		fmt.Fprint(w, `{"token":"t"}`)
   258  	})
   259  
   260  	token, _, err := client.Apps.CreateInstallationToken(context.Background(), 1, nil)
   261  	if err != nil {
   262  		t.Errorf("Apps.CreateInstallationToken returned error: %v", err)
   263  	}
   264  
   265  	want := &InstallationToken{Token: String("t")}
   266  	if !reflect.DeepEqual(token, want) {
   267  		t.Errorf("Apps.CreateInstallationToken returned %+v, want %+v", token, want)
   268  	}
   269  }
   270  
   271  func TestAppsService_CreateInstallationTokenWithOptions(t *testing.T) {
   272  	client, mux, _, teardown := setup()
   273  	defer teardown()
   274  
   275  	installationTokenOptions := &InstallationTokenOptions{
   276  		RepositoryIDs: []int64{1234},
   277  		Permissions: &InstallationPermissions{
   278  			Contents: String("write"),
   279  			Issues:   String("read"),
   280  		},
   281  	}
   282  
   283  	// Convert InstallationTokenOptions into an io.ReadCloser object for comparison.
   284  	wantBody, err := GetReadCloser(installationTokenOptions)
   285  	if err != nil {
   286  		t.Errorf("GetReadCloser returned error: %v", err)
   287  	}
   288  
   289  	mux.HandleFunc("/app/installations/1/access_tokens", func(w http.ResponseWriter, r *http.Request) {
   290  		// Read request body contents.
   291  		var gotBodyBytes []byte
   292  		gotBodyBytes, err = ioutil.ReadAll(r.Body)
   293  		if err != nil {
   294  			t.Errorf("ReadAll returned error: %v", err)
   295  		}
   296  		r.Body = ioutil.NopCloser(bytes.NewBuffer(gotBodyBytes))
   297  
   298  		if !reflect.DeepEqual(r.Body, wantBody) {
   299  			t.Errorf("request sent %+v, want %+v", r.Body, wantBody)
   300  		}
   301  
   302  		testMethod(t, r, "POST")
   303  		fmt.Fprint(w, `{"token":"t"}`)
   304  	})
   305  
   306  	token, _, err := client.Apps.CreateInstallationToken(context.Background(), 1, installationTokenOptions)
   307  	if err != nil {
   308  		t.Errorf("Apps.CreateInstallationToken returned error: %v", err)
   309  	}
   310  
   311  	want := &InstallationToken{Token: String("t")}
   312  	if !reflect.DeepEqual(token, want) {
   313  		t.Errorf("Apps.CreateInstallationToken returned %+v, want %+v", token, want)
   314  	}
   315  }
   316  
   317  func TestAppsService_CreateAttachement(t *testing.T) {
   318  	client, mux, _, teardown := setup()
   319  	defer teardown()
   320  
   321  	mux.HandleFunc("/content_references/11/attachments", func(w http.ResponseWriter, r *http.Request) {
   322  		testMethod(t, r, "POST")
   323  		testHeader(t, r, "Accept", mediaTypeContentAttachmentsPreview)
   324  
   325  		w.WriteHeader(http.StatusOK)
   326  		w.Write([]byte(`{"id":1,"title":"title1","body":"body1"}`))
   327  	})
   328  
   329  	got, _, err := client.Apps.CreateAttachment(context.Background(), 11, "title1", "body1")
   330  	if err != nil {
   331  		t.Errorf("CreateAttachment returned error: %v", err)
   332  	}
   333  
   334  	want := &Attachment{ID: Int64(1), Title: String("title1"), Body: String("body1")}
   335  	if !reflect.DeepEqual(got, want) {
   336  		t.Errorf("CreateAttachment = %+v, want %+v", got, want)
   337  	}
   338  }
   339  
   340  func TestAppsService_FindOrganizationInstallation(t *testing.T) {
   341  	client, mux, _, teardown := setup()
   342  	defer teardown()
   343  
   344  	mux.HandleFunc("/orgs/o/installation", func(w http.ResponseWriter, r *http.Request) {
   345  		testMethod(t, r, "GET")
   346  		fmt.Fprint(w, `{"id":1, "app_id":1, "target_id":1, "target_type": "Organization"}`)
   347  	})
   348  
   349  	installation, _, err := client.Apps.FindOrganizationInstallation(context.Background(), "o")
   350  	if err != nil {
   351  		t.Errorf("Apps.FindOrganizationInstallation returned error: %v", err)
   352  	}
   353  
   354  	want := &Installation{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("Organization")}
   355  	if !reflect.DeepEqual(installation, want) {
   356  		t.Errorf("Apps.FindOrganizationInstallation returned %+v, want %+v", installation, want)
   357  	}
   358  }
   359  
   360  func TestAppsService_FindRepositoryInstallation(t *testing.T) {
   361  	client, mux, _, teardown := setup()
   362  	defer teardown()
   363  
   364  	mux.HandleFunc("/repos/o/r/installation", func(w http.ResponseWriter, r *http.Request) {
   365  		testMethod(t, r, "GET")
   366  		fmt.Fprint(w, `{"id":1, "app_id":1, "target_id":1, "target_type": "Organization"}`)
   367  	})
   368  
   369  	installation, _, err := client.Apps.FindRepositoryInstallation(context.Background(), "o", "r")
   370  	if err != nil {
   371  		t.Errorf("Apps.FindRepositoryInstallation returned error: %v", err)
   372  	}
   373  
   374  	want := &Installation{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("Organization")}
   375  	if !reflect.DeepEqual(installation, want) {
   376  		t.Errorf("Apps.FindRepositoryInstallation returned %+v, want %+v", installation, want)
   377  	}
   378  }
   379  
   380  func TestAppsService_FindRepositoryInstallationByID(t *testing.T) {
   381  	client, mux, _, teardown := setup()
   382  	defer teardown()
   383  
   384  	mux.HandleFunc("/repositories/1/installation", func(w http.ResponseWriter, r *http.Request) {
   385  		testMethod(t, r, "GET")
   386  		fmt.Fprint(w, `{"id":1, "app_id":1, "target_id":1, "target_type": "Organization"}`)
   387  	})
   388  
   389  	installation, _, err := client.Apps.FindRepositoryInstallationByID(context.Background(), 1)
   390  	if err != nil {
   391  		t.Errorf("Apps.FindRepositoryInstallationByID returned error: %v", err)
   392  	}
   393  
   394  	want := &Installation{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("Organization")}
   395  	if !reflect.DeepEqual(installation, want) {
   396  		t.Errorf("Apps.FindRepositoryInstallationByID returned %+v, want %+v", installation, want)
   397  	}
   398  }
   399  
   400  func TestAppsService_FindUserInstallation(t *testing.T) {
   401  	client, mux, _, teardown := setup()
   402  	defer teardown()
   403  
   404  	mux.HandleFunc("/users/u/installation", func(w http.ResponseWriter, r *http.Request) {
   405  		testMethod(t, r, "GET")
   406  		fmt.Fprint(w, `{"id":1, "app_id":1, "target_id":1, "target_type": "User"}`)
   407  	})
   408  
   409  	installation, _, err := client.Apps.FindUserInstallation(context.Background(), "u")
   410  	if err != nil {
   411  		t.Errorf("Apps.FindUserInstallation returned error: %v", err)
   412  	}
   413  
   414  	want := &Installation{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("User")}
   415  	if !reflect.DeepEqual(installation, want) {
   416  		t.Errorf("Apps.FindUserInstallation returned %+v, want %+v", installation, want)
   417  	}
   418  }
   419  
   420  // GetReadWriter converts a body interface into an io.ReadWriter object.
   421  func GetReadWriter(body interface{}) (io.ReadWriter, error) {
   422  	var buf io.ReadWriter
   423  	if body != nil {
   424  		buf = new(bytes.Buffer)
   425  		enc := json.NewEncoder(buf)
   426  		err := enc.Encode(body)
   427  		if err != nil {
   428  			return nil, err
   429  		}
   430  	}
   431  	return buf, nil
   432  }
   433  
   434  // GetReadCloser converts a body interface into an io.ReadCloser object.
   435  func GetReadCloser(body interface{}) (io.ReadCloser, error) {
   436  	buf, err := GetReadWriter(body)
   437  	if err != nil {
   438  		return nil, err
   439  	}
   440  
   441  	all, err := ioutil.ReadAll(buf)
   442  	if err != nil {
   443  		return nil, err
   444  	}
   445  	return ioutil.NopCloser(bytes.NewBuffer(all)), nil
   446  }