You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChatRoom.spec.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. import { $pres } from 'strophe.js';
  2. import { XMPPEvents } from '../../service/xmpp/XMPPEvents';
  3. import ChatRoom, { parser } from './ChatRoom';
  4. // This rule makes creating the xml elements take up way more
  5. // space than necessary.
  6. /* eslint-disable newline-per-chained-call */
  7. // These rules makes the xml strings harder to read
  8. /* eslint-disable operator-linebreak, max-len */
  9. describe('ChatRoom', () => {
  10. describe('packet2JSON', () => {
  11. let nodes = [];
  12. beforeEach(() => {
  13. nodes = [];
  14. });
  15. it('translates attributes correctly', () => {
  16. const p = $pres({
  17. to: 'tojid',
  18. from: 'fromjid'
  19. })
  20. .c('fake-with-attr', {
  21. fakeAttr1: 'attrValue1',
  22. fakeAttr2: 'attrValue2'
  23. }).up();
  24. parser.packet2JSON(p.tree(), nodes);
  25. expect(nodes.length).toBe(1);
  26. const fakeWithAttr = nodes
  27. .find(n => n.tagName === 'fake-with-attr');
  28. expect(fakeWithAttr).toBeTruthy();
  29. expect(Object.keys(fakeWithAttr.attributes).length).toEqual(2);
  30. expect(fakeWithAttr.attributes.fakeAttr1).toBeTruthy();
  31. expect(fakeWithAttr.attributes.fakeAttr1).toEqual('attrValue1');
  32. expect(fakeWithAttr.attributes.fakeAttr2).toBeTruthy();
  33. expect(fakeWithAttr.attributes.fakeAttr2).toEqual('attrValue2');
  34. expect(fakeWithAttr.children.length).toEqual(0);
  35. expect(fakeWithAttr.value).toBeFalsy();
  36. });
  37. it('translates element text correctly', () => {
  38. const p = $pres({
  39. to: 'tojid',
  40. from: 'fromjid'
  41. })
  42. .c('element-name').t('element-name-text').up();
  43. parser.packet2JSON(p.tree(), nodes);
  44. expect(nodes.length).toBe(1);
  45. const elem = nodes.find(n => n.tagName === 'element-name');
  46. expect(elem).toBeTruthy();
  47. expect(Object.keys(elem.attributes).length).toEqual(0);
  48. expect(elem.children.length).toEqual(0);
  49. expect(elem.value).toEqual('element-name-text');
  50. });
  51. it('translates elements with children correctly', () => {
  52. const p = $pres({
  53. to: 'tojid',
  54. from: 'fromjid'
  55. })
  56. .c('identity')
  57. .c('user')
  58. .c('id').t('id-text').up()
  59. .c('name').t('name-text').up()
  60. .c('avatar').t('avatar-text').up()
  61. .up()
  62. .c('group').t('group-text').up()
  63. .up();
  64. parser.packet2JSON(p.tree(), nodes);
  65. const identity = nodes.find(n => n.tagName === 'identity');
  66. expect(identity).toBeTruthy();
  67. expect(Object.keys(identity.attributes).length).toEqual(0);
  68. expect(identity.children.length).toEqual(2);
  69. {
  70. const user = identity.children
  71. .find(n => n.tagName === 'user');
  72. expect(user).toBeTruthy();
  73. expect(Object.keys(user.attributes).length).toEqual(0);
  74. expect(user.children.length).toEqual(3);
  75. {
  76. const id = user.children
  77. .find(n => n.tagName === 'id');
  78. expect(id).toBeTruthy();
  79. expect(Object.keys(id.attributes).length).toEqual(0);
  80. expect(id.children.length).toEqual(0);
  81. expect(id.value).toEqual('id-text');
  82. }
  83. {
  84. const name = user.children
  85. .find(n => n.tagName === 'name');
  86. expect(name).toBeTruthy();
  87. expect(Object.keys(name.attributes).length).toEqual(0);
  88. expect(name.children.length).toEqual(0);
  89. expect(name.value).toEqual('name-text');
  90. }
  91. {
  92. const avatar = user.children
  93. .find(n => n.tagName === 'avatar');
  94. expect(avatar).toBeTruthy();
  95. expect(Object.keys(avatar.attributes).length).toEqual(0);
  96. expect(avatar.children.length).toEqual(0);
  97. expect(avatar.value).toEqual('avatar-text');
  98. }
  99. expect(user.value).toBeFalsy();
  100. }
  101. {
  102. const group = identity.children
  103. .find(n => n.tagName === 'group');
  104. expect(group).toBeTruthy();
  105. expect(Object.keys(group.attributes).length).toEqual(0);
  106. expect(group.children.length).toEqual(0);
  107. expect(group.value).toEqual('group-text');
  108. }
  109. expect(identity.value).toBeFalsy();
  110. });
  111. });
  112. describe('onPresence', () => {
  113. let room;
  114. let emitterSpy;
  115. beforeEach(() => {
  116. const xmpp = {
  117. options: {},
  118. addListener: () => {} // eslint-disable-line no-empty-function
  119. };
  120. room = new ChatRoom(
  121. {} /* connection */,
  122. 'jid',
  123. 'password',
  124. xmpp,
  125. {} /* options */);
  126. emitterSpy = spyOn(room.eventEmitter, 'emit');
  127. });
  128. it('parses status correctly', () => {
  129. const presStr = '' +
  130. '<presence to="tojid" from="fromjid">' +
  131. '<x xmlns=\'http://jabber.org/protocol/muc#user\'>' +
  132. '<item jid=\'fulljid\'/>' +
  133. '</x>' +
  134. '<status>status-text</status>' +
  135. '</presence>';
  136. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  137. room.onPresence(pres);
  138. expect(emitterSpy.calls.count()).toEqual(3);
  139. expect(emitterSpy.calls.argsFor(0)).toEqual([
  140. XMPPEvents.PRESENCE_RECEIVED,
  141. jasmine.any(Object)
  142. ]);
  143. expect(emitterSpy.calls.argsFor(1)).toEqual([
  144. XMPPEvents.MUC_JOIN_IN_PROGRESS
  145. ]);
  146. expect(emitterSpy.calls.argsFor(2)).toEqual([
  147. XMPPEvents.MUC_MEMBER_JOINED,
  148. 'fromjid',
  149. undefined, // nick
  150. null, // role
  151. false, // isHiddenDomain
  152. undefined, // statsID
  153. 'status-text',
  154. undefined,
  155. undefined,
  156. 'fulljid',
  157. undefined, // features
  158. 0 // isReplaceParticipant
  159. ]);
  160. });
  161. it('parses muc user item correctly', () => {
  162. const presStr = '' +
  163. '<presence to="tojid" from="fromjid">' +
  164. '<x xmlns="http://jabber.org/protocol/muc#user">' +
  165. '<item jid="jid=attr" affiliation="affiliation-attr" role="role-attr"/>' +
  166. '</x>' +
  167. '</presence>';
  168. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  169. room.onPresence(pres);
  170. expect(emitterSpy.calls.count()).toEqual(3);
  171. expect(emitterSpy.calls.argsFor(0)).toEqual([
  172. XMPPEvents.PRESENCE_RECEIVED,
  173. jasmine.any(Object)
  174. ]);
  175. expect(emitterSpy.calls.argsFor(1)).toEqual([
  176. XMPPEvents.MUC_JOIN_IN_PROGRESS
  177. ]);
  178. expect(emitterSpy).toHaveBeenCalledWith(
  179. XMPPEvents.MUC_MEMBER_JOINED,
  180. 'fromjid',
  181. undefined, // nick
  182. 'role-attr', // role
  183. jasmine.any(Boolean), // isHiddenDomain
  184. undefined, // statsID
  185. undefined,
  186. undefined,
  187. undefined,
  188. 'jid=attr',
  189. undefined, // features
  190. 0); // isReplaceParticipant
  191. });
  192. it('parses muc user replacing other user correctly', () => {
  193. const presStr = '' +
  194. '<presence to="tojid" from="fromjid">' +
  195. '<x xmlns="http://jabber.org/protocol/muc#user">' +
  196. '<item jid="jid=attr" affiliation="affiliation-attr" role="role-attr"/>' +
  197. '</x>' +
  198. '<flip_device />' +
  199. '</presence>';
  200. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  201. room.onPresence(pres);
  202. expect(emitterSpy.calls.count()).toEqual(3);
  203. expect(emitterSpy.calls.argsFor(0)).toEqual([
  204. XMPPEvents.PRESENCE_RECEIVED,
  205. jasmine.any(Object)
  206. ]);
  207. expect(emitterSpy.calls.argsFor(1)).toEqual([
  208. XMPPEvents.MUC_JOIN_IN_PROGRESS
  209. ]);
  210. expect(emitterSpy).toHaveBeenCalledWith(
  211. XMPPEvents.MUC_MEMBER_JOINED,
  212. 'fromjid',
  213. undefined, // nick
  214. 'role-attr', // role
  215. jasmine.any(Boolean), // isHiddenDomain
  216. undefined, // statsID
  217. undefined,
  218. undefined,
  219. undefined,
  220. 'jid=attr',
  221. undefined, // features
  222. 1); // isReplaceParticipant
  223. });
  224. it('parses identity correctly', () => {
  225. const presStr = '' +
  226. '<presence to="tojid" from="fromjid">' +
  227. '<x xmlns=\'http://jabber.org/protocol/muc#user\'>' +
  228. '<item jid=\'fulljid\'/>' +
  229. '</x>' +
  230. '<status>status-text</status>' +
  231. '<identity>' +
  232. '<user>' +
  233. '<id>id-text</id>' +
  234. '<name>name-text</name>' +
  235. '<avatar>avatar-text</avatar>' +
  236. '</user>' +
  237. '<group>group-text</group>' +
  238. '</identity>' +
  239. '</presence>';
  240. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  241. const expectedIdentity = {
  242. user: {
  243. id: 'id-text',
  244. name: 'name-text',
  245. avatar: 'avatar-text'
  246. },
  247. group: 'group-text'
  248. };
  249. room.onPresence(pres);
  250. expect(emitterSpy.calls.count()).toEqual(3);
  251. expect(emitterSpy.calls.argsFor(0)).toEqual([
  252. XMPPEvents.PRESENCE_RECEIVED,
  253. jasmine.any(Object)
  254. ]);
  255. expect(emitterSpy.calls.argsFor(1)).toEqual([
  256. XMPPEvents.MUC_JOIN_IN_PROGRESS
  257. ]);
  258. expect(emitterSpy.calls.argsFor(2)).toEqual([
  259. XMPPEvents.MUC_MEMBER_JOINED,
  260. 'fromjid',
  261. undefined, // nick
  262. null, // role
  263. false, // isHiddenDomain
  264. undefined, // statsID
  265. 'status-text',
  266. expectedIdentity,
  267. undefined,
  268. 'fulljid',
  269. undefined, // features
  270. 0 // isReplaceParticipant
  271. ]);
  272. });
  273. it('parses bot correctly', () => {
  274. const expectedBotType = 'some_bot_type';
  275. const presStr = '' +
  276. '<presence to="tojid" from="fromjid">' +
  277. '<x xmlns=\'http://jabber.org/protocol/muc#user\'>' +
  278. '<item jid=\'fulljid\'/>' +
  279. '</x>' +
  280. '<status>status-text</status>' +
  281. `<bot type="${expectedBotType}"/>` +
  282. '</presence>';
  283. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  284. room.onPresence(pres);
  285. expect(emitterSpy.calls.count()).toEqual(3);
  286. expect(emitterSpy.calls.argsFor(0)).toEqual([
  287. XMPPEvents.PRESENCE_RECEIVED,
  288. jasmine.any(Object)
  289. ]);
  290. expect(emitterSpy.calls.argsFor(1)).toEqual([
  291. XMPPEvents.MUC_JOIN_IN_PROGRESS
  292. ]);
  293. expect(emitterSpy.calls.argsFor(2)).toEqual([
  294. XMPPEvents.MUC_MEMBER_JOINED,
  295. 'fromjid',
  296. undefined, // nick
  297. null, // role
  298. false, // isHiddenDomain
  299. undefined, // statsID
  300. 'status-text',
  301. undefined,
  302. expectedBotType,
  303. 'fulljid',
  304. undefined, // features
  305. 0 // isReplaceParticipant
  306. ]);
  307. });
  308. });
  309. describe('sendMessage', () => {
  310. let room;
  311. let connectionSpy;
  312. beforeEach(() => {
  313. const xmpp = {
  314. options: {},
  315. addListener: () => {} // eslint-disable-line no-empty-function
  316. };
  317. room = new ChatRoom(
  318. // eslint-disable-next-line no-empty-function
  319. { send: () => {} } /* connection */,
  320. 'jid',
  321. 'password',
  322. xmpp,
  323. {} /* options */);
  324. connectionSpy = spyOn(room.connection, 'send');
  325. });
  326. it('sends a string msg with elementName body correctly', () => {
  327. room.sendMessage('string message', 'body', 'receiver');
  328. expect(connectionSpy.calls.argsFor(0).toString()).toBe(
  329. '<message to="jid" type="groupchat" xmlns="jabber:client">' +
  330. '<body>string message</body>' +
  331. '</message>');
  332. });
  333. it('sends a object msg with elementName body correctly', () => {
  334. room.sendMessage({ object: 'message' }, 'body', 'receiver');
  335. expect(connectionSpy.calls.argsFor(0).toString()).toBe(
  336. '<message to="jid" type="groupchat" xmlns="jabber:client">' +
  337. '<body object="message"/>' +
  338. '</message>');
  339. });
  340. it('sends a string msg with elementName json-message correctly', () => {
  341. room.sendMessage('string message', 'json-message', 'receiver');
  342. expect(connectionSpy.calls.argsFor(0).toString()).toBe(
  343. '<message to="jid" type="groupchat" xmlns="jabber:client">' +
  344. '<json-message xmlns="http://jitsi.org/jitmeet">string message</json-message>' +
  345. '</message>');
  346. });
  347. it('sends a object msg with elementName json-message correctly', () => {
  348. room.sendMessage({ object: 'message' }, 'json-message', 'receiver');
  349. expect(connectionSpy.calls.argsFor(0).toString()).toBe(
  350. '<message to="jid" type="groupchat" xmlns="jabber:client">' +
  351. '<json-message object="message" xmlns="http://jitsi.org/jitmeet"/>' +
  352. '</message>');
  353. });
  354. });
  355. });