<commit_before>///<reference path="../../../build/js/declarations.d.ts" />

(() => {

    let seededChance = new Chance(1);
    let fixtures = {

        buildUser: (overrides = {}) => {

            let userId = seededChance.guid();
            let defaultUser:global.IUserData = {
                _self: '/users/'+userId,
                userId: userId,
                email: seededChance.email(),
                firstName: seededChance.first(),
                lastName: seededChance.last(),
                _userCredential: {
                    userCredentialId: seededChance.guid(),
                    password: <PASSWORD>.string(),
                }
            };

            return _.merge(defaultUser, overrides);
        },
        get user():common.models.User {
            return new common.models.User(fixtures.buildUser());
        },
        get users() {
            return _.range(10).map(() => fixtures.user);
        }
    };

    const seededEmail = '<EMAIL>'; //the email that was seeded by the API

    describe('UserService', () => {

        let userService:common.services.user.UserService;
        let $httpBackend:ng.IHttpBackendService;
        let authService:NgJwtAuth.NgJwtAuthService;
        let ngRestAdapter:NgRestAdapter.NgRestAdapterService;
        let $mdDialog:ng.material.IDialogService;
        let $timeout:ng.ITimeoutService;

        beforeEach(()=> {

            module('app');

            inject((_$httpBackend_, _userService_, _ngJwtAuthService_, _ngRestAdapter_, _$mdDialog_, _$timeout_) => {

                if (!userService) { //dont rebind, so each test gets the singleton
                    $httpBackend = _$httpBackend_;
                    userService = _userService_;
                    authService = _ngJwtAuthService_;
                    ngRestAdapter = _ngRestAdapter_;
                    $mdDialog = _$mdDialog_;
                    $timeout = _$timeout_;
                }
            });

        });

        afterEach(() => {
            $httpBackend.verifyNoOutstandingExpectation();
            $httpBackend.verifyNoOutstandingRequest();
        });

        describe('Initialisation', () => {

            it('should be an injectable service', () => {

                return expect(userService).to.be.an('object');
            });

        });

        describe('All users', () => {

            it ('should return all users', () => {

                let users = _.clone(fixtures.users); //get a new user copy

                $httpBackend.expectGET('/api/users').respond(users);

                let allUsersPromise = userService.getAllUsers();

                expect(allUsersPromise).eventually.to.be.fulfilled;
                expect(allUsersPromise).eventually.to.deep.equal(users);

                $httpBackend.flush();

            });

            it('should reject the promise getting users fails', () => {

                $httpBackend.expectGET('/api/users').respond(500);

                let allUsersPromise = userService.getAllUsers();

                expect(allUsersPromise).eventually.to.be.rejected;

                $httpBackend.flush();

            });

        });

        describe('User Registration', () => {


            before(() => authService.logout()); //make sure we are logged out

            describe('Username/Password (non social)', () => {


                it('should be able to create a new user and attempt login immediately',  () => {

                    let user = _.clone(fixtures.user);
                    delete user._self;


                    $httpBackend.expectPUT(/\/api\/users\/.+/, (requestObj) => {
                        return _.isEqual(_.keys(user), _.keys(JSON.parse(requestObj))); //as we are not aware of what the userId or userCredentialId is we cannot test full equality
                    }).respond(204);
                    $httpBackend.expectGET('/api/auth/jwt/login', (headers) => /Basic .*/.test(headers['Authorization'])).respond(200);
                    //note the above auth request does not return a valid token so the login will not be successful so we can't test for that

                    userService.registerAndLogin(user.email, user._userCredential.password, user.firstName, user.lastName);

                    $httpBackend.flush();

                });

            });

            describe('Email checking', () => {


                it('should be able to poll the api to check if an email has been registered', () => {

                    let notExistingEmail = '<EMAIL>';

                    $httpBackend.expectHEAD('/api/users/email/'+notExistingEmail).respond(404);
                    $httpBackend.expectHEAD('/api/users/email/'+seededEmail).respond(200);

                    let notExistingCheck = userService.isEmailRegistered(notExistingEmail);
                    let existingCheck = userService.isEmailRegistered(seededEmail);

                    $httpBackend.flush();

                    expect(notExistingCheck).eventually.to.become(false);
                    expect(existingCheck).eventually.to.become(true);

                });

            });

        });

        describe('Password Reset', () => {

            it('should open the password reset dialog', () => {

                sinon.spy($mdDialog, 'show');

                userService.promptResetPassword();

                $timeout.flush();

                expect($mdDialog.show).to.have.been.called;

                (<any>$mdDialog).show.restore();

            });

            it('should be able to send a reset password email', () => {

                let email = '<EMAIL>';

                $httpBackend.expectDELETE('/api/users/' + email + '/password').respond(202);

                let resetPasswordPromise = userService.resetPassword(email);

                expect(resetPasswordPromise).eventually.to.be.fulfilled;

                $httpBackend.flush();

            });

        });
    });


})();<commit_msg>Added frontend unit test for confirmEmail in UserService.<commit_after>///<reference path="../../../build/js/declarations.d.ts" />

(() => {

    let seededChance = new Chance(1);
    let fixtures = {

        buildUser: (overrides = {}) => {

            let userId = seededChance.guid();
            let defaultUser:global.IUserData = {
                _self: '/users/'+userId,
                userId: userId,
                email: seededChance.email(),
                firstName: seededChance.first(),
                lastName: seededChance.last(),
                _userCredential: {
                    userCredentialId: seededChance.guid(),
                    password: <PASSWORD>(),
                }
            };

            return _.merge(defaultUser, overrides);
        },
        get user():common.models.User {
            return new common.models.User(fixtures.buildUser());
        },
        get users() {
            return _.range(10).map(() => fixtures.user);
        }
    };

    const seededEmail = '<EMAIL>'; //the email that was seeded by the API

    describe('UserService', () => {

        let userService:common.services.user.UserService;
        let $httpBackend:ng.IHttpBackendService;
        let authService:NgJwtAuth.NgJwtAuthService;
        let ngRestAdapter:NgRestAdapter.NgRestAdapterService;
        let $mdDialog:ng.material.IDialogService;
        let $timeout:ng.ITimeoutService;

        beforeEach(()=> {

            module('app');

            inject((_$httpBackend_, _userService_, _ngJwtAuthService_, _ngRestAdapter_, _$mdDialog_, _$timeout_) => {

                if (!userService) { //dont rebind, so each test gets the singleton
                    $httpBackend = _$httpBackend_;
                    userService = _userService_;
                    authService = _ngJwtAuthService_;
                    ngRestAdapter = _ngRestAdapter_;
                    $mdDialog = _$mdDialog_;
                    $timeout = _$timeout_;
                }
            });

        });

        afterEach(() => {
            $httpBackend.verifyNoOutstandingExpectation();
            $httpBackend.verifyNoOutstandingRequest();
        });

        describe('Initialisation', () => {

            it('should be an injectable service', () => {

                return expect(userService).to.be.an('object');
            });

        });

        describe('All users', () => {

            it ('should return all users', () => {

                let users = _.clone(fixtures.users); //get a new user copy

                $httpBackend.expectGET('/api/users').respond(users);

                let allUsersPromise = userService.getAllUsers();

                expect(allUsersPromise).eventually.to.be.fulfilled;
                expect(allUsersPromise).eventually.to.deep.equal(users);

                $httpBackend.flush();

            });

            it('should reject the promise getting users fails', () => {

                $httpBackend.expectGET('/api/users').respond(500);

                let allUsersPromise = userService.getAllUsers();

                expect(allUsersPromise).eventually.to.be.rejected;

                $httpBackend.flush();

            });

        });

        describe('User Registration', () => {


            before(() => authService.logout()); //make sure we are logged out

            describe('Username/Password (non social)', () => {


                it('should be able to create a new user and attempt login immediately',  () => {

                    let user = _.clone(fixtures.user);
                    delete user._self;


                    $httpBackend.expectPUT(/\/api\/users\/.+/, (requestObj) => {
                        return _.isEqual(_.keys(user), _.keys(JSON.parse(requestObj))); //as we are not aware of what the userId or userCredentialId is we cannot test full equality
                    }).respond(204);
                    $httpBackend.expectGET('/api/auth/jwt/login', (headers) => /Basic .*/.test(headers['Authorization'])).respond(200);
                    //note the above auth request does not return a valid token so the login will not be successful so we can't test for that

                    userService.registerAndLogin(user.email, user._userCredential.password, user.firstName, user.lastName);

                    $httpBackend.flush();

                });

            });

            describe('Email checking', () => {


                it('should be able to poll the api to check if an email has been registered', () => {

                    let notExistingEmail = '<EMAIL>';

                    $httpBackend.expectHEAD('/api/users/email/'+notExistingEmail).respond(404);
                    $httpBackend.expectHEAD('/api/users/email/'+seededEmail).respond(200);

                    let notExistingCheck = userService.isEmailRegistered(notExistingEmail);
                    let existingCheck = userService.isEmailRegistered(seededEmail);

                    $httpBackend.flush();

                    expect(notExistingCheck).eventually.to.become(false);
                    expect(existingCheck).eventually.to.become(true);

                });

            });

        });

        describe('Password Reset', () => {

            it('should open the password reset dialog', () => {

                sinon.spy($mdDialog, 'show');

                userService.promptResetPassword();

                $timeout.flush();

                expect($mdDialog.show).to.have.been.called;

                (<any>$mdDialog).show.restore();

            });

            it('should be able to send a reset password email', () => {

                let email = '<EMAIL>';

                $httpBackend.expectDELETE('/api/users/' + email + '/password').respond(202);

                let resetPasswordPromise = userService.resetPassword(email);

                expect(resetPasswordPromise).eventually.to.be.fulfilled;

                $httpBackend.flush();

            });

        });

        describe('Change Email', () => {

            it('should be able to send a patch request to confirm email change', () => {

                let user = _.clone(fixtures.user);

                const emailToken = 'cf8a43a2646fd46c2081960ff1150a6b48d5ed062da3d59559af5030eea21548';

                $httpBackend.expectPATCH('/api/users/' + user.userId, user,
                    (headers) => {
                        return headers['email-confirm-token'] == emailToken;
                    }).respond(202);

                let emailConfirmationPromise = userService.confirmEmail(user, emailToken);

                expect(emailConfirmationPromise).eventually.to.be.fulfilled;

                $httpBackend.flush();
            });

        });
    });


})();
{'fraction_non_alphanumeric': 0.1260299772424076, 'fraction_numerical': 0.006827277721101781, 'mean_word_length': 1.6931530008453086, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 64, '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': '7380776', 'n_tokens_mistral': 3426, 'n_tokens_neox': 3112, 'n_words': 631}