2019-07-21 05:41:30 +00:00
|
|
|
package permissions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/appdatabase"
|
2023-08-11 11:28:45 +00:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
2019-07-21 05:41:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func setupTestDB(t *testing.T) (*Database, func()) {
|
2023-08-11 11:28:45 +00:00
|
|
|
db, cleanup, err := helpers.SetupTestSQLDB(appdatabase.DbInitializer{}, "perm-tests-")
|
2019-07-21 05:41:30 +00:00
|
|
|
require.NoError(t, err)
|
2023-08-11 11:28:45 +00:00
|
|
|
return NewDB(db), func() { require.NoError(t, cleanup()) }
|
2019-07-21 05:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setupTestAPI(t *testing.T) (*API, func()) {
|
|
|
|
db, cancel := setupTestDB(t)
|
2019-07-25 05:35:09 +00:00
|
|
|
return &API{db: db}, cancel
|
2019-07-21 05:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
})
|
2022-03-14 12:38:12 +00:00
|
|
|
require.Equal(t, expected[0].Name, rst[0].Name)
|
|
|
|
require.Equal(t, expected[0].Permissions, rst[0].Permissions)
|
2019-07-21 05:41:30 +00:00
|
|
|
|
|
|
|
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)
|
2022-03-14 12:38:12 +00:00
|
|
|
require.Equal(t, perms.Name, rst[0].Name)
|
|
|
|
require.Equal(t, perms.Permissions, rst[0].Permissions)
|
|
|
|
|
2019-07-21 05:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|