<commit_before>﻿'use strict';

describe('Module: tubular.services', function () {

    describe('Interceptor: Auth', function () {
        var AuthInterceptor, $httpBackend, tubularHttp;

        beforeEach(function () {
            module('tubular.services');

            inject(function (_tubularAuthInterceptor_, _$httpBackend_, _tubularHttp_) {
                AuthInterceptor = _tubularAuthInterceptor_;
                $httpBackend = _$httpBackend_;
                tubularHttp = _tubularHttp_;
            });
        });

        it('should be defined', function () {
            expect(AuthInterceptor).toBeDefined();
            expect($httpBackend).toBeDefined();
            expect(AuthInterceptor.request).toBeDefined();
            expect(AuthInterceptor.requestError).toBeDefined();
            expect(AuthInterceptor.response).toBeDefined();
            expect(AuthInterceptor.responseError).toBeDefined();
        })

        it('should not add any authorization header', function () {
            var config = {
                method: 'GET',
                url: '/other/file.html'
            };

            expect(AuthInterceptor.request(config).headers).toBeDefined();
            expect(AuthInterceptor.request(config).headers.Authorization).toBeUndefined();
        });

        it('should add authorization header', function () {
            var config = {
                method: 'GET',
                url: '/api/dummy'
            };

            tubularHttp.setRequireAuthentication(true);
            tubularHttp.userData.bearerToken = "<PASSWORD>";

            expect(AuthInterceptor.request(config).headers).toBeDefined();
            expect(AuthInterceptor.request(config).headers.Authorization).toBeDefined();
            expect(AuthInterceptor.request(config).headers.Authorization).toContain('yeah');
        });

        it('should NOT try to use refresh tokens', done => {
            var config = {
                method: 'GET',
                url: '/api/dummy',
                headers : {
                }
            };

            var rejection = {
                config : config,
                status : 401
            };

            tubularHttp.setRequireAuthentication(true);
            expect(tubularHttp.useRefreshTokens).toBe(false);

            tubularHttp.userData.bearerToken = "<PASSWORD>";
            expect(rejection.triedRefreshTokens).toBeUndefined();
            
            AuthInterceptor
                .responseError(rejection)
                .then(function () { }, rejection => {
                    expect(rejection).toBeDefined();
                    expect(rejection.triedRefreshTokens).toBeUndefined();
                    
                    done();
                });
        });

        it('should try to use refresh tokens', done => {
            var config = {
                method: 'GET',
                url: '/api/dummy',
                headers : {
                }
            };

            var rejection = {
                config : config,
                status : 401
            };

            tubularHttp.setRequireAuthentication(true);
            expect(tubularHttp.useRefreshTokens).toBe(false);

            tubularHttp.useRefreshTokens = true;
            tubularHttp.userData.refreshToken = '<PASSWORD>';

            tubularHttp.userData.bearerToken = "<PASSWORD>";
            expect(rejection.triedRefreshTokens).toBeUndefined();
            
            AuthInterceptor
                .responseError(rejection)
                .then(function () { }, rejection => {
                    expect(rejection).toBeDefined();
                    expect(rejection.triedRefreshTokens).toBeDefined();
                    expect(rejection.triedRefreshTokens).toBe(true);
                    
                    done();
                });
        });
    });
});<commit_msg>Fixed unit tests on refresh tokens.<commit_after>﻿'use strict';

describe('Module: tubular.services', function () {

    describe('Interceptor: Auth', function () {
        var AuthInterceptor, $httpBackend, tubularHttp;

        beforeEach(function () {
            module('tubular.services');

            inject(function (_tubularAuthInterceptor_, _$httpBackend_, _tubularHttp_) {
                AuthInterceptor = _tubularAuthInterceptor_;
                $httpBackend = _$httpBackend_;
                tubularHttp = _tubularHttp_;
            });
        });

        it('should be defined', function () {
            expect(AuthInterceptor).toBeDefined();
            expect($httpBackend).toBeDefined();
            expect(AuthInterceptor.request).toBeDefined();
            expect(AuthInterceptor.requestError).toBeDefined();
            expect(AuthInterceptor.response).toBeDefined();
            expect(AuthInterceptor.responseError).toBeDefined();
        })

        it('should not add any authorization header', function () {
            var config = {
                method: 'GET',
                url: '/other/file.html'
            };

            expect(AuthInterceptor.request(config).headers).toBeDefined();
            expect(AuthInterceptor.request(config).headers.Authorization).toBeUndefined();
        });

        it('should add authorization header', function () {
            var config = {
                method: 'GET',
                url: '/api/dummy'
            };

            tubularHttp.setRequireAuthentication(true);
            tubularHttp.userData.bearerToken = "<PASSWORD>";

            expect(AuthInterceptor.request(config).headers).toBeDefined();
            expect(AuthInterceptor.request(config).headers.Authorization).toBeDefined();
            expect(AuthInterceptor.request(config).headers.Authorization).toContain('yeah');
        });

        it('should NOT try to use refresh tokens', done => {
            var config = {
                method: 'GET',
                url: '/api/dummy',
                headers: {
                }
            };

            var rejection = {
                config: config,
                status: 401
            };

            tubularHttp.setRequireAuthentication(true);
            expect(tubularHttp.useRefreshTokens).toBe(false);

            tubularHttp.userData.bearerToken = "<PASSWORD>";
            expect(rejection.triedRefreshTokens).toBeUndefined();

            AuthInterceptor
                .responseError(rejection)
                .then(function () { }, rejection => {
                    expect(rejection).toBeDefined();
                    expect(rejection.triedRefreshTokens).toBeUndefined();

                    done();
                });

            $httpBackend.flush();
        });

        it('should try to use refresh tokens', done => {
            var config = {
                method: 'GET',
                url: '/api/dummy',
                headers: {
                }
            };

            var rejection = {
                config: config,
                status: 401
            };

            tubularHttp.setRequireAuthentication(true);
            expect(tubularHttp.useRefreshTokens).toBe(false);

            tubularHttp.useRefreshTokens = true;
            tubularHttp.userData.refreshToken = '<PASSWORD>';

            tubularHttp.userData.bearerToken = "<PASSWORD>";
            expect(rejection.triedRefreshTokens).toBeUndefined();

            $httpBackend.expectPOST(tubularHttp.refreshTokenUrl, data => {
                return data === 'grant_type=refresh_token&refresh_token=' + tubularHttp.userData.refreshToken;
            }).respond(200, { access_token: '<PASSWORD>', refresh_token: '<PASSWORD>' });

            $httpBackend.expectGET('/api/dummy').respond(200);

            AuthInterceptor
                .responseError(rejection)
                .then(function (resp) {
                    expect(resp).toBeDefined();
                    expect(tubularHttp.userData.bearerToken).not.toBe('original_bearer');
                    expect(tubularHttp.userData.refreshToken).not.toBe('original_refresh');
                    done();
                }, rejection => {

                });

            $httpBackend.flush();
        });
    });
});
{'fraction_non_alphanumeric': 0.11401686423072223, 'fraction_numerical': 0.0021996822681168275, 'mean_word_length': 1.6692759295499022, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 22, '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': '5563645', 'n_tokens_mistral': 2046, 'n_tokens_neox': 1915, 'n_words': 292}