PageRenderTime 34ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/www/lib/auth0-lock/test/reset.tests.js

https://gitlab.com/nioto/michel-app
JavaScript | 378 lines | 296 code | 72 blank | 10 comment | 0 complexity | 2fac9118dd8b040ff8f09ecfdef8716f MD5 | raw file
  1. /**
  2. * Mocha config
  3. */
  4. mocha.timeout(60000);
  5. mocha.ui('bdd');
  6. mocha.reporter('html');
  7. mocha.globals(['jQuery*', '__widgetjp*', 'Auth0*']);
  8. /**
  9. * Test reset
  10. */
  11. describe('reset', function () {
  12. beforeEach(function (done) {
  13. this.widget = new Auth0Lock('0HP71GSd6PuoRYJ3DXKdiXCUUdGmBbup', 'mdocs.auth0.com');
  14. this.client = this.widget.$auth0;
  15. this.options = {};
  16. done();
  17. });
  18. afterEach(function (done) {
  19. global.window.location.hash = '';
  20. global.window.Auth0 = null;
  21. this.options = null;
  22. this.widget.hide(done);
  23. });
  24. describe('Reset widget', function() {
  25. it('should show the loading pane on submit', function (done) {
  26. var widget = this.widget;
  27. var readyLoading = false;
  28. this.client.changePassword = function(options, callback) {
  29. callback(null, "success message");
  30. };
  31. widget
  32. .once('reset ready', function () {
  33. $('#a0-reset_easy_email').val('ohmy@mandatory.com');
  34. $('#a0-reset_easy_password').val('123');
  35. $('#a0-reset_easy_repeat_password').val('123');
  36. widget
  37. .once('loading ready', function () {
  38. readyLoading = true;
  39. expect($('#a0-lock h1').html()).to.be(widget.options.i18n.t('reset:title'));
  40. });
  41. bean.fire($('.a0-reset form')[0], 'submit');
  42. })
  43. .showReset({}, onsuccess);
  44. function onsuccess() {
  45. expect(readyLoading).to.be(true);
  46. done();
  47. }
  48. });
  49. it('should invoke callback function when reset is successfull', function(done) {
  50. var widget = this.widget;
  51. // mock success from reset password
  52. this.client.changePassword = function(options, callback) {
  53. callback(null, "success message");
  54. }
  55. widget
  56. .once('reset ready', function () {
  57. $('#a0-reset_easy_email').val('ohmy@mandatory.com');
  58. $('#a0-reset_easy_password').val('123');
  59. $('#a0-reset_easy_repeat_password').val('123');
  60. bean.fire($('.a0-reset form')[0], 'submit');
  61. })
  62. .showReset({}, onsuccess);
  63. function onsuccess(err, message) {
  64. expect(err).to.equal(null);
  65. expect(message).to.not.be(undefined);
  66. done();
  67. }
  68. });
  69. it('should invoke callback function with error when reset errors', function(done) {
  70. var widget = this.widget;
  71. // mock error from reset password
  72. this.client.changePassword = function(options, callback) {
  73. callback(new Error('Email does not exist'))
  74. }
  75. widget
  76. .once('reset ready', function () {
  77. $('#a0-reset_easy_email').val('unregistered@email.com');
  78. $('#a0-reset_easy_password').val('123');
  79. $('#a0-reset_easy_repeat_password').val('123');
  80. bean.fire($('.a0-reset form')[0], 'submit');
  81. })
  82. .showReset({}, onerror);
  83. function onerror(err, message) {
  84. expect(err).to.not.be(null);
  85. expect(message).to.be(undefined);
  86. done();
  87. }
  88. });
  89. });
  90. describe('when requires_username is enabled', function() {
  91. beforeEach(function() {
  92. // Mock `_isUsernameRequired` so it asumes database has enabled
  93. // requires_username on it's configuration
  94. this.options._isUsernameRequired = function() { return true; };
  95. });
  96. it('should invalidate an empty username', function (done) {
  97. var auth0 = this.widget;
  98. var email = 'pepo@example.com';
  99. var password = '12345';
  100. auth0
  101. .once('reset ready', function() {
  102. $('#a0-reset_easy_password').val(password);
  103. $('#a0-reset_easy_repeat_password').val(password);
  104. bean.fire($('.a0-reset form')[0], 'submit');
  105. expect($('.a0-email .a0-error-input')).to.not.be.empty();
  106. done();
  107. })
  108. .showReset(this.options);
  109. });
  110. it('should invalidate an invalid username', function (done) {
  111. var auth0 = this.widget;
  112. var username = '1.1.1.1';
  113. var password = '12345';
  114. auth0
  115. .once('reset ready', function() {
  116. $('#a0-reset_easy_email').val(username);
  117. $('#a0-reset_easy_password').val(password);
  118. $('#a0-reset_easy_repeat_password').val(password);
  119. bean.fire($('.a0-reset form')[0], 'submit');
  120. expect($('.a0-email .a0-error-input')).to.not.be.empty();
  121. expect($('.a0-email .a0-error-input .a0-error-message')).to.not.be.empty();
  122. expect($('.a0-error-message').text()).to.equal(auth0.options.i18n.t('invalid'));
  123. done();
  124. })
  125. .showReset(this.options);
  126. });
  127. it('should still invalidate an invalid email', function (done) {
  128. var auth0 = this.widget;
  129. var email = 'pepo@example';
  130. var password = '12345';
  131. auth0
  132. .once('reset ready', function() {
  133. $('#a0-reset_easy_email').val(email);
  134. $('#a0-reset_easy_password').val(password);
  135. $('#a0-reset_easy_repeat_password').val(password);
  136. bean.fire($('.a0-reset form')[0], 'submit');
  137. expect($('.a0-email .a0-error-input')).to.not.be.empty();
  138. expect($('.a0-email .a0-error-input .a0-error-message')).to.not.be.empty();
  139. expect($('.a0-error-message').text()).to.equal(auth0.options.i18n.t('invalid'));
  140. done();
  141. })
  142. .showReset(this.options);
  143. });
  144. it('should send valid username on submit', function (done) {
  145. var auth0 = this.widget;
  146. var username = 'pepe';
  147. var email = 'pepo@example.com';
  148. var password = '12345';
  149. this.client.changePassword = function(options) {
  150. expect(options.username).to.equal(username);
  151. expect(options.password).to.equal(password);
  152. done();
  153. }
  154. auth0
  155. .once('reset ready', function() {
  156. $('#a0-reset_easy_email').val(username);
  157. $('#a0-reset_easy_password').val(password);
  158. $('#a0-reset_easy_repeat_password').val(password);
  159. bean.fire($('.a0-reset form')[0], 'submit');
  160. })
  161. .showReset(this.options);
  162. });
  163. it('should still send valid email on submit', function (done) {
  164. var auth0 = this.widget;
  165. var email = 'pepo@example.com';
  166. var password = '12345';
  167. this.client.changePassword = function(options) {
  168. expect(options.username).to.equal(email);
  169. expect(options.password).to.equal(password);
  170. done();
  171. }
  172. auth0
  173. .once('reset ready', function() {
  174. $('#a0-reset_easy_email').val(email);
  175. $('#a0-reset_easy_password').val(password);
  176. $('#a0-reset_easy_repeat_password').val(password);
  177. bean.fire($('.a0-reset form')[0], 'submit');
  178. })
  179. .showReset(this.options);
  180. });
  181. });
  182. describe('Events', function() {
  183. it('should fire up an submit event on submit', function(done) {
  184. var auth0 = this.widget;
  185. var email = 'pepo@example.com';
  186. var password = '12345';
  187. this.client.changePassword = function() {};
  188. auth0
  189. .once('reset submit', function(opts) {
  190. expect(opts).to.not.be(undefined);
  191. done();
  192. })
  193. .once('reset ready', function() {
  194. $('#a0-reset_easy_email').val(email);
  195. $('#a0-reset_easy_password').val(password);
  196. $('#a0-reset_easy_repeat_password').val(password);
  197. bean.fire($('.a0-reset form')[0], 'submit');
  198. })
  199. .showReset(this.options);
  200. });
  201. it('should fire up an error event on password missmatch', function(done) {
  202. var auth0 = this.widget;
  203. var email = 'pepo@example.com';
  204. var password = '12345';
  205. this.client.changePassword = function() {};
  206. auth0
  207. .once('reset error', function(err) {
  208. expect(err.message).to.be('password missmatch');
  209. done();
  210. })
  211. .once('reset ready', function() {
  212. $('#a0-reset_easy_email').val(email);
  213. $('#a0-reset_easy_password').val(password);
  214. $('#a0-reset_easy_repeat_password').val(password + 'invalid');
  215. bean.fire($('.a0-reset form')[0], 'submit');
  216. })
  217. .showReset(this.options);
  218. });
  219. it('should fire up an error event on password empty', function(done) {
  220. var auth0 = this.widget;
  221. var email = 'pepo@example.com';
  222. var password = '12345';
  223. this.client.changePassword = function() {};
  224. auth0
  225. .once('reset error', function(err) {
  226. expect(err.message).to.be('password empty');
  227. done();
  228. })
  229. .once('reset ready', function() {
  230. $('#a0-reset_easy_email').val(email);
  231. $('#a0-reset_easy_repeat_password').val(password + 'invalid');
  232. bean.fire($('.a0-reset form')[0], 'submit');
  233. })
  234. .showReset(this.options);
  235. });
  236. it('should fire up an error event on repeat password empty', function(done) {
  237. var auth0 = this.widget;
  238. var email = 'pepo@example.com';
  239. var password = '12345';
  240. this.client.changePassword = function() {};
  241. auth0
  242. .once('reset error', function(err) {
  243. expect(err.message).to.be('repeat password empty');
  244. done();
  245. })
  246. .once('reset ready', function() {
  247. $('#a0-reset_easy_email').val(email);
  248. $('#a0-reset_easy_password').val(password);
  249. bean.fire($('.a0-reset form')[0], 'submit');
  250. })
  251. .showReset(this.options);
  252. });
  253. it('should fire up an error event on email invalid', function(done) {
  254. var auth0 = this.widget;
  255. var email = 'pepo!&&*example.com';
  256. var password = '12345';
  257. auth0
  258. .once('reset error', function(err) {
  259. expect(err.message).to.be('email invalid');
  260. done();
  261. })
  262. .once('reset ready', function() {
  263. $('#a0-reset_easy_email').val(email);
  264. $('#a0-reset_easy_password').val(password);
  265. $('#a0-reset_easy_repeat_password').val(password);
  266. bean.fire($('.a0-reset form')[0], 'submit');
  267. })
  268. .showReset(this.options);
  269. });
  270. it('should fire up an error event on username empty', function (done) {
  271. var auth0 = this.widget;
  272. var email = 'pepo@example.com';
  273. var password = '12345';
  274. this.options._isUsernameRequired = function() { return true; };
  275. auth0
  276. .once('reset error', function(err) {
  277. expect(err.message).to.be('username empty');
  278. done();
  279. })
  280. .once('reset ready', function() {
  281. $('#a0-reset_easy_password').val(password);
  282. $('#a0-reset_easy_repeat_password').val(password);
  283. bean.fire($('.a0-reset form')[0], 'submit');
  284. })
  285. .showReset(this.options);
  286. });
  287. it('should fire up an error event on username invalid', function (done) {
  288. var auth0 = this.widget;
  289. var username = '1.1.1.1';
  290. var password = '12345';
  291. this.options._isUsernameRequired = function() { return true; };
  292. auth0
  293. .once('reset error', function(err) {
  294. expect(err.message).to.be('username invalid');
  295. done();
  296. })
  297. .once('reset ready', function() {
  298. $('#a0-reset_easy_email').val(username);
  299. $('#a0-reset_easy_password').val(password);
  300. $('#a0-reset_easy_repeat_password').val(password);
  301. bean.fire($('.a0-reset form')[0], 'submit');
  302. })
  303. .showReset(this.options);
  304. });
  305. });
  306. });