Search code examples
authenticationember.jsember-app-kitember-qunitember-simple-auth

Testing ember-simple-auth within Ember App Kit


I'm attempting to perform integration/acceptance tests for my Ember app. I'm specifically testing user authentication (e.g. – submitting the login form) and protected pages/states that require authenticated users.

General notes about my app:

  • Using Ember App Kit
  • Using ember-simple-auth for authentication
  • I have api-stubs for my ember-simple-auth forms to hit using the Devise authorizer. These work fine when running the app in-browser.

I have three problems:

1. Devise Authenticator & Ephemeral Storage

From the ember-simple-auth API, it refers to using Ephemeral storage for tests. I have done so, much like this. However, it seems that the sessions is still getting stored in local storage. If I do not perform localStorage.clear() in each test setup/teardown tests fail because I remain logged in when each test runs after the first.

Am I able to prevent storing the session in local storage between each test when I'm using the Devise authenticator for my app?

2. Multiple Acceptance Tests

If I attempt to log in a user in more than 1 test(), my tests spin off into an infinite loop. The first test will pass, but when the second test submits the login form the entire test suite stops and reboots.

Integration Test #1

App = null

module('Acceptance - Page #1',
  setup: ->
    App = startApp()

  teardown: ->
    Ember.run(App, 'destroy')
)

test('page #1 behind authentication', ->
  expect(1)

  visit('/page-1')
  fillIn('input#identification', '[email protected]')
  fillIn('input#password', 'password')
  click('button[type="submit"]')
  andThen(->
    equal(true, true) # This test works fine
  )
)

Integration Test #2

App = null

module('Acceptance - Page #2',
  setup: ->
    App = startApp()

  teardown: ->
    Ember.run(App, 'destroy')
)

test('page #2 behind authentication', ->
  expect(1)

  visit('/page-2')
  fillIn('input#identification', '[email protected]')
  fillIn('input#password', 'password')
  click('button[type="submit"]')
  andThen(->
    equal(true, true) # Never runs, tests start over, infinite loop begins
  )
)

3. EAK api-stubs & Testem

EAK's api-stubs do not seem to be available for Testem, so the "log in" process in these acceptance test when run via the command line/Testem fail.

I attempted setting up sinon.js, but above mentioned issues have prevented me from deciding if it's actually working correctly or not. What is the best way to successfully stub logging in a user with ember-simple-auth? Is it possible to use EAK's api-stubs for Testem?


Solution

  • With the help of @marcoow and a few other SO questions and GitHub issues I've been able to resolve all my problems:

    1. Devise Authenticator & Ephemeral Storage

    I was using an outdated API option in my code. Updating to use the newer API's storeFactory option resolved my session localStorage issue.

    #  app/initializers/simple-auth.coffee
    if Ember.testing == true
      options = Ember.merge({ storeFactory: 'session-store:ephemeral' }, options)
    

    2. Multiple Acceptance Tests

    This turned out to be related to another library I was loading called fastclick. After updating my index file to only load this library in non-test environments, my form submission/infinite loop problems went away.

    // app/index.html
    <!-- @if tests=false -->
      <script src="/vendor/emberjs-touch/lib/ember-fastclick.js"></script>
    <!-- @endif -->
    

    3. EAK api-stubs & Testem

    I found other people facing similar issues to me on StackOverflow. Their questions were eventually responded to/resolved which helped me resolve me own issues. In the comments of this GH issue, there is a link to a work-around to this issue. Example here.