0

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', 'test@test.de')
  .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

kunerd
  • 1,076
  • 10
  • 25
  • What error do you get? – Kingpin2k Dec 22 '13 at 21:32
  • No error, it does nothing. It stays in `not logged in` state and so the test didn't pass. Maybe it's important, that the mocking works fine with my self build user registration form. – kunerd Dec 23 '13 at 10:45

3 Answers3

1

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.

marcoow
  • 4,062
  • 1
  • 14
  • 21
  • Yes, I did, because I'm using Doorkeeper on my server side and that also uses /oauth/token endpoint. Without mocking the test works as described in my question. – kunerd Jan 03 '14 at 09:11
  • 1
    Can you maybe publish a sample app on github that shows the problem (I suppose your project isn't open source)?. Without having access to sth. that actually shows the problem it's hard to come up with ideas. – marcoow Jan 07 '14 at 13:08
  • 2
    The problem is that the response from the server (or mockjax actually) is undefined in the AJAX completion handler. Ember.SimpleAuth does this when retrieving the access token: Ember.$.ajax(Ember.SimpleAuth.serverTokenEndpoint, requestOptions).then(function(response) { ... }) response is undefined so the session isn't actually set up. Must be related to mockjax somehow I think. – marcoow Jan 08 '14 at 14:31
1

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', 'test@test.de')
  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')
Pedro
  • 449
  • 2
  • 9
  • Sorry, the indentation error I have done as I wrote the question. I have edited my question and now it should be the same as my real code. Thank you for your help. – kunerd Jan 04 '14 at 18:44
0

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.

Community
  • 1
  • 1
kunerd
  • 1,076
  • 10
  • 25