test.message.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. var async = require( 'async' );
  2. var parseString = require( 'xml2js' ).parseString;
  3. var fs = require( 'fs' );
  4. var should = require( 'should' );
  5. var testConfig = require( './config' );
  6. var rongSDK = require( '../index' );
  7. var base64Voice = new Buffer( fs.readFileSync( './testvoice.amr' ) ).toString( 'base64' );
  8. var base64Image = new Buffer( fs.readFileSync( './raindrop.jpg' ) ).toString( 'base64' );
  9. var optionalArgs = [ 'push content', 'push data' ];
  10. describe( 'Message Test', function() {
  11. before( function( done ) {
  12. // Init the SDK before testing.
  13. rongSDK.init( testConfig.appKey, testConfig.appSecret );
  14. done();
  15. } );
  16. after( function( done ) {
  17. done();
  18. } );
  19. describe( 'Publish Message', function() {
  20. it( 'Text message: should return OK', function( done ) {
  21. var textMessageObject = { content : testConfig.message.textMsg };
  22. // Testing for all the situations of arguments.
  23. var args = [ testConfig.message.fromUserId, testConfig.message.toUserId, 'RC:TxtMsg', JSON.stringify( textMessageObject ) ];
  24. var argsArray = [];
  25. for( var i=0; i<3; ++i ) {
  26. argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
  27. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
  28. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
  29. }
  30. async.each( argsArray, function( _args, cb ) {
  31. rongSDK.message.publish.apply( this, _args.concat( function( err, resultText ) {
  32. if( _args[ _args.length - 1 ] === 'xml' ) {
  33. should.not.exists( err );
  34. parseString( resultText, function( err, result ) {
  35. parseInt( result.xml.code[0] ).should.equal( 200 );
  36. cb();
  37. } );
  38. }
  39. else {
  40. should.not.exists( err );
  41. var result = JSON.parse( resultText );
  42. result.code.should.equal( 200 );
  43. cb();
  44. }
  45. } ) );
  46. }, function() {
  47. done();
  48. } );
  49. } );
  50. it( 'Image message: should return OK', function( done ) {
  51. var imageMessageObject = { content : base64Image, imageUrl : 'http://lanceju-com.qiniudn.com/raindrop.jpg' };
  52. var args = [ testConfig.message.fromUserId, testConfig.message.toUserId, 'RC:ImgMsg', JSON.stringify( imageMessageObject ) ];
  53. var argsArray = [];
  54. for( var i=0; i<3; ++i ) {
  55. argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
  56. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
  57. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
  58. }
  59. async.each( argsArray, function( _args, cb ) {
  60. rongSDK.message.publish.apply( this, _args.concat( function( err, resultText ) {
  61. if( _args[ _args.length - 1 ] === 'xml' ) {
  62. should.not.exists( err );
  63. parseString( resultText, function( err, result ) {
  64. parseInt( result.xml.code[0] ).should.equal( 200 );
  65. cb();
  66. } );
  67. }
  68. else {
  69. should.not.exists( err );
  70. var result = JSON.parse( resultText );
  71. result.code.should.equal( 200 );
  72. cb();
  73. }
  74. } ) );
  75. }, function() {
  76. done();
  77. } );
  78. } );
  79. it( 'Voice message: should return OK', function( done ) {
  80. var voiceMessageObject = { content : base64Voice, duration : 4 };
  81. var args = [testConfig.message.fromUserId, testConfig.message.toUserId, 'RC:VcMsg', JSON.stringify( voiceMessageObject ) ];
  82. var argsArray = [];
  83. for( var i=0; i<3; ++i ) {
  84. argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
  85. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
  86. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
  87. }
  88. async.each( argsArray, function( _args, cb ) {
  89. rongSDK.message.publish.apply( this, _args.concat( function( err, resultText ) {
  90. if( _args[ _args.length - 1 ] === 'xml' ) {
  91. should.not.exists( err );
  92. parseString( resultText, function( err, result ) {
  93. parseInt( result.xml.code[0] ).should.equal( 200 );
  94. cb();
  95. } );
  96. }
  97. else {
  98. should.not.exists( err );
  99. var result = JSON.parse( resultText );
  100. result.code.should.equal( 200 );
  101. cb();
  102. }
  103. } ) );
  104. }, function() {
  105. done();
  106. } );
  107. } );
  108. it( 'Image Text message: should return OK', function( done ) {
  109. var imageTextMessageObject = { title : 'hellotitle', content : 'hello', imageUrl : 'http://lanceju-com.qiniudn.com/raindrop.jpg', extra : 'image from a user' };
  110. var args = [testConfig.message.fromUserId, testConfig.message.toUserId, 'RC:ImgTextMsg', JSON.stringify( imageTextMessageObject ) ];
  111. var argsArray = [];
  112. for( var i=0; i<3; ++i ) {
  113. argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
  114. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
  115. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
  116. }
  117. async.each( argsArray, function( _args, cb ) {
  118. rongSDK.message.publish.apply( this, _args.concat( function( err, resultText ) {
  119. if( _args[ _args.length - 1 ] === 'xml' ) {
  120. should.not.exists( err );
  121. parseString( resultText, function( err, result ) {
  122. parseInt( result.xml.code[0] ).should.equal( 200 );
  123. cb();
  124. } );
  125. }
  126. else {
  127. should.not.exists( err );
  128. var result = JSON.parse( resultText );
  129. result.code.should.equal( 200 );
  130. cb();
  131. }
  132. } ) );
  133. }, function() {
  134. done();
  135. } );
  136. } );
  137. it( 'Location message: should return OK', function( done ) {
  138. var locationMessageObject = { content : 'You got a location message', latitude : 24.114, longitude : 334.221, poi : '北京市朝阳区北苑路北辰泰岳大厦', extra : 'The location of rong cloud' };
  139. var args = [testConfig.message.fromUserId, testConfig.message.toUserId, 'RC:LBSMsg', JSON.stringify( locationMessageObject ) ];
  140. var argsArray = [];
  141. for( var i=0; i<3; ++i ) {
  142. argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
  143. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
  144. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
  145. }
  146. async.each( argsArray, function( _args, cb ) {
  147. rongSDK.message.private.publish.apply( this, _args.concat( function( err, resultText ) {
  148. if( _args[ _args.length - 1 ] === 'xml' ) {
  149. should.not.exists( err );
  150. parseString( resultText, function( err, result ) {
  151. parseInt( result.xml.code[0] ).should.equal( 200 );
  152. cb();
  153. } );
  154. }
  155. else {
  156. should.not.exists( err );
  157. var result = JSON.parse( resultText );
  158. result.code.should.equal( 200 );
  159. cb();
  160. }
  161. } ) );
  162. }, function() {
  163. done();
  164. } );
  165. } );
  166. it( 'ContactNtf message: should return OK', function( done ) {
  167. var contactNtfMessageObject = { operation:"op1",sourceUserId:"24",targetUserId:"21",message:"haha",extra:"helloExtra"};
  168. var args = [testConfig.message.fromUserId, testConfig.message.toUserId, 'RC:ContactNtf', JSON.stringify( contactNtfMessageObject ) ];
  169. var argsArray = [];
  170. for( var i=0; i<3; ++i ) {
  171. argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
  172. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
  173. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
  174. }
  175. async.each( argsArray, function( _args, cb ) {
  176. rongSDK.message.private.publish.apply( this, _args.concat( function( err, resultText ) {
  177. if( _args[ _args.length - 1 ] === 'xml' ) {
  178. should.not.exists( err );
  179. parseString( resultText, function( err, result ) {
  180. parseInt( result.xml.code[0] ).should.equal( 200 );
  181. cb();
  182. } );
  183. }
  184. else {
  185. should.not.exists( err );
  186. var result = JSON.parse( resultText );
  187. result.code.should.equal( 200 );
  188. cb();
  189. }
  190. } ) );
  191. }, function() {
  192. done();
  193. } );
  194. } );
  195. } );
  196. describe( 'Publish Template Message', function() {
  197. it( 'Send template message: should return OK', function( done ) {
  198. var content = JSON.stringify( { content : "aa{c}{e}{d}", extra : "bb" } );
  199. var values = [ { "{c}":"1","{d}":"2","{e}":"3"} ];
  200. /*
  201. rongSDK.message.private.publish_template( testConfig.message.fromUserId, [testConfig.message.toUserId], 'RC:TxtMsg', content, values, [ 'push content for user' ], ['push data for user'], function( err, resultText ) {
  202. return done();
  203. should.not.exists( err );
  204. var result = JSON.parse( resultText );
  205. result.code.should.equal( 200 );
  206. done();
  207. } );
  208. */
  209. var args = [testConfig.message.fromUserId, [testConfig.message.toUserId], 'RC:TxtMsg', content, values, [ 'push content for user' ], ['push data for user'] ];
  210. var argsArray = [];
  211. argsArray.push( args.concat() );
  212. argsArray.push( args.concat( 'json' ) );
  213. argsArray.push( args.concat( 'xml' ) );
  214. async.each( argsArray, function( _args, cb ) {
  215. rongSDK.message.private.publish_template.apply( this, _args.concat( function( err, resultText ) {
  216. if( _args[ _args.length - 1 ] === 'xml' ) {
  217. should.not.exists( err );
  218. parseString( resultText, function( err, result ) {
  219. parseInt( result.xml.code[0] ).should.equal( 200 );
  220. cb();
  221. } );
  222. }
  223. else {
  224. should.not.exists( err );
  225. var result = JSON.parse( resultText );
  226. result.code.should.equal( 200 );
  227. cb();
  228. }
  229. } ) );
  230. }, function() {
  231. done();
  232. } );
  233. } );
  234. it( 'Send system message: should return OK', function( done ) {
  235. var args = [testConfig.message.fromUserId, [testConfig.message.toUserId], 'RC:TxtMsg', JSON.stringify( { content : 'Hello, world!' } )];
  236. var argsArray = [];
  237. for( var i=0; i<3; ++i ) {
  238. argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
  239. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
  240. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
  241. }
  242. async.each( argsArray, function( _args, cb ) {
  243. rongSDK.message.system.publish.apply( this, _args.concat( function( err, resultText ) {
  244. if( _args[ _args.length - 1 ] === 'xml' ) {
  245. should.not.exists( err );
  246. parseString( resultText, function( err, result ) {
  247. parseInt( result.xml.code[0] ).should.equal( 200 );
  248. cb();
  249. } );
  250. }
  251. else {
  252. should.not.exists( err );
  253. var result = JSON.parse( resultText );
  254. result.code.should.equal( 200 );
  255. cb();
  256. }
  257. } ) );
  258. }, function() {
  259. done();
  260. } );
  261. } );
  262. it( 'Send group message before joining the group: should return error', function( done ) {
  263. rongSDK.message.group.publish( testConfig.message.fromUserId, 'ProgrammerGroup1', 'RC:TxtMsg', JSON.stringify( { content : 'Hello, world!' } ), null, null, function( err, resultText ) {
  264. should.exists( err );
  265. // Joining the group
  266. rongSDK.group.join( testConfig.message.fromUserId, 'ProgrammerGroup1', 'Programmers group', function( err, resultText ) {
  267. should.not.exists( err );
  268. var result = JSON.parse( resultText );
  269. result.code.should.equal( 200 );
  270. done();
  271. } );
  272. } );
  273. } );
  274. it( 'Send group message after joining the group: should return OK', function( done ) {
  275. var args = [testConfig.message.fromUserId, 'ProgrammerGroup1', 'RC:TxtMsg', JSON.stringify( { content : 'Hello, world!' } )];
  276. var argsArray = [];
  277. for( var i=0; i<3; ++i ) {
  278. argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
  279. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
  280. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
  281. }
  282. async.each( argsArray, function( _args, cb ) {
  283. rongSDK.message.group.publish.apply( this, _args.concat( function( err, resultText ) {
  284. if( _args[ _args.length - 1 ] === 'xml' ) {
  285. should.not.exists( err );
  286. parseString( resultText, function( err, result ) {
  287. parseInt( result.xml.code[0] ).should.equal( 200 );
  288. cb();
  289. } );
  290. }
  291. else {
  292. should.not.exists( err );
  293. var result = JSON.parse( resultText );
  294. result.code.should.equal( 200 );
  295. cb();
  296. }
  297. } ) );
  298. }, function() {
  299. // Dismiss the group after testing.
  300. rongSDK.group.quit( testConfig.message.fromUserId, 'ProgrammerGroup1', function( err, resultText ) {
  301. should.not.exists( err );
  302. var result = JSON.parse( resultText );
  303. result.code.should.equal( 200 );
  304. done();
  305. } );
  306. } );
  307. } );
  308. it( 'Send chatroom message: should return OK', function( done ) {
  309. var args = [testConfig.message.fromUserId, 'my chatroom', 'RC:TxtMsg', JSON.stringify( { content : 'Hello, world!' } ) ];
  310. var argsArray = [];
  311. for( var i=0; i<3; ++i ) {
  312. argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
  313. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
  314. argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
  315. }
  316. async.each( argsArray, function( _args, cb ) {
  317. rongSDK.message.chatroom.publish.apply( this, _args.concat( function( err, resultText ) {
  318. if( _args[ _args.length - 1 ] === 'xml' ) {
  319. should.not.exists( err );
  320. parseString( resultText, function( err, result ) {
  321. parseInt( result.xml.code[0] ).should.equal( 200 );
  322. cb();
  323. } );
  324. }
  325. else {
  326. should.not.exists( err );
  327. var result = JSON.parse( resultText );
  328. result.code.should.equal( 200 );
  329. cb();
  330. }
  331. } ) );
  332. }, function() {
  333. done();
  334. } );
  335. } );
  336. } );
  337. // Since this API is a charing service, make sure your appKey&appSecret is from an charged account.
  338. // describe( 'Broadcast Message', function() {
  339. // it( 'Should return OK', function( done ) {
  340. // rongSDK.message.broadcast( testConfig.message.fromUserId, 'RC:TxtMsg', testConfig.message.textMsg, function( err, resultText ) {
  341. // should.not.exists( err );
  342. // var result = JSON.parse( resultText );
  343. // result.code.should.equal( 200 );
  344. // done();
  345. // } );
  346. // } );
  347. // } );
  348. } );