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

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