AngularJS + Jasmine + JWT Token in $http requests
Asked Answered
G

0

3

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?

Guitarfish answered 19/6, 2015 at 21:43 Comment(2)
could you include the code for your Remote factory and any error message it's producing?Ridgeway
A bit late commenting here but it sounds pretty obvious that you had to mock the resource provider.Burrow

© 2022 - 2024 — McMap. All rights reserved.