2024-08-06 19:28:55 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
persistence "github.com/status-im/status-go/services/connector/database"
|
|
|
|
"github.com/status-im/status-go/signal"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFailToRevokePermissionsWithMissingDAppFields(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_RevokePermissions)
|
|
|
|
t.Cleanup(close)
|
2024-08-06 19:28:55 +00:00
|
|
|
|
|
|
|
// Missing DApp fields
|
|
|
|
request, err := ConstructRPCRequest("wallet_revokePermissions", []interface{}{}, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
result, err := state.cmd.Execute(state.ctx, request)
|
2024-08-06 19:28:55 +00:00
|
|
|
assert.Equal(t, ErrRequestMissingDAppData, err)
|
|
|
|
assert.Empty(t, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailToRevokePermissionsForUnpermittedDApp(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_RevokePermissions)
|
|
|
|
t.Cleanup(close)
|
2024-08-06 19:28:55 +00:00
|
|
|
|
|
|
|
request, err := ConstructRPCRequest("wallet_revokePermissions", []interface{}{}, &testDAppData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
result, err := state.cmd.Execute(state.ctx, request)
|
2024-08-06 19:28:55 +00:00
|
|
|
assert.Equal(t, ErrDAppIsNotPermittedByUser, err)
|
|
|
|
assert.Empty(t, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRevokePermissionsSucceeded(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_RevokePermissions)
|
|
|
|
t.Cleanup(close)
|
2024-08-06 19:28:55 +00:00
|
|
|
|
|
|
|
sharedAccount := types.BytesToAddress(types.FromHex("0x6d0aa2a774b74bb1d36f97700315adf962c69fcg"))
|
|
|
|
dAppPermissionRevoked := false
|
|
|
|
|
|
|
|
signal.SetMobileSignalHandler(signal.MobileSignalHandler(func(s []byte) {
|
|
|
|
var evt EventType
|
|
|
|
err := json.Unmarshal(s, &evt)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
switch evt.Type {
|
|
|
|
case signal.EventConnectorDAppPermissionRevoked:
|
|
|
|
dAppPermissionRevoked = true
|
|
|
|
}
|
|
|
|
}))
|
2024-09-03 08:41:56 +00:00
|
|
|
t.Cleanup(signal.ResetMobileSignalHandler)
|
2024-08-06 19:28:55 +00:00
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
err := PersistDAppData(state.walletDb, testDAppData, sharedAccount, 0x123)
|
2024-08-06 19:28:55 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
request, err := ConstructRPCRequest("wallet_revokePermissions", []interface{}{}, &testDAppData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
result, err := state.cmd.Execute(state.ctx, request)
|
2024-08-06 19:28:55 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, result)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
dApp, err := persistence.SelectDAppByUrl(state.walletDb, testDAppData.URL)
|
2024-08-06 19:28:55 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, dApp)
|
|
|
|
|
|
|
|
assert.True(t, dAppPermissionRevoked)
|
|
|
|
}
|