From e9c924b83b01a7d06b478146aaa79ec69b672e41 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Fri, 9 Jun 2023 16:56:56 +1000 Subject: [PATCH] fix message receiver disappearing messages config unit test --- Session.xcodeproj/project.pbxproj | 16 +- ...sageReceiverDisappearingMessagesSpec.swift | 127 -------------- .../MessagingKit/MessagingKitTestSpec.swift | 160 ++++++++++++++++++ 3 files changed, 172 insertions(+), 131 deletions(-) delete mode 100644 SessionMessagingKitTests/Sending & Receiving/MessageReceiverDisappearingMessagesSpec.swift create mode 100644 SessionTests/MessagingKit/MessagingKitTestSpec.swift diff --git a/Session.xcodeproj/project.pbxproj b/Session.xcodeproj/project.pbxproj index 1afac614d..0b948f3b9 100644 --- a/Session.xcodeproj/project.pbxproj +++ b/Session.xcodeproj/project.pbxproj @@ -115,7 +115,6 @@ 7B3A3930297A3919002FE4AC /* MediaInfoVC+MediaPreviewView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B3A392F297A3919002FE4AC /* MediaInfoVC+MediaPreviewView.swift */; }; 7B3A39322980D02B002FE4AC /* SessionCarouselView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B3A39312980D02B002FE4AC /* SessionCarouselView.swift */; }; 7B3A3934298882D6002FE4AC /* SessionCarouselViewDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B3A3933298882D6002FE4AC /* SessionCarouselViewDelegate.swift */; }; - 7B44B0702A305ED200175982 /* MessageReceiverDisappearingMessagesSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B44B06F2A305ED200175982 /* MessageReceiverDisappearingMessagesSpec.swift */; }; 7B46AAAF28766DF4001AF2DC /* AllMediaViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B46AAAE28766DF4001AF2DC /* AllMediaViewController.swift */; }; 7B4C75CB26B37E0F0000AC89 /* UnsendRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B4C75CA26B37E0F0000AC89 /* UnsendRequest.swift */; }; 7B4C75CD26BB92060000AC89 /* DeletedMessageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B4C75CC26BB92060000AC89 /* DeletedMessageView.swift */; }; @@ -124,6 +123,7 @@ 7B521E0A29BFF84400C3C36A /* GroupLeavingJob.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B521E0929BFF84400C3C36A /* GroupLeavingJob.swift */; }; 7B5233C42900E90F00F8F375 /* SessionLabelCarouselView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B5233C32900E90F00F8F375 /* SessionLabelCarouselView.swift */; }; 7B5233C6290636D700F8F375 /* _015_DisappearingMessagesConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B5233C5290636D700F8F375 /* _015_DisappearingMessagesConfiguration.swift */; }; + 7B6ACADE2A32D3A9009AFB73 /* MessagingKitTestSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B6ACADD2A32D3A9009AFB73 /* MessagingKitTestSpec.swift */; }; 7B7037432834B81F000DCF35 /* ReactionContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B7037422834B81F000DCF35 /* ReactionContainerView.swift */; }; 7B7037452834BCC0000DCF35 /* ReactionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B7037442834BCC0000DCF35 /* ReactionView.swift */; }; 7B71A98F2925E2A600E54854 /* SessionFooterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B71A98E2925E2A600E54854 /* SessionFooterView.swift */; }; @@ -1240,7 +1240,6 @@ 7B3A392F297A3919002FE4AC /* MediaInfoVC+MediaPreviewView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MediaInfoVC+MediaPreviewView.swift"; sourceTree = ""; }; 7B3A39312980D02B002FE4AC /* SessionCarouselView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SessionCarouselView.swift; sourceTree = ""; }; 7B3A3933298882D6002FE4AC /* SessionCarouselViewDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SessionCarouselViewDelegate.swift; sourceTree = ""; }; - 7B44B06F2A305ED200175982 /* MessageReceiverDisappearingMessagesSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageReceiverDisappearingMessagesSpec.swift; sourceTree = ""; }; 7B46AAAE28766DF4001AF2DC /* AllMediaViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AllMediaViewController.swift; sourceTree = ""; }; 7B4C75CA26B37E0F0000AC89 /* UnsendRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnsendRequest.swift; sourceTree = ""; }; 7B4C75CC26BB92060000AC89 /* DeletedMessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeletedMessageView.swift; sourceTree = ""; }; @@ -1249,6 +1248,7 @@ 7B521E0929BFF84400C3C36A /* GroupLeavingJob.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupLeavingJob.swift; sourceTree = ""; }; 7B5233C32900E90F00F8F375 /* SessionLabelCarouselView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SessionLabelCarouselView.swift; sourceTree = ""; }; 7B5233C5290636D700F8F375 /* _015_DisappearingMessagesConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = _015_DisappearingMessagesConfiguration.swift; sourceTree = ""; }; + 7B6ACADD2A32D3A9009AFB73 /* MessagingKitTestSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessagingKitTestSpec.swift; sourceTree = ""; }; 7B7037422834B81F000DCF35 /* ReactionContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReactionContainerView.swift; sourceTree = ""; }; 7B7037442834BCC0000DCF35 /* ReactionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReactionView.swift; sourceTree = ""; }; 7B71A98E2925E2A600E54854 /* SessionFooterView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SessionFooterView.swift; sourceTree = ""; }; @@ -2367,6 +2367,14 @@ path = "Emoji Picker"; sourceTree = ""; }; + 7B6ACADC2A32D38D009AFB73 /* MessagingKit */ = { + isa = PBXGroup; + children = ( + 7B6ACADD2A32D3A9009AFB73 /* MessagingKitTestSpec.swift */, + ); + path = MessagingKit; + sourceTree = ""; + }; 7B7CB18C270D06350079FF93 /* Views & Modals */ = { isa = PBXGroup; children = ( @@ -3907,7 +3915,6 @@ children = ( FD3C907027E445E500CD579F /* MessageReceiverDecryptionSpec.swift */, FD3C906C27E43C4B00CD579F /* MessageSenderEncryptionSpec.swift */, - 7B44B06F2A305ED200175982 /* MessageReceiverDisappearingMessagesSpec.swift */, ); path = "Sending & Receiving"; sourceTree = ""; @@ -3955,6 +3962,7 @@ FD71160A28D00BAE00B47552 /* SessionTests */ = { isa = PBXGroup; children = ( + 7B6ACADC2A32D38D009AFB73 /* MessagingKit */, FD71161228D00D5300B47552 /* Conversations */, FD71161828D00E0100B47552 /* Settings */, ); @@ -6165,6 +6173,7 @@ FD71161A28D00E1100B47552 /* NotificationContentViewModelSpec.swift in Sources */, FD23EA5C28ED00F80058676E /* Mock.swift in Sources */, FD23EA6128ED0B260058676E /* CombineExtensions.swift in Sources */, + 7B6ACADE2A32D3A9009AFB73 /* MessagingKitTestSpec.swift in Sources */, FD2AAAED28ED3E1000A49611 /* MockGeneralCache.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -6224,7 +6233,6 @@ FD3C906F27E43E8700CD579F /* MockBox.swift in Sources */, FDC4389A27BA002500C60D73 /* OpenGroupAPISpec.swift in Sources */, FD23EA6228ED0B260058676E /* CombineExtensions.swift in Sources */, - 7B44B0702A305ED200175982 /* MessageReceiverDisappearingMessagesSpec.swift in Sources */, FD83B9C527CF3E2A005E1583 /* OpenGroupSpec.swift in Sources */, FDC2908B27D707F3005DAE71 /* SendMessageRequestSpec.swift in Sources */, FD8ECF822934387A00C0D1BB /* ConfigUserProfileSpec.swift in Sources */, diff --git a/SessionMessagingKitTests/Sending & Receiving/MessageReceiverDisappearingMessagesSpec.swift b/SessionMessagingKitTests/Sending & Receiving/MessageReceiverDisappearingMessagesSpec.swift deleted file mode 100644 index f459e31bd..000000000 --- a/SessionMessagingKitTests/Sending & Receiving/MessageReceiverDisappearingMessagesSpec.swift +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved. - -import Foundation -import Sodium -import GRDB -import SessionUtilitiesKit - -import Quick -import Nimble - -@testable import SessionMessagingKit - -class MessageReceiverDisappearingMessagesSpec: QuickSpec { - // MARK: - Spec - - override func spec() { - var mockStorage: Storage! - var mockProto: SNProtoContent! - var mockMessage: VisibleMessage! - - describe("a MessageReceiver") { - beforeEach { - mockStorage = Storage( - customWriter: try! DatabaseQueue(), - customMigrations: [ - SNUtilitiesKit.migrations(), - SNMessagingKit.migrations(), - ] - ) - - let timestampMs: UInt64 = UInt64(Date().timeIntervalSince1970) - 10 * 60 * 1000 // 10 minutes ago - - let proto = SNProtoContent.builder() - proto.setExpirationType(.deleteAfterSend) - proto.setExpirationTimer(UInt32(DisappearingMessagesConfiguration.DefaultDuration.disappearAfterSend.seconds)) - proto.setLastDisappearingMessageChangeTimestamp(timestampMs) - mockProto = try? proto.build() - - mockMessage = VisibleMessage( - sender: "TestId", - sentTimestamp: timestampMs, - text: "Test" - ) - } - - afterEach { - mockStorage = nil - mockProto = nil - } - - // MARK: - Basic Tests - - it("Handle disappearing messages config update properly") { - let updatedConfig: DisappearingMessagesConfiguration? = mockStorage.write { db in - try MessageReceiver.handle( - db, - threadId: "TestId", - threadVariant: .contact, - message: mockMessage, - serverExpirationTimestamp: nil, - associatedWithProto: mockProto - ) - - return try? DisappearingMessagesConfiguration.fetchOne(db, id: "TestId") - } - - expect(updatedConfig?.isEnabled) - .toEventually( - beTrue(), - timeout: .milliseconds(100) - ) - expect(updatedConfig?.durationSeconds) - .toEventually( - equal(DisappearingMessagesConfiguration.DefaultDuration.disappearAfterSend.seconds), - timeout: .milliseconds(100) - ) - expect(updatedConfig?.type) - .toEventually( - equal(.disappearAfterSend), - timeout: .milliseconds(100) - ) - } - - it("Do NOT handle outdated disappearing messages config update") { - let config: DisappearingMessagesConfiguration = DisappearingMessagesConfiguration - .defaultWith("TestId") - .with( - isEnabled: false, - durationSeconds: 0, - type: .unknown, - lastChangeTimestampMs: Int64(Date().timeIntervalSince1970) - ) - - let updatedConfig: DisappearingMessagesConfiguration? = mockStorage.write { db in - try config.save(db) - - try MessageReceiver.handle( - db, - threadId: "TestId", - threadVariant: .contact, - message: mockMessage, - serverExpirationTimestamp: nil, - associatedWithProto: mockProto - ) - - return try? DisappearingMessagesConfiguration.fetchOne(db, id: "TestId") - } - - expect(updatedConfig?.isEnabled) - .toEventually( - beFalse(), - timeout: .milliseconds(100) - ) - expect(updatedConfig?.durationSeconds) - .toEventually( - equal(0), - timeout: .milliseconds(100) - ) - expect(updatedConfig?.type) - .toEventually( - equal(.unknown), - timeout: .milliseconds(100) - ) - } - } - } -} diff --git a/SessionTests/MessagingKit/MessagingKitTestSpec.swift b/SessionTests/MessagingKit/MessagingKitTestSpec.swift new file mode 100644 index 000000000..203f51fc1 --- /dev/null +++ b/SessionTests/MessagingKit/MessagingKitTestSpec.swift @@ -0,0 +1,160 @@ +// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved. + +import Combine +import GRDB +import Quick +import Nimble + +@testable import Session + +class MessageReceiverDisappearingMessagesSpec: QuickSpec { + // MARK: - Spec + + override func spec() { + var mockStorage: Storage! + var mockProto: SNProtoContent! + var mockMessage: VisibleMessage! + + describe("a MessageReceiver") { + beforeEach { + mockStorage = Storage( + customWriter: try! DatabaseQueue(), + customMigrations: [ + SNUtilitiesKit.migrations(), + SNSnodeKit.migrations(), + SNMessagingKit.migrations(), + SNUIKit.migrations() + ] + ) + + let timestampMs: UInt64 = UInt64(Date().timeIntervalSince1970) - 10 * 60 * 1000 // 10 minutes ago + + let proto = SNProtoContent.builder() + let dataMessage = SNProtoDataMessage.builder() + proto.setExpirationType(.deleteAfterSend) + proto.setExpirationTimer(UInt32(DisappearingMessagesConfiguration.DefaultDuration.disappearAfterSend.seconds)) + proto.setLastDisappearingMessageChangeTimestamp(timestampMs) + dataMessage.setBody("Test") + proto.setDataMessage(try! dataMessage.build()) + mockProto = try? proto.build() + + mockMessage = VisibleMessage( + sender: "TestId", + sentTimestamp: timestampMs, + recipient: getUserHexEncodedPublicKey(), + text: "Test" + ) + mockMessage.receivedTimestamp = UInt64(Date().timeIntervalSince1970) + + mockStorage.write { db in + try SessionThread.fetchOrCreate( + db, + id: "TestId", + variant: .contact, + shouldBeVisible: true + ) + } + } + + afterEach { + mockStorage = nil + mockProto = nil + } + + // MARK: - Basic Tests + + context("Receive a newer disappearing message config update") { + it("Update local config properly") { + mockStorage.write { db in + do { + try MessageReceiver.handle( + db, + threadId: "TestId", + threadVariant: .contact, + message: mockMessage, + serverExpirationTimestamp: nil, + associatedWithProto: mockProto + ) + } catch { + print(error) + } + + } + + let updatedConfig: DisappearingMessagesConfiguration? = mockStorage.read { db in + try DisappearingMessagesConfiguration.fetchOne(db, id: "TestId") + } + + expect(updatedConfig?.isEnabled) + .toEventually( + beTrue(), + timeout: .milliseconds(100) + ) + expect(updatedConfig?.durationSeconds) + .toEventually( + equal(DisappearingMessagesConfiguration.DefaultDuration.disappearAfterSend.seconds), + timeout: .milliseconds(100) + ) + expect(updatedConfig?.type) + .toEventually( + equal(.disappearAfterSend), + timeout: .milliseconds(100) + ) + } + } + + context("Receive a outdated disappearing message config update") { + it("Do NOT update local config") { + let config: DisappearingMessagesConfiguration = DisappearingMessagesConfiguration + .defaultWith("TestId") + .with( + isEnabled: false, + durationSeconds: 0, + type: .unknown, + lastChangeTimestampMs: Int64(Date().timeIntervalSince1970) + ) + + mockStorage.write { db in + try config.save(db) + } + + mockStorage.write { db in + do { + try MessageReceiver.handle( + db, + threadId: "TestId", + threadVariant: .contact, + message: mockMessage, + serverExpirationTimestamp: nil, + associatedWithProto: mockProto + ) + } catch { + print(error) + } + + } + + let updatedConfig: DisappearingMessagesConfiguration? = mockStorage.read { db in + try DisappearingMessagesConfiguration.fetchOne(db, id: "TestId") + } + + expect(updatedConfig?.isEnabled) + .toEventually( + beFalse(), + timeout: .milliseconds(100) + ) + expect(updatedConfig?.durationSeconds) + .toEventually( + equal(0), + timeout: .milliseconds(100) + ) + expect(updatedConfig?.type) + .toEventually( + equal(.unknown), + timeout: .milliseconds(100) + ) + } + } + } + } +}