test.user.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. var should = require( 'should' );
  2. var testConfig = require( './config' );
  3. var rongSDK = require( '../index' );
  4. describe( 'User Test', function() {
  5. before( function( done ) {
  6. // Init the SDK before testing.
  7. rongSDK.init( testConfig.appKey, testConfig.appSecret );
  8. done();
  9. } );
  10. after( function( done ) {
  11. done();
  12. } );
  13. describe( 'Get token', function() {
  14. it( 'Should return OK', function( done ) {
  15. rongSDK.user.getToken( testConfig.token.userId, testConfig.token.name, testConfig.token.portraitUri, function( err, resultText ) {
  16. should.not.exists( err );
  17. var result = JSON.parse( resultText );
  18. result.code.should.equal( 200 );
  19. done();
  20. } );
  21. } );
  22. } );
  23. describe( 'Refresh user info', function() {
  24. it( 'Should return OK', function( done ) {
  25. rongSDK.user.refresh( testConfig.token.userId, testConfig.token.name, testConfig.token.portraitUri, function( err, resultText ) {
  26. should.not.exists( err );
  27. var result = JSON.parse( resultText );
  28. result.code.should.equal( 200 );
  29. done();
  30. } );
  31. } );
  32. } );
  33. describe( 'Check user online status', function() {
  34. it( 'Check a non-existing user, should return OK', function( done ) {
  35. rongSDK.user.checkOnline( 'im not here', function( err, resultText ) {
  36. should.not.exists( err );
  37. var result = JSON.parse( resultText );
  38. result.code.should.equal( 200 );
  39. result.status.should.equal( '0' ); // Notice: the status is a string, not int.
  40. done();
  41. } );
  42. } );
  43. } );
  44. describe( 'Block user', function() {
  45. it( 'Block a user within 1 minute, should return OK', function( done ) {
  46. rongSDK.user.block( testConfig.token.userId, 1, function( err, resultText ) {
  47. should.not.exists( err );
  48. var result = JSON.parse( resultText );
  49. result.code.should.equal( 200 );
  50. // Check if the user is in the blocked list.
  51. rongSDK.user.block.query ( function( err, resultText ) {
  52. should.not.exists( err );
  53. var result = JSON.parse( resultText );
  54. result.code.should.equal( 200 );
  55. should.exists( result.users );
  56. var isUserBlocked = findUser( result.users, testConfig.token.userId );
  57. isUserBlocked.should.equal( true );
  58. done();
  59. } );
  60. } );
  61. } );
  62. } );
  63. describe( 'Unblock user', function() {
  64. it( 'Unblock the previously blocked user, should return OK', function( done ) {
  65. rongSDK.user.unblock( testConfig.token.userId, function( err, resultText ) {
  66. should.not.exists( err );
  67. var result = JSON.parse( resultText );
  68. result.code.should.equal( 200 );
  69. // Check if the user is in the blocked list.
  70. rongSDK.user.block.query( function( err, resultText ) {
  71. should.not.exists( err );
  72. var result = JSON.parse( resultText );
  73. result.code.should.equal( 200 );
  74. should.exists( result.users );
  75. var isUserBlocked = findUser( result.users, testConfig.token.userId );
  76. isUserBlocked.should.equal( false );
  77. done();
  78. } );
  79. } );
  80. } );
  81. } );
  82. describe( 'Query blocked users', function() {
  83. it( 'The test is obtained in the block/unblock user API tests', function( done ) {
  84. done();
  85. } );
  86. } );
  87. describe( 'Black list', function() {
  88. it( 'Add a user to the black list, should return OK', function( done ) {
  89. rongSDK.user.blacklist.add( testConfig.message.toUserId, testConfig.message.fromUserId, function( err, resultText ) {
  90. should.not.exists( err );
  91. var result = JSON.parse( resultText );
  92. result.code.should.equal( 200 );
  93. done();
  94. } );
  95. } );
  96. // TODO Send a message from fromUserId to toUserId
  97. it( 'Query a user\'s black list, should get the "Black User"', function( done ) {
  98. rongSDK.user.blacklist.query( testConfig.message.toUserId, function( err, resultText ) {
  99. should.not.exists( err );
  100. var result = JSON.parse( resultText );
  101. var isInBlacklist = findUserId( result.users, testConfig.message.fromUserId );
  102. isInBlacklist.should.equal( true );
  103. result.code.should.equal( 200 );
  104. done();
  105. } );
  106. } );
  107. it( 'Remove a user from the black list, should return OK', function( done ) {
  108. rongSDK.user.blacklist.remove( testConfig.message.toUserId, testConfig.message.fromUserId, function( err, resultText ) {
  109. should.not.exists( err );
  110. var result = JSON.parse( resultText );
  111. result.code.should.equal( 200 );
  112. done();
  113. } );
  114. } );
  115. // TODO Send a message from fromUserId to toUserId
  116. it( 'Query a user\'s black list, should get not the "Black User"', function( done ) {
  117. rongSDK.user.blacklist.query( testConfig.message.toUserId, function( err, resultText ) {
  118. should.not.exists( err );
  119. var result = JSON.parse( resultText );
  120. var isInBlacklist = findUserId( result.users, testConfig.message.fromUserId );
  121. isInBlacklist.should.equal( false );
  122. result.code.should.equal( 200 );
  123. done();
  124. } );
  125. } );
  126. } );
  127. } );
  128. function findUser( users, userId ) {
  129. var found = false;
  130. for( var i=0; i<users.length; ++i ) {
  131. if( users[i].userId === userId ) {
  132. found = true;
  133. break;
  134. }
  135. }
  136. return found;
  137. }
  138. function findUserId( userIDs, userId ) {
  139. var found = false;
  140. for( var i=0; i<userIDs.length; ++i ) {
  141. if( userIDs[i] === userId ) {
  142. found = true;
  143. break;
  144. }
  145. }
  146. return found;
  147. }