|
| 1 | +package coderd_test |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"testing" |
| 6 | + |
| 7 | +"github.com/stretchr/testify/require" |
| 8 | + |
| 9 | +"github.com/coder/coder/v2/coderd/coderdtest" |
| 10 | +"github.com/coder/coder/v2/coderd/rbac" |
| 11 | +"github.com/coder/coder/v2/codersdk" |
| 12 | +"github.com/coder/coder/v2/testutil" |
| 13 | +) |
| 14 | + |
| 15 | +funcTestProvisionerKeys(t*testing.T) { |
| 16 | +t.Parallel() |
| 17 | + |
| 18 | +ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitLong) |
| 19 | +t.Cleanup(cancel) |
| 20 | +client:=coderdtest.New(t,nil) |
| 21 | +owner:=coderdtest.CreateFirstUser(t,client) |
| 22 | +orgAdmin,_:=coderdtest.CreateAnotherUser(t,client,owner.OrganizationID,rbac.ScopedRoleOrgAdmin(owner.OrganizationID)) |
| 23 | +member,_:=coderdtest.CreateAnotherUser(t,client,owner.OrganizationID) |
| 24 | +otherOrg,err:=client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{ |
| 25 | +Name:"other", |
| 26 | +}) |
| 27 | +require.NoError(t,err,"create org") |
| 28 | +outsideOrgAdmin,_:=coderdtest.CreateAnotherUser(t,client,otherOrg.ID,rbac.ScopedRoleOrgAdmin(otherOrg.ID)) |
| 29 | + |
| 30 | +// member cannot create a provisioner key |
| 31 | +_,err=member.CreateProvisionerKey(ctx,otherOrg.ID, codersdk.CreateProvisionerKeyRequest{ |
| 32 | +Name:"key", |
| 33 | +}) |
| 34 | +require.ErrorContains(t,err,"Resource not found") |
| 35 | + |
| 36 | +// member cannot list provisioner keys |
| 37 | +_,err=member.ListProvisionerKeys(ctx,otherOrg.ID) |
| 38 | +require.ErrorContains(t,err,"Resource not found") |
| 39 | + |
| 40 | +// member cannot delete a provisioner key |
| 41 | +err=member.DeleteProvisionerKey(ctx,otherOrg.ID,"key") |
| 42 | +require.ErrorContains(t,err,"Resource not found") |
| 43 | + |
| 44 | +// outside org admin cannot create a provisioner key |
| 45 | +_,err=outsideOrgAdmin.CreateProvisionerKey(ctx,owner.OrganizationID, codersdk.CreateProvisionerKeyRequest{ |
| 46 | +Name:"key", |
| 47 | +}) |
| 48 | +require.ErrorContains(t,err,"Resource not found") |
| 49 | + |
| 50 | +// outside org admin cannot list provisioner keys |
| 51 | +_,err=outsideOrgAdmin.ListProvisionerKeys(ctx,owner.OrganizationID) |
| 52 | +require.ErrorContains(t,err,"Resource not found") |
| 53 | + |
| 54 | +// outside org admin cannot delete a provisioner key |
| 55 | +err=outsideOrgAdmin.DeleteProvisionerKey(ctx,owner.OrganizationID,"key") |
| 56 | +require.ErrorContains(t,err,"Resource not found") |
| 57 | + |
| 58 | +// org admin can list provisioner keys and get an empty list |
| 59 | +keys,err:=orgAdmin.ListProvisionerKeys(ctx,owner.OrganizationID) |
| 60 | +require.NoError(t,err,"org admin list provisioner keys") |
| 61 | +require.Len(t,keys,0,"org admin list provisioner keys") |
| 62 | + |
| 63 | +// org admin can create a provisioner key |
| 64 | +_,err=orgAdmin.CreateProvisionerKey(ctx,owner.OrganizationID, codersdk.CreateProvisionerKeyRequest{ |
| 65 | +Name:"key", |
| 66 | +}) |
| 67 | +require.NoError(t,err,"org admin create provisioner key") |
| 68 | + |
| 69 | +// org admin can conflict on name creating a provisioner key |
| 70 | +_,err=orgAdmin.CreateProvisionerKey(ctx,owner.OrganizationID, codersdk.CreateProvisionerKeyRequest{ |
| 71 | +Name:"key", |
| 72 | +}) |
| 73 | +require.Error(t,err,"org admin create provisioner key") |
| 74 | + |
| 75 | +// org admin can list provisioner keys |
| 76 | +keys,err=orgAdmin.ListProvisionerKeys(ctx,owner.OrganizationID) |
| 77 | +require.NoError(t,err,"org admin list provisioner keys") |
| 78 | +require.Len(t,keys,1,"org admin list provisioner keys") |
| 79 | + |
| 80 | +// org admin can delete a provisioner key |
| 81 | +err=orgAdmin.DeleteProvisionerKey(ctx,owner.OrganizationID,"key") |
| 82 | +require.NoError(t,err,"org admin delete provisioner key") |
| 83 | + |
| 84 | +// org admin cannot delete a provisioner key that doesn't exist |
| 85 | +err=orgAdmin.DeleteProvisionerKey(ctx,owner.OrganizationID,"key") |
| 86 | +require.Error(t,err,"org admin delete provisioner key") |
| 87 | +} |