123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- // _____ _
- // |_ _| |_ _ _ ___ ___ _ __ __ _
- // | | | ' \| '_/ -_) -_) ' \/ _` |_
- // |_| |_||_|_| \___\___|_|_|_\__,_(_)
- //
- // Threema iOS Client
- // Copyright (c) 2020 Threema GmbH
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Affero General Public License, version 3,
- // as published by the Free Software Foundation.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with this program. If not, see <https://www.gnu.org/licenses/>.
- import XCTest
- @testable import Threema
- class ThreemaPushNotificationTests: XCTestCase {
-
- func testCompleteDictionaryDecoding() throws {
- let from = "ABCDEFGH"
- let nick = "Hansmuster"
- let messageId = "0123456789abcdef"
-
- let payload = [
- "cmd": "newmsg",
- "from": from,
- "nick": nick,
- "messageId": messageId,
- "voip": "true",
- ]
-
- let threemaPushNotification = try ThreemaPushNotification(from: payload)
-
- XCTAssertEqual(threemaPushNotification.from, from)
- XCTAssertEqual(threemaPushNotification.nickname, nick)
- XCTAssertEqual(threemaPushNotification.messageId, messageId)
- let actualVoip = try XCTUnwrap(threemaPushNotification.voip)
- XCTAssertTrue(actualVoip)
- }
-
- // MARK: Command tests
-
- func testNewMessageCommandDictionaryDecoding() throws {
- let payload = [
- "cmd": "newmsg",
- "from": "ABCDEFGH",
- "nick": "Hansmuster",
- "messageId": "0123456789abcdef",
- "voip": "true",
- ]
-
- let threemaPushNotification = try ThreemaPushNotification(from: payload)
-
- XCTAssertEqual(threemaPushNotification.command, ThreemaPushNotification.Command.newMessage)
- }
-
- func testNewGroupMessageCommandDictionaryDecoding() throws {
- let payload = [
- "cmd": "newgroupmsg",
- "from": "ABCDEFGH",
- "nick": "Hansmuster",
- "messageId": "0123456789abcdef",
- "voip": "true",
- ]
-
- let threemaPushNotification = try ThreemaPushNotification(from: payload)
-
- XCTAssertEqual(threemaPushNotification.command, ThreemaPushNotification.Command.newGroupMessage)
- }
-
- // MARK: Voip tests
-
- func testMissingVoipDictionaryDecoding() throws {
- let payload = [
- "cmd": "newmsg",
- "from": "ABCDEFGH",
- "nick": "Hansmuster",
- "messageId": "0123456789abcdef",
- ]
-
- let threemaPushNotification = try ThreemaPushNotification(from: payload)
-
- XCTAssertNil(threemaPushNotification.voip)
- }
-
- func testFalseVoipStringDictionaryDecoding() throws {
- let payload = [
- "cmd": "newmsg",
- "from": "ABCDEFGH",
- "nick": "Hansmuster",
- "messageId": "0123456789abcdef",
- "voip": "false",
- ]
-
- let threemaPushNotification = try ThreemaPushNotification(from: payload)
-
- let actualVoip = try XCTUnwrap(threemaPushNotification.voip)
- XCTAssertFalse(actualVoip)
- }
-
- func testTrueVoipStringDictionaryDecoding() throws {
- let payload = [
- "cmd": "newmsg",
- "from": "ABCDEFGH",
- "nick": "Hansmuster",
- "messageId": "0123456789abcdef",
- "voip": "true",
- ]
-
- let threemaPushNotification = try ThreemaPushNotification(from: payload)
-
- let actualVoip = try XCTUnwrap(threemaPushNotification.voip)
- XCTAssertTrue(actualVoip)
- }
-
- func testTrueVoipDictionaryDecoding() throws {
- let payload: [String: Any] = [
- "cmd": "newmsg",
- "from": "ABCDEFGH",
- "nick": "Hansmuster",
- "messageId": "0123456789abcdef",
- "voip": true,
- ]
-
- let threemaPushNotification = try ThreemaPushNotification(from: payload)
-
- let actualVoip = try XCTUnwrap(threemaPushNotification.voip)
- XCTAssertTrue(actualVoip)
- }
-
- func testFalseVoipDictionaryDecoding() throws {
- let payload: [String: Any] = [
- "cmd": "newmsg",
- "from": "ABCDEFGH",
- "nick": "Hansmuster",
- "messageId": "0123456789abcdef",
- "voip": false,
- ]
-
- let threemaPushNotification = try ThreemaPushNotification(from: payload)
-
- let actualVoip = try XCTUnwrap(threemaPushNotification.voip)
- XCTAssertFalse(actualVoip)
- }
- // MARK: Missing nick test
-
- func testMissingNickDictionaryDecoding() throws {
- let payload = [
- "cmd": "newmsg",
- "from": "ABCDEFGH",
- "messageId": "0123456789abcdef",
- "voip": "true",
- ]
-
- let threemaPushNotification = try ThreemaPushNotification(from: payload)
-
- XCTAssertNil(threemaPushNotification.nickname)
- }
-
- // MARK: Failing payloads
-
- func testWrongCommandKeyDictionaryDecoding() throws {
- let payload = [
- "cmds": "newmsg",
- "from": "ABCDEFGH",
- "nick": "Hansmuster",
- "messageId": "0123456789abcdef",
- "voip": "true",
- ]
-
- XCTAssertThrowsError(try ThreemaPushNotification(from: payload)) { error in
- XCTAssertEqual(error as? ThreemaPushNotificationError, .keyNotFoundOrTypeMissmatch(ThreemaPushNotificationDictionary.commandKey))
- }
- }
-
- func testMissingCommandDictionaryDecoding() throws {
- let payload = [
- "from": "ABCDEFGH",
- "nick": "Hansmuster",
- "messageId": "0123456789abcdef",
- "voip": "true",
- ]
-
- XCTAssertThrowsError(try ThreemaPushNotification(from: payload)) { error in
- XCTAssertEqual(error as? ThreemaPushNotificationError, .keyNotFoundOrTypeMissmatch(ThreemaPushNotificationDictionary.commandKey))
- }
- }
-
- func testWrongCommandValueDictionaryDecoding() throws {
- let command = "foobar"
-
- let payload = [
- "cmd": command,
- "from": "ABCDEFGH",
- "nick": "Hansmuster",
- "messageId": "0123456789abcdef",
- "voip": "true",
- ]
-
- XCTAssertThrowsError(try ThreemaPushNotification(from: payload)) { error in
- XCTAssertEqual(error as? ThreemaPushNotificationError, .unknownCommand(command))
- }
- }
-
- func testMissingMessageIdDictionaryDecoding() throws {
- let payload = [
- "cmd": "newmsg",
- "from": "ABCDEFGH",
- "nick": "Hansmuster",
- "voip": "true",
- ]
-
- XCTAssertThrowsError(try ThreemaPushNotification(from: payload)) { error in
- XCTAssertEqual(error as? ThreemaPushNotificationError, .keyNotFoundOrTypeMissmatch(ThreemaPushNotificationDictionary.messageIdKey))
- }
- }
-
- }
|