Search code examples
testingasynchronousember.jsqunit

Ember-auth signin test fails with json


I am having some issues with testing my signin/signout and related features of my app. The app works, but the test fail.

For testing, I use a QUnit with testem (I also tried teaspoon)

test "after signin, should redirect user back to previous page", ->
    visit '/library'
    fillIn '.signin-email', '[email protected]'
    fillIn '.signin-password', 'examplepass'
    click '.signin-btn'
    andThen ->
        equal(testing().path(), 'library', "Should redirect back to library (was #{testing().path()})")

After running the test, I get a failure: (screenshot here )

Authentication: visiting restricted page as non authenticated user: after signin, should redirect user back to previous page (2, 0, 2)Rerun544 ms
{user_id: 2, auth_token: wVveiyDLuXBXu69pQ2XQwg}
Source: 
    at Test.QUnitAdapter.Test.Adapter.extend.exception (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:50149:5)
    at superWrapper [as exception] (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:13374:16)
    at Object.onerror (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:50009:22)
    at onerror (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:20453:16)
    at EventTarget.trigger (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:20286:22)
    at null.<anonymous> (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:20439:14)
    at EventTarget.trigger (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:20286:22)
    at http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:20588:17
Should redirect back to library (was signin)
Expected:   
"library"
Result: 
"signin"
Diff:   
"library" "signin" 
Source: 
    at http://localhost:7357/public/assets/spec/javascripts/integration/authentication_pages_spec.js.js:22:14
    at andThen (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:50258:20)
    at http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:49817:21
    at isolate (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:49989:14)
    at http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:49972:12
    at invokeCallback (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:20463:19)
    at null.<anonymous> (http://localhost:7357/public/assets/application-aad0a1b2c887cc25124c361787446e83.js:20513:11)

Also, auth.coffee:

App.Auth = Em.Auth.extend
  request: 'jquery'
  response: 'json'
  strategy: 'token'
  session: 'cookie'
  modules: [
    'emberData',
    'authRedirectable',
    'actionRedirectable'
  ]

  signInEndPoint: '/signin'
  signOutEndPoint: '/signout'
  tokenKey: 'auth_token'
  tokenIdKey: 'user_id'
  tokenLocation: 'param'
  emberData:
    userModel: 'user'  # create user model on login
  authRedirectable:
    route: 'signin'
  actionRedirectable:
    signInRoute: 'library'
    # signInSmart: true
    # signInBlacklist: ['signin']
    signOutRoute: 'index'

I am unable to find the source of the error, so maybe it is something to do with ember-auth. Any ideas would be very appreciated.

Update 1 [Jan 4th]:

I've written an additional test, which passes only halfway. The test is simpler than the previous in that it does not check a redirect, but only checks that the user name appears in the UI after signin.

test "after signin, TEST", ->

    visit '/library'
    fillIn '.signin-email', '[email protected]'
    fillIn '.signin-password', 'foobargaz'
    click '.signin-btn'
    andThen ->
        ok exists('.menu-signout'), "signout button exists"

The assertions passes, but I get an additional error reporting the returned JSON as seen in this screenshot. The screenshot basically shows:

  1. [Fail] {user_id: 2, auth_token: wVveiyDLuXBXu69pQ2XQwg}
  2. [Pass] signout button exists

Additionally, I've also run the tests by mocking the ajax requests with mockjax, but with the same failure.

Third, I should note that I had to patch "ember-auth-request-jquery.js" to make it work with ember testing as suggested here


Solution

  • It turns out my coffeescript was not the best in the world.

    The module function in QUnit should NOT compile to:

    module('Authentication: visiting restricted page as non authenticated user', function() {
        return setup(function() {
            return Em.run(App, App.advanceReadiness);
        });
    
    });
    

    but to:

    module('Authentication: visiting restricted page as non authenticated user', {
        setup: function() {
            Ember.run(App, App.advanceReadiness);
        },
        // This is also new
        teardown: function() {
            App.reset();
        }
    });
    

    Additionally, in my spec_helper.coffee file I had something like this:

    QUnit.testStart(function() {
        // FIXME: this below made it fail every time
        // Ember.run(function() {
        //   return App.reset();
        // });
        Ember.testing = true;
    });
    
    QUnit.testDone(function() {
        Ember.testing = false;
    });
    
    QUnit.done(function() {
        return Ember.run(function() {
            return App.reset();
        });
    });
    

    which seems to have caused some issues, so I just deleted it and the tests now pass.