...

Source file src/edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/common/actor_test.go

Documentation: edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/common

     1  // Copyright 2018 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package common
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull"
    25  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull/pulltest"
    26  )
    27  
    28  func TestIsActor(t *testing.T) {
    29  	ctx := context.Background()
    30  	prctx := &pulltest.Context{
    31  		TeamMemberships: map[string][]string{
    32  			"mhaypenny": {"cool-org/team1", "regular-org/team2"},
    33  		},
    34  		OrgMemberships: map[string][]string{
    35  			"mhaypenny": {"cool-org", "regular-org"},
    36  		},
    37  		CollaboratorsValue: []*pull.Collaborator{
    38  			{
    39  				Name: "mhaypenny",
    40  				Permissions: []pull.CollaboratorPermission{
    41  					{Permission: pull.PermissionAdmin},
    42  				},
    43  			},
    44  			{
    45  				Name: "jstrawnickel",
    46  				Permissions: []pull.CollaboratorPermission{
    47  					{Permission: pull.PermissionWrite},
    48  				},
    49  			},
    50  		},
    51  	}
    52  
    53  	assertActor := func(t *testing.T, a *Actors, user string) {
    54  		isActor, err := a.IsActor(ctx, prctx, user)
    55  		require.NoError(t, err)
    56  		assert.Truef(t, isActor, "%s is not an actor", user)
    57  	}
    58  
    59  	assertNotActor := func(t *testing.T, a *Actors, user string) {
    60  		isActor, err := a.IsActor(ctx, prctx, user)
    61  		require.NoError(t, err)
    62  		assert.Falsef(t, isActor, "%s is an actor", user)
    63  	}
    64  
    65  	t.Run("users", func(t *testing.T) {
    66  		a := &Actors{
    67  			Users: []string{"mhaypenny"},
    68  		}
    69  
    70  		assertActor(t, a, "mhaypenny")
    71  		assertNotActor(t, a, "ttest")
    72  	})
    73  
    74  	t.Run("teams", func(t *testing.T) {
    75  		a := &Actors{
    76  			Teams: []string{"regular-org/team2"},
    77  		}
    78  
    79  		assertActor(t, a, "mhaypenny")
    80  		assertNotActor(t, a, "ttest")
    81  	})
    82  
    83  	t.Run("organizations", func(t *testing.T) {
    84  		a := &Actors{
    85  			Organizations: []string{"cool-org"},
    86  		}
    87  
    88  		assertActor(t, a, "mhaypenny")
    89  		assertNotActor(t, a, "ttest")
    90  	})
    91  
    92  	t.Run("admins", func(t *testing.T) {
    93  		a := &Actors{Admins: true}
    94  
    95  		assertActor(t, a, "mhaypenny")
    96  		assertNotActor(t, a, "jstrawnickel")
    97  		assertNotActor(t, a, "ttest")
    98  	})
    99  
   100  	t.Run("write", func(t *testing.T) {
   101  		a := &Actors{WriteCollaborators: true}
   102  
   103  		assertActor(t, a, "jstrawnickel")
   104  		assertActor(t, a, "mhaypenny")
   105  		assertNotActor(t, a, "ttest")
   106  	})
   107  
   108  	t.Run("permissions", func(t *testing.T) {
   109  		a := &Actors{
   110  			Permissions: []pull.Permission{pull.PermissionTriage},
   111  		}
   112  
   113  		assertActor(t, a, "mhaypenny")
   114  		assertActor(t, a, "jstrawnickel")
   115  		assertNotActor(t, a, "ttest")
   116  	})
   117  }
   118  
   119  func TestIsEmpty(t *testing.T) {
   120  	a := &Actors{}
   121  	assert.True(t, a.IsEmpty(), "Actors struct was not empty")
   122  
   123  	a = &Actors{Users: []string{"user"}}
   124  	assert.False(t, a.IsEmpty(), "Actors struct was empty")
   125  
   126  	a = &Actors{Teams: []string{"org/team"}}
   127  	assert.False(t, a.IsEmpty(), "Actors struct was empty")
   128  
   129  	a = &Actors{Organizations: []string{"org"}}
   130  	assert.False(t, a.IsEmpty(), "Actors struct was empty")
   131  
   132  	a = nil
   133  	assert.True(t, a.IsEmpty(), "nil struct was not empty")
   134  }
   135  

View as plain text