ember.jsfirebaseemberfire

Unable to mock session with Emberfire


I'm writing a very basic acceptance test against a route that has both admin and non-admin capabilities. My test makes an assertion that if I'm coming to the app for the first time, I don't see logged in capabilities. In my application, I'm using password authentication as follows:

this.get('session').open('firebase', {
  provider: 'password',
  email: email,
  password: password
});

I have found that when I am not authenticated in the app, then run the acceptance test, it passes. However, if I then log in on the app, then run tests, my assertion fails because the session is restored, when I think it shouldn't be. Here's the test:

import { test } from 'qunit';
import moduleForAcceptance from 'app/tests/helpers/module-for-acceptance';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';
import replaceAppRef from '../helpers/replace-app-ref';
import replaceFirebaseAppService from '../helpers/replace-firebase-app-service';
import stubFirebase from '../helpers/stub-firebase';
import unstubFirebase from '../helpers/unstub-firebase';
import { emptyApplication } from '../helpers/create-test-ref';

moduleForAcceptance('Acceptance | index', {
  beforeEach: function() {
    stubFirebase();
    application = startApp();
    replaceFirebaseAppService(application, { });
    replaceAppRef(application, emptyApplication());
  },
  afterEach: function() {
    unstubFirebase();
    destroyApp(application);
  }
});

test('empty app - not authenticated', function(assert) {
  visit('/');
  andThen(function() {
    assert.equal(currentURL(), page.url, 'on the correct page');

    // this works if there's no session - fails otherwise
    assert.notOk(page.something.isVisible, 'cannot do something');
  });
});

I think replaceFirebaseAppService should be overriding the torii-adapter but it doesn't appear to be. Any help would be greatly appreciated.

I'm using:

Ember      : 2.7.0
Ember Data : 2.7.0
Firebase   : 3.2.1
EmberFire  : 2.0.1
jQuery     : 2.2.4

Solution

  • Upon looking at Emberfire closer, replaceFirebaseAppService is trying to replace the torii adapter registered at torii-adapter:firebase when it was being registered by my application as torii-adapter:application.

    What I ended up doing was basically replicating replaceFirebaseAppService in my own helper:

    import stubFirebase from '../helpers/stub-firebase';
    import startApp from '../helpers/start-app';
    import replaceAppRef from '../helpers/replace-app-ref';
    import createOfflineRef from './create-offline-ref';
    
    export default function startFirebaseApp(fixtures = { }) {
      stubFirebase();
      let application = startApp();
    
      // override default torii-adapter
      const mock = { };
      application.register('service:firebaseMock', mock, {
        instantiate: false,
        singleton: true
      });
      application.inject('torii-provider:application', 'firebaseApp', 'service:firebaseMock');
      application.inject('torii-adapter:application', 'firebaseApp', 'service:firebaseMock');
    
      // setup any fixture data and return instance
      replaceAppRef(application, createOfflineRef(fixtures));
      return application;
    }
    

    This prevents the torii-adapter from resolving any session data that I may have from using my application. Then I can use the provided torii helper to mock my session where I need it:

    // torii helper
    import { stubValidSession } from 'app/tests/helpers/torii';
    
    // mock a valid session
    stubValidSession(application, { });
    

    Hope that saves someone else some time.