Fix up migration.

Added migration test.
Fix some tests.
This commit is contained in:
Mikunj 2020-05-04 09:42:02 +10:00
parent 0d48031e9c
commit 45a71181d1
7 changed files with 98 additions and 31 deletions

View file

@ -8,6 +8,8 @@
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
2400888E239F30A600305217 /* SessionRestorationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2400888D239F30A600305217 /* SessionRestorationView.swift */; }; 2400888E239F30A600305217 /* SessionRestorationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2400888D239F30A600305217 /* SessionRestorationView.swift */; };
241C1192245F8878005CB2F4 /* LK001UpdateFriendRequestStatusStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 241C1191245F8878005CB2F4 /* LK001UpdateFriendRequestStatusStorage.swift */; };
241C1194245F8CE2005CB2F4 /* LK001UpdateFriendRequestStatusStorageTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 241C1193245F8C95005CB2F4 /* LK001UpdateFriendRequestStatusStorageTest.swift */; };
241C6314231F64C000B4198E /* JazzIcon.swift in Sources */ = {isa = PBXBuildFile; fileRef = 241C630E231F5AAC00B4198E /* JazzIcon.swift */; }; 241C6314231F64C000B4198E /* JazzIcon.swift in Sources */ = {isa = PBXBuildFile; fileRef = 241C630E231F5AAC00B4198E /* JazzIcon.swift */; };
241C6315231F64CE00B4198E /* CGFloat+Rounding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 241C6312231F5F1D00B4198E /* CGFloat+Rounding.swift */; }; 241C6315231F64CE00B4198E /* CGFloat+Rounding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 241C6312231F5F1D00B4198E /* CGFloat+Rounding.swift */; };
241C6316231F64CE00B4198E /* UIColor+Helper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 241C6310231F5C4400B4198E /* UIColor+Helper.swift */; }; 241C6316231F64CE00B4198E /* UIColor+Helper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 241C6310231F5C4400B4198E /* UIColor+Helper.swift */; };
@ -740,6 +742,8 @@
1C93CF3971B64E8B6C1F9AC1 /* Pods-SignalShareExtension.test.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SignalShareExtension.test.xcconfig"; path = "Pods/Target Support Files/Pods-SignalShareExtension/Pods-SignalShareExtension.test.xcconfig"; sourceTree = "<group>"; }; 1C93CF3971B64E8B6C1F9AC1 /* Pods-SignalShareExtension.test.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SignalShareExtension.test.xcconfig"; path = "Pods/Target Support Files/Pods-SignalShareExtension/Pods-SignalShareExtension.test.xcconfig"; sourceTree = "<group>"; };
1CE3CD5C23334683BDD3D78C /* Pods-Signal.test.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Signal.test.xcconfig"; path = "Pods/Target Support Files/Pods-Signal/Pods-Signal.test.xcconfig"; sourceTree = "<group>"; }; 1CE3CD5C23334683BDD3D78C /* Pods-Signal.test.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Signal.test.xcconfig"; path = "Pods/Target Support Files/Pods-Signal/Pods-Signal.test.xcconfig"; sourceTree = "<group>"; };
2400888D239F30A600305217 /* SessionRestorationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SessionRestorationView.swift; sourceTree = "<group>"; }; 2400888D239F30A600305217 /* SessionRestorationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SessionRestorationView.swift; sourceTree = "<group>"; };
241C1191245F8878005CB2F4 /* LK001UpdateFriendRequestStatusStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LK001UpdateFriendRequestStatusStorage.swift; sourceTree = "<group>"; };
241C1193245F8C95005CB2F4 /* LK001UpdateFriendRequestStatusStorageTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LK001UpdateFriendRequestStatusStorageTest.swift; sourceTree = "<group>"; };
241C630E231F5AAC00B4198E /* JazzIcon.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JazzIcon.swift; sourceTree = "<group>"; }; 241C630E231F5AAC00B4198E /* JazzIcon.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JazzIcon.swift; sourceTree = "<group>"; };
241C6310231F5C4400B4198E /* UIColor+Helper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIColor+Helper.swift"; sourceTree = "<group>"; }; 241C6310231F5C4400B4198E /* UIColor+Helper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIColor+Helper.swift"; sourceTree = "<group>"; };
241C6312231F5F1D00B4198E /* CGFloat+Rounding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CGFloat+Rounding.swift"; sourceTree = "<group>"; }; 241C6312231F5F1D00B4198E /* CGFloat+Rounding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CGFloat+Rounding.swift"; sourceTree = "<group>"; };
@ -1621,6 +1625,15 @@
/* End PBXFrameworksBuildPhase section */ /* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */ /* Begin PBXGroup section */
241C1190245F8765005CB2F4 /* Migrations */ = {
isa = PBXGroup;
children = (
241C1191245F8878005CB2F4 /* LK001UpdateFriendRequestStatusStorage.swift */,
241C1193245F8C95005CB2F4 /* LK001UpdateFriendRequestStatusStorageTest.swift */,
);
path = Migrations;
sourceTree = "<group>";
};
34074F54203D0722004596AE /* Sounds */ = { 34074F54203D0722004596AE /* Sounds */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
@ -2776,6 +2789,7 @@
B846365922B7417900AF1514 /* Loki */ = { B846365922B7417900AF1514 /* Loki */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
241C1190245F8765005CB2F4 /* Migrations */,
B8C9689223FA1B05005F64E0 /* Redesign */, B8C9689223FA1B05005F64E0 /* Redesign */,
B8544E3623D520F600299F14 /* Jazz Icon */, B8544E3623D520F600299F14 /* Jazz Icon */,
); );
@ -3970,6 +3984,7 @@
349EA07C2162AEA800F7B17F /* OWS111UDAttributesMigration.swift in Sources */, 349EA07C2162AEA800F7B17F /* OWS111UDAttributesMigration.swift in Sources */,
34480B561FD0A7A400BC14EF /* DebugLogger.m in Sources */, 34480B561FD0A7A400BC14EF /* DebugLogger.m in Sources */,
459B775C207BA46C0071D0AB /* OWSQuotedReplyModel.m in Sources */, 459B775C207BA46C0071D0AB /* OWSQuotedReplyModel.m in Sources */,
241C1192245F8878005CB2F4 /* LK001UpdateFriendRequestStatusStorage.swift in Sources */,
340872D622397E6800CB25B0 /* AttachmentCaptionToolbar.swift in Sources */, 340872D622397E6800CB25B0 /* AttachmentCaptionToolbar.swift in Sources */,
34ABB2C42090C59700C727A6 /* OWSResaveCollectionDBMigration.m in Sources */, 34ABB2C42090C59700C727A6 /* OWSResaveCollectionDBMigration.m in Sources */,
4C948FF72146EB4800349F0D /* BlockListCache.swift in Sources */, 4C948FF72146EB4800349F0D /* BlockListCache.swift in Sources */,
@ -4263,6 +4278,7 @@
files = ( files = (
456F6E2F1E261D1000FD2210 /* PeerConnectionClientTest.swift in Sources */, 456F6E2F1E261D1000FD2210 /* PeerConnectionClientTest.swift in Sources */,
3491D9A121022DB7001EF5A1 /* CDSSigningCertificateTest.m in Sources */, 3491D9A121022DB7001EF5A1 /* CDSSigningCertificateTest.m in Sources */,
241C1194245F8CE2005CB2F4 /* LK001UpdateFriendRequestStatusStorageTest.swift in Sources */,
34BBC861220E883300857249 /* ImageEditorModelTest.swift in Sources */, 34BBC861220E883300857249 /* ImageEditorModelTest.swift in Sources */,
340B02BA1FA0D6C700F9CFEC /* ConversationViewItemTest.m in Sources */, 340B02BA1FA0D6C700F9CFEC /* ConversationViewItemTest.m in Sources */,
458E383A1D6699FA0094BD24 /* OWSDeviceProvisioningURLParserTest.m in Sources */, 458E383A1D6699FA0094BD24 /* OWSDeviceProvisioningURLParserTest.m in Sources */,

View file

@ -0,0 +1,42 @@
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import Foundation
import SignalServiceKit
@objc
public class LK001UpdateFriendRequestStatusStorage: OWSDatabaseMigration {
// MARK: -
// Increment a similar constant for each migration.
// 100-114 are reserved for signal migrations
@objc
class func migrationId() -> String {
return "001"
}
override public func runUp(completion: @escaping OWSDatabaseMigrationCompletion) {
self.doMigrationAsync(completion: completion)
}
private func doMigrationAsync(completion: @escaping OWSDatabaseMigrationCompletion) {
DispatchQueue.global().async {
self.dbReadWriteConnection().readWrite { transaction in
guard let threads = TSThread.allObjectsInCollection() as? [TSThread] else {
owsFailDebug("Failed to convert objects to TSThread")
return
}
for thread in threads {
guard let thread = thread as? TSContactThread,
let friendRequestStatus = LKFriendRequestStatus(rawValue: thread.friendRequestStatus) else {
continue;
}
OWSPrimaryStorage.shared().setFriendRequestStatus(friendRequestStatus, for: thread.contactIdentifier(), transaction: transaction)
}
}
}
}
}

View file

@ -0,0 +1,28 @@
@testable import SignalServiceKit
import XCTest
import Curve25519Kit
class LK001UpdateFriendRequestStatusStorageTest : XCTestCase {
private var storage: OWSPrimaryStorage { OWSPrimaryStorage.shared() }
override func setUp() {
super.setUp()
// Activate the mock environment
ClearCurrentAppContextForTests()
SetCurrentAppContext(TestAppContext())
MockSSKEnvironment.activate()
// Register a mock user
let identityManager = OWSIdentityManager.shared()
let seed = Randomness.generateRandomBytes(16)!
let keyPair = Curve25519.generateKeyPair(fromSeed: seed + seed)
let databaseConnection = identityManager.value(forKey: "dbConnection") as! YapDatabaseConnection
databaseConnection.setObject(keyPair, forKey: OWSPrimaryStorageIdentityKeyStoreIdentityKey, inCollection: OWSPrimaryStorageIdentityKeyStoreCollection)
TSAccountManager.sharedInstance().phoneNumberAwaitingVerification = keyPair.hexEncodedPublicKey
TSAccountManager.sharedInstance().didRegister()
}
func test_shouldMigrateFriendRequestStatusCorrectly() {
}
}

View file

@ -101,10 +101,6 @@ NS_ASSUME_NONNULL_BEGIN
[self updatePublicChatMapping]; [self updatePublicChatMapping];
} }
if ([self isVersion:previousVersion lessThan:@"1.1.2"] && [self.tsAccountManager isRegistered]) {
[self updateFriendRequestStatusStorage];
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[[OWSDatabaseMigrationRunner alloc] init] runAllOutstandingWithCompletion:completion]; [[[OWSDatabaseMigrationRunner alloc] init] runAllOutstandingWithCompletion:completion];
}); });
@ -210,22 +206,6 @@ NS_ASSUME_NONNULL_BEGIN
}]; }];
} }
# pragma mark Loki - Upgrading Friend Request Status
// Versions less than or equal to 1.1.1 stored the friend request status on the thread
+ (void)updateFriendRequestStatusStorage
{
OWSPrimaryStorage *storage = OWSPrimaryStorage.sharedManager;
[OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
NSArray *threads = [TSThread allObjectsInCollection];
for (TSThread *thread in threads) {
if (thread.isGroupThread) { return; }
NSString *hexEncodedPublicKey = thread.contactIdentifier;
[storage setFriendRequestStatus:thread.friendRequestStatus forContact:hexEncodedPublicKey transaction:transaction];
}
}];
}
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

View file

@ -48,6 +48,7 @@ NS_ASSUME_NONNULL_BEGIN
[[OWS112TypingIndicatorsMigration alloc] init], [[OWS112TypingIndicatorsMigration alloc] init],
[[OWS113MultiAttachmentMediaMessages alloc] init], [[OWS113MultiAttachmentMediaMessages alloc] init],
[[OWS114RemoveDynamicInteractions alloc] init], [[OWS114RemoveDynamicInteractions alloc] init],
[[LK001UpdateFriendRequestStatusStorage alloc] init]
]; ];
} }

View file

@ -322,7 +322,7 @@ class FriendRequestProtocolTests : XCTestCase {
let allGroupTypes: [GroupType] = [.closedGroup, .openGroup, .rssFeed] let allGroupTypes: [GroupType] = [.closedGroup, .openGroup, .rssFeed]
for groupType in allGroupTypes { for groupType in allGroupTypes {
guard let groupThread = createGroupThread(groupType: groupType) else { return XCTFail() } guard let groupThread = createGroupThread(groupType: groupType) else { return XCTFail() }
XCTAssertTrue(FriendRequestProtocol.getFriendRequestUIState(for: groupThread) == .none) XCTAssertTrue(FriendRequestProtocol.getFriendRequestUIStatus(for: groupThread) == .none)
} }
} }
@ -343,8 +343,8 @@ class FriendRequestProtocolTests : XCTestCase {
let masterThread = createContactThread(for: master) let masterThread = createContactThread(for: master)
let slaveThread = createContactThread(for: slave) let slaveThread = createContactThread(for: slave)
XCTAssertTrue(FriendRequestProtocol.getFriendRequestUIState(for: masterThread) == .none) XCTAssertTrue(FriendRequestProtocol.getFriendRequestUIStatus(for: masterThread) == .none)
XCTAssertTrue(FriendRequestProtocol.getFriendRequestUIState(for: slaveThread) == .none ) XCTAssertTrue(FriendRequestProtocol.getFriendRequestUIStatus(for: slaveThread) == .none )
} }
func test_getFriendRequestUIStateShouldReturnTheCorrectStates() { func test_getFriendRequestUIStateShouldReturnTheCorrectStates() {
@ -364,7 +364,7 @@ class FriendRequestProtocolTests : XCTestCase {
storage.dbReadWriteConnection.readWrite { transaction in storage.dbReadWriteConnection.readWrite { transaction in
self.storage.setFriendRequestStatus(friendRequestStatus, for: bob, transaction: transaction) self.storage.setFriendRequestStatus(friendRequestStatus, for: bob, transaction: transaction)
} }
XCTAssertEqual(FriendRequestProtocol.getFriendRequestUIState(for: bobThread), uiState, "Expected FriendRequestUIState to be \(uiState)") XCTAssertEqual(FriendRequestProtocol.getFriendRequestUIStatus(for: bobThread), uiState, "Expected FriendRequestUIState to be \(uiState)")
} }
} }
@ -398,8 +398,8 @@ class FriendRequestProtocolTests : XCTestCase {
self.storage.setFriendRequestStatus(friendRequestStatus, for: slave, transaction: transaction) self.storage.setFriendRequestStatus(friendRequestStatus, for: slave, transaction: transaction)
} }
XCTAssertEqual(FriendRequestProtocol.getFriendRequestUIState(for: masterThread), uiState, "Expected FriendRequestUIState to be \(uiState.rawValue)") XCTAssertEqual(FriendRequestProtocol.getFriendRequestUIStatus(for: masterThread), uiState, "Expected FriendRequestUIState to be \(uiState.rawValue)")
XCTAssertEqual(FriendRequestProtocol.getFriendRequestUIState(for: slaveThread), uiState, "Expected FriendRequestUIState to be \(uiState.rawValue)") XCTAssertEqual(FriendRequestProtocol.getFriendRequestUIStatus(for: slaveThread), uiState, "Expected FriendRequestUIState to be \(uiState.rawValue)")
} }
} }
@ -420,7 +420,7 @@ class FriendRequestProtocolTests : XCTestCase {
self.storage.setFriendRequestStatus(.friends, for: slave, transaction: transaction) self.storage.setFriendRequestStatus(.friends, for: slave, transaction: transaction)
} }
XCTAssertTrue(FriendRequestProtocol.getFriendRequestUIState(for: masterThread) == .friends) XCTAssertTrue(FriendRequestProtocol.getFriendRequestUIStatus(for: masterThread) == .friends)
} }
func test_getFriendRequestUIStateShouldPreferReceivedOverSent() { func test_getFriendRequestUIStateShouldPreferReceivedOverSent() {
@ -441,7 +441,7 @@ class FriendRequestProtocolTests : XCTestCase {
self.storage.setFriendRequestStatus(.requestReceived, for: slave, transaction: transaction) self.storage.setFriendRequestStatus(.requestReceived, for: slave, transaction: transaction)
} }
XCTAssertTrue(FriendRequestProtocol.getFriendRequestUIState(for: masterThread) == .received) XCTAssertTrue(FriendRequestProtocol.getFriendRequestUIStatus(for: masterThread) == .received)
} }
// MARK: - acceptFriendRequest // MARK: - acceptFriendRequest

View file

@ -1187,7 +1187,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
} }
if (signalMessage.isFriendRequest) { if (signalMessage.isFriendRequest) {
[LKFriendRequestProtocol failedToSendFriendRequestToHexEncodedPublicKey:recipientID transaction:transaction]; [LKFriendRequestProtocol setFriendRequestStatusToSendingIfNeededForHexEncodedPublicKey:recipientID transaction:transaction];
} }
}]; }];
@ -1201,7 +1201,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
} }
if (signalMessage.isFriendRequest) { if (signalMessage.isFriendRequest) {
[LKFriendRequestProtocol sentFriendRequestToHexEncodedPublicKey:recipientID transaction:transaction]; [LKFriendRequestProtocol setFriendRequestStatusToFailedIfNeededForHexEncodedPublicKey:recipientID transaction:transaction];
} }
}]; }];
// Handle the error // Handle the error
@ -1233,7 +1233,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
NSDate *expirationDate = [[NSDate new] dateByAddingTimeInterval:expirationInterval]; NSDate *expirationDate = [[NSDate new] dateByAddingTimeInterval:expirationInterval];
[message saveFriendRequestExpiresAt:[NSDate ows_millisecondsSince1970ForDate:expirationDate] withTransaction:transaction]; [message saveFriendRequestExpiresAt:[NSDate ows_millisecondsSince1970ForDate:expirationDate] withTransaction:transaction];
} }
[LKFriendRequestProtocol sentFriendRequestToHexEncodedPublicKey:recipientID transaction:transaction]; [LKFriendRequestProtocol setFriendRequestStatusToSentIfNeededForHexEncodedPublicKey:recipientID transaction:transaction];
}]; }];
} }
// Invoke the completion handler // Invoke the completion handler