ember-simple-auth qunit test with jquery mockjax

693 views Asked by At

I use ember-simple-auth within my application. For my test I use QUnit together with jquery-mockjax. But I didn't get my test, login with correct credentials, to work with a mocked response. If I didn't mock, the test below works. The mocked response looks exactly like a server response.

My question is, how should I mock the response for ember-simple-auth?

test "with correct credentials", ->
  expect(2)

  response = 
    access_token : "92d50f562503e40fe783c2ebf359e0c2b37fa80758e2e3603d7e3e82a485522a"
    expires_in : 7200
    token_type : "bearer"

  # if I remove the following line, the test works
  mock_http('/oauth/token', response, 200)

  visit("login")
  .fillIn('#identification', '[email protected]')
  .fillIn('#password', 'tester')
  .click('.btn-success').then ->
    ok(find("a:contains('Logout')").length, 'logout link not visible')
      ok(not find("a:contains('Login')").length, 'login link still visible')

the following test also works with mocking:

test "with wrong credentials", ->
  expect(2)

  response = 
    error : 'some error occured'

  mock_http('/oauth/token', response, 401)

  visit("login")
  .fillIn('#identification', 'test')
  .fillIn('#password', 'wrong')
  .click('.btn-success').then ->
    ok(not find("a:contains('Logout')").length, 'logout link not visible')
    ok(find("a:contains('Login')").length, 'login link still visible')

EDIT:

Following a jsBin, that shows the problem: http://jsbin.com/ASaSaRiX/6/edit

3

There are 3 answers

1
kunerd On BEST ANSWER

The problem is caused by jQuery version conflicts with mockjax. With the help from marcoow I found this stackoverflow question. With jQuery < 1.10 it works. Mhh... that's not nice.

By the way a working jsBin: http://jsbin.com/ASaSaRiX/11

EDIT: You can find more detailed information here. The problem is caused by a changed in jQuery.

@marcoow: One fix is to add dataType: 'json' to the request options of Ember.SimpleAuth. Maybe you have some time to look at the informations given in the link above.

3
marcoow On

I'm the author of Ember.SimpleAuth.

Did you configure Ember.SimpleAuth to use '/oauth/token' as the token endpoint? Otherwise it would use '/token' as the server endpoint so your mock wouldn't have any effect.

1
Pedro On

Your indetation in the first code block does not seem to be correct. It should read like this: (notice indentation change at the mock_http line)

test "with correct credentials", ->
  expect(2)

  response = 
    access_token : "92d50f562503e40fe783c2ebf359e0c2b37fa80758e2e3603d7e3e82a485522a"
    expires_in : 7200
    token_type : "bearer"

  # if I remove the following line, the test works
  mock_http('/oauth/token', response, 200)

  visit("login")
  fillIn('#identification', '[email protected]')
  fillIn('#password', 'tester')
  click('.btn-success').then ->
    ok(find("a:contains('Logout')").length, 'logout link not visible')
    ok(not find("a:contains('Login')").length, 'login link still visible')