change-username-test.js 14 KB

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