@ -279,6 +279,8 @@ class FriendRequestProtocolTests : XCTestCase {
// MARK: - a c c e p t F r i e n d R e q u e s t
// TODO: A d d t e s t t o s e e i f m e s s a g e w a s s e n t ?
func test_acceptFriendRequestShouldSetStatusToFriendsIfWeReceivedAFriendRequest ( ) {
// C a s e : B o b s e n t u s a f r i e n d r e q u e s t , w e s h o u l d b e c o m e f r i e n d s w i t h h i m o n a c c e p t i n g
let bob = Curve25519 . generateKeyPair ( ) . hexEncodedPublicKey
@ -368,4 +370,87 @@ class FriendRequestProtocolTests : XCTestCase {
XCTAssertTrue ( self . isFriendRequestStatus ( . requestReceived , for : bob , transaction : transaction ) )
}
}
// MARK: - d e c l i n e F r i e n d R e q u e s t
func test_declineFriendRequestShouldChangeStatusFromReceivedToNone ( ) {
let bob = generateHexEncodedPublicKey ( )
storage . dbReadWriteConnection . readWrite { transaction in
self . storage . setFriendRequestStatus ( . requestReceived , for : bob , transaction : transaction )
}
storage . dbReadWriteConnection . readWrite { transaction in
FriendRequestProtocol . declineFriendRequest ( from : bob , using : transaction )
XCTAssertTrue ( self . isFriendRequestStatus ( . none , for : bob , transaction : transaction ) )
}
}
func test_declineFriendRequestShouldNotChangeStatusToNoneFromOtherStatuses ( ) {
let otherStatuses : [ LKFriendRequestStatus ] = [ . none , . requestSending , . requestSent , . requestExpired , . friends ]
let bob = generateHexEncodedPublicKey ( )
for status in otherStatuses {
storage . dbReadWriteConnection . readWrite { transaction in
self . storage . setFriendRequestStatus ( status , for : bob , transaction : transaction )
}
storage . dbReadWriteConnection . readWrite { transaction in
FriendRequestProtocol . declineFriendRequest ( from : bob , using : transaction )
XCTAssertTrue ( self . isFriendRequestStatus ( status , for : bob , transaction : transaction ) )
}
}
}
func test_declineFriendRequestShouldDeletePreKeyBundleIfNeeded ( ) {
let shouldExpectDeletedPreKeyBundle = { ( status : LKFriendRequestStatus ) -> Bool in
return status = = . requestReceived
}
let statuses : [ LKFriendRequestStatus ] = [ . none , . requestSending , . requestSent , . requestReceived , . requestExpired , . friends ]
for status in statuses {
let bob = generateHexEncodedPublicKey ( )
let bundle = storage . generatePreKeyBundle ( forContact : bob )
storage . dbReadWriteConnection . readWrite { transaction in
self . storage . setPreKeyBundle ( bundle , forContact : bob , transaction : transaction )
self . storage . setFriendRequestStatus ( status , for : bob , transaction : transaction )
}
storage . dbReadWriteConnection . readWrite { transaction in
FriendRequestProtocol . declineFriendRequest ( from : bob , using : transaction )
}
let storedBundle = storage . getPreKeyBundle ( forContact : bob )
if ( shouldExpectDeletedPreKeyBundle ( status ) ) {
XCTAssertNil ( storedBundle , " Was expecting PreKeyBundle to be deleted for friend request status \( status . rawValue ) " )
} else {
XCTAssertNotNil ( storedBundle , " Was expecting PreKeyBundle to not be deleted for friend request status \( status . rawValue ) " )
}
}
}
func test_declineFriendRequestShouldWorkWithMultipleLinkedDevices ( ) {
// C a s e : B o b s e n d s 2 f r i e n d r e q u e s t s t o A l i c e .
// W h e n A l i c e d e c l i n e s , i t s h o u l d c h a n g e t h e s t a t u s e s f r o m r e q u e s t R e c e i v e d t o n o n e s o f r i e n d r e q u e s t l o g i c c a n b e r e - t r i g g e r e d .
let master = generateHexEncodedPublicKey ( )
let slave = generateHexEncodedPublicKey ( )
let otherSlave = generateHexEncodedPublicKey ( )
guard let masterDevice = getDevice ( for : master ) else { return XCTFail ( ) }
guard let slaveDevice = getDevice ( for : slave ) else { return XCTFail ( ) }
guard let otherSlaveDevice = getDevice ( for : otherSlave ) else { return XCTFail ( ) }
storage . dbReadWriteConnection . readWrite { transaction in
self . storage . addDeviceLink ( DeviceLink ( between : masterDevice , and : slaveDevice ) , in : transaction )
self . storage . addDeviceLink ( DeviceLink ( between : masterDevice , and : otherSlaveDevice ) , in : transaction )
self . storage . setFriendRequestStatus ( . requestSent , for : master , transaction : transaction )
self . storage . setFriendRequestStatus ( . requestReceived , for : slave , transaction : transaction )
self . storage . setFriendRequestStatus ( . requestReceived , for : otherSlave , transaction : transaction )
}
storage . dbReadWriteConnection . readWrite { transaction in
FriendRequestProtocol . declineFriendRequest ( from : master , using : transaction )
XCTAssertTrue ( self . isFriendRequestStatus ( . requestSent , for : master , transaction : transaction ) )
XCTAssertTrue ( self . isFriendRequestStatus ( . none , for : slave , transaction : transaction ) )
XCTAssertTrue ( self . isFriendRequestStatus ( . none , for : otherSlave , transaction : transaction ) )
}
}
}