Skip to content

Commit

Permalink
#1221 update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sosnovsky committed Dec 17, 2021
1 parent 27838c4 commit 51db16d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ final class ComposeMessageService {
logger.logDebug("validate recipients: \(recipients)")
logger.logDebug("validate recipient keyStates: \(recipients.map(\.keyState))")

let domainsWithPasswordSupport = ["gmail.com", "flowcrypt.com"] // TODO:
let domainsWithPasswordSupport = ["flowcrypt.com"]

guard withMessagePassword || !hasRecipientsWithoutPubKey(withPasswordSupport: true) else {
throw MessageValidationError.needsMessagePassword
Expand Down
12 changes: 8 additions & 4 deletions FlowCryptAppTests/Core/FlowCryptCoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ final class FlowCryptCoreTests: XCTestCase {
replyToMimeMsg: nil,
atts: [],
pubKeys: nil,
signingPrv: nil
signingPrv: nil,
password: nil
)
let r = try await core.composeEmail(msg: msg, fmt: .plain)
let mime = String(data: r.mimeEncoded, encoding: .utf8)!
Expand All @@ -135,7 +136,8 @@ final class FlowCryptCoreTests: XCTestCase {
replyToMimeMsg: nil,
atts: [],
pubKeys: [TestData.k0.pub, TestData.k1.pub],
signingPrv: nil
signingPrv: nil,
password: nil
)
let r = try await self.core.composeEmail(msg: msg, fmt: .encryptInline)
let mime = String(data: r.mimeEncoded, encoding: .utf8)!
Expand All @@ -161,7 +163,8 @@ final class FlowCryptCoreTests: XCTestCase {
subject: "subj", replyToMimeMsg: nil,
atts: [attachment],
pubKeys: [TestData.k0.pub, TestData.k1.pub],
signingPrv: nil
signingPrv: nil,
password: nil
)
let r = try await core.composeEmail(msg: msg, fmt: .encryptInline)
let mime = String(data: r.mimeEncoded, encoding: .utf8)!
Expand Down Expand Up @@ -191,7 +194,8 @@ final class FlowCryptCoreTests: XCTestCase {
replyToMimeMsg: nil,
atts: [],
pubKeys: [k.public],
signingPrv: nil
signingPrv: nil,
password: nil
)
let mime = try await core.composeEmail(msg: msg, fmt: .encryptInline)
let keys = [PrvKeyInfo(private: k.private!, longid: k.ids[0].longid, passphrase: passphrase, fingerprints: k.fingerprints)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ class ComposeMessageServiceTests: XCTestCase {
ComposeMessageRecipient(email: "[email protected]", state: recipientIdleState)
]
let validKeyDetails = EncryptedStorageMock.createFakeKeyDetails(expiration: nil)
let keypair = Keypair(
primaryFingerprint: "",
private: "",
public: "public key",
passphrase: nil,
source: "",
allFingerprints: [],
allLongids: []
)

var core = CoreComposeMessageMock()
var encryptedStorage = EncryptedStorageMock()
Expand Down Expand Up @@ -204,17 +213,7 @@ class ComposeMessageServiceTests: XCTestCase {
}

func testValidateMessageInputWithAllEmptyRecipientPubKeys() async {
encryptedStorage.getKeypairsResult = [
Keypair(
primaryFingerprint: "",
private: "",
public: "public key",
passphrase: nil,
source: "",
allFingerprints: [],
allLongids: []
)
]
encryptedStorage.getKeypairsResult = [keypair]
recipients.forEach { recipient in
contactsService.retrievePubKeysResult = { _ in
[]
Expand Down Expand Up @@ -242,17 +241,7 @@ class ComposeMessageServiceTests: XCTestCase {
let keyDetails = EncryptedStorageMock.createFakeKeyDetails(expiration: Int(Date().timeIntervalSince1970 - 60))
return CoreRes.ParseKeys(format: .armored, keyDetails: [keyDetails])
}
encryptedStorage.getKeypairsResult = [
Keypair(
primaryFingerprint: "",
private: "",
public: "public key",
passphrase: nil,
source: "",
allFingerprints: [],
allLongids: []
)
]
encryptedStorage.getKeypairsResult = [keypair]
recipients.forEach { recipient in
contactsService.retrievePubKeysResult = { _ in
["pubKey"]
Expand Down Expand Up @@ -280,17 +269,7 @@ class ComposeMessageServiceTests: XCTestCase {
let keyDetails = EncryptedStorageMock.createFakeKeyDetails(expiration: nil, revoked: true)
return CoreRes.ParseKeys(format: .armored, keyDetails: [keyDetails])
}
encryptedStorage.getKeypairsResult = [
Keypair(
primaryFingerprint: "",
private: "",
public: "public key",
passphrase: nil,
source: "",
allFingerprints: [],
allLongids: []
)
]
encryptedStorage.getKeypairsResult = [keypair]
recipients.forEach { recipient in
contactsService.retrievePubKeysResult = { _ in
["pubKey"]
Expand Down Expand Up @@ -330,17 +309,7 @@ class ComposeMessageServiceTests: XCTestCase {
}
return CoreRes.ParseKeys(format: .armored, keyDetails: allKeyDetails)
}
encryptedStorage.getKeypairsResult = [
Keypair(
primaryFingerprint: "",
private: "",
public: "public key",
passphrase: nil,
source: "",
allFingerprints: [],
allLongids: []
)
]
encryptedStorage.getKeypairsResult = [keypair]
recipients.forEach { recipient in
contactsService.retrievePubKeysResult = { _ in
["revoked", "expired", "valid"]
Expand Down Expand Up @@ -377,24 +346,15 @@ class ComposeMessageServiceTests: XCTestCase {
"valid",
"valid"
],
signingPrv: nil)
signingPrv: nil,
password: nil)

XCTAssertNotNil(result)
XCTAssertEqual(result, expected)
}

func testValidateMessageInputWithoutOneRecipientPubKey() async throws {
encryptedStorage.getKeypairsResult = [
Keypair(
primaryFingerprint: "",
private: "",
public: "public key",
passphrase: nil,
source: "",
allFingerprints: [],
allLongids: []
)
]
encryptedStorage.getKeypairsResult = [keypair]
let recWithoutPubKey = recipients[0].email
recipients.forEach { _ in
contactsService.retrievePubKeysResult = { recipient in
Expand Down Expand Up @@ -422,18 +382,64 @@ class ComposeMessageServiceTests: XCTestCase {
}
}

func testSuccessfulMessageValidation() async throws {
encryptedStorage.getKeypairsResult = [
Keypair(
primaryFingerprint: "",
private: "",
public: "public key",
passphrase: nil,
source: "",
allFingerprints: [],
allLongids: []
func testValidateMessageInputWithMessagePasswordSupport() async throws {
encryptedStorage.getKeypairsResult = [keypair]
contactsService.retrievePubKeysResult = { _ in return [] }

let message = "some message"
let subject = "Some subject"
let password = "123"
let email = "[email protected]"
let recipient = ComposeMessageRecipient(
email: "[email protected]",
state: recipientIdleState
)

do {
_ = try await sut.validateAndProduceSendableMsg(
input: ComposeMessageInput(type: .idle),
contextToSend: ComposeMessageContext(
message: message,
recipients: [recipient],
subject: subject
),
email: email,
signingPrv: nil
)
]
XCTFail("expected to throw above")
} catch {
XCTAssertEqual(error as? MessageValidationError, MessageValidationError.needsMessagePassword)
}

let result = try? await sut.validateAndProduceSendableMsg(
input: ComposeMessageInput(type: .idle),
contextToSend: ComposeMessageContext(
message: message,
recipients: [recipient],
subject: subject,
password: password
),
email: email,
signingPrv: nil
)
let expected = SendableMsg(
text: message,
to: [recipient.email],
cc: [],
bcc: [],
from: email,
subject: subject,
replyToMimeMsg: nil,
atts: [],
pubKeys: ["public key"],
signingPrv: nil,
password: password)

XCTAssertEqual(result, expected)
}

func testSuccessfulMessageValidation() async throws {
encryptedStorage.getKeypairsResult = [keypair]
recipients.enumerated().forEach { element, index in
contactsService.retrievePubKeysResult = { recipient in
["pubKey"]
Expand Down Expand Up @@ -470,7 +476,8 @@ class ComposeMessageServiceTests: XCTestCase {
"pubKey",
"pubKey"
],
signingPrv: nil)
signingPrv: nil,
password: nil)

XCTAssertNotNil(result)
XCTAssertEqual(result, expected)
Expand Down

0 comments on commit 51db16d

Please sign in to comment.