message.js 12 KB

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