github.com/argoproj/argo-cd@v1.8.7/cmd/argocd-util/commands/projects_test.go (about) 1 package commands 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 9 10 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" 11 "github.com/argoproj/argo-cd/pkg/client/clientset/versioned/fake" 12 ) 13 14 const ( 15 namespace = "default" 16 ) 17 18 func newProj(name string, roleNames ...string) *v1alpha1.AppProject { 19 var roles []v1alpha1.ProjectRole 20 for i := range roleNames { 21 roles = append(roles, v1alpha1.ProjectRole{Name: roleNames[i]}) 22 } 23 return &v1alpha1.AppProject{ObjectMeta: v1.ObjectMeta{ 24 Name: name, 25 Namespace: namespace, 26 }, Spec: v1alpha1.AppProjectSpec{ 27 Roles: roles, 28 }} 29 } 30 31 func TestUpdateProjects_FindMatchingProject(t *testing.T) { 32 clientset := fake.NewSimpleClientset(newProj("foo", "test"), newProj("bar", "test")) 33 34 modification, err := getModification("set", "*", "*", "allow") 35 assert.NoError(t, err) 36 err = updateProjects(clientset.ArgoprojV1alpha1().AppProjects(namespace), "ba*", "*", "set", modification, false) 37 assert.NoError(t, err) 38 39 fooProj, err := clientset.ArgoprojV1alpha1().AppProjects(namespace).Get(context.Background(), "foo", v1.GetOptions{}) 40 assert.NoError(t, err) 41 assert.Len(t, fooProj.Spec.Roles[0].Policies, 0) 42 43 barProj, err := clientset.ArgoprojV1alpha1().AppProjects(namespace).Get(context.Background(), "bar", v1.GetOptions{}) 44 assert.NoError(t, err) 45 assert.EqualValues(t, barProj.Spec.Roles[0].Policies, []string{"p, proj:bar:test, *, set, bar/*, allow"}) 46 } 47 48 func TestUpdateProjects_FindMatchingRole(t *testing.T) { 49 clientset := fake.NewSimpleClientset(newProj("proj", "foo", "bar")) 50 51 modification, err := getModification("set", "*", "*", "allow") 52 assert.NoError(t, err) 53 err = updateProjects(clientset.ArgoprojV1alpha1().AppProjects(namespace), "*", "fo*", "set", modification, false) 54 assert.NoError(t, err) 55 56 proj, err := clientset.ArgoprojV1alpha1().AppProjects(namespace).Get(context.Background(), "proj", v1.GetOptions{}) 57 assert.NoError(t, err) 58 assert.EqualValues(t, proj.Spec.Roles[0].Policies, []string{"p, proj:proj:foo, *, set, proj/*, allow"}) 59 assert.Len(t, proj.Spec.Roles[1].Policies, 0) 60 } 61 62 func TestGetModification_SetPolicy(t *testing.T) { 63 modification, err := getModification("set", "*", "*", "allow") 64 assert.NoError(t, err) 65 policy := modification("proj", "myaction") 66 assert.Equal(t, "*, myaction, proj/*, allow", policy) 67 } 68 69 func TestGetModification_RemovePolicy(t *testing.T) { 70 modification, err := getModification("remove", "*", "*", "allow") 71 assert.NoError(t, err) 72 policy := modification("proj", "myaction") 73 assert.Equal(t, "", policy) 74 } 75 76 func TestGetModification_NotSupported(t *testing.T) { 77 _, err := getModification("bar", "*", "*", "allow") 78 assert.Errorf(t, err, "modification bar is not supported") 79 }