message.js 9.8 KB

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