<commit_before>describe('cliffhanger.users module', function () {
    beforeEach(angular.mock.module('ngRoute'));
    beforeEach(angular.mock.module('cliffhanger.users'));

    var scope, root, mockUserService, loginCtrl, location;
    describe('LoginCtrl', function () {
        beforeEach(inject(function ($controller, $rootScope, $location, $q, $log, userService) {
            scope = $rootScope.$new();
            root = $rootScope;
            location = $location;
            mockUserService = userService;

            //create mockUserService spies
            serviceError = false;
            spyOn(mockUserService, 'login').and.callFake(function () {
                //TODO
                var deferred = $q.defer();

                if (!serviceError) {

                    var user = {
                        username: 'testUsername',
                        password: '<PASSWORD>',
                        role: {
                            roleID: 'DEVELOPER'
                        }
                    }

                    var response = {
                        data: user,
                        status: 'Success'
                    }

                    deferred.resolve(response);
                    root.user = user;
                    root.$apply();
                } else {
                    var badResponse = {
                        data: 'Unsuccessful login!',
                        status: 'Error'
                    }
                    deferred.resolve(badResponse);
                }
                return deferred.promise;
            });
            spyOn(mockUserService, 'register').and.callFake(function () {
                //TODO
                var deferred = $q.defer();

                if (!serviceError) {

                    var user = {
                        username: 'testUsername',
                        password: '<PASSWORD>',
                        role: {
                            roleID: 'DEVELOPER'
                        }
                    }

                    var response = {
                        data: user,
                        status: 'Success'
                    }

                    deferred.resolve(response);
                    root.$apply();
                } else {
                    var badResponse = {
                        data: 'Unsuccessful register!',
                        status: 'Error'
                    }
                    deferred.resolve(badResponse);
                }
                return deferred.promise;
            });

            loginCtrl = $controller('LoginCtrl', {
                $scope: scope,
                $rootScope: root,
                userService: mockUserService,
                $location: location
            });



        }));

        it('should be defined', function () {
            expect(loginCtrl).toBeDefined();
        })

        it('should be able to login', function () {
            root.$apply();

            scope.login();
            scope.$apply();

            expect(mockUserService.login).toHaveBeenCalled();
            expect(root.theme.color).toBe('green');
            expect(location.path()).toBe('/developer/datasets');
        })


        it('should display alert if failed to login', function () {
            serviceError = true;
            scope.login();
            scope.$apply();

            expect(mockUserService.login).toHaveBeenCalled();
            expect(scope.alerts[0]).toEqual('Unsuccessful login!');

            expect(root.theme.color).not.toBe('green');
            expect(location.path()).not.toBe('/developer/datasets');
        })

    })
})<commit_msg>register user unit tests<commit_after>describe('cliffhanger.users module', function () {
    beforeEach(angular.mock.module('ngRoute'));
    beforeEach(angular.mock.module('cliffhanger.users'));

    var scope, root, mockUserService, loginCtrl, location;
    describe('LoginCtrl', function () {
        beforeEach(inject(function ($controller, $rootScope, $location, $q, $log, userService) {
            scope = $rootScope.$new();
            root = $rootScope;
            location = $location;
            mockUserService = userService;

            scope.alerts = [];

            //create mockUserService spies
            serviceError = false;
            spyOn(mockUserService, 'login').and.callFake(function () {
                //TODO
                var deferred = $q.defer();

                if (!serviceError) {

                    var user = {
                        username: 'testUsername',
                        password: '<PASSWORD>',
                        role: {
                            roleID: 'DEVELOPER'
                        }
                    }

                    var response = {
                        data: user,
                        status: 'Success'
                    }

                    deferred.resolve(response);
                    root.user = user;
                    root.$apply();
                } else {
                    var badResponse = {
                        data: 'Unsuccessful login!',
                        status: 'Error'
                    }
                    deferred.resolve(badResponse);
                }
                return deferred.promise;
            });
            spyOn(mockUserService, 'register').and.callFake(function () {
                //TODO
                var deferred = $q.defer();

                if (!serviceError) {

                    var user = {
                        username: 'testUsername',
                        password: '<PASSWORD>',
                        role: {
                            roleID: 'DEVELOPER'
                        }
                    }

                    var response = {
                        data: user,
                        status: 'Success'
                    }

                    deferred.resolve(response);
                } else {
                    var badResponse = {
                        data: 'Unsuccessful register!',
                        status: 'Error'
                    }
                    deferred.resolve(badResponse);
                }
                return deferred.promise;
            });

            loginCtrl = $controller('LoginCtrl', {
                $scope: scope,
                $rootScope: root,
                userService: mockUserService,
                $location: location
            });



        }));

        it('should be defined', function () {
            expect(loginCtrl).toBeDefined();
        })

        it('should be able to login', function () {
            root.$apply();
            scope.login();
            scope.$apply();

            expect(mockUserService.login).toHaveBeenCalled();
            expect(root.theme.color).toBe('green');
            expect(location.path()).toBe('/developer/datasets');
        })


        it('should display alert if failed to login', function () {
            serviceError = true;
            scope.login();
            scope.$apply();

            expect(mockUserService.login).toHaveBeenCalled();
            expect(scope.alerts[0]).toEqual('Unsuccessful login!');

            expect(root.theme.color).not.toBe('green');
            expect(location.path()).not.toBe('/developer/datasets');
        })

        it('should be able to register a new user', function () {
            spyOn(scope, 'login');

            scope.register();
            scope.$apply();

            expect(mockUserService.register).toHaveBeenCalled();
            expect(scope.login).toHaveBeenCalled();
        })

        it('should display alert if failed to register a new user', function () {
            spyOn(scope, 'login');
            serviceError = true;

            scope.register();
            scope.$apply();

            expect(mockUserService.register).toHaveBeenCalled();
            expect(scope.login).not.toHaveBeenCalled();
            expect(scope.alerts[0]).toEqual('Unsuccessful register!');
        })

    })
})
{'fraction_non_alphanumeric': 0.11133176120143974, 'fraction_numerical': 0.00037234702742956435, 'mean_word_length': 1.1374005305039787, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6485199', 'n_tokens_mistral': 1912, 'n_tokens_neox': 1775, 'n_words': 341}