123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596 |
- import Ember from 'ember';
- import { module, test } from 'qunit';
- import startApp from '../helpers/start-app';
- import getToastMessage from '../helpers/toast-message';
- import createUser from '../helpers/create-user';
- var application, container, auth;
- module('Acceptance: Change Username', {
- beforeEach: function() {
- application = startApp();
- container = application.__container__;
- auth = container.lookup('service:auth');
- },
- afterEach: function() {
- Ember.run(application, 'destroy');
- Ember.$.mockjax.clear();
- }
- });
- test('/options/change-username form can be accessed', function(assert) {
- var user = createUser();
- auth.setProperties({
- 'isAuthenticated': true,
- 'user': user
- });
- Ember.$.mockjax({
- url: '/api/users/' + user.id + '/username/',
- status: 200,
- responseText: {
- 'length_min': 2,
- 'length_max': 20,
- 'changes_left': 2,
- 'next_on': null
- }
- });
- Ember.$.mockjax({
- url: '/api/username-changes/',
- status: 200,
- responseText: {
- 'count': 0,
- 'next': null,
- 'previous': null,
- 'results': []
- }
- });
- assert.expect(3);
- visit('/options/change-username/');
- andThen(function() {
- assert.equal(currentPath(), 'options.username');
- assert.ok(find('#id_username'));
- var listMessage = Ember.$.trim(find('.last-username-changes .list-group-item').text());
- assert.equal(listMessage, 'Your username was never changed.');
- });
- });
- test('/options/change-username form handles backend error', function(assert) {
- var user = createUser();
- auth.setProperties({
- 'isAuthenticated': true,
- 'user': user
- });
- Ember.$.mockjax({
- url: '/api/users/' + user.id + '/username/',
- status: 403,
- responseText: {
- 'detail': 'Nope!'
- }
- });
- Ember.$.mockjax({
- url: '/api/username-changes/',
- status: 200,
- responseText: {
- 'count': 0,
- 'next': null,
- 'previous': null,
- 'results': []
- }
- });
- assert.expect(3);
- visit('/options/change-username/');
- andThen(function() {
- assert.equal(currentPath(), 'options.username');
- var errorMessage = Ember.$.trim(find('.error-message p').text());
- assert.equal(errorMessage, 'Nope!');
- var listMessage = Ember.$.trim(find('.last-username-changes .list-group-item').text());
- assert.equal(listMessage, 'Your username was never changed.');
- });
- });
- test('/options/change-username disallows username change', function(assert) {
- var user = createUser();
- auth.setProperties({
- 'isAuthenticated': true,
- 'user': user
- });
- var nextOn = moment();
- nextOn.add(7, 'days');
- Ember.$.mockjax({
- url: '/api/users/' + user.id + '/username/',
- status: 200,
- responseText: {
- 'length_min': 2,
- 'length_max': 20,
- 'changes_left': 0,
- 'next_on': nextOn.format()
- }
- });
- Ember.$.mockjax({
- url: '/api/username-changes/',
- status: 200,
- responseText: {
- 'count': 0,
- 'next': null,
- 'previous': null,
- 'results': []
- }
- });
- assert.expect(4);
- visit('/options/change-username/');
- andThen(function() {
- assert.equal(currentPath(), 'options.username');
- var errorMessage = Ember.$.trim(find('.panel-body p').first().text());
- assert.equal(errorMessage, "You can't change your username now.");
- var expiresMessage = Ember.$.trim(find('.panel-body p').last().text());
- assert.equal(expiresMessage, 'Next change will be possible in 7 days.');
- var listMessage = Ember.$.trim(find('.last-username-changes .list-group-item').text());
- assert.equal(listMessage, 'Your username was never changed.');
- });
- });
- test('/options/change-username changes username', function(assert) {
- var user = createUser();
- auth.setProperties({
- 'isAuthenticated': true,
- 'user': user
- });
- var nextOn = moment();
- nextOn.add(7, 'days');
- Ember.$.mockjax({
- url: '/api/users/' + user.id + '/username/',
- status: 200,
- type: 'GET',
- responseText: {
- 'length_min': 2,
- 'length_max': 20,
- 'changes_left': 3,
- 'next_on': null
- }
- });
- Ember.$.mockjax({
- url: '/api/users/' + user.id + '/username/',
- status: 200,
- type: 'POST',
- responseText: {
- 'username': 'NewName',
- 'slug': 'newname',
- 'options': {
- 'length_min': 2,
- 'length_max': 20,
- 'changes_left': 3,
- 'next_on': null
- }
- }
- });
- Ember.$.mockjax({
- url: '/api/username-changes/',
- status: 200,
- responseText: {
- 'count': 0,
- 'next': null,
- 'previous': null,
- 'results': []
- }
- });
- assert.expect(4);
- visit('/options/change-username/');
- fillIn('#id_username', 'NewName');
- click('.panel-form .panel-footer .btn-primary');
- andThen(function() {
- assert.equal(currentPath(), 'options.username');
- assert.equal(getToastMessage(), 'Your username has been changed.');
- var listedUsername = Ember.$.trim(find('.last-username-changes .item-name').text());
- assert.equal(listedUsername, 'NewName');
- assert.ok(find('.last-username-changes').text().indexOf('BobBoberson') !== -1);
- });
- });
- test('/options/change-username handles API error', function(assert) {
- var user = createUser();
- auth.setProperties({
- 'isAuthenticated': true,
- 'user': user
- });
- var nextOn = moment();
- nextOn.add(7, 'days');
- Ember.$.mockjax({
- url: '/api/users/' + user.id + '/username/',
- status: 200,
- type: 'GET',
- responseText: {
- 'length_min': 2,
- 'length_max': 20,
- 'changes_left': 3,
- 'next_on': null
- }
- });
- Ember.$.mockjax({
- url: '/api/users/' + user.id + '/username/',
- status: 400,
- type: 'POST',
- responseText: {
- 'detail': 'Not good new name.'
- }
- });
- Ember.$.mockjax({
- url: '/api/username-changes/',
- status: 200,
- responseText: {
- 'count': 0,
- 'next': null,
- 'previous': null,
- 'results': []
- }
- });
- assert.expect(3);
- visit('/options/change-username/');
- fillIn('#id_username', 'NewName');
- click('.panel-form .panel-footer .btn-primary');
- andThen(function() {
- assert.equal(currentPath(), 'options.username');
- assert.equal(getToastMessage(), 'Not good new name.');
- assert.equal(find('.last-username-changes').text().indexOf('NewName'), -1);
- });
- });
- test('/options/change-username handles empty form submit', function(assert) {
- var user = createUser();
- auth.setProperties({
- 'isAuthenticated': true,
- 'user': user
- });
- var nextOn = moment();
- nextOn.add(7, 'days');
- Ember.$.mockjax({
- url: '/api/users/' + user.id + '/username/',
- status: 200,
- type: 'GET',
- responseText: {
- 'length_min': 2,
- 'length_max': 20,
- 'changes_left': 3,
- 'next_on': null
- }
- });
- Ember.$.mockjax({
- url: '/api/username-changes/',
- status: 200,
- responseText: {
- 'count': 0,
- 'next': null,
- 'previous': null,
- 'results': []
- }
- });
- assert.expect(2);
- visit('/options/change-username/');
- click('.panel-form .panel-footer .btn-primary');
- andThen(function() {
- assert.equal(currentPath(), 'options.username');
- assert.equal(getToastMessage(), 'Enter new username.');
- });
- });
- test('/options/change-username handles too long username', function(assert) {
- var user = createUser();
- auth.setProperties({
- 'isAuthenticated': true,
- 'user': user
- });
- var nextOn = moment();
- nextOn.add(7, 'days');
- Ember.$.mockjax({
- url: '/api/users/' + user.id + '/username/',
- status: 200,
- type: 'GET',
- responseText: {
- 'length_min': 2,
- 'length_max': 5,
- 'changes_left': 3,
- 'next_on': null
- }
- });
- Ember.$.mockjax({
- url: '/api/username-changes/',
- status: 200,
- responseText: {
- 'count': 0,
- 'next': null,
- 'previous': null,
- 'results': []
- }
- });
- assert.expect(3);
- visit('/options/change-username/');
- fillIn('#id_username', 'NewNameTooLong');
- click('.panel-form .panel-footer .btn-primary');
- andThen(function() {
- assert.equal(currentPath(), 'options.username');
- assert.equal(getToastMessage(), 'Form contains errors.');
- var validationMessage = Ember.$.trim(find('.panel-form .form-group .help-block.errors').text());
- assert.equal(validationMessage, 'Username cannot be longer than 5 characters.');
- });
- });
- test('/options/change-username handles too short username', function(assert) {
- var user = createUser();
- auth.setProperties({
- 'isAuthenticated': true,
- 'user': user
- });
- var nextOn = moment();
- nextOn.add(7, 'days');
- Ember.$.mockjax({
- url: '/api/users/' + user.id + '/username/',
- status: 200,
- type: 'GET',
- responseText: {
- 'length_min': 12,
- 'length_max': 25,
- 'changes_left': 3,
- 'next_on': null
- }
- });
- Ember.$.mockjax({
- url: '/api/username-changes/',
- status: 200,
- responseText: {
- 'count': 0,
- 'next': null,
- 'previous': null,
- 'results': []
- }
- });
- assert.expect(3);
- visit('/options/change-username/');
- fillIn('#id_username', 'TooShort');
- click('.panel-form .panel-footer .btn-primary');
- andThen(function() {
- assert.equal(currentPath(), 'options.username');
- assert.equal(getToastMessage(), 'Form contains errors.');
- var validationMessage = Ember.$.trim(find('.panel-form .form-group .help-block.errors').text());
- assert.equal(validationMessage, 'Username must be at least 12 characters long.');
- });
- });
- test('/options/change-username handles invalid username', function(assert) {
- var user = createUser();
- auth.setProperties({
- 'isAuthenticated': true,
- 'user': user
- });
- var nextOn = moment();
- nextOn.add(7, 'days');
- Ember.$.mockjax({
- url: '/api/users/' + user.id + '/username/',
- status: 200,
- type: 'GET',
- responseText: {
- 'length_min': 2,
- 'length_max': 25,
- 'changes_left': 3,
- 'next_on': null
- }
- });
- Ember.$.mockjax({
- url: '/api/username-changes/',
- status: 200,
- responseText: {
- 'count': 0,
- 'next': null,
- 'previous': null,
- 'results': []
- }
- });
- assert.expect(3);
- visit('/options/change-username/');
- fillIn('#id_username', 'us3rn#me');
- click('.panel-form .panel-footer .btn-primary');
- andThen(function() {
- assert.equal(currentPath(), 'options.username');
- assert.equal(getToastMessage(), 'Form contains errors.');
- var validationMessage = Ember.$.trim(find('.panel-form .form-group .help-block.errors').text());
- assert.equal(validationMessage, 'Username can only contain latin alphabet letters and digits.');
- });
- });
- test('/options/change-username handles same username', function(assert) {
- var user = createUser();
- auth.setProperties({
- 'isAuthenticated': true,
- 'user': user
- });
- var nextOn = moment();
- nextOn.add(7, 'days');
- Ember.$.mockjax({
- url: '/api/users/' + user.id + '/username/',
- status: 200,
- type: 'GET',
- responseText: {
- 'length_min': 2,
- 'length_max': 25,
- 'changes_left': 3,
- 'next_on': null
- }
- });
- Ember.$.mockjax({
- url: '/api/username-changes/',
- status: 200,
- responseText: {
- 'count': 0,
- 'next': null,
- 'previous': null,
- 'results': []
- }
- });
- assert.expect(3);
- visit('/options/change-username/');
- fillIn('#id_username', user.get('username'));
- click('.panel-form .panel-footer .btn-primary');
- andThen(function() {
- assert.equal(currentPath(), 'options.username');
- assert.equal(getToastMessage(), 'Form contains errors.');
- var validationMessage = Ember.$.trim(find('.panel-form .form-group .help-block.errors').text());
- assert.equal(validationMessage, 'New username is same as current one.');
- });
- });
- test('/options/change-username displays filtered history', function(assert) {
- var user = createUser();
- auth.setProperties({
- 'isAuthenticated': true,
- 'user': user
- });
- var nextOn = moment();
- nextOn.add(7, 'days');
- Ember.$.mockjax({
- url: '/api/users/' + user.id + '/username/',
- status: 200,
- type: 'GET',
- responseText: {
- 'length_min': 2,
- 'length_max': 25,
- 'changes_left': 3,
- 'next_on': null
- }
- });
- Ember.$.mockjax({
- url: '/api/username-changes/',
- status: 200,
- responseText: {
- 'count': 0,
- 'next': null,
- 'previous': null,
- 'results': [
- {
- "id": 26,
- "user": {
- "id": 42,
- "username": "LoremIpsum",
- "slug": "loremipsum",
- "avatar_hash": "b03dc23d"
- },
- "changed_by": {
- "id": 42,
- "username": "LoremIpsum",
- "slug": "loremipsum",
- "avatar_hash": "b03dc23d"
- },
- "changed_by_username": "LoremIpsum",
- "changed_by_slug": "loremipsum",
- "changed_on": moment().format(),
- "new_username": "GoodName",
- "old_username": "BobBoberson"
- },
- {
- "id": 27,
- "user": {
- "id": 40,
- "username": "LoremIpsum",
- "slug": "loremipsum",
- "avatar_hash": "b03dc23d"
- },
- "changed_by": {
- "id": 42,
- "username": "LoremIpsum",
- "slug": "loremipsum",
- "avatar_hash": "b03dc23d"
- },
- "changed_by_username": "LoremIpsum",
- "changed_by_slug": "loremipsum",
- "changed_on": moment().format(),
- "new_username": "WrongName",
- "old_username": "BobBoberson"
- }
- ]
- }
- });
- assert.expect(4);
- visit('/options/change-username/');
- andThen(function() {
- assert.equal(currentPath(), 'options.username');
- assert.equal(find('.last-username-changes .list-group-item').length, 1);
- assert.ok(find('.last-username-changes').text().indexOf('GoodName') !== -1);
- assert.equal(find('.last-username-changes').text().indexOf('WrongName'), -1);
- });
- });
|