How to make Ember Cli Mirage to work with Ember Simple auth

1.6k views Asked by At

For development and testing I want to use Ember CLi Mirage. I'm trying to get it to work with simple auth and oauth2. How do I have to set up Mirage to work with a session token?

This is what I'm doing so far:

import Ember from 'ember';

export default Ember.Controller.extend({

    actions: {
        authenticate() {
            var data = this.getProperties('username', 'password');
            this.get('session').authenticate('simple-auth-authenticator:oauth2-password-grant', data);
        }
    }

});

And in mirage I'm not sure how to set up my token route:

this.post('/token');
2

There are 2 answers

3
Sam Selikoff On BEST ANSWER

For custom work like this, pass a function in as the second parameter to your route definition:

this.post('/token', function(db, request) {
  // generate a token

  return {
    token: token
  };
});

I'd have to know more about your backend to offer more specific guidance, but this is the general idea. Hope it helps!

0
Bracke On

I use the following in my tests:

import { test } from 'qunit';
import { authenticateSession } from 'app-name/tests/helpers/ember-simple-auth'; 
import moduleForAcceptance from 'app-name/tests/helpers/module-for-acceptance';

moduleForAcceptance('Acceptance | whatever');

test('visiting /subpage-that-requires-authentication', function(assert) {

  authenticateSession(this.application);

  visit('subpage-that-requires-authentication');

  andThen(function() {

    assert.equal(currentURL(), 'subpage-that-requires-authentication');
  });
});