New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

authenticateSession does not authenticate a User in tests #1742

Open
belgoros opened this Issue Feb 8, 2019 · 3 comments

Comments

Projects
None yet
2 participants
@belgoros
Copy link

belgoros commented Feb 8, 2019

I'm using Implicit Grant authentication and redirect a User to the external corporate login page if he is not authenticated in index route:

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import config from '../config/environment';
import isFastBoot from 'ember-simple-auth/utils/is-fastboot';
import UnauthenticatedRouteMixin from 'ember-simple-auth/mixins/unauthenticated-route-mixin';
import window from 'ember-window-mock';

export default Route.extend(UnauthenticatedRouteMixin, {
  session:      service('session'),
   _isFastBoot: isFastBoot(),

  beforeModel: function() {
    if (this.get('session.isAuthenticated')) {
      this.transitionTo('dashboard');
    } else {
      if (!this.get('_isFastBoot')) {
        let oauthUrl = config.oauthUrl;
        let clientId = config.clientID;
        let redirectURI = `${window.location.origin}/callback`;
        let responseType = `token`;
        let scope = `profile%20openid`;
        window.location.replace(oauthUrl
                              + `?client_id=${clientId}`
                              + `&redirect_uri=${redirectURI}`
                              + `&response_type=${responseType}`
                              + `&scope=${scope}`
        );
      }
    }
  }
});

In an acceptance test:

import { module, test } from 'qunit';
import { visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { authenticateSession } from 'ember-simple-auth/test-support';
import setupMirageTest from 'ember-cli-mirage/test-support/setup-mirage';

module('Acceptance | Dashboard', function(hooks) {
  setupApplicationTest(hooks);
  setupMirageTest(hooks);


  test('Authenticated users can visit /dashboard', async function(assert) {
    let shop = this.server.create('shop');
    this.server.create('user', { shop });
    
    await authenticateSession({
      token: 'abcdDEF',
      token_type: 'Bearer'
    });
    await visit('/dashboard');

    assert.equal(currentURL(), '/dashboard', 'user is on dashboard page');
  });
});

When putting a break point in beforeModel hook of index route, I always pass in to the else block, i.e. not authenticated.

What am I missing? Thank you.

Used versions:

ember-simple-auth: 1.8.1
ember: 3.7.3
node: v10.12.0
npm: 6.7.0
@belgoros

This comment has been minimized.

Copy link
Author

belgoros commented Feb 11, 2019

Even after changing the value for access_token:

await authenticateSession({
      access_token: 'abcdDEF',
      token_type: 'Bearer'
    });

has't solved the problem and this.get('session.isAuthenticated') still returns false in index route beforeModel hook.

@pichfl

This comment has been minimized.

Copy link

pichfl commented Feb 11, 2019

You should be calling this._super() inside beforeModel() as documented here to allow Ember Simple Auth to do its work.

@belgoros

This comment has been minimized.

Copy link
Author

belgoros commented Feb 11, 2019

@pichfl Nice catch, thank you ! But, unfortunately, even after adding:

import UnauthenticatedRouteMixin from 'ember-simple-auth/mixins/unauthenticated-route-mixin';
import window from 'ember-window-mock';

export default Route.extend(UnauthenticatedRouteMixin, {
  session:      service('session'),
   _isFastBoot: isFastBoot(),

  beforeModel: function() {
    this._super(...arguments);
    console.log('+++++++++++ session.isAuthenticated: ' + this.get('session.isAuthenticated'));
...

it still returns false for this.get('session.isAuthenticated').

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment