<commit_before>/* global describe, it, expect, jasmine */
var DeepstreamServer = require( 'deepstream.io' ),
    deepstreamClient = require( '../../src/client' ),
    TestLogger = require( '../tools/test-logger' );
    
describe( 'login', function() {
    var deepstreamServer,
        logger = new TestLogger(),
        clientA,
        clientB,
        permissionHandler = {
        	isValidUser: function( handshakeData, authData, callback ) {
        		if( authData.username === 'validUserA' || authData.username === 'validUserB' ) {
        			callback( null, authData.username );
        		} else {
        			callback( 'Invalid user' );
        		}
        	},
        
        	canPerformAction: function( username, message, callback ) {
        		callback( null, true );
        	}
        };
    
    /**************** SETUP ****************/
    it( 'starts the server', function( done ){
        deepstreamServer = new DeepstreamServer();
        deepstreamServer.on( 'started', done );
        deepstreamServer.set( 'logger', logger );
        deepstreamServer.set( 'showLogo', false );
        deepstreamServer.set( 'maxAuthAttempts', 2 );
        deepstreamServer.set( 'permissionHandler', permissionHandler );
        deepstreamServer.start();
    });
    
    /**************** TESTS ****************/
    it( 'tries to log in with an invalid user', function( done ) {
        clientA = deepstreamClient( 'localhost:6021' );
        clientA.login({ username: 'Egon'}, function( success, event, error ){
            expect( success ).toBe( false );
            expect( event ).toBe( 'INVALID_AUTH_DATA' );
            expect( error ).toBe( 'Invalid user' );
            done();
        });
    });
    
    it( 'tries to log in a second time and exceeds maxAuthAttempts', function(done) {
        var firstcall = true;
 
        clientA.login({ username: 'Egon'}, function( success, event, error ){
            if( firstcall ) {
                expect( success ).toBe( false );
                expect( event ).toBe( 'INVALID_AUTH_DATA' );
                expect( error ).toBe( 'Invalid user' );
                firstcall = false;
            } else {
                expect( success ).toBe( false );
                expect( event ).toBe( 'TOO_MANY_AUTH_ATTEMPTS' );
                expect( error ).toBe( 'too many authentication attempts' );
                done();
            }
        });
    });
    
    it( 'tries to log in again after the client has been kicked', function(done) {
        clientA.on( 'error', function( error, event, topic ){
            expect( topic ).toBe( 'X' );
            expect( event ).toBe( 'IS_CLOSED' );
            done();
        });
        
        clientA.login({ username: 'Egon'}, function( success, event, error ){
            expect( this ).toBe( 'never called' );
        });
    });
    
     /**************** TEAR DOWN ****************/
    it( 'closes the clients', function() {
        clientA.close();
        //clientB.close();
    });
    
    it( 'shuts clients and server down', function(done) {
      deepstreamServer.on( 'stopped', done );
      deepstreamServer.stop();
    });
});<commit_msg>added successful login test<commit_after>/* global describe, it, expect, jasmine */
var DeepstreamServer = require( 'deepstream.io' ),
    deepstreamClient = require( '../../src/client' ),
    TestLogger = require( '../tools/test-logger' );
    
describe( 'login', function() {
    var deepstreamServer,
        logger = new TestLogger(),
        clientA,
        clientB,
        permissionHandler = {
        	isValidUser: function( handshakeData, authData, callback ) {
        		if( authData.username === 'validUserA' || authData.username === 'validUserB' ) {
        			callback( null, authData.username );
        		} else {
        			callback( 'Invalid user' );
        		}
        	},
        
        	canPerformAction: function( username, message, callback ) {
        		callback( null, true );
        	}
        };
    
    /**************** SETUP ****************/
    it( 'starts the server', function( done ){
        deepstreamServer = new DeepstreamServer();
        deepstreamServer.on( 'started', done );
        deepstreamServer.set( 'logger', logger );
        deepstreamServer.set( 'showLogo', false );
        deepstreamServer.set( 'maxAuthAttempts', 2 );
        deepstreamServer.set( 'permissionHandler', permissionHandler );
        deepstreamServer.start();
    });
    
    /**************** TESTS ****************/
    it( 'tries to log in with an invalid user', function( done ) {
        clientA = deepstreamClient( 'localhost:6021' );
        clientA.login({ username: 'Egon'}, function( success, event, error ){
            expect( success ).toBe( false );
            expect( event ).toBe( 'INVALID_AUTH_DATA' );
            expect( error ).toBe( 'Invalid user' );
            done();
        });
    });
    
    it( 'tries to log in a second time and exceeds maxAuthAttempts', function(done) {
        var firstcall = true;
 
        clientA.login({ username: 'Egon'}, function( success, event, error ){
            if( firstcall ) {
                expect( success ).toBe( false );
                expect( event ).toBe( 'INVALID_AUTH_DATA' );
                expect( error ).toBe( 'Invalid user' );
                firstcall = false;
            } else {
                expect( success ).toBe( false );
                expect( event ).toBe( 'TOO_MANY_AUTH_ATTEMPTS' );
                expect( error ).toBe( 'too many authentication attempts' );
                done();
            }
        });
    });
    
    it( 'tries to log in again after the client has been kicked', function(done) {
        clientA.on( 'error', function( error, event, topic ){
            expect( topic ).toBe( 'X' );
            expect( event ).toBe( 'IS_CLOSED' );
            done();
        });
        
        clientA.login({ username: 'Egon'}, function( success, event, error ){
            expect( this ).toBe( 'never called' );
        });
    });
    
    it( 'recreates the client and logs in successfully', function(done){
        clientA = deepstreamClient( 'localhost:6021' );
        clientA.on( 'error', function(){
            console.log( 'clientA error', arguments );
        })
        clientA.login({ username: 'validUserA'}, function( success, event, error ){
            expect( success ).toBe( true );
            done();
        });
    });
    
     /**************** TEAR DOWN ****************/
    it( 'closes the clients', function() {
        clientA.close();
        //clientB.close();
    });
    
    it( 'shuts clients and server down', function(done) {
      deepstreamServer.on( 'stopped', done );
      deepstreamServer.stop();
    });
});
{'fraction_non_alphanumeric': 0.16082260689451103, 'fraction_numerical': 0.00207131232430833, 'mean_word_length': 2.00711743772242, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7432335', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1810, 'n_words': 494}