AngularJS + Jasmine + JWT Token in $http requests

918 views Asked by At

I'm trying to build some test with an API that works with JWT authentication tokens, but the factories that I'm trying to test use $resouce, which needs to be configured with JWT Token. In the app I configure it in the .config, but in Jasmine I have no idea how it should be, because Jasmine runs before the app.

I'm trying to find the way to configure Jasmine's requests or maybe run the tests after the app is configured.

Here I'm trying to configure the Jasmine's requests.

describe("Sync.Remote", function () {
    var Remote = angular.injector(['sync.remote']).get('Remote')('Test', 'test')
      , testingObject = { name: "Mario", lastname: "López" }

    beforeEach(inject(function (_$httpProvider_, _jwtInterceptorProvider_, _$resourceProvider_) {
        $httpProvider = _$httpProvider_
        jwtInterceptorProvider = _jwtInterceptorProvider_
        $resourceProvider = _$resourceProvider_

        jwtInterceptorProvider.authPrefix = "JWT "
        jwtInterceptorProvider.tokenGetter = function(config) {
            if(config.url.indexOf("http://some.website.com") === 0) {
                return "XXXXXXX-JWT-TOKEN"
            }
        }

        $httpProvider.interceptors.push('jwtInterceptor')
        $httpProvider.defaults.withCredentials = true
        $resourceProvider.defaults.stripTrailingSlashes = false
    }))

    it("#_create es capaz de crear un objeto en la nube", function (done) {
        Remote._create(testingObject)
        .then(function (item) {
            expect(item.id).not.toBe(undefined)
        }, function (err) {
            expect(0).toEqual(1)
        })
        .finally(done)
    })
})

But it doesn't work because the $resouce service is inside the Remote factory, it is a dependecy actually. Any ideas?

0

There are 0 answers