message.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. describe('MessageService', function() {
  2. let messageService;
  3. beforeEach(function() {
  4. // load threema services
  5. module('3ema.services');
  6. // Inject the MessageService
  7. inject(function(MessageService) {
  8. messageService = MessageService;
  9. });
  10. });
  11. describe ('getAccess', () => {
  12. let test = (m, r) => {
  13. return expect(messageService.getAccess(m, r));
  14. };
  15. it('invalid arguments', () => {
  16. test().toEqual(jasmine.objectContaining({
  17. quote: false,
  18. ack: false,
  19. dec: false,
  20. delete: false,
  21. download: false,
  22. copy: false,
  23. }));
  24. })
  25. const testCases = [['contact', {id: 'ECHOECHO', type: 'contact'}, true],
  26. ['gateway contact', {id: '*THREEMA', type: 'contact'}, true],
  27. ['group', {id: 1, type: 'group'}, false],
  28. ['distributionList', {id: 1, type: 'distributionList'}, false]]
  29. testCases.forEach((testData) => {
  30. let type = testData[0];
  31. let receiver = testData[1];
  32. let canAckDec = testData[2];
  33. describe(type, () => {
  34. it('text messages ', () => {
  35. test({isOutbox: false, type: 'text'}, receiver)
  36. .toEqual(jasmine.objectContaining({
  37. quote: true,
  38. ack: true && canAckDec,
  39. dec: true && canAckDec,
  40. delete: true,
  41. download: false,
  42. copy: false,
  43. }));
  44. test({isOutbox: true, type: 'text'}, receiver)
  45. .toEqual(jasmine.objectContaining({
  46. quote: true,
  47. ack: false,
  48. dec: false,
  49. delete: true,
  50. download: false,
  51. copy: false,
  52. }));
  53. test({isOutbox: false, type: 'text', state: 'user-ack'}, receiver)
  54. .toEqual(jasmine.objectContaining({
  55. quote: true,
  56. ack: false,
  57. dec: true && canAckDec,
  58. delete: true,
  59. download: false,
  60. copy: false,
  61. }));
  62. test({isOutbox: false, type: 'text', state: 'user-dec'}, receiver)
  63. .toEqual(jasmine.objectContaining({
  64. quote: true,
  65. ack: true && canAckDec,
  66. dec: false,
  67. delete: true,
  68. download: false,
  69. copy: false,
  70. }));
  71. });
  72. it('location messages ', () => {
  73. test({isOutbox: false, type: 'text'}, receiver)
  74. .toEqual(jasmine.objectContaining({
  75. quote: true,
  76. ack: true && canAckDec,
  77. dec: true && canAckDec,
  78. delete: true,
  79. download: false,
  80. copy: false,
  81. }));
  82. test({isOutbox: true, type: 'text'}, receiver)
  83. .toEqual(jasmine.objectContaining({
  84. quote: true,
  85. ack: false,
  86. dec: false,
  87. delete: true,
  88. download: false,
  89. copy: false,
  90. }));
  91. test({isOutbox: false, type: 'text', state: 'user-ack'}, receiver)
  92. .toEqual(jasmine.objectContaining({
  93. quote: true,
  94. ack: false,
  95. dec: true && canAckDec,
  96. delete: true,
  97. download: false,
  98. copy: false,
  99. }));
  100. test({isOutbox: false, type: 'text', state: 'user-dec'}, receiver)
  101. .toEqual(jasmine.objectContaining({
  102. quote: true,
  103. ack: true && canAckDec,
  104. dec: false,
  105. delete: true,
  106. download: false,
  107. copy: false,
  108. }));
  109. });
  110. ['image', 'video', 'audio', 'file'].forEach((type) => {
  111. it('inbox ' + type, () => {
  112. test({isOutbox: false, type: type}, receiver)
  113. .toEqual(jasmine.objectContaining({
  114. quote: false,
  115. ack: true && canAckDec,
  116. dec: true && canAckDec,
  117. delete: true,
  118. download: true,
  119. copy: false,
  120. }));
  121. });
  122. it('inbox (caption) ' + type, () => {
  123. test({isOutbox: false, type: type, caption: 'test'}, receiver)
  124. .toEqual(jasmine.objectContaining({
  125. quote: true,
  126. ack: true && canAckDec,
  127. dec: true && canAckDec,
  128. delete: true,
  129. download: true,
  130. copy: false,
  131. }));
  132. });
  133. it('outbox ' + type, () => {
  134. test({isOutbox: false, type: type}, receiver)
  135. .toEqual(jasmine.objectContaining({
  136. quote: false,
  137. ack: true && canAckDec,
  138. dec: true && canAckDec,
  139. delete: true,
  140. download: true,
  141. copy: false,
  142. }));
  143. });
  144. it('outbox (caption) ' + type, () => {
  145. test({isOutbox: false, type: type, caption: 'test'}, receiver)
  146. .toEqual(jasmine.objectContaining({
  147. quote: true,
  148. ack: true && canAckDec,
  149. dec: true && canAckDec,
  150. delete: true,
  151. download: true,
  152. copy: false,
  153. }));
  154. });
  155. });
  156. });
  157. });
  158. });
  159. describe ('showStatusIcon', () => {
  160. let test = (m, r) => {
  161. return expect(messageService.showStatusIcon(m, r));
  162. };
  163. it ('invalid arguments', () => {
  164. test(null, null).toEqual(false);
  165. test({}, null).toEqual(false);
  166. test(null, {}).toEqual(false);
  167. test({}, {}).toEqual(false);
  168. });
  169. let testAll = (receiver, outboxTrue, outboxFalse, inboxTrue, inboxFalse) => {
  170. let testDirection = (outbox, t, f) => {
  171. let testDirections = (states, toBe) => {
  172. states.forEach((state) => {
  173. it(state, () => {
  174. test({isOutbox: outbox, state: state}, receiver).toBe(toBe);
  175. });
  176. });
  177. };
  178. testDirections(t, true);
  179. testDirections(f, false);
  180. };
  181. describe ('outbox', () => {
  182. testDirection(true, outboxTrue, outboxFalse);
  183. });
  184. describe ('inbox', () => {
  185. testDirection(false, inboxTrue, inboxFalse);
  186. });
  187. };
  188. describe ('normal contact', () => {
  189. testAll({id: 'ECHOECHO', type: 'contact'},
  190. ['delivered', 'read', 'send-failed', 'sent', 'pending', 'sending', 'user-ack', 'user-dec'],
  191. [],
  192. ['user-ack', 'user-dec'],
  193. ['delivered', 'read', 'send-failed', 'sent', 'pending', 'sending']
  194. );
  195. });
  196. describe ('gateway contact', () => {
  197. testAll({id: '*THREEMA', type: 'contact'},
  198. ['send-failed', 'pending', 'sending'],
  199. ['delivered', 'read', 'sent', 'user-ack', 'user-dec'],
  200. ['user-ack', 'user-dec'],
  201. ['delivered', 'read', 'send-failed', 'sent', 'pending', 'sending']
  202. );
  203. });
  204. describe ('Group', () => {
  205. testAll({id: 1, type: 'group'},
  206. ['send-failed', 'pending', 'sending'],
  207. ['delivered', 'read', 'sent', 'user-ack', 'user-dec'],
  208. [],
  209. ['delivered', 'read', 'send-failed', 'sent', 'pending', 'sending', 'user-ack', 'user-dec']
  210. );
  211. });
  212. describe ('Distribution List', () => {
  213. testAll({id: 1, type: 'distributionList'},
  214. [],
  215. ['send-failed', 'pending', 'sending', 'delivered', 'read', 'sent', 'user-ack', 'user-dec'],
  216. [],
  217. ['delivered', 'read', 'send-failed', 'sent', 'pending', 'sending', 'user-ack', 'user-dec']
  218. );
  219. });
  220. });
  221. describe('getFileName', () => {
  222. it('image', () => {
  223. expect(messageService.getFileName({type: 'image', id: '123'}))
  224. .toEqual('image-123.jpg');
  225. });
  226. it('audio', () => {
  227. expect(messageService.getFileName({type: 'audio', id: '123'}))
  228. .toEqual('audio-123.mp4');
  229. });
  230. it('video', () => {
  231. expect(messageService.getFileName({type: 'video', id: '123'}))
  232. .toEqual('video-123.mpg');
  233. });
  234. it('file', () => {
  235. expect(messageService.getFileName({type: 'file', id: '123', file: {
  236. type: 'xyz',
  237. name: 'the-quick-fox.xyz'
  238. }})).toEqual('the-quick-fox.xyz');
  239. });
  240. it('file without file object', () => {
  241. expect(messageService.getFileName({type: 'file', id: '123'}))
  242. .toEqual('file-123');
  243. });
  244. it('text', () => {
  245. expect(messageService.getFileName({type: 'text', id: '123'}))
  246. .toEqual(null);
  247. });
  248. it('no file types', () => {
  249. ['text', 'location', 'status', 'ballot'].forEach((type) => {
  250. expect(messageService.getFileName({type: type, id: '123'}))
  251. .toEqual(null);
  252. });
  253. });
  254. it('no type', () => {
  255. expect(messageService.getFileName({id: 1}))
  256. .toEqual(null);
  257. });
  258. it('no id', () => {
  259. expect(messageService.getFileName({type: 'image'}))
  260. .toEqual(null);
  261. });
  262. it('no type', () => {
  263. expect(messageService.getFileName({id: 1}))
  264. .toEqual(null);
  265. });
  266. it('no message', () => {
  267. expect(messageService.getFileName(null))
  268. .toEqual(null);
  269. expect(messageService.getFileName())
  270. .toEqual(null);
  271. });
  272. });
  273. });