106 lines
2.4 KiB
Go
106 lines
2.4 KiB
Go
|
package commands
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestFailToRequestPermissionsWithMissingDAppFields(t *testing.T) {
|
||
|
cmd := &RequestPermissionsCommand{}
|
||
|
|
||
|
// Missing DApp fields
|
||
|
request, err := ConstructRPCRequest("wallet_requestPermissions", []interface{}{}, nil)
|
||
|
assert.NoError(t, err)
|
||
|
|
||
|
result, err := cmd.Execute(request)
|
||
|
assert.Equal(t, ErrRequestMissingDAppData, err)
|
||
|
assert.Empty(t, result)
|
||
|
}
|
||
|
|
||
|
func TestRequestPermissionsResponse(t *testing.T) {
|
||
|
cmd := &RequestPermissionsCommand{}
|
||
|
|
||
|
testCases := []struct {
|
||
|
name string
|
||
|
params []interface{}
|
||
|
expectedError error
|
||
|
expectedCapability string
|
||
|
}{
|
||
|
{
|
||
|
name: "Single valid key",
|
||
|
params: []interface{}{
|
||
|
map[string]interface{}{
|
||
|
"eth_requestAccounts": struct{}{},
|
||
|
},
|
||
|
},
|
||
|
expectedError: nil,
|
||
|
expectedCapability: "eth_requestAccounts",
|
||
|
},
|
||
|
{
|
||
|
name: "Single valid key",
|
||
|
params: []interface{}{
|
||
|
map[string]interface{}{
|
||
|
"eth_accounts": struct{}{},
|
||
|
},
|
||
|
},
|
||
|
expectedError: nil,
|
||
|
expectedCapability: "eth_accounts",
|
||
|
},
|
||
|
{
|
||
|
name: "Multiple keys",
|
||
|
params: []interface{}{
|
||
|
map[string]interface{}{
|
||
|
"eth_requestAccounts": struct{}{},
|
||
|
"eth_sendTransaction": struct{}{},
|
||
|
},
|
||
|
},
|
||
|
expectedError: ErrMultipleKeysFound,
|
||
|
expectedCapability: "",
|
||
|
},
|
||
|
{
|
||
|
name: "No keys",
|
||
|
params: []interface{}{
|
||
|
map[string]interface{}{},
|
||
|
},
|
||
|
expectedError: ErrNoRequestPermissionsParamsFound,
|
||
|
expectedCapability: "",
|
||
|
},
|
||
|
{
|
||
|
name: "Nil params",
|
||
|
params: nil,
|
||
|
expectedError: ErrEmptyRPCParams,
|
||
|
expectedCapability: "",
|
||
|
},
|
||
|
{
|
||
|
name: "Invalid param type",
|
||
|
params: []interface{}{
|
||
|
"invalid_param_type",
|
||
|
},
|
||
|
expectedError: ErrInvalidParamType,
|
||
|
expectedCapability: "",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for _, tc := range testCases {
|
||
|
t.Run(tc.name, func(t *testing.T) {
|
||
|
request, err := ConstructRPCRequest("wallet_requestPermissions", tc.params, &testDAppData)
|
||
|
assert.NoError(t, err)
|
||
|
|
||
|
response, err := cmd.Execute(request)
|
||
|
if tc.expectedError != nil {
|
||
|
assert.Error(t, err)
|
||
|
assert.Equal(t, err, tc.expectedError)
|
||
|
} else {
|
||
|
assert.NoError(t, err)
|
||
|
|
||
|
var permission Permission
|
||
|
err = json.Unmarshal([]byte(response), &permission)
|
||
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, permission.ParentCapability, tc.expectedCapability)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|