Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ChatRoom.spec.js 15KB

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