github.com/argoproj/argo-cd/v2@v2.10.5/test/e2e/scoped_repository_test.go (about)

     1  package e2e
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/argoproj/argo-cd/v2/test/e2e/fixture"
     8  
     9  	"github.com/argoproj/argo-cd/v2/pkg/apiclient/project"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  
    13  	. "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
    14  	accountFixture "github.com/argoproj/argo-cd/v2/test/e2e/fixture/account"
    15  	projectFixture "github.com/argoproj/argo-cd/v2/test/e2e/fixture/project"
    16  	repoFixture "github.com/argoproj/argo-cd/v2/test/e2e/fixture/repos"
    17  )
    18  
    19  func TestCreateRepositoryWithProject(t *testing.T) {
    20  	prjConsequence := projectFixture.Given(t).
    21  		When().
    22  		Name("argo-project").
    23  		Create().
    24  		Then()
    25  
    26  	path := "https://github.com/argoproj/argo-cd.git"
    27  	repoFixture.Given(t, true).
    28  		When().
    29  		Path(path).
    30  		Project("argo-project").
    31  		Create().
    32  		Then().
    33  		And(func(r *Repository, err error) {
    34  			assert.Equal(t, r.Repo, path)
    35  			assert.Equal(t, r.Project, "argo-project")
    36  
    37  			prjConsequence.And(func(projectResponse *project.DetailedProjectsResponse, err error) {
    38  				assert.Equal(t, len(projectResponse.Repositories), 1)
    39  				assert.Equal(t, projectResponse.Repositories[0].Repo, path)
    40  			})
    41  		})
    42  }
    43  
    44  func TestCreateRepositoryNonAdminUserPermissionDenied(t *testing.T) {
    45  	accountFixture.Given(t).
    46  		Name("test").
    47  		When().
    48  		Create().
    49  		Login()
    50  
    51  	path := "https://github.com/argoproj/argo-cd.git"
    52  	repoFixture.Given(t, true).
    53  		When().
    54  		Path(path).
    55  		Project("argo-project").
    56  		IgnoreErrors().
    57  		Create().
    58  		Then().
    59  		AndCLIOutput(func(output string, err error) {
    60  			assert.True(t, strings.Contains(err.Error(), "PermissionDenied desc = permission denied: repositories, create"))
    61  		})
    62  }
    63  
    64  func TestCreateRepositoryNonAdminUserWithWrongProject(t *testing.T) {
    65  	accountFixture.Given(t).
    66  		Name("test").
    67  		When().
    68  		Create().
    69  		Login().
    70  		SetPermissions([]fixture.ACL{
    71  			{
    72  				Resource: "repositories",
    73  				Action:   "*",
    74  				Scope:    "wrong-project/*",
    75  			},
    76  		}, "org-admin")
    77  
    78  	path := "https://github.com/argoproj/argo-cd.git"
    79  	repoFixture.Given(t, true).
    80  		When().
    81  		Path(path).
    82  		Project("argo-project").
    83  		IgnoreErrors().
    84  		Create().
    85  		Then().
    86  		AndCLIOutput(func(output string, err error) {
    87  			assert.True(t, strings.Contains(err.Error(), "PermissionDenied desc = permission denied: repositories, create"))
    88  		})
    89  }
    90  
    91  func TestDeleteRepositoryRbacAllowed(t *testing.T) {
    92  	accountFixture.Given(t).
    93  		Name("test").
    94  		When().
    95  		Create().
    96  		Login().
    97  		SetPermissions([]fixture.ACL{
    98  			{
    99  				Resource: "repositories",
   100  				Action:   "create",
   101  				Scope:    "argo-project/*",
   102  			},
   103  			{
   104  				Resource: "repositories",
   105  				Action:   "delete",
   106  				Scope:    "argo-project/*",
   107  			},
   108  			{
   109  				Resource: "repositories",
   110  				Action:   "get",
   111  				Scope:    "argo-project/*",
   112  			},
   113  		}, "org-admin")
   114  
   115  	path := "https://github.com/argoproj/argo-cd.git"
   116  	repoFixture.Given(t, true).
   117  		When().
   118  		Path(path).
   119  		Project("argo-project").
   120  		Create().
   121  		Then().
   122  		And(func(r *Repository, err error) {
   123  			assert.Equal(t, r.Repo, path)
   124  			assert.Equal(t, r.Project, "argo-project")
   125  		}).
   126  		When().
   127  		Delete().
   128  		Then().
   129  		AndCLIOutput(func(output string, err error) {
   130  			assert.True(t, strings.Contains(output, "Repository 'https://github.com/argoproj/argo-cd.git' removed"))
   131  		})
   132  }
   133  
   134  func TestDeleteRepositoryRbacDenied(t *testing.T) {
   135  	accountFixture.Given(t).
   136  		Name("test").
   137  		When().
   138  		Create().
   139  		Login().
   140  		SetPermissions([]fixture.ACL{
   141  			{
   142  				Resource: "repositories",
   143  				Action:   "create",
   144  				Scope:    "argo-project/*",
   145  			},
   146  			{
   147  				Resource: "repositories",
   148  				Action:   "delete",
   149  				Scope:    "argo-pr/*",
   150  			},
   151  			{
   152  				Resource: "repositories",
   153  				Action:   "get",
   154  				Scope:    "argo-project/*",
   155  			},
   156  		}, "org-admin")
   157  
   158  	path := "https://github.com/argoproj/argo-cd.git"
   159  	repoFixture.Given(t, true).
   160  		When().
   161  		Path(path).
   162  		Project("argo-project").
   163  		Create().
   164  		Then().
   165  		And(func(r *Repository, err error) {
   166  			assert.Equal(t, r.Repo, path)
   167  			assert.Equal(t, r.Project, "argo-project")
   168  		}).
   169  		When().
   170  		IgnoreErrors().
   171  		Delete().
   172  		Then().
   173  		AndCLIOutput(func(output string, err error) {
   174  			assert.True(t, strings.Contains(err.Error(), "PermissionDenied desc = permission denied: repositories, delete"))
   175  		})
   176  }
   177  
   178  func TestDeleteRepository(t *testing.T) {
   179  	path := "https://github.com/argoproj/argo-cd.git"
   180  	repoFixture.Given(t, false).
   181  		When().
   182  		Path(path).
   183  		Project("argo-project").
   184  		Create().
   185  		Then().
   186  		And(func(r *Repository, err error) {
   187  			assert.Equal(t, r.Repo, path)
   188  		}).
   189  		When().
   190  		Delete().
   191  		Then().
   192  		And(func(r *Repository, err error) {
   193  			assert.Equal(t, err.Error(), "repo not found")
   194  		})
   195  
   196  }
   197  
   198  func TestListRepoCLIOutput(t *testing.T) {
   199  	path := "https://github.com/argoproj/argo-cd.git"
   200  	repoFixture.Given(t, false).
   201  		When().
   202  		Path(path).
   203  		Project("argo-project").
   204  		Create().
   205  		Then().
   206  		AndCLIOutput(func(output string, err error) {
   207  			assert.Equal(t, `Repository 'https://github.com/argoproj/argo-cd.git' added`, output)
   208  		}).
   209  		When().
   210  		List().
   211  		Then().
   212  		AndCLIOutput(func(output string, err error) {
   213  			assert.Equal(t, `TYPE  NAME  REPO                                     INSECURE  OCI    LFS    CREDS  STATUS      MESSAGE  PROJECT
   214  git         https://github.com/argoproj/argo-cd.git  false     false  false  false  Successful           argo-project`, output)
   215  		})
   216  }
   217  
   218  func TestGetRepoCLIOutput(t *testing.T) {
   219  	path := "https://github.com/argoproj/argo-cd.git"
   220  	repoFixture.Given(t, false).
   221  		When().
   222  		Path(path).
   223  		Project("argo-project").
   224  		Create().
   225  		Then().
   226  		AndCLIOutput(func(output string, err error) {
   227  			assert.Equal(t, `Repository 'https://github.com/argoproj/argo-cd.git' added`, output)
   228  		}).
   229  		When().
   230  		Get().
   231  		Then().
   232  		AndCLIOutput(func(output string, err error) {
   233  			assert.Equal(t, `TYPE  NAME  REPO                                     INSECURE  OCI    LFS    CREDS  STATUS      MESSAGE  PROJECT
   234  git         https://github.com/argoproj/argo-cd.git  false     false  false  false  Successful           argo-project`, output)
   235  		})
   236  }