Fix up more tests.

Update friend request status in message sender.
pull/175/head
Mikunj 5 years ago
parent 8fcb61353e
commit 25ae8ca3ba

@ -182,8 +182,7 @@ typedef void (^BuildOutgoingMessageCompletionBlock)(TSOutgoingMessage *savedMess
// Loki: If we're not friends then always set the message to a friend request message.
// If we're friends then the assumption is that we have the other user's pre key bundle.
BOOL isNoteToSelf = [LKSessionMetaProtocol isMessageNoteToSelf:thread];
NSString *messageClassAsString = (thread.isContactFriend || thread.isGroupThread || isNoteToSelf) ? @"TSOutgoingMessage" : @"LKFriendRequestMessage";
NSString *messageClassAsString = (thread.isContactFriend || thread.isGroupThread || thread.isNoteToSelf) ? @"TSOutgoingMessage" : @"LKFriendRequestMessage";
Class messageClass = NSClassFromString(messageClassAsString);
TSOutgoingMessage *message =

@ -105,7 +105,7 @@ public final class FriendRequestProtocol : NSObject {
// We sent a friend request to this device before, how can we be sure that it hasn't expired?
} else if friendRequestStatus == .none || friendRequestStatus == .requestExpired {
// TODO: Need to track these so that we can expire them and resend incase the other user wasn't online after we sent
MultiDeviceProtocol.getAutoGeneratedMultiDeviceFRMessageSend(for: thread.contactIdentifier(), in: transaction) // NOT hexEncodedPublicKey
MultiDeviceProtocol.getAutoGeneratedMultiDeviceFRMessageSend(for: device, in: transaction) // NOT hexEncodedPublicKey
.done(on: OWSDispatch.sendingQueue()) { autoGeneratedFRMessageSend in
let messageSender = SSKEnvironment.shared.messageSender
messageSender.sendMessage(autoGeneratedFRMessageSend)
@ -121,12 +121,7 @@ public final class FriendRequestProtocol : NSObject {
return;
}
// TODO: Should we create the threads here??
guard let thread = TSContactThread.getWithContactId(hexEncodedPublicKey, transaction: transaction) else {
print("[Loki] Not going to send friend request acceptance message because thread does not exist for \(hexEncodedPublicKey)")
return
}
let thread = TSContactThread.getOrCreateThread(withContactId: hexEncodedPublicKey, transaction: transaction)
let ephemeralMessage = EphemeralMessage(in: thread)
let messageSenderJobQueue = SSKEnvironment.shared.messageSenderJobQueue
messageSenderJobQueue.add(message: ephemeralMessage, transaction: transaction)
@ -152,6 +147,30 @@ public final class FriendRequestProtocol : NSObject {
}
}
@objc(sendingFriendRequestToHexEncodedPublicKey:transaction:)
public static func sendingFriendRequest(to hexEncodedPublicKey: String, transaction: YapDatabaseReadWriteTransaction) {
let friendRequestStatus = storage.getFriendRequestStatus(for: hexEncodedPublicKey, transaction: transaction)
if (friendRequestStatus == .none || friendRequestStatus == .requestExpired) {
storage.setFriendRequestStatus(.requestSending, for: hexEncodedPublicKey, transaction: transaction)
}
}
@objc(sentFriendRequestToHexEncodedPublicKey:transaction:)
public static func sentFriendRequest(to hexEncodedPublicKey: String, transaction: YapDatabaseReadWriteTransaction) {
let friendRequestStatus = storage.getFriendRequestStatus(for: hexEncodedPublicKey, transaction: transaction)
if (friendRequestStatus == .none || friendRequestStatus == .requestExpired || friendRequestStatus == .requestSending) {
storage.setFriendRequestStatus(.requestSent, for: hexEncodedPublicKey, transaction: transaction)
}
}
@objc(failedToSendFriendRequestToHexEncodedPublicKey:transaction:)
public static func failedToSendFriendRequest(to hexEncodedPublicKey: String, transaction: YapDatabaseReadWriteTransaction) {
let friendRequestStatus = storage.getFriendRequestStatus(for: hexEncodedPublicKey, transaction: transaction)
if (friendRequestStatus == .requestSending) {
storage.setFriendRequestStatus(.none, for: hexEncodedPublicKey, transaction: transaction)
}
}
// MARK: - Receiving
@objc(isFriendRequestFromBeforeRestoration:)
public static func isFriendRequestFromBeforeRestoration(_ envelope: SSKProtoEnvelope) -> Bool {

@ -7,6 +7,7 @@ import Curve25519Kit
class FriendRequestProtocolTests : XCTestCase {
private var storage: OWSPrimaryStorage { OWSPrimaryStorage.shared() }
private var messageSender: OWSFakeMessageSender { return MockSSKEnvironment.shared.messageSender as! OWSFakeMessageSender }
override func setUp() {
super.setUp()
@ -445,8 +446,6 @@ class FriendRequestProtocolTests : XCTestCase {
// MARK: - acceptFriendRequest
// TODO: Add test to see if message was sent?
func test_acceptFriendRequestShouldSetStatusToFriendsIfWeReceivedAFriendRequest() {
// Case: Bob sent us a friend request, we should become friends with him on accepting
let bob = generateHexEncodedPublicKey()
@ -460,7 +459,9 @@ class FriendRequestProtocolTests : XCTestCase {
}
}
func test_acceptFriendRequestShouldSendAMessageIfStatusIsNoneOrExpired() {
// TODO: Add test to see if an accept message is sent out
func test_acceptFriendRequestShouldSendAFriendRequestMessageIfStatusIsNoneOrExpired() {
// Case: Somehow our friend request status doesn't match the UI
// Since user accepted then we should send a friend request message
let statuses: [LKFriendRequestStatus] = [.none, .requestExpired]
@ -470,10 +471,25 @@ class FriendRequestProtocolTests : XCTestCase {
self.storage.setFriendRequestStatus(status, for: bob, transaction: transaction)
}
let expectation = self.expectation(description: "sent message")
messageSender.sendMessageWasCalledBlock = { [weak messageSender] sentMessage in
guard sentMessage is FriendRequestMessage else {
XCTFail("unexpected sentMessage: \(sentMessage)")
return
}
expectation.fulfill()
guard let strongMessageSender = messageSender else {
return
}
strongMessageSender.sendMessageWasCalledBlock = nil
}
storage.dbReadWriteConnection.readWrite { transaction in
FriendRequestProtocol.acceptFriendRequest(from: bob, using: transaction)
XCTAssertTrue(self.isFriendRequestStatus([.requestSending, .requestSent], for: bob, transaction: transaction))
}
self.wait(for: [expectation], timeout: 1.0)
}
}
@ -514,9 +530,15 @@ class FriendRequestProtocolTests : XCTestCase {
storage.dbReadWriteConnection.readWrite { transaction in
FriendRequestProtocol.acceptFriendRequest(from: master, using: transaction)
XCTAssertTrue(self.isFriendRequestStatus([.requestSending, .requestSent], for: master, transaction: transaction))
XCTAssertTrue(self.isFriendRequestStatus(.friends, for: slave, transaction: transaction))
XCTAssertTrue(self.isFriendRequestStatus(.requestSent, for: otherSlave, transaction: transaction))
}
eventually {
self.storage.dbReadWriteConnection.readWrite { transaction in
// TODO: Re-enable this case when we split friend request logic from OWSMessageSender
// XCTAssertTrue(self.isFriendRequestStatus([.requestSending, .requestSent], for: master, transaction: transaction))
XCTAssertTrue(self.isFriendRequestStatus(.friends, for: slave, transaction: transaction))
XCTAssertTrue(self.isFriendRequestStatus(.requestSent, for: otherSlave, transaction: transaction))
}
}
}

@ -175,29 +175,22 @@ public final class SyncMessagesProtocol : NSObject {
// TODO: Does the function below need to handle multi device??
// We need to send a FR message here directly to the user. Multi device doesn't come into play.
let autoGeneratedFRMessage = MultiDeviceProtocol.getAutoGeneratedMultiDeviceFRMessage(for: hexEncodedPublicKey, in: transaction)
thread.friendRequestStatus = .requestSending
thread.isForceHidden = true
thread.save(with: transaction)
storage.setFriendRequestStatus(.requestSending, for: hexEncodedPublicKey, transaction: transaction)
// This takes into account multi device
messageSender.send(autoGeneratedFRMessage, success: {
DispatchQueue.main.async {
storage.dbReadWriteConnection.readWrite { transaction in
autoGeneratedFRMessage.remove(with: transaction)
thread.friendRequestStatus = .requestSent
thread.isForceHidden = false
thread.save(with: transaction)
storage.setFriendRequestStatus(.requestSent, for: hexEncodedPublicKey, transaction: transaction)
}
}
}, failure: { error in
DispatchQueue.main.async {
storage.dbReadWriteConnection.readWrite { transaction in
storage.setFriendRequestStatus(friendRequestStatus, for: hexEncodedPublicKey, transaction: transaction)
autoGeneratedFRMessage.remove(with: transaction)
thread.friendRequestStatus = .none
thread.isForceHidden = false
thread.save(with: transaction)
}

File diff suppressed because it is too large Load Diff

@ -3,6 +3,7 @@
//
#import "OWSFakeMessageSender.h"
#import <SignalServiceKit/SignalServiceKit-Swift.h>
NS_ASSUME_NONNULL_BEGIN
@ -24,6 +25,18 @@ NS_ASSUME_NONNULL_BEGIN
}
}
- (void)sendMessage:(OWSMessageSend *)messageSend
{
if (self.stubbedFailingError) {
messageSend.failure(self.stubbedFailingError);
} else {
messageSend.success();
}
if (self.sendMessageWasCalledBlock) {
self.sendMessageWasCalledBlock(messageSend.message);
}
}
- (void)sendAttachment:(DataSource *)dataSource
contentType:(NSString *)contentType
sourceFilename:(nullable NSString *)sourceFilename

@ -0,0 +1,40 @@
import XCTest
extension XCTestCase {
/// Simple helper for asynchronous testing.
/// Usage in XCTestCase method:
/// func testSomething() {
/// doAsyncThings()
/// eventually {
/// /* XCTAssert goes here... */
/// }
/// }
/// Cloure won't execute until timeout is met. You need to pass in an
/// timeout long enough for your asynchronous process to finish, if it's
/// expected to take more than the default 0.1 second.
///
/// - Parameters:
/// - timeout: amout of time in seconds to wait before executing the
/// closure.
/// - closure: a closure to execute when `timeout` seconds has passed
func eventually(timeout: TimeInterval = 0.1, closure: @escaping () -> Void) {
let expectation = self.expectation(description: "")
expectation.fulfillAfter(timeout)
self.waitForExpectations(timeout: 60) { _ in
closure()
}
}
}
extension XCTestExpectation {
/// Call `fulfill()` after some time.
///
/// - Parameter time: amout of time after which `fulfill()` will be called.
func fulfillAfter(_ time: TimeInterval) {
DispatchQueue.main.asyncAfter(deadline: .now() + time) {
self.fulfill()
}
}
}
Loading…
Cancel
Save