Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds more unit tests for WCPairing #437

Merged
merged 3 commits into from
Aug 19, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions Tests/WalletConnectSignTests/WCPairingTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import XCTest
import WalletConnectPairing
@testable import WalletConnectPairing
@testable import WalletConnectSign

final class WCPairingTests: XCTestCase {
Expand Down Expand Up @@ -35,18 +35,61 @@ final class WCPairingTests: XCTestCase {
XCTAssertEqual(pairing.expiryDate, inactiveExpiry)
}

func testUpdateExpiry() {
func testUpdateExpiryForTopic() {
var pairing = WCPairing(topic: "")
let activeExpiry = referenceDate.advanced(by: WCPairing.timeToLiveActive)
try? pairing.updateExpiry()
XCTAssertEqual(pairing.expiryDate, activeExpiry)
}

func testUpdateExpiryForUri() {
var pairing = WCPairing(uri: WalletConnectURI.stub())
let activeExpiry = referenceDate.advanced(by: WCPairing.timeToLiveActive)
try? pairing.updateExpiry()
XCTAssertEqual(pairing.expiryDate, activeExpiry)
}

func testActivate() {
func testActivateTopic() {
var pairing = WCPairing(topic: "")
let activeExpiry = referenceDate.advanced(by: WCPairing.timeToLiveActive)
XCTAssertFalse(pairing.active)
pairing.activate()
XCTAssertTrue(pairing.active)
XCTAssertEqual(pairing.expiryDate, activeExpiry)
}

func testActivateURI() {
var pairing = WCPairing(uri: WalletConnectURI.stub())
let activeExpiry = referenceDate.advanced(by: WCPairing.timeToLiveActive)
XCTAssertFalse(pairing.active)
pairing.activate()
XCTAssertTrue(pairing.active)
XCTAssertEqual(pairing.expiryDate, activeExpiry)
}

func testUpdateExpiry_WhenValueIsGreaterThanMax_ShouldThrowInvalidUpdateExpiryValue() {
var pairing = WCPairing(topic: "", relay: .stub(), peerMetadata: .stub(), expiryDate: referenceDate)
XCTAssertThrowsError(try pairing.updateExpiry(40 * .day)) { error in
XCTAssertEqual(error as! WCPairing.Errors, WCPairing.Errors.invalidUpdateExpiryValue)
}
}

func testUpdateExpiry_WhenNewExpiryDateIsLessThanExpiryDate_ShouldThrowInvalidUpdateExpiryValue() {
let expiryDate = referenceDate.advanced(by: 10 * .minute)
var pairing = WCPairing(topic: "", relay: .stub(), peerMetadata: .stub(), expiryDate: expiryDate)
XCTAssertThrowsError(try pairing.updateExpiry(40 * .day)) { error in

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expire date you set is 10 minutes into the future, the update value should be less than this, like the test says, but you're testing with a 40-day date (greater than max, like the test above).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ops! 😅

XCTAssertEqual(error as! WCPairing.Errors, WCPairing.Errors.invalidUpdateExpiryValue)
}
}

func testActivate_WhenUpdateExpiryFails_ShouldActivateAndThrowError() {
let expiryDate = referenceDate.advanced(by: 10 * .minute)
var pairing = WCPairing(topic: "", relay: .stub(), peerMetadata: .stub(), expiryDate: expiryDate)
XCTAssertFalse(pairing.active)
pairing.activate()
XCTAssertThrowsError(try pairing.updateExpiry(10 * .minute)) { error in

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

activate() makes a call to updateExpiry() internally (which sets a 30-day expiry).

Is there a reason to call activate before a throwing update? Does it make a difference for this test to invert the order?

Copy link
Contributor Author

@MaisaMilena MaisaMilena Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to check the error inside the activate() function, but if I understood it right, the error dies inside activate(). I changed the test, now checking for the expiryDate update. If an error occurs, it shouldn't be updated.

XCTAssertEqual(error as! WCPairing.Errors, WCPairing.Errors.invalidUpdateExpiryValue)
}
XCTAssertTrue(pairing.active)
}
}