<commit_before>/*jslint node: true */
/*jslint newcap: true */
/*global before, beforeEach, afterEach, after, describe, it */
/*jslint nomen: true */
/*jshint expr: true*/
"use strict";

var addplayer = require('../addPlayer');
var sinon = require('sinon');
var playermanagementservice = require('../../lib/ts/PlayerManagementService');
var sinonChai = require("sinon-chai");
var chai = require("chai");
chai.should();
chai.use(sinonChai);
require('mocha-sinon');
var expressValidator = require('express-validator')();

describe("Testing of expressjs route for adding a player to a club", function () {
    //1. Module Setup
    var ap,
        sandbox,
        stubAddPlayer,
        pms,
        incomingExpressRequest,
        outgoingExpressResponse,
        outgoingExpressResponseSpy;

    beforeEach(function () {
        pms = new playermanagementservice(null);
        incomingExpressRequest = {
            body: { clubname: 'Western Knights', cityname: 'Perth',
                    firstname: 'Ken', lastname: 'Bacon', dob: '29 Dec 1991', address: '999 Millenium Way',
                    suburb: 'Mosman Park', postcode: '6021', phone: '9343 2213', email: '<EMAIL>' },
            session: { authenticated: true, user: { } }
        };
        expressValidator(incomingExpressRequest, {}, function () {
            console.log('Unit Test Stub - Validation performed');
        });

        //sandbox to cleanup global spies
        sandbox = sinon.sandbox.create();
        stubAddPlayer = sandbox.stub(pms, 'AddPlayer');
        stubAddPlayer.yields(null);
        outgoingExpressResponse = {
            redirect: function (view) { /* just a stub to be overridden by sinon */ console.log('This code for should not be executed in a unit test %s', view); },
            render: function (view) { /* just a stub to be overridden by sinon */ console.log('This code for should not be executed in a unit test %s', view); }
        };
        outgoingExpressResponseSpy = sandbox.spy(outgoingExpressResponse, 'render');

        //this will be setup to be injected soon enough
        ap = new addplayer(pms);
    });

    function assertViewUpdated(redirectView, spy, alertType, messages) {
        spy.should.have.been.calledWith(redirectView, sinon.match({ flash: {
            type: alertType,
            messages: messages
        }}));
    }

    function assertPlayerCreatedAndViewUpdated(redirectView, spy, alertType, messages) {
        assertViewUpdated(redirectView, spy, alertType, messages);
        stubAddPlayer.should.have.been.called;
    }

    function assertPlayerValidationFailedViewUpdatedWithFailureMessage(redirectView, spy, alertType, messages) {
        assertViewUpdated(redirectView, spy, alertType, messages);
        stubAddPlayer.should.not.have.been.called;
    }

    function enactRequestBodyValidationTest(incomingExpressRequest, expectedMessage, done) {
        //2. exercise
        ap.post(incomingExpressRequest, outgoingExpressResponse);

        //3. verify
        assertPlayerValidationFailedViewUpdatedWithFailureMessage('addPlayer', outgoingExpressResponseSpy, 'alert-danger', expectedMessage);

        //4. teardown
        done();
    }

    function notifiesValidationErrors(propertyname, expectedMessage, done) {
        //1. setup - changing the default behaviour
        incomingExpressRequest.body[propertyname] = '';
        //2. exercise
        enactRequestBodyValidationTest(incomingExpressRequest, [{ msg: expectedMessage }], done);
    }

    //2. Module Exercise

    it("Add a valid player", function (done) {
        //1. setup

        //2. exercise
        ap.post(incomingExpressRequest, outgoingExpressResponse);

        //3. verify
        assertPlayerCreatedAndViewUpdated('addPlayer', outgoingExpressResponseSpy,
            'alert-success',
            [{ msg: 'Player has been successfully added.' }]);

        //4. teardown
        done();
    });

    it("Notifies via response when an error during call to addPlayer in service", function (done) {
        //1. setup - changing the default behaviour
        var expectedErrorMessage = 'Add Player Service Failure',
            expectedError = new Error(expectedErrorMessage);
        stubAddPlayer.yields(expectedError);

        //2. exercise
        ap.post(incomingExpressRequest, outgoingExpressResponse);

        //3. verify
        assertPlayerCreatedAndViewUpdated('addPlayer', outgoingExpressResponseSpy, 'alert-danger', [{ msg: expectedErrorMessage }]);
<commit_msg>Added failing test for retrieving club and city name on AddPlayer page<commit_after>/*jslint node: true */
/*jslint newcap: true */
/*global before, beforeEach, afterEach, after, describe, it */
/*jslint nomen: true */
/*jshint expr: true*/
"use strict";

var addplayer = require('../addPlayer');
var sinon = require('sinon');
var playermanagementservice = require('../../lib/ts/PlayerManagementService');
var clubmanagementservice = require('../../lib/ts/ClubManagementService');
var sinonChai = require("sinon-chai");
var chai = require("chai");
chai.should();
chai.use(sinonChai);
require('mocha-sinon');
var expressValidator = require('express-validator')();

describe("Testing of expressjs route for adding a player to a club", function () {
    //1. Module Setup
    var ap,
        sandbox,
        stubAddPlayer,
        pms,
        clubms,
        incomingGetRequest,
        incomingExpressRequest,
        outgoingExpressResponse,
        outgoingExpressResponseSpy,
        stubGetClubs;

    beforeEach(function () {
        pms = new playermanagementservice(null);
        clubms = new clubmanagementservice(null);
        incomingGetRequest = {
            session: {
                user: { email: '<EMAIL>' }
            }
        };
        incomingExpressRequest = {
            body: { clubname: 'Western Knights', cityname: 'Perth',
                    firstname: 'Ken', lastname: 'Bacon', dob: '29 Dec 1991', address: '999 Millenium Way',
                    suburb: 'Mosman Park', postcode: '6021', phone: '9343 2213', email: '<EMAIL>' },
            session: { authenticated: true, user: { } }
        };
        expressValidator(incomingExpressRequest, {}, function () {
            console.log('Unit Test Stub - Validation performed');
        });

        //sandbox to cleanup global spies
        sandbox = sinon.sandbox.create();
        stubAddPlayer = sandbox.stub(pms, 'AddPlayer');
        stubAddPlayer.yields(null);
        outgoingExpressResponse = {
            redirect: function (view) { /* just a stub to be overridden by sinon */ console.log('This code for should not be executed in a unit test %s', view); },
            render: function (view) { /* just a stub to be overridden by sinon */ console.log('This code for should not be executed in a unit test %s', view); }
        };
        outgoingExpressResponseSpy = sandbox.spy(outgoingExpressResponse, 'render');
        
        stubGetClubs = sandbox.stub(clubms, 'GetClubs');
        stubGetClubs.yields(null, [{ club: 'Club1', city: 'City1' }]);

        //this will be setup to be injected soon enough
        ap = new addplayer(pms, clubms);
    });

    function assertViewUpdated(redirectView, spy, alertType, messages) {
        spy.should.have.been.calledWith(redirectView, sinon.match({ flash: {
            type: alertType,
            messages: messages
        }}));
    }

    function assertPlayerCreatedAndViewUpdated(redirectView, spy, alertType, messages) {
        assertViewUpdated(redirectView, spy, alertType, messages);
        stubAddPlayer.should.have.been.called;
    }

    function assertPlayerValidationFailedViewUpdatedWithFailureMessage(redirectView, spy, alertType, messages) {
        assertViewUpdated(redirectView, spy, alertType, messages);
        stubAddPlayer.should.not.have.been.called;
    }

    function enactRequestBodyValidationTest(incomingExpressRequest, expectedMessage, done) {
        //2. exercise
        ap.post(incomingExpressRequest, outgoingExpressResponse);

        //3. verify
        assertPlayerValidationFailedViewUpdatedWithFailureMessage('addPlayer', outgoingExpressResponseSpy, 'alert-danger', expectedMessage);

        //4. teardown
        done();
    }

    function notifiesValidationErrors(propertyname, expectedMessage, done) {
        //1. setup - changing the default behaviour
        incomingExpressRequest.body[propertyname] = '';
        //2. exercise
        enactRequestBodyValidationTest(incomingExpressRequest, [{ msg: expectedMessage }], done);
    }
    
    function assertViewCreatedWithClubAndCity() {
        outgoingExpressResponseSpy.should.have.been.calledWith('addPlayer', { clubs: [{ club: 'Club1', city: 'City1'}]});
    }

    //2. Module Exercise
    
    it("Includes club and city of the current user", function (done) {
        //1. setup
        
        //2. exercise
        ap.get(incomingGetRequest, outgoingExpressResponse);
        
        //3. verify
        assertViewCreatedWithClubAndCity();
        
        //4. teardown
        done();
    });

    it("Add a valid player", function (done) {
        //1. setup

        //2. exercise
        ap.post(incomingExpressRequest, outgoingExpressResponse);

        //3. verify
        assertPlayerCreatedAndViewUpdated('addPlayer', outgoingExpressResponseSpy,
            'alert-success',
            [{ msg: 'Player has been successfully added.' }]);

        //4. teardown
        done();
    });

    it("Notifies via response when an error during call to addPlayer in service", function (done) {
        //1. setup - changing the default behaviour
        var expectedErrorMessage = 'Add Player Service Failure',
            expectedError = new Error(expectedErrorMessage);
        stubAddPlayer.yields(expectedError);

        //2. exercise
        ap.post(incomingExpressRequest, outgoingExpressResponse);

        //3. verify
        assertPlayerCreatedAndViewUpdated('addPlayer', outgoingExpressResponseSpy, 'alert-danger', [{ msg: expectedErrorMessage }]);

{'fraction_non_alphanumeric': 0.11285016449008076, 'fraction_numerical': 0.00777589472634832, 'mean_word_length': 2.788519637462236, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '4306796', 'n_tokens_mistral': 2770, 'n_tokens_neox': 2664, 'n_words': 704}