Search code examples
javascriptjasminedropboxdropbox-apikarma-runner

Karma/Jasmine Tests and dropbox authentication in javascript


I'm attempting to create some tests using Karma and Jasmine for a javascript application that uses the Dropbox Datastore api. Here is a simplified test using the introductory Dropbox code from https://www.dropbox.com/developers/datastore/tutorial/js

I've manually authorized the application with Dropbox in the browser before running the test, but when I run the test, it says the client is not authenticated and no error occurs. Is there something extra that needs to be done for it to authenticate when running the tests?

'use strict';

describe('dropbox', function () {

    var client = null;

    beforeEach(function() {
    client = new Dropbox.Client({key: '46tjf8x15q98xic'});

    // Try to finish OAuth authorization.
    client.authenticate({interactive: false}, function (error) {
        if (error) {
            alert('Authentication error: ' + error);
        }
    });
});

it('client is not null', function() {
    expect( client ).not.toBeNull();
});

it('authenticated is true', function() {
    expect( client.isAuthenticated() ).toEqual( true );
});
});


Running "karma:unit" (karma) task
INFO [karma]: Karma v0.10.9 server started at http://localhost:8080/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 33.0.1750 (Mac OS X 10.9.2)]: Connected on socket BKoS8rqqeeL7fg3cHEQl
Chrome 33.0.1750 (Mac OS X 10.9.2) dropbox authenticated is true FAILED
Expected false to equal true.
Error: Expected false to equal true.
at null.<anonymous> (/Users/davidsmith/Sites/myapp/test/spec/dropbox.js:23:38)
Chrome 33.0.1750 (Mac OS X 10.9.2): Executed 2 of 2 (1 FAILED) (0.301 secs / 0.009 secs)
Warning: Task "karma:unit" failed. Use --force to continue.

Aborted due to warnings.

Solution

  • So, to be able to run the tests, I looked at the local storage for my browser and copied my app's dropbox key/value pair.

    In my test script, I added that key/value pair with

    var value = '{"key":"46tjf8x15q98xic","token":"srMz5w4ReBsAAAAAAAAAAWfQfibrbJfeI7LVKsbMvxRfX1pdpS6SOKqvN6DcgK1B","uid":"1407454"}';
    
    localStorage.setItem('dropbox-auth:default:cHKvNCKVzU7Jmnyaj1InU8TBCOc', value );
    

    Another related problem (not shown above) is that openDefaultDatastore is asyncronous so in my test script I added some code to wait until openDefaultDatastore finishes before running my tests.