123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- import assert from 'assert';
- import React from 'react'; // jshint ignore:line
- import misago from 'misago/index';
- import SignIn from 'misago/components/sign-in'; // jshint ignore:line
- import modal from 'misago/services/modal';
- import snackbar from 'misago/services/snackbar';
- import * as testUtils from 'misago/utils/test-utils';
- let snackbarStore = null;
- describe("Sign In", function() {
- beforeEach(function() {
- snackbarStore = testUtils.snackbarStoreMock();
- snackbar.init(snackbarStore);
- testUtils.initModal(modal);
- misago._context = {
- 'SETTINGS': {
- forum_name: 'Test Forum'
- },
- 'AUTH_API': '/test-api/auth/',
- 'REQUEST_ACTIVATION_URL': '/request-activation/',
- 'FORGOTTEN_PASSWORD_URL': '/forgotten-password/'
- };
- /* jshint ignore:start */
- testUtils.render(<SignIn />);
- /* jshint ignore:end */
- });
- afterEach(function() {
- testUtils.emptyTestContainers();
- testUtils.snackbarClear(snackbar);
- $.mockjax.clear();
- });
- it("renders", function() {
- assert.ok($('#test-mount .modal-sign-in #id_username').length,
- "username input rendered");
- assert.ok($('#test-mount .modal-sign-in #id_password').length,
- "password input rendered");
- assert.equal(
- $('#test-mount .modal-footer .btn-default').attr('href'),
- misago.get('FORGOTTEN_PASSWORD_URL'),
- "forgotten password form url is valid");
- });
- it("handles empty submit", function(done) {
- snackbarStore.callback(function(message) {
- assert.deepEqual(message, {
- message: "Fill out both fields.",
- type: 'error'
- }, "form validation rejected empty form");
- done();
- });
- testUtils.simulateSubmit('#test-mount form');
- });
- it("handles partial submit", function(done) {
- snackbarStore.callback(function(message) {
- assert.deepEqual(message, {
- message: "Fill out both fields.",
- type: 'error'
- }, "form validation rejected empty form");
- done();
- });
- testUtils.simulateChange('#id_username', 'loremipsum');
- testUtils.simulateSubmit('#test-mount form');
- });
- it("handles backend error", function(done) {
- snackbarStore.callback(function(message) {
- assert.deepEqual(message, {
- message: "Unknown error has occured.",
- type: 'error'
- }, "form raised alert about backend error");
- done();
- });
- $.mockjax({
- url: '/test-api/auth/',
- status: 500
- });
- testUtils.simulateChange('#id_username', 'SomeFake');
- testUtils.simulateChange('#id_password', 'pass1234');
- testUtils.simulateSubmit('#test-mount form');
- });
- it("handles invalid credentials", function(done) {
- let testMessage = 'Login or password is incorrect.';
- snackbarStore.callback(function(message) {
- assert.deepEqual(message, {
- message: testMessage,
- type: 'error'
- }, "form raised alert about invalid credentials");
- done();
- });
- $.mockjax({
- url: '/test-api/auth/',
- status: 400,
- responseText: {
- 'detail': testMessage,
- 'code': 'invalid_login'
- }
- });
- testUtils.simulateChange('#id_username', 'SomeFake');
- testUtils.simulateChange('#id_password', 'pass1234');
- testUtils.simulateSubmit('#test-mount form');
- });
- it("to admin-activated account", function(done) {
- let testMessage = "This account has to be activated by admin.";
- snackbarStore.callback(function(message) {
- assert.deepEqual(message, {
- message: testMessage,
- type: 'info'
- }, "form raised alert about admin-activated account");
- done();
- });
- $.mockjax({
- url: '/test-api/auth/',
- status: 400,
- responseText: {
- 'detail': testMessage,
- 'code': 'inactive_admin'
- }
- });
- testUtils.simulateChange('#id_username', 'SomeFake');
- testUtils.simulateChange('#id_password', 'pass1234');
- testUtils.simulateSubmit('#test-mount form');
- });
- it("to user-activated account", function(done) {
- let testMessage = "This account has to be activated.";
- snackbarStore.callback(function(message) {
- assert.deepEqual(message, {
- message: testMessage,
- type: 'info'
- }, "form raised alert about user-activated account");
- let activateButton = $('#test-mount .modal-footer .btn-success');
- assert.ok(activateButton.length, "activation button displayed");
- assert.equal(
- activateButton.attr('href'), misago.get('REQUEST_ACTIVATION_URL'),
- "button to activation form has valid url");
- done();
- });
- $.mockjax({
- url: '/test-api/auth/',
- status: 400,
- responseText: {
- 'detail': testMessage,
- 'code': 'inactive_user'
- }
- });
- testUtils.simulateChange('#id_username', 'SomeFake');
- testUtils.simulateChange('#id_password', 'pass1234');
- testUtils.simulateSubmit('#test-mount form');
- });
- it("from banned IP", function(done) {
- $.mockjax({
- url: '/test-api/auth/',
- status: 403,
- responseText: {
- 'ban': {
- 'expires_on': null,
- 'message': {
- 'plain': 'Your ip is banned for spamming.',
- 'html': '<p>Your ip is banned for spamming.</p>',
- }
- }
- }
- });
- testUtils.simulateChange('#id_username', 'SomeFake');
- testUtils.simulateChange('#id_password', 'pass1234');
- testUtils.simulateSubmit('#test-mount form');
- testUtils.onElement('.page-error-banned .lead', function() {
- assert.equal(
- $('.page .message-body .lead p').text().trim(),
- "Your ip is banned for spamming.",
- "displayed error banned page with ban message.");
- done();
- });
- });
- it("to banned account", function(done) {
- $.mockjax({
- url: '/test-api/auth/',
- status: 400,
- responseText: {
- 'detail': {
- 'expires_on': null,
- 'message': {
- 'plain': 'You are banned for trolling.',
- 'html': '<p>You are banned for trolling.</p>',
- }
- },
- 'code': 'banned'
- }
- });
- testUtils.simulateChange('#id_username', 'SomeFake');
- testUtils.simulateChange('#id_password', 'pass1234');
- testUtils.simulateSubmit('#test-mount form');
- testUtils.onElement('.page-error-banned .lead', function() {
- assert.equal(
- $('.page .message-body .lead p').text().trim(),
- "You are banned for trolling.",
- "displayed error banned page with ban message.");
- done();
- });
- });
- it("login successfully", function(done) {
- $('body').append('<div id="hidden-login-form"></div>');
- $.mockjax({
- url: '/test-api/auth/',
- status: 200,
- responseText: {
- 'detail': 'ok'
- }
- });
- let form = $('#hidden-login-form');
- form.on('submit', function(e) {
- e.stopPropagation();
- assert.equal(form.find('input[name="username"]').val(), 'SomeFake',
- "form was filled with valid username.");
- assert.equal(form.find('input[name="password"]').val(), 'pass1234',
- "form was filled with valid password.");
- form.remove();
- done();
- return false;
- });
- testUtils.simulateChange('#id_username', 'SomeFake');
- testUtils.simulateChange('#id_password', 'pass1234');
- testUtils.simulateSubmit('#test-mount form');
- });
- });
|