Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ChatRoom.spec.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. ]);
  164. });
  165. it('parses muc user item correctly', () => {
  166. const presStr = '' +
  167. '<presence to="tojid" from="fromjid">' +
  168. '<x xmlns="http://jabber.org/protocol/muc#user">' +
  169. '<item jid="jid=attr" affiliation="affiliation-attr" role="role-attr"/>' +
  170. '</x>' +
  171. '</presence>';
  172. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  173. room.onPresence(pres);
  174. expect(emitterSpy.calls.count()).toEqual(3);
  175. expect(emitterSpy.calls.argsFor(0)).toEqual([
  176. XMPPEvents.PRESENCE_RECEIVED,
  177. jasmine.any(Object)
  178. ]);
  179. expect(emitterSpy.calls.argsFor(1)).toEqual([
  180. XMPPEvents.MUC_JOIN_IN_PROGRESS
  181. ]);
  182. expect(emitterSpy).toHaveBeenCalledWith(
  183. XMPPEvents.MUC_MEMBER_JOINED,
  184. 'fromjid',
  185. undefined, // nick
  186. 'role-attr', // role
  187. jasmine.any(Boolean), // isHiddenDomain
  188. undefined, // statsID
  189. undefined,
  190. undefined,
  191. undefined,
  192. 'jid=attr',
  193. undefined, // features
  194. 0); // isReplaceParticipant
  195. });
  196. it('parses muc user replacing other user correctly', () => {
  197. const presStr = '' +
  198. '<presence to="tojid" from="fromjid">' +
  199. '<x xmlns="http://jabber.org/protocol/muc#user">' +
  200. '<item jid="jid=attr" affiliation="affiliation-attr" role="role-attr"/>' +
  201. '</x>' +
  202. '<flip_device />' +
  203. '</presence>';
  204. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  205. room.onPresence(pres);
  206. expect(emitterSpy.calls.count()).toEqual(3);
  207. expect(emitterSpy.calls.argsFor(0)).toEqual([
  208. XMPPEvents.PRESENCE_RECEIVED,
  209. jasmine.any(Object)
  210. ]);
  211. expect(emitterSpy.calls.argsFor(1)).toEqual([
  212. XMPPEvents.MUC_JOIN_IN_PROGRESS
  213. ]);
  214. expect(emitterSpy).toHaveBeenCalledWith(
  215. XMPPEvents.MUC_MEMBER_JOINED,
  216. 'fromjid',
  217. undefined, // nick
  218. 'role-attr', // role
  219. jasmine.any(Boolean), // isHiddenDomain
  220. undefined, // statsID
  221. undefined,
  222. undefined,
  223. undefined,
  224. 'jid=attr',
  225. undefined, // features
  226. 1); // isReplaceParticipant
  227. });
  228. it('parses identity correctly', () => {
  229. const presStr = '' +
  230. '<presence to="tojid" from="fromjid">' +
  231. '<x xmlns=\'http://jabber.org/protocol/muc#user\'>' +
  232. '<item jid=\'fulljid\'/>' +
  233. '</x>' +
  234. '<status>status-text</status>' +
  235. '<identity>' +
  236. '<user>' +
  237. '<id>id-text</id>' +
  238. '<name>name-text</name>' +
  239. '<avatar>avatar-text</avatar>' +
  240. '</user>' +
  241. '<group>group-text</group>' +
  242. '</identity>' +
  243. '</presence>';
  244. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  245. const expectedIdentity = {
  246. user: {
  247. id: 'id-text',
  248. name: 'name-text',
  249. avatar: 'avatar-text'
  250. },
  251. group: 'group-text'
  252. };
  253. room.onPresence(pres);
  254. expect(emitterSpy.calls.count()).toEqual(3);
  255. expect(emitterSpy.calls.argsFor(0)).toEqual([
  256. XMPPEvents.PRESENCE_RECEIVED,
  257. jasmine.any(Object)
  258. ]);
  259. expect(emitterSpy.calls.argsFor(1)).toEqual([
  260. XMPPEvents.MUC_JOIN_IN_PROGRESS
  261. ]);
  262. expect(emitterSpy.calls.argsFor(2)).toEqual([
  263. XMPPEvents.MUC_MEMBER_JOINED,
  264. 'fromjid',
  265. undefined, // nick
  266. null, // role
  267. false, // isHiddenDomain
  268. undefined, // statsID
  269. 'status-text',
  270. expectedIdentity,
  271. undefined,
  272. 'fulljid',
  273. undefined, // features
  274. 0 // isReplaceParticipant
  275. ]);
  276. });
  277. it('parses bot correctly', () => {
  278. const expectedBotType = 'some_bot_type';
  279. const presStr = '' +
  280. '<presence to="tojid" from="fromjid">' +
  281. '<x xmlns=\'http://jabber.org/protocol/muc#user\'>' +
  282. '<item jid=\'fulljid\'/>' +
  283. '</x>' +
  284. '<status>status-text</status>' +
  285. `<bot type="${expectedBotType}"/>` +
  286. '</presence>';
  287. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  288. room.onPresence(pres);
  289. expect(emitterSpy.calls.count()).toEqual(3);
  290. expect(emitterSpy.calls.argsFor(0)).toEqual([
  291. XMPPEvents.PRESENCE_RECEIVED,
  292. jasmine.any(Object)
  293. ]);
  294. expect(emitterSpy.calls.argsFor(1)).toEqual([
  295. XMPPEvents.MUC_JOIN_IN_PROGRESS
  296. ]);
  297. expect(emitterSpy.calls.argsFor(2)).toEqual([
  298. XMPPEvents.MUC_MEMBER_JOINED,
  299. 'fromjid',
  300. undefined, // nick
  301. null, // role
  302. false, // isHiddenDomain
  303. undefined, // statsID
  304. 'status-text',
  305. undefined,
  306. expectedBotType,
  307. 'fulljid',
  308. undefined, // features
  309. 0 // isReplaceParticipant
  310. ]);
  311. });
  312. });
  313. describe('sendMessage', () => {
  314. let room;
  315. let connectionSpy;
  316. beforeEach(() => {
  317. const xmpp = {
  318. moderator: new Moderator({
  319. options: {}
  320. }),
  321. options: {},
  322. addListener: () => {} // eslint-disable-line no-empty-function
  323. };
  324. room = new ChatRoom(
  325. // eslint-disable-next-line no-empty-function
  326. { send: () => {} } /* connection */,
  327. 'jid',
  328. 'password',
  329. xmpp,
  330. {} /* options */);
  331. connectionSpy = spyOn(room.connection, 'send');
  332. });
  333. it('sends a string msg with elementName body correctly', () => {
  334. room.sendMessage('string message', 'body', 'receiver');
  335. expect(connectionSpy.calls.argsFor(0).toString()).toBe(
  336. '<message to="jid" type="groupchat" xmlns="jabber:client">' +
  337. '<body>string message</body>' +
  338. '</message>');
  339. });
  340. it('sends a object msg with elementName body correctly', () => {
  341. room.sendMessage({ object: 'message' }, 'body', 'receiver');
  342. expect(connectionSpy.calls.argsFor(0).toString()).toBe(
  343. '<message to="jid" type="groupchat" xmlns="jabber:client">' +
  344. '<body object="message"/>' +
  345. '</message>');
  346. });
  347. it('sends a string msg with elementName json-message correctly', () => {
  348. room.sendMessage('string message', 'json-message', 'receiver');
  349. expect(connectionSpy.calls.argsFor(0).toString()).toBe(
  350. '<message to="jid" type="groupchat" xmlns="jabber:client">' +
  351. '<json-message xmlns="http://jitsi.org/jitmeet">string message</json-message>' +
  352. '</message>');
  353. });
  354. it('sends a object msg with elementName json-message correctly', () => {
  355. room.sendMessage({ object: 'message' }, 'json-message', 'receiver');
  356. expect(connectionSpy.calls.argsFor(0).toString()).toBe(
  357. '<message to="jid" type="groupchat" xmlns="jabber:client">' +
  358. '<json-message object="message" xmlns="http://jitsi.org/jitmeet"/>' +
  359. '</message>');
  360. });
  361. });
  362. });