ThreemaPushNotificationTests.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import XCTest
  21. @testable import Threema
  22. class ThreemaPushNotificationTests: XCTestCase {
  23. func testCompleteDictionaryDecoding() throws {
  24. let from = "ABCDEFGH"
  25. let nick = "Hansmuster"
  26. let messageId = "0123456789abcdef"
  27. let payload = [
  28. "cmd": "newmsg",
  29. "from": from,
  30. "nick": nick,
  31. "messageId": messageId,
  32. "voip": "true",
  33. ]
  34. let threemaPushNotification = try ThreemaPushNotification(from: payload)
  35. XCTAssertEqual(threemaPushNotification.from, from)
  36. XCTAssertEqual(threemaPushNotification.nickname, nick)
  37. XCTAssertEqual(threemaPushNotification.messageId, messageId)
  38. let actualVoip = try XCTUnwrap(threemaPushNotification.voip)
  39. XCTAssertTrue(actualVoip)
  40. }
  41. // MARK: Command tests
  42. func testNewMessageCommandDictionaryDecoding() throws {
  43. let payload = [
  44. "cmd": "newmsg",
  45. "from": "ABCDEFGH",
  46. "nick": "Hansmuster",
  47. "messageId": "0123456789abcdef",
  48. "voip": "true",
  49. ]
  50. let threemaPushNotification = try ThreemaPushNotification(from: payload)
  51. XCTAssertEqual(threemaPushNotification.command, ThreemaPushNotification.Command.newMessage)
  52. }
  53. func testNewGroupMessageCommandDictionaryDecoding() throws {
  54. let payload = [
  55. "cmd": "newgroupmsg",
  56. "from": "ABCDEFGH",
  57. "nick": "Hansmuster",
  58. "messageId": "0123456789abcdef",
  59. "voip": "true",
  60. ]
  61. let threemaPushNotification = try ThreemaPushNotification(from: payload)
  62. XCTAssertEqual(threemaPushNotification.command, ThreemaPushNotification.Command.newGroupMessage)
  63. }
  64. // MARK: Voip tests
  65. func testMissingVoipDictionaryDecoding() throws {
  66. let payload = [
  67. "cmd": "newmsg",
  68. "from": "ABCDEFGH",
  69. "nick": "Hansmuster",
  70. "messageId": "0123456789abcdef",
  71. ]
  72. let threemaPushNotification = try ThreemaPushNotification(from: payload)
  73. XCTAssertNil(threemaPushNotification.voip)
  74. }
  75. func testFalseVoipStringDictionaryDecoding() throws {
  76. let payload = [
  77. "cmd": "newmsg",
  78. "from": "ABCDEFGH",
  79. "nick": "Hansmuster",
  80. "messageId": "0123456789abcdef",
  81. "voip": "false",
  82. ]
  83. let threemaPushNotification = try ThreemaPushNotification(from: payload)
  84. let actualVoip = try XCTUnwrap(threemaPushNotification.voip)
  85. XCTAssertFalse(actualVoip)
  86. }
  87. func testTrueVoipStringDictionaryDecoding() throws {
  88. let payload = [
  89. "cmd": "newmsg",
  90. "from": "ABCDEFGH",
  91. "nick": "Hansmuster",
  92. "messageId": "0123456789abcdef",
  93. "voip": "true",
  94. ]
  95. let threemaPushNotification = try ThreemaPushNotification(from: payload)
  96. let actualVoip = try XCTUnwrap(threemaPushNotification.voip)
  97. XCTAssertTrue(actualVoip)
  98. }
  99. func testTrueVoipDictionaryDecoding() throws {
  100. let payload: [String: Any] = [
  101. "cmd": "newmsg",
  102. "from": "ABCDEFGH",
  103. "nick": "Hansmuster",
  104. "messageId": "0123456789abcdef",
  105. "voip": true,
  106. ]
  107. let threemaPushNotification = try ThreemaPushNotification(from: payload)
  108. let actualVoip = try XCTUnwrap(threemaPushNotification.voip)
  109. XCTAssertTrue(actualVoip)
  110. }
  111. func testFalseVoipDictionaryDecoding() throws {
  112. let payload: [String: Any] = [
  113. "cmd": "newmsg",
  114. "from": "ABCDEFGH",
  115. "nick": "Hansmuster",
  116. "messageId": "0123456789abcdef",
  117. "voip": false,
  118. ]
  119. let threemaPushNotification = try ThreemaPushNotification(from: payload)
  120. let actualVoip = try XCTUnwrap(threemaPushNotification.voip)
  121. XCTAssertFalse(actualVoip)
  122. }
  123. // MARK: Missing nick test
  124. func testMissingNickDictionaryDecoding() throws {
  125. let payload = [
  126. "cmd": "newmsg",
  127. "from": "ABCDEFGH",
  128. "messageId": "0123456789abcdef",
  129. "voip": "true",
  130. ]
  131. let threemaPushNotification = try ThreemaPushNotification(from: payload)
  132. XCTAssertNil(threemaPushNotification.nickname)
  133. }
  134. // MARK: Failing payloads
  135. func testWrongCommandKeyDictionaryDecoding() throws {
  136. let payload = [
  137. "cmds": "newmsg",
  138. "from": "ABCDEFGH",
  139. "nick": "Hansmuster",
  140. "messageId": "0123456789abcdef",
  141. "voip": "true",
  142. ]
  143. XCTAssertThrowsError(try ThreemaPushNotification(from: payload)) { error in
  144. XCTAssertEqual(error as? ThreemaPushNotificationError, .keyNotFoundOrTypeMissmatch(ThreemaPushNotificationDictionary.commandKey))
  145. }
  146. }
  147. func testMissingCommandDictionaryDecoding() throws {
  148. let payload = [
  149. "from": "ABCDEFGH",
  150. "nick": "Hansmuster",
  151. "messageId": "0123456789abcdef",
  152. "voip": "true",
  153. ]
  154. XCTAssertThrowsError(try ThreemaPushNotification(from: payload)) { error in
  155. XCTAssertEqual(error as? ThreemaPushNotificationError, .keyNotFoundOrTypeMissmatch(ThreemaPushNotificationDictionary.commandKey))
  156. }
  157. }
  158. func testWrongCommandValueDictionaryDecoding() throws {
  159. let command = "foobar"
  160. let payload = [
  161. "cmd": command,
  162. "from": "ABCDEFGH",
  163. "nick": "Hansmuster",
  164. "messageId": "0123456789abcdef",
  165. "voip": "true",
  166. ]
  167. XCTAssertThrowsError(try ThreemaPushNotification(from: payload)) { error in
  168. XCTAssertEqual(error as? ThreemaPushNotificationError, .unknownCommand(command))
  169. }
  170. }
  171. func testMissingMessageIdDictionaryDecoding() throws {
  172. let payload = [
  173. "cmd": "newmsg",
  174. "from": "ABCDEFGH",
  175. "nick": "Hansmuster",
  176. "voip": "true",
  177. ]
  178. XCTAssertThrowsError(try ThreemaPushNotification(from: payload)) { error in
  179. XCTAssertEqual(error as? ThreemaPushNotificationError, .keyNotFoundOrTypeMissmatch(ThreemaPushNotificationDictionary.messageIdKey))
  180. }
  181. }
  182. }