-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add permission checks for requests * Add permission checks for event emissions * Add tests for basic permission validation * Fixed permission validation with test cases * Improve session permission tests Co-authored-by: André Vants
- Loading branch information
André Vants
committed
May 24, 2022
1 parent
bb010be
commit 35c827e
Showing
5 changed files
with
198 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import XCTest | ||
@testable import WalletConnectAuth | ||
|
||
final class WCSessionTests: XCTestCase { | ||
|
||
let ethAccount = Account("eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb")! | ||
let polyAccount = Account("eip155:137:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb")! | ||
let cosmosAccount = Account("cosmos:cosmoshub-4:cosmos1t2uflqwqe0fsj0shcfkrvpukewcw40yjj6hdc0")! | ||
|
||
func testHasPermissionForMethod() { | ||
let namespace = [ | ||
"eip155": SessionNamespace( | ||
accounts: [ethAccount], | ||
methods: ["method"], | ||
events: [], | ||
extensions: nil) | ||
] | ||
var session = WCSession.stub() | ||
session.updateNamespaces(namespace) | ||
XCTAssertTrue(session.hasPermission(forMethod: "method", onChain: ethAccount.blockchain)) | ||
} | ||
|
||
func testHasPermissionForMethodInExtension() { | ||
let namespace = [ | ||
"eip155": SessionNamespace( | ||
accounts: [ethAccount], | ||
methods: [], | ||
events: [], | ||
extensions: [ | ||
SessionNamespace.Extension( | ||
accounts: [ethAccount], | ||
methods: ["method"], | ||
events: [])]) | ||
] | ||
var session = WCSession.stub() | ||
session.updateNamespaces(namespace) | ||
XCTAssertTrue(session.hasPermission(forMethod: "method", onChain: ethAccount.blockchain)) | ||
} | ||
|
||
func testDenyPermissionForMethodInOtherChain() { | ||
let namespace = [ | ||
"eip155": SessionNamespace( | ||
accounts: [ethAccount], | ||
methods: [], | ||
events: [], | ||
extensions: nil), | ||
"cosmos": SessionNamespace( | ||
accounts: [cosmosAccount], | ||
methods: ["method"], | ||
events: [], | ||
extensions: nil), | ||
] | ||
var session = WCSession.stub() | ||
session.updateNamespaces(namespace) | ||
XCTAssertFalse(session.hasPermission(forMethod: "method", onChain: ethAccount.blockchain)) | ||
} | ||
|
||
func testDenyPermissionForMethodInOtherChainExtension() { | ||
let namespace = [ | ||
"eip155": SessionNamespace( | ||
accounts: [ethAccount, polyAccount], | ||
methods: [], | ||
events: [], | ||
extensions: [ | ||
SessionNamespace.Extension( | ||
accounts: [polyAccount], | ||
methods: ["method"], | ||
events: [])]) | ||
] | ||
var session = WCSession.stub() | ||
session.updateNamespaces(namespace) | ||
XCTAssertFalse(session.hasPermission(forMethod: "method", onChain: ethAccount.blockchain)) | ||
} | ||
|
||
func testHasPermissionForEvent() { | ||
let namespace = [ | ||
"eip155": SessionNamespace( | ||
accounts: [ethAccount], | ||
methods: [], | ||
events: ["event"], | ||
extensions: nil) | ||
] | ||
var session = WCSession.stub() | ||
session.updateNamespaces(namespace) | ||
XCTAssertTrue(session.hasPermission(forEvent: "event", onChain: ethAccount.blockchain)) | ||
} | ||
|
||
func testHasPermissionForEventInExtension() { | ||
let namespace = [ | ||
"eip155": SessionNamespace( | ||
accounts: [ethAccount], | ||
methods: [], | ||
events: [], | ||
extensions: [ | ||
SessionNamespace.Extension( | ||
accounts: [ethAccount], | ||
methods: [], | ||
events: ["event"])]) | ||
] | ||
var session = WCSession.stub() | ||
session.updateNamespaces(namespace) | ||
XCTAssertTrue(session.hasPermission(forEvent: "event", onChain: ethAccount.blockchain)) | ||
} | ||
|
||
func testDenyPermissionForEventInOtherChain() { | ||
let namespace = [ | ||
"eip155": SessionNamespace( | ||
accounts: [ethAccount], | ||
methods: [], | ||
events: [], | ||
extensions: nil), | ||
"cosmos": SessionNamespace( | ||
accounts: [cosmosAccount], | ||
methods: [], | ||
events: ["event"], | ||
extensions: nil), | ||
] | ||
var session = WCSession.stub() | ||
session.updateNamespaces(namespace) | ||
XCTAssertFalse(session.hasPermission(forEvent: "event", onChain: ethAccount.blockchain)) | ||
} | ||
|
||
func testDenyPermissionForEventInOtherChainExtension() { | ||
let namespace = [ | ||
"eip155": SessionNamespace( | ||
accounts: [ethAccount, polyAccount], | ||
methods: [], | ||
events: [], | ||
extensions: [ | ||
SessionNamespace.Extension( | ||
accounts: [polyAccount], | ||
methods: [], | ||
events: ["event"])]) | ||
] | ||
var session = WCSession.stub() | ||
session.updateNamespaces(namespace) | ||
XCTAssertFalse(session.hasPermission(forEvent: "event", onChain: ethAccount.blockchain)) | ||
} | ||
} |