satellite/console/wasm: add more unit tests

Change-Id: Ie134f8a08d690ce013039ed1a4e484f8b6a1a6d5
This commit is contained in:
Jessica Grebenschikov 2021-01-05 14:24:46 -08:00 committed by Jess G
parent 2d2359667d
commit 1709117b0d
3 changed files with 108 additions and 0 deletions

View File

@ -335,6 +335,22 @@ func (client *Uplink) DeleteBucket(ctx context.Context, satellite *Satellite, bu
return nil
}
// ListBuckets returns a list of all buckets in a project.
func (client *Uplink) ListBuckets(ctx context.Context, satellite *Satellite) ([]*uplink.Bucket, error) {
var buckets = []*uplink.Bucket{}
project, err := client.GetProject(ctx, satellite)
if err != nil {
return buckets, err
}
defer func() { err = errs.Combine(err, project.Close()) }()
iter := project.ListBuckets(ctx, &uplink.ListBucketsOptions{})
for iter.Next() {
buckets = append(buckets, iter.Item())
}
return buckets, iter.Err()
}
// GetProject returns a uplink.Project which allows interactions with a specific project.
func (client *Uplink) GetProject(ctx context.Context, satellite *Satellite) (*uplink.Project, error) {
access := client.Access[satellite.ID()]

View File

@ -11,6 +11,7 @@ import (
"storj.io/common/testcontext"
"storj.io/storj/private/testplanet"
console "storj.io/storj/satellite/console/consolewasm"
"storj.io/uplink"
)
// TestGenerateAccessGrant confirms that the access grant produced by the wasm access code
@ -38,3 +39,44 @@ func TestGenerateAccessGrant(t *testing.T) {
require.Equal(t, wasmAccessString, uplinkCliAccessString)
})
}
// TestDefaultAccess confirms that you can perform basic uplink operations with
// the default access grant created from wasm code.
func TestDefaultAccess(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 10, UplinkCount: 1,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
satellitePeer := planet.Satellites[0]
satelliteNodeURL := satellitePeer.NodeURL().String()
uplinkPeer := planet.Uplinks[0]
APIKey := uplinkPeer.APIKey[satellitePeer.ID()]
projectID := uplinkPeer.Projects[0].ID.String()
require.Equal(t, 1, len(uplinkPeer.Projects))
passphrase := "supersecretpassphrase"
testbucket1 := "buckettest1"
testfilename := "file.txt"
testdata := []byte("fun data")
// Create an access with the console access grant code that allows full access.
access, err := console.GenAccessGrant(satelliteNodeURL, APIKey.Serialize(), passphrase, projectID)
require.NoError(t, err)
newAccess, err := uplink.ParseAccess(access)
require.NoError(t, err)
uplinkPeer.Access[satellitePeer.ID()] = newAccess
// Confirm that we can create a bucket, upload/download/delete an object, and delete the bucket with the new access.
require.NoError(t, uplinkPeer.CreateBucket(ctx, satellitePeer, testbucket1))
err = uplinkPeer.Upload(ctx, satellitePeer, testbucket1, testfilename, testdata)
require.NoError(t, err)
data, err := uplinkPeer.Download(ctx, satellitePeer, testbucket1, testfilename)
require.NoError(t, err)
require.Equal(t, data, testdata)
buckets, err := uplinkPeer.ListBuckets(ctx, satellitePeer)
require.NoError(t, err)
require.Equal(t, len(buckets), 1)
err = uplinkPeer.DeleteObject(ctx, satellitePeer, testbucket1, testfilename)
require.NoError(t, err)
require.NoError(t, uplinkPeer.DeleteBucket(ctx, satellitePeer, testbucket1))
})
}

View File

@ -97,3 +97,53 @@ func TestSetPermissionWithBuckets(t *testing.T) {
require.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
})
}
func TestSetPermissionUplinkOperations(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 10, UplinkCount: 1,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
satellitePeer := planet.Satellites[0]
satelliteNodeURL := satellitePeer.NodeURL().String()
uplinkPeer := planet.Uplinks[0]
APIKey := uplinkPeer.APIKey[satellitePeer.ID()]
apiKeyString := APIKey.Serialize()
projectID := uplinkPeer.Projects[0].ID.String()
require.Equal(t, 1, len(uplinkPeer.Projects))
allPermission := console.Permission{
AllowDownload: true,
AllowUpload: true,
AllowList: true,
AllowDelete: true,
NotBefore: time.Now().Add(-24 * time.Hour),
NotAfter: time.Now().Add(48 * time.Hour),
}
restrictedKey, err := console.SetPermission(apiKeyString, []string{}, allPermission)
require.NoError(t, err)
passphrase := "supersecretpassphrase"
restrictedAccessGrant, err := console.GenAccessGrant(satelliteNodeURL, restrictedKey.Serialize(), passphrase, projectID)
require.NoError(t, err)
restrictedAccess, err := uplink.ParseAccess(restrictedAccessGrant)
require.NoError(t, err)
uplinkPeer.APIKey[satellitePeer.ID()] = restrictedKey
uplinkPeer.Access[satellitePeer.ID()] = restrictedAccess
testbucket1 := "buckettest1"
testfilename := "file.txt"
testdata := []byte("fun data")
// Confirm that we can create a bucket, upload/download/delete an object, and delete the bucket with the new restricted access.
require.NoError(t, uplinkPeer.CreateBucket(ctx, satellitePeer, testbucket1))
err = uplinkPeer.Upload(ctx, satellitePeer, testbucket1, testfilename, testdata)
require.NoError(t, err)
data, err := uplinkPeer.Download(ctx, satellitePeer, testbucket1, testfilename)
require.NoError(t, err)
require.Equal(t, data, testdata)
buckets, err := uplinkPeer.ListBuckets(ctx, satellitePeer)
require.NoError(t, err)
require.Equal(t, len(buckets), 1)
err = uplinkPeer.DeleteObject(ctx, satellitePeer, testbucket1, testfilename)
require.NoError(t, err)
require.NoError(t, uplinkPeer.DeleteBucket(ctx, satellitePeer, testbucket1))
})
}