sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/cmd/invitations-accepter/main_test.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  
    24  	"sigs.k8s.io/prow/pkg/github"
    25  	"sigs.k8s.io/prow/pkg/github/fakegithub"
    26  )
    27  
    28  func TestAcceptInvitations(t *testing.T) {
    29  
    30  	testCases := []struct {
    31  		id              string
    32  		repoInvitations map[int]github.UserRepoInvitation
    33  		orgInvitations  map[string]github.UserOrgInvitation
    34  	}{
    35  		{
    36  			id: "no invitations to accept",
    37  		},
    38  		{
    39  			id: "one repo invitation to accept",
    40  			repoInvitations: map[int]github.UserRepoInvitation{
    41  				1: {InvitationID: 1, Repository: &github.Repo{FullName: "foo/bar"}},
    42  			},
    43  		},
    44  		{
    45  			id: "multiple repo invitations to accept",
    46  			repoInvitations: map[int]github.UserRepoInvitation{
    47  				1: {InvitationID: 1, Repository: &github.Repo{FullName: "foo/bar"}},
    48  				2: {InvitationID: 2, Repository: &github.Repo{FullName: "james/bond"}},
    49  				3: {InvitationID: 3, Repository: &github.Repo{FullName: "captain/hook"}},
    50  			},
    51  		},
    52  
    53  		{
    54  			id: "one org invitation to accept",
    55  			orgInvitations: map[string]github.UserOrgInvitation{
    56  				"org-1": {Org: github.UserOrganization{Login: "org-1"}},
    57  			},
    58  		},
    59  		{
    60  			id: "multiple org invitations to accept",
    61  			orgInvitations: map[string]github.UserOrgInvitation{
    62  				"org-1": {Org: github.UserOrganization{Login: "org-1"}},
    63  				"org-2": {Org: github.UserOrganization{Login: "org-2"}},
    64  				"org-3": {Org: github.UserOrganization{Login: "org-3"}},
    65  			},
    66  		},
    67  		{
    68  			id: "multiple org and repo invitations to accept",
    69  			repoInvitations: map[int]github.UserRepoInvitation{
    70  				1: {InvitationID: 1, Repository: &github.Repo{FullName: "foo/bar"}},
    71  				2: {InvitationID: 2, Repository: &github.Repo{FullName: "james/bond"}},
    72  				3: {InvitationID: 3, Repository: &github.Repo{FullName: "captain/hook"}},
    73  			},
    74  			orgInvitations: map[string]github.UserOrgInvitation{
    75  				"org-1": {Org: github.UserOrganization{Login: "org-1"}},
    76  				"org-2": {Org: github.UserOrganization{Login: "org-2"}},
    77  				"org-3": {Org: github.UserOrganization{Login: "org-3"}},
    78  			},
    79  		},
    80  	}
    81  
    82  	for _, tc := range testCases {
    83  		t.Run(tc.id, func(t *testing.T) {
    84  			fgh := &fakegithub.FakeClient{UserRepoInvitations: tc.repoInvitations, UserOrgInvitations: tc.orgInvitations}
    85  
    86  			if err := acceptInvitations(fgh, false); err != nil {
    87  				t.Fatalf("error wasn't expected: %v", err)
    88  			}
    89  
    90  			actualOrgInvitations, err := fgh.ListCurrentUserOrgInvitations()
    91  			if err != nil {
    92  				t.Fatalf("error not expected: %v", err)
    93  			}
    94  
    95  			var orgInvitationsExpected []github.UserOrgInvitation
    96  			if diff := cmp.Diff(actualOrgInvitations, orgInvitationsExpected); diff != "" {
    97  				t.Fatal(diff)
    98  			}
    99  
   100  			actualRepoInvitations, err := fgh.ListCurrentUserRepoInvitations()
   101  			if err != nil {
   102  				t.Fatalf("error not expected: %v", err)
   103  			}
   104  
   105  			var repoInvitationsExpected []github.UserRepoInvitation
   106  			if diff := cmp.Diff(actualRepoInvitations, repoInvitationsExpected); diff != "" {
   107  				t.Fatal(diff)
   108  			}
   109  		})
   110  	}
   111  }