<commit_before>describe('Service: facebook', function () {
    // load the controller's module
    beforeEach(module('coreGamesUi.services'));

    beforeEach(module(function ($provide) {
    }));

    var service, location, http, q, window, rootScope;
    var fbAppId = 'someid';
    var fbPerms = 'email,profile';
    var fbSourceId = 'FBID1';
    var authResponse = {field: 'X134', userID: fbSourceId};

    describe('without a cordova facebook plugin available', function () {
        beforeEach(inject(function ($injector, $q, $location, $httpBackend, $window, $rootScope) {
            rootScope = $rootScope;
            location = $location;
            http = $httpBackend;
            window = $window;
            q = $q;
            spyOn(location, 'path');

            window.FB = {
                init: function (params) {
                    console.log('init called');
                    expect(params).toEqual({appId: 'someid', xfbml: false, version: 'v2.2'});
                }
            };

            service = $injector.get('jtbFacebook');
        }));

        describe('with good http call', function () {
            var finalCheck;
            beforeEach(function () {
                finalCheck = false;
                http.expectGET('/api/social/apis').respond({facebookAppId: fbAppId, facebookPermissions: fbPerms});
            });
            afterEach(function () {
                http.flush();
                window.fbAsyncInit();
                rootScope.$apply();
                expect(finalCheck).toEqual(true);
            });

            describe('with connected status', function () {
                beforeEach(function () {
                    window.FB.getLoginStatus = function (cb) {
                        cb({status: 'connected', authResponse: authResponse});
                    };
                });

                it('cold start initialization - can auto signin', function () {
                    service.canAutoSignIn().then(function (data) {
                        expect(data).toEqual({auto: true, permissions: 'email,profile'});
                        expect(service.currentAuthorization()).toEqual(authResponse);
                        finalCheck = true;
                    }, function () {
                        fail('should not be here');
                    });
                    window.FB.api = function (path, cb) {
                        expect(path).toEqual('/me/permissions');
                        cb({
                            data: [{permission: 'email', status: 'granted'}, {
                                permission: 'profile',
                                status: 'granted'
                            }, {permission: 'ignoreMe', status: '??'}]
                        });
                    };
                });

                it('cold start initialization - can auto signin even if some permissions', function () {
                    service.canAutoSignIn().then(function (data) {
                        expect(data).toEqual({auto: true, permissions: 'email,profile'});
                        finalCheck = true;
                    }, function () {
                        fail('should not be here');
                    });
                    window.FB.api = function (path, cb) {
                        expect(path).toEqual('/me/permissions');
                        cb({
                            data: [{permission: 'email', status: 'declined'}, {
                                permission: 'profile',
                                status: 'granted'
                            }]
                        });
                    };
                });

                it('cold start initialization - cannot auto signin if some permissions missing', function () {
                    service.canAutoSignIn().then(function (data) {
                        fail('should not be here');
                    }, function () {
                        console.log('failed due to permissions');
                        finalCheck = true;
                    });
                    window.FB.api = function (path, cb) {
                        expect(path).toEqual('/me/permissions');
                        cb({data: [{permission: 'profile', status: 'granted'}]});
                    };
                });

                it('cold start initialization - cannot auto signin if some permissions error', function () {
                    service.canAutoSignIn().then(function (data) {
                        fail('should not be here');
                    }, function () {
                        console.log('failed due to error checking perms');
                        finalCheck = true;
                    });
                    window.FB.api = function (path, cb) {
                        expect(path).toEqual('/me/permissions');
                        cb({error: 'bad stuff'});
                    };
                });

                it('cold start initialization - player and fb matches successfully', function () {
                    var player = {source:'facebook', sourceId: fbSourceId};
                    service.playerAndFBMatch(player).then(function (data) {
                        expect(data).toEqual(true);
                        finalCheck = true;
                    }, function () {
                        fail('should not be here');
                    });
                });

                it('cold start initialization - player and fb do not match id successfully', function () {
                    var player = {source:'facebook', sourceId: fbSourceId + 'X'};
                    service.playerAndFBMatch(player).then(function (data) {
                        expect(data).toEqual(false);
                        finalCheck = true;
                    }, function () {
                        fail('should not be here');
                    });
                });
            });

            describe('with not connected status', function () {
                beforeEach(function () {
                    window.FB.getLoginStatus = function (cb) {
                        cb({status: 'not connected'});
                    };
                });

                it('cold start initialization - cannot auto signin if status not connected', function () {
                    service.canAutoSignIn().then(function (data) {
                        fail('should not be here');
                    }, function () {
                        console.log('failed due to not connected');
                        finalCheck = true;
                    });
                });

                it('cold start initialization - initiate login successfully', function () {
                    window.FB.login = function (cb, params) {
                        expect(params).toEqual({scope: fbPerms});
                        cb({status: 'connected', authResponse: authResponse});
                    };
                    service.initiateFBLogin().then(function (data) {
                        expect(data).toEqual({auto: true, permissions: 'email,profile'});
                        expect(service.currentAuthorization()).toEqual(authResponse);
                        finalCheck = true;
                    }, function () {
                        fail('should not be here');
                    });
                });

                it('cold start initialization - initiate login successfully', function () {
                    window.FB.login = function (cb, params) {
                        expect(params).toEqual({scope: fbPerms});
                        cb({status: 'error'});
                    };
                    service.initiateFBLogin().then(function (data) {
                        fail('should not be here');
                    }, function () {
                        console.log('failed to login');
                        finalCheck = true;
                    });
                });

                it('cold start initialization - initiate login throws exceptions', function () {
                    window.FB.login = function (cb, params) {
                        throw 'blah';
                    };
                    service.initiateFBLogin().then(function (data) {
                        fail('should not be here');
                    }, function () {
                        console.log('failed to login');
                        finalCheck = true;
                    });
                });
<commit_msg>fb tests<commit_after>describe('Service: facebook', function () {
    // load the controller's module
    beforeEach(module('coreGamesUi.services'));

    beforeEach(module(function ($provide) {
    }));

    var service, location, http, q, window, rootScope;
    var fbAppId = 'someid';
    var fbPerms = 'email,profile';
    var fbSourceId = 'FBID1';
    var authResponse = {field: 'X134', userID: fbSourceId};

    describe('without a cordova facebook plugin available', function () {
        beforeEach(inject(function ($injector, $q, $location, $httpBackend, $window, $rootScope) {
            rootScope = $rootScope;
            location = $location;
            http = $httpBackend;
            window = $window;
            q = $q;
            spyOn(location, 'path');

            window.FB = {
                init: function (params) {
                    console.log('init called');
                    expect(params).toEqual({appId: 'someid', xfbml: false, version: 'v2.2'});
                }
            };

            service = $injector.get('jtbFacebook');
        }));

        describe('with good http call', function () {
            var finalCheck;
            beforeEach(function () {
                finalCheck = false;
                http.expectGET('/api/social/apis').respond({facebookAppId: fbAppId, facebookPermissions: fbPerms});
            });
            afterEach(function () {
                http.flush();
                window.fbAsyncInit();
                rootScope.$apply();
                expect(finalCheck).toEqual(true);
            });

            describe('with connected status', function () {
                beforeEach(function () {
                    window.FB.getLoginStatus = function (cb) {
                        cb({status: 'connected', authResponse: authResponse});
                    };
                });

                it('cold start initialization - can auto signin', function () {
                    service.canAutoSignIn().then(function (data) {
                        expect(data).toEqual({auto: true, permissions: 'email,profile'});
                        expect(service.currentAuthorization()).toEqual(authResponse);
                        finalCheck = true;
                    }, function () {
                        fail('should not be here');
                    });
                    window.FB.api = function (path, cb) {
                        expect(path).toEqual('/me/permissions');
                        cb({
                            data: [{permission: 'email', status: 'granted'}, {
                                permission: 'profile',
                                status: 'granted'
                            }, {permission: 'ignoreMe', status: '??'}]
                        });
                    };
                });

                it('cold start initialization - can auto signin even if some permissions', function () {
                    service.canAutoSignIn().then(function (data) {
                        expect(data).toEqual({auto: true, permissions: 'email,profile'});
                        finalCheck = true;
                    }, function () {
                        fail('should not be here');
                    });
                    window.FB.api = function (path, cb) {
                        expect(path).toEqual('/me/permissions');
                        cb({
                            data: [{permission: 'email', status: 'declined'}, {
                                permission: 'profile',
                                status: 'granted'
                            }]
                        });
                    };
                });

                it('cold start initialization - cannot auto signin if some permissions missing', function () {
                    service.canAutoSignIn().then(function (data) {
                        fail('should not be here');
                    }, function () {
                        console.log('failed due to permissions');
                        finalCheck = true;
                    });
                    window.FB.api = function (path, cb) {
                        expect(path).toEqual('/me/permissions');
                        cb({data: [{permission: 'profile', status: 'granted'}]});
                    };
                });

                it('cold start initialization - cannot auto signin if some permissions error', function () {
                    service.canAutoSignIn().then(function (data) {
                        fail('should not be here');
                    }, function () {
                        console.log('failed due to error checking perms');
                        finalCheck = true;
                    });
                    window.FB.api = function (path, cb) {
                        expect(path).toEqual('/me/permissions');
                        cb({error: 'bad stuff'});
                    };
                });

                it('cold start initialization - player and fb matches successfully', function () {
                    var player = {source:'facebook', sourceId: fbSourceId};
                    service.playerAndFBMatch(player).then(function (data) {
                        expect(data).toEqual(true);
                        finalCheck = true;
                    }, function () {
                        fail('should not be here');
                    });
                });

                it('cold start initialization - player and fb do not match id successfully', function () {
                    var player = {source:'facebook', sourceId: fbSourceId + 'X'};
                    service.playerAndFBMatch(player).then(function (data) {
                        expect(data).toEqual(false);
                        finalCheck = true;
                    }, function () {
                        fail('should not be here');
                    });
                });
            });

            describe('with not connected status', function () {
                beforeEach(function () {
                    window.FB.getLoginStatus = function (cb) {
                        cb({status: 'not connected'});
                    };
                });

                it('cold start initialization - cannot auto signin if status not connected', function () {
                    service.canAutoSignIn().then(function (data) {
                        fail('should not be here');
                    }, function () {
                        console.log('failed due to not connected');
                        finalCheck = true;
                    });
                });

                it('cold start initialization - initiate login successfully', function () {
                    window.FB.login = function (cb, params) {
                        expect(params).toEqual({scope: fbPerms});
                        cb({status: 'connected', authResponse: authResponse});
                    };
                    service.initiateFBLogin().then(function (data) {
                        expect(data).toEqual({auto: true, permissions: 'email,profile'});
                        expect(service.currentAuthorization()).toEqual(authResponse);
                        finalCheck = true;
                    }, function () {
                        fail('should not be here');
                    });
                });

                it('cold start initialization - initiate login successfully', function () {
                    window.FB.login = function (cb, params) {
                        expect(params).toEqual({scope: fbPerms});
                        cb({status: 'error'});
                    };
                    service.initiateFBLogin().then(function (data) {
                        fail('should not be here');
                    }, function () {
                        console.log('failed to login');
                        finalCheck = true;
                    });
                });

                it('cold start initialization - initiate login throws exceptions', function () {
                    window.FB.login = function (cb, params) {
                        throw 'blah';
                    };
                    service.initiateFBLogin().then(function (data) {
                        fail('should not be here');
                    }, function () {
                        console.log('failed to login');
                        finalCheck = true;
                    });
                });

                it('cold start initialization - player and fb do not match if not logged in', function () {
                    var player = {source:'facebook', sourceId: fbSourceId};
                    service.playerAndFBMatch(player).then(function (data) {
                        expect(data).toEqual(false);
                        finalCheck = true;
                    }, function () {
                        fail('should not be here');
                    });
                });
{'fraction_non_alphanumeric': 0.113914635541824, 'fraction_numerical': 0.0006865774116031583, 'mean_word_length': 1.149938499384994, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7382069', 'n_tokens_mistral': 3921, 'n_tokens_neox': 3591, 'n_words': 880}