change-username-test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. import Ember from 'ember';
  2. import { module, test } from 'qunit';
  3. import startApp from '../helpers/start-app';
  4. import getToastMessage from '../helpers/toast-message';
  5. import createUser from '../helpers/create-user';
  6. var application, container, auth;
  7. module('Acceptance: Change Username', {
  8. beforeEach: function() {
  9. application = startApp();
  10. container = application.__container__;
  11. auth = container.lookup('service:auth');
  12. },
  13. afterEach: function() {
  14. Ember.run(application, 'destroy');
  15. Ember.$.mockjax.clear();
  16. }
  17. });
  18. test('/options/change-username form can be accessed', function(assert) {
  19. var user = createUser();
  20. auth.setProperties({
  21. 'isAuthenticated': true,
  22. 'user': user
  23. });
  24. Ember.$.mockjax({
  25. url: '/api/users/' + user.id + '/username/',
  26. status: 200,
  27. responseText: {
  28. 'length_min': 2,
  29. 'length_max': 20,
  30. 'changes_left': 2,
  31. 'next_on': null
  32. }
  33. });
  34. Ember.$.mockjax({
  35. url: '/api/username-changes/',
  36. status: 200,
  37. responseText: {
  38. 'count': 0,
  39. 'next': null,
  40. 'previous': null,
  41. 'results': []
  42. }
  43. });
  44. assert.expect(3);
  45. visit('/options/change-username/');
  46. andThen(function() {
  47. assert.equal(currentPath(), 'options.username');
  48. assert.ok(find('#id_username'));
  49. var listMessage = Ember.$.trim(find('.last-username-changes .list-group-item').text());
  50. listMessage = Ember.$.trim(listMessage.replace('info_outline', ''));
  51. assert.equal(listMessage, 'Your username was never changed.');
  52. });
  53. });
  54. test('/options/change-username form handles backend error', function(assert) {
  55. var user = createUser();
  56. auth.setProperties({
  57. 'isAuthenticated': true,
  58. 'user': user
  59. });
  60. Ember.$.mockjax({
  61. url: '/api/users/' + user.id + '/username/',
  62. status: 403,
  63. responseText: {
  64. 'detail': 'Nope!'
  65. }
  66. });
  67. Ember.$.mockjax({
  68. url: '/api/username-changes/',
  69. status: 200,
  70. responseText: {
  71. 'count': 0,
  72. 'next': null,
  73. 'previous': null,
  74. 'results': []
  75. }
  76. });
  77. assert.expect(3);
  78. visit('/options/change-username/');
  79. andThen(function() {
  80. assert.equal(currentPath(), 'options.username');
  81. var errorMessage = Ember.$.trim(find('.error-message p').text());
  82. assert.equal(errorMessage, 'Nope!');
  83. var listMessage = Ember.$.trim(find('.last-username-changes .list-group-item').text());
  84. listMessage = Ember.$.trim(listMessage.replace('info_outline', ''));
  85. assert.equal(listMessage, 'Your username was never changed.');
  86. });
  87. });
  88. test('/options/change-username disallows username change', function(assert) {
  89. var user = createUser();
  90. auth.setProperties({
  91. 'isAuthenticated': true,
  92. 'user': user
  93. });
  94. var nextOn = moment();
  95. nextOn.add(7, 'days');
  96. Ember.$.mockjax({
  97. url: '/api/users/' + user.id + '/username/',
  98. status: 200,
  99. responseText: {
  100. 'length_min': 2,
  101. 'length_max': 20,
  102. 'changes_left': 0,
  103. 'next_on': nextOn.format()
  104. }
  105. });
  106. Ember.$.mockjax({
  107. url: '/api/username-changes/',
  108. status: 200,
  109. responseText: {
  110. 'count': 0,
  111. 'next': null,
  112. 'previous': null,
  113. 'results': []
  114. }
  115. });
  116. assert.expect(4);
  117. visit('/options/change-username/');
  118. andThen(function() {
  119. assert.equal(currentPath(), 'options.username');
  120. var errorMessage = Ember.$.trim(find('.panel-body p').first().text());
  121. assert.equal(errorMessage, "You can't change your username now.");
  122. var expiresMessage = Ember.$.trim(find('.panel-body p').last().text());
  123. assert.equal(expiresMessage, 'Next change will be possible in 7 days.');
  124. var listMessage = Ember.$.trim(find('.last-username-changes .list-group-item').text());
  125. listMessage = Ember.$.trim(listMessage.replace('info_outline', ''));
  126. assert.equal(listMessage, 'Your username was never changed.');
  127. });
  128. });
  129. test('/options/change-username changes username', function(assert) {
  130. var user = createUser();
  131. auth.setProperties({
  132. 'isAuthenticated': true,
  133. 'user': user
  134. });
  135. var nextOn = moment();
  136. nextOn.add(7, 'days');
  137. Ember.$.mockjax({
  138. url: '/api/users/' + user.id + '/username/',
  139. status: 200,
  140. type: 'GET',
  141. responseText: {
  142. 'length_min': 2,
  143. 'length_max': 20,
  144. 'changes_left': 3,
  145. 'next_on': null
  146. }
  147. });
  148. Ember.$.mockjax({
  149. url: '/api/users/' + user.id + '/username/',
  150. status: 200,
  151. type: 'POST',
  152. responseText: {
  153. 'username': 'NewName',
  154. 'slug': 'newname',
  155. 'options': {
  156. 'length_min': 2,
  157. 'length_max': 20,
  158. 'changes_left': 3,
  159. 'next_on': null
  160. }
  161. }
  162. });
  163. Ember.$.mockjax({
  164. url: '/api/username-changes/',
  165. status: 200,
  166. responseText: {
  167. 'count': 0,
  168. 'next': null,
  169. 'previous': null,
  170. 'results': []
  171. }
  172. });
  173. assert.expect(4);
  174. visit('/options/change-username/');
  175. fillIn('#id_username', 'NewName');
  176. click('.panel-form .panel-footer .btn-primary');
  177. andThen(function() {
  178. assert.equal(currentPath(), 'options.username');
  179. assert.equal(getToastMessage(), 'Your username has been changed.');
  180. var listedUsername = Ember.$.trim(find('.last-username-changes .first-row .item-name').text());
  181. assert.equal(listedUsername, 'NewName');
  182. assert.ok(find('.last-username-changes').text().indexOf('BobBoberson') !== -1);
  183. });
  184. });
  185. test('/options/change-username handles API error', function(assert) {
  186. var user = createUser();
  187. auth.setProperties({
  188. 'isAuthenticated': true,
  189. 'user': user
  190. });
  191. var nextOn = moment();
  192. nextOn.add(7, 'days');
  193. Ember.$.mockjax({
  194. url: '/api/users/' + user.id + '/username/',
  195. status: 200,
  196. type: 'GET',
  197. responseText: {
  198. 'length_min': 2,
  199. 'length_max': 20,
  200. 'changes_left': 3,
  201. 'next_on': null
  202. }
  203. });
  204. Ember.$.mockjax({
  205. url: '/api/users/' + user.id + '/username/',
  206. status: 400,
  207. type: 'POST',
  208. responseText: {
  209. 'detail': 'Not good new name.'
  210. }
  211. });
  212. Ember.$.mockjax({
  213. url: '/api/username-changes/',
  214. status: 200,
  215. responseText: {
  216. 'count': 0,
  217. 'next': null,
  218. 'previous': null,
  219. 'results': []
  220. }
  221. });
  222. assert.expect(3);
  223. visit('/options/change-username/');
  224. fillIn('#id_username', 'NewName');
  225. click('.panel-form .panel-footer .btn-primary');
  226. andThen(function() {
  227. assert.equal(currentPath(), 'options.username');
  228. assert.equal(getToastMessage(), 'Not good new name.');
  229. assert.equal(find('.last-username-changes').text().indexOf('NewName'), -1);
  230. });
  231. });
  232. test('/options/change-username handles empty form submit', function(assert) {
  233. var user = createUser();
  234. auth.setProperties({
  235. 'isAuthenticated': true,
  236. 'user': user
  237. });
  238. var nextOn = moment();
  239. nextOn.add(7, 'days');
  240. Ember.$.mockjax({
  241. url: '/api/users/' + user.id + '/username/',
  242. status: 200,
  243. type: 'GET',
  244. responseText: {
  245. 'length_min': 2,
  246. 'length_max': 20,
  247. 'changes_left': 3,
  248. 'next_on': null
  249. }
  250. });
  251. Ember.$.mockjax({
  252. url: '/api/username-changes/',
  253. status: 200,
  254. responseText: {
  255. 'count': 0,
  256. 'next': null,
  257. 'previous': null,
  258. 'results': []
  259. }
  260. });
  261. assert.expect(2);
  262. visit('/options/change-username/');
  263. click('.panel-form .panel-footer .btn-primary');
  264. andThen(function() {
  265. assert.equal(currentPath(), 'options.username');
  266. assert.equal(getToastMessage(), 'Form contains errors.');
  267. });
  268. });
  269. test('/options/change-username handles too long username', function(assert) {
  270. var user = createUser();
  271. auth.setProperties({
  272. 'isAuthenticated': true,
  273. 'user': user
  274. });
  275. var nextOn = moment();
  276. nextOn.add(7, 'days');
  277. Ember.$.mockjax({
  278. url: '/api/users/' + user.id + '/username/',
  279. status: 200,
  280. type: 'GET',
  281. responseText: {
  282. 'length_min': 2,
  283. 'length_max': 5,
  284. 'changes_left': 3,
  285. 'next_on': null
  286. }
  287. });
  288. Ember.$.mockjax({
  289. url: '/api/username-changes/',
  290. status: 200,
  291. responseText: {
  292. 'count': 0,
  293. 'next': null,
  294. 'previous': null,
  295. 'results': []
  296. }
  297. });
  298. assert.expect(3);
  299. visit('/options/change-username/');
  300. fillIn('#id_username', 'NewNameTooLong');
  301. click('.panel-form .panel-footer .btn-primary');
  302. andThen(function() {
  303. assert.equal(currentPath(), 'options.username');
  304. assert.equal(getToastMessage(), 'Form contains errors.');
  305. var validationMessage = Ember.$.trim(find('.panel-form .form-group .help-block.errors').text());
  306. assert.equal(validationMessage, 'Username cannot be longer than 5 characters.');
  307. });
  308. });
  309. test('/options/change-username handles too short username', function(assert) {
  310. var user = createUser();
  311. auth.setProperties({
  312. 'isAuthenticated': true,
  313. 'user': user
  314. });
  315. var nextOn = moment();
  316. nextOn.add(7, 'days');
  317. Ember.$.mockjax({
  318. url: '/api/users/' + user.id + '/username/',
  319. status: 200,
  320. type: 'GET',
  321. responseText: {
  322. 'length_min': 12,
  323. 'length_max': 25,
  324. 'changes_left': 3,
  325. 'next_on': null
  326. }
  327. });
  328. Ember.$.mockjax({
  329. url: '/api/username-changes/',
  330. status: 200,
  331. responseText: {
  332. 'count': 0,
  333. 'next': null,
  334. 'previous': null,
  335. 'results': []
  336. }
  337. });
  338. assert.expect(3);
  339. visit('/options/change-username/');
  340. fillIn('#id_username', 'TooShort');
  341. click('.panel-form .panel-footer .btn-primary');
  342. andThen(function() {
  343. assert.equal(currentPath(), 'options.username');
  344. assert.equal(getToastMessage(), 'Form contains errors.');
  345. var validationMessage = Ember.$.trim(find('.panel-form .form-group .help-block.errors').text());
  346. assert.equal(validationMessage, 'Username must be at least 12 characters long.');
  347. });
  348. });
  349. test('/options/change-username handles invalid username', function(assert) {
  350. var user = createUser();
  351. auth.setProperties({
  352. 'isAuthenticated': true,
  353. 'user': user
  354. });
  355. var nextOn = moment();
  356. nextOn.add(7, 'days');
  357. Ember.$.mockjax({
  358. url: '/api/users/' + user.id + '/username/',
  359. status: 200,
  360. type: 'GET',
  361. responseText: {
  362. 'length_min': 2,
  363. 'length_max': 25,
  364. 'changes_left': 3,
  365. 'next_on': null
  366. }
  367. });
  368. Ember.$.mockjax({
  369. url: '/api/username-changes/',
  370. status: 200,
  371. responseText: {
  372. 'count': 0,
  373. 'next': null,
  374. 'previous': null,
  375. 'results': []
  376. }
  377. });
  378. assert.expect(3);
  379. visit('/options/change-username/');
  380. fillIn('#id_username', 'us3rn#me');
  381. click('.panel-form .panel-footer .btn-primary');
  382. andThen(function() {
  383. assert.equal(currentPath(), 'options.username');
  384. assert.equal(getToastMessage(), 'Form contains errors.');
  385. var validationMessage = Ember.$.trim(find('.panel-form .form-group .help-block.errors').text());
  386. assert.equal(validationMessage, 'Username can only contain latin alphabet letters and digits.');
  387. });
  388. });
  389. test('/options/change-username handles same username', function(assert) {
  390. var user = createUser();
  391. auth.setProperties({
  392. 'isAuthenticated': true,
  393. 'user': user
  394. });
  395. var nextOn = moment();
  396. nextOn.add(7, 'days');
  397. Ember.$.mockjax({
  398. url: '/api/users/' + user.id + '/username/',
  399. status: 200,
  400. type: 'GET',
  401. responseText: {
  402. 'length_min': 2,
  403. 'length_max': 25,
  404. 'changes_left': 3,
  405. 'next_on': null
  406. }
  407. });
  408. Ember.$.mockjax({
  409. url: '/api/username-changes/',
  410. status: 200,
  411. responseText: {
  412. 'count': 0,
  413. 'next': null,
  414. 'previous': null,
  415. 'results': []
  416. }
  417. });
  418. assert.expect(3);
  419. visit('/options/change-username/');
  420. fillIn('#id_username', user.get('username'));
  421. click('.panel-form .panel-footer .btn-primary');
  422. andThen(function() {
  423. assert.equal(currentPath(), 'options.username');
  424. assert.equal(getToastMessage(), 'Form contains errors.');
  425. var validationMessage = Ember.$.trim(find('.panel-form .form-group .help-block.errors').text());
  426. assert.equal(validationMessage, 'New username is same as current one.');
  427. });
  428. });
  429. test('/options/change-username displays filtered history', function(assert) {
  430. var user = createUser();
  431. auth.setProperties({
  432. 'isAuthenticated': true,
  433. 'user': user
  434. });
  435. var nextOn = moment();
  436. nextOn.add(7, 'days');
  437. Ember.$.mockjax({
  438. url: '/api/users/' + user.id + '/username/',
  439. status: 200,
  440. type: 'GET',
  441. responseText: {
  442. 'length_min': 2,
  443. 'length_max': 25,
  444. 'changes_left': 3,
  445. 'next_on': null
  446. }
  447. });
  448. Ember.$.mockjax({
  449. url: '/api/username-changes/',
  450. status: 200,
  451. responseText: {
  452. 'count': 0,
  453. 'next': null,
  454. 'previous': null,
  455. 'results': [
  456. {
  457. "id": 26,
  458. "user": {
  459. "id": 42,
  460. "username": "LoremIpsum",
  461. "slug": "loremipsum",
  462. "avatar_hash": "b03dc23d"
  463. },
  464. "changed_by": {
  465. "id": 42,
  466. "username": "LoremIpsum",
  467. "slug": "loremipsum",
  468. "avatar_hash": "b03dc23d"
  469. },
  470. "changed_by_username": "LoremIpsum",
  471. "changed_by_slug": "loremipsum",
  472. "changed_on": moment().format(),
  473. "new_username": "GoodName",
  474. "old_username": "BobBoberson"
  475. },
  476. {
  477. "id": 27,
  478. "user": {
  479. "id": 40,
  480. "username": "LoremIpsum",
  481. "slug": "loremipsum",
  482. "avatar_hash": "b03dc23d"
  483. },
  484. "changed_by": {
  485. "id": 42,
  486. "username": "LoremIpsum",
  487. "slug": "loremipsum",
  488. "avatar_hash": "b03dc23d"
  489. },
  490. "changed_by_username": "LoremIpsum",
  491. "changed_by_slug": "loremipsum",
  492. "changed_on": moment().format(),
  493. "new_username": "WrongName",
  494. "old_username": "BobBoberson"
  495. }
  496. ]
  497. }
  498. });
  499. assert.expect(4);
  500. visit('/options/change-username/');
  501. andThen(function() {
  502. assert.equal(currentPath(), 'options.username');
  503. assert.equal(find('.last-username-changes .list-group-item').length, 1);
  504. assert.ok(find('.last-username-changes').text().indexOf('GoodName') !== -1);
  505. assert.equal(find('.last-username-changes').text().indexOf('WrongName'), -1);
  506. });
  507. });