register.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. (function () {
  2. 'use strict';
  3. var app = null;
  4. QUnit.acceptance("Register", {
  5. beforeEach: function() {
  6. app = initTestMisago();
  7. app.settings.account_activation = 'none';
  8. app.context.USERS_API = '/test-api/users/';
  9. },
  10. afterEach: function() {
  11. app.destroy();
  12. }
  13. });
  14. QUnit.test('registration with empty credentials', function(assert) {
  15. var done = assert.async();
  16. app.modal('register');
  17. click('.modal-register .btn-primary');
  18. onElement('.alerts .alert-danger', function() {
  19. assert.equal(getAlertMessage(), "Form contains errors.",
  20. "empty registration form failed.");
  21. done();
  22. });
  23. });
  24. QUnit.test('registration with invalid credentials', function(assert) {
  25. var done = assert.async();
  26. app.modal('register');
  27. waitForElement('.modal-register');
  28. fillIn('#id_username', '###');
  29. fillIn('#id_email', 'notemail');
  30. fillIn('#id_password', 'nah');
  31. click('.modal-register .btn-primary');
  32. onElement('.alerts .alert-danger', function() {
  33. assert.equal(getAlertMessage(), "Form contains errors.",
  34. "invalid registration form failed.");
  35. assert.equal(
  36. getFieldFeedback('id_username'),
  37. "Username can only contain latin alphabet letters and digits.",
  38. "username field was validated.");
  39. assert.equal(
  40. getFieldFeedback('id_email'),
  41. "Enter a valid email address.",
  42. "email field was validated.");
  43. assert.equal(
  44. getFieldFeedback('id_password'),
  45. "Valid password must be at least 5 characters long.",
  46. "password field was validated.");
  47. done();
  48. });
  49. });
  50. QUnit.test('from banned ip', function(assert) {
  51. $.mockjax({
  52. url: '/test-api/users/',
  53. status: 403,
  54. responseText: {
  55. 'ban': {
  56. 'expires_on': null,
  57. 'message': {
  58. 'plain': 'Your ip is banned for spamming.',
  59. 'html': '<p>Your ip is banned for spamming.</p>',
  60. }
  61. }
  62. }
  63. });
  64. var done = assert.async();
  65. app.modal('register');
  66. waitForElement('.modal-register');
  67. fillIn('#id_username', 'bob');
  68. fillIn('#id_email', 'bob@boberson.com');
  69. fillIn('#id_password', 'Som3S3cureP4ss!!!');
  70. click('.modal-register .btn-primary');
  71. onElement('.page-error-banned .lead', function() {
  72. assert.equal(
  73. getElementText('.page .message-body .lead'),
  74. "Your ip is banned for spamming.",
  75. "login form displayed error banned page with ban message.");
  76. waitForElementRemoval('.modal-signin');
  77. then(function() {
  78. assert.ok(true, "signin modal was closed.");
  79. done();
  80. });
  81. });
  82. });
  83. QUnit.test('registration with backend-rejected credentials', function(assert) {
  84. $.mockjax({
  85. url: '/test-api/users/',
  86. status: 400,
  87. responseText: {
  88. 'username': [
  89. "That username is already taken."
  90. ]
  91. }
  92. });
  93. var done = assert.async();
  94. app.modal('register');
  95. waitForElement('.modal-register');
  96. fillIn('#id_username', 'bob');
  97. fillIn('#id_email', 'bob@boberson.com');
  98. fillIn('#id_password', 'Som3S3cureP4ss!!!');
  99. click('.modal-register .btn-primary');
  100. onElement('.alerts .alert-danger', function() {
  101. assert.equal(getAlertMessage(), "Form contains errors.",
  102. "invalid registration form was rejected.");
  103. assert.equal(
  104. getFieldFeedback('id_username'),
  105. "That username is already taken.",
  106. "username rejected by backend.");
  107. done();
  108. });
  109. });
  110. QUnit.test('registration passed with active user', function(assert) {
  111. $.mockjax({
  112. url: '/test-api/users/',
  113. status: 200,
  114. responseText: {
  115. 'activation': 'active',
  116. 'username': "Boberson",
  117. 'email': "bob@boberson.com",
  118. }
  119. });
  120. var done = assert.async();
  121. app.modal('register');
  122. waitForElement('.modal-register');
  123. fillIn('#id_username', 'bob');
  124. fillIn('#id_email', 'bob@boberson.com');
  125. fillIn('#id_password', 'Som3S3cureP4ss!!!');
  126. click('.modal-register .btn-primary');
  127. onElement('.modal-message', function() {
  128. app.runloop.stop('refresh-after-registration');
  129. assert.equal(
  130. getElementText('.modal-message .lead'),
  131. "Boberson, your account has been created and you were signed in.",
  132. "user has been registered and signed in successfully.");
  133. done();
  134. });
  135. });
  136. QUnit.test('registration passed with user activation', function(assert) {
  137. $.mockjax({
  138. url: '/test-api/users/',
  139. status: 200,
  140. responseText: {
  141. 'activation': 'user',
  142. 'username': "Boberson",
  143. 'email': "bob@boberson.com",
  144. }
  145. });
  146. var done = assert.async();
  147. app.modal('register');
  148. waitForElement('.modal-register');
  149. fillIn('#id_username', 'bob');
  150. fillIn('#id_email', 'bob@boberson.com');
  151. fillIn('#id_password', 'Som3S3cureP4ss!!!');
  152. click('.modal-register .btn-primary');
  153. onElement('.modal-message', function() {
  154. app.runloop.stop('refresh-after-registration');
  155. assert.equal(
  156. getElementText('.modal-message .lead'),
  157. "Boberson, your account has been created but you need to activate it before you will be able to sign in.",
  158. "user has been registered and signed in successfully.");
  159. done();
  160. });
  161. });
  162. QUnit.test('registration passed with admin activation', function(assert) {
  163. $.mockjax({
  164. url: '/test-api/users/',
  165. status: 200,
  166. responseText: {
  167. 'activation': 'admin',
  168. 'username': "Boberson",
  169. 'email': "bob@boberson.com",
  170. }
  171. });
  172. var done = assert.async();
  173. app.modal('register');
  174. waitForElement('.modal-register');
  175. fillIn('#id_username', 'bob');
  176. fillIn('#id_email', 'bob@boberson.com');
  177. fillIn('#id_password', 'Som3S3cureP4ss!!!');
  178. click('.modal-register .btn-primary');
  179. onElement('.modal-message', function() {
  180. app.runloop.stop('refresh-after-registration');
  181. assert.equal(
  182. getElementText('.modal-message .lead'),
  183. "Boberson, your account has been created but board administrator will have to activate it before you will be able to sign in.",
  184. "user has been registered and signed in successfully.");
  185. done();
  186. });
  187. });
  188. }());