mirror of
https://github.com/status-im/status-go.git
synced 2026-08-31 17:11:23 +00:00
Part of the Go project layout migration, item 31. Pure move plus import-path rewrite across 687 files. No API or behaviour change. The services keep their grouping under pkg/services/<name> rather than being promoted to pkg/<name>: 27 top-level directories in pkg/ would read worse than what we have, and the grouping is what makes "an RPC service" identifiable at a glance. Paths that follow the move: the logosstorage test target and generate step, the two wallet token-list tools, the migration-order check (and the pre-rebase hook symlinked to it), and the storage env helper. refs #7067
96 lines
2.4 KiB
Go
96 lines
2.4 KiB
Go
package permissions
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/status-im/status-go/internal/db/appdatabase"
|
|
"github.com/status-im/status-go/internal/testutils"
|
|
)
|
|
|
|
func setupTestDB(t *testing.T) (*Database, func()) {
|
|
db, cleanup, err := testutils.SetupTestSQLDB(appdatabase.DbInitializer{}, "perm-tests-")
|
|
require.NoError(t, err)
|
|
return NewDB(db), func() { require.NoError(t, cleanup()) }
|
|
}
|
|
|
|
func setupTestAPI(t *testing.T) (*API, func()) {
|
|
db, cancel := setupTestDB(t)
|
|
return &API{db: db}, cancel
|
|
}
|
|
|
|
func TestDappPermissionsStored(t *testing.T) {
|
|
api, cancel := setupTestAPI(t)
|
|
defer cancel()
|
|
|
|
expected := []DappPermissions{
|
|
{
|
|
Name: "first",
|
|
Permissions: []string{"r", "w"},
|
|
},
|
|
{
|
|
Name: "second",
|
|
Permissions: []string{"r", "x"},
|
|
},
|
|
{
|
|
Name: "third",
|
|
},
|
|
}
|
|
for _, perms := range expected {
|
|
require.NoError(t, api.AddDappPermissions(context.TODO(), perms))
|
|
}
|
|
rst, err := api.GetDappPermissions(context.TODO())
|
|
require.NoError(t, err)
|
|
// sort in lexicographic order by name
|
|
sort.Slice(rst, func(i, j int) bool {
|
|
return rst[i].Name < rst[j].Name
|
|
})
|
|
require.Equal(t, expected[0].Name, rst[0].Name)
|
|
require.Equal(t, expected[0].Permissions, rst[0].Permissions)
|
|
|
|
data, err := json.Marshal(rst)
|
|
require.NoError(t, err)
|
|
fmt.Println(string(data))
|
|
}
|
|
|
|
func TestDappPermissionsReplacedOnUpdated(t *testing.T) {
|
|
api, cancel := setupTestAPI(t)
|
|
defer cancel()
|
|
|
|
perms := DappPermissions{
|
|
Name: "first",
|
|
Permissions: []string{"r", "w"},
|
|
}
|
|
require.NoError(t, api.AddDappPermissions(context.TODO(), perms))
|
|
perms.Permissions = append(perms.Permissions, "x")
|
|
require.NoError(t, api.AddDappPermissions(context.TODO(), perms))
|
|
rst, err := api.GetDappPermissions(context.TODO())
|
|
require.NoError(t, err)
|
|
require.Len(t, rst, 1)
|
|
require.Equal(t, perms.Name, rst[0].Name)
|
|
require.Equal(t, perms.Permissions, rst[0].Permissions)
|
|
|
|
}
|
|
|
|
func TestDappPermissionsDeleted(t *testing.T) {
|
|
api, cancel := setupTestAPI(t)
|
|
defer cancel()
|
|
|
|
perms := DappPermissions{
|
|
Name: "first",
|
|
}
|
|
require.NoError(t, api.AddDappPermissions(context.TODO(), perms))
|
|
rst, err := api.GetDappPermissions(context.TODO())
|
|
require.NoError(t, err)
|
|
require.Len(t, rst, 1)
|
|
require.NoError(t, api.DeleteDappPermissions(context.TODO(), perms.Name))
|
|
rst, err = api.GetDappPermissions(context.TODO())
|
|
require.NoError(t, err)
|
|
require.Len(t, rst, 0)
|
|
}
|