message.js 12 KB

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