如何使 Ember Cli Mirage 与 Ember 简单授权一起使用

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

对于开发和测试,我想使用 Ember CLi Mirage。我试图让它与简单的 auth 和 oauth2 一起工作。我必须如何设置 Mirage 才能使用会话令牌?

这是我目前所做的:

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);
        }
    }

});

在海市蜃楼中,我不确定如何设置我的令牌路由:

this.post('/token');

对于这样的自定义工作,pass a function 作为路由定义的第二个参数:

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

  return {
    token: token
  };
});

我必须更多地了解您的后端才能提供更具体的指导,但这是总体思路。希望对您有所帮助!

我在测试中使用了以下内容:

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');
  });
});