<commit_before>import Ember from 'ember';
import { stubRequest } from 'ember-cli-fake-server';
import MockLocation from './mock-location';
import MockTitle from './mock-title';
import { stubValidSession } from './torii'; // provided by Torii

Ember.Test.registerHelper('createStubUser', function(app, userData) {
  userData = userData || {};
  userData.id = userData.id || 'user1';
  const defaultUserData = {
    name: 'stubbed user',
    email: '<EMAIL>',
    verified: true,
    _links: {
      ssh_keys: { href: `/users/${userData.id}/ssh_keys` },
      roles:  { href: `/users/${userData.id}/roles` },
      otp_configurations:  { href: `/users/${userData.id}/otp_configurations` },
      current_otp_configuration: { },  // None by default
      self: { href: `/users/${userData.id}` }
    }
  };

  return Ember.$.extend(true, defaultUserData, userData);
});

Ember.Test.registerHelper('createStubRole', function(app, roleData) {
  roleData = roleData || {};
  roleData.id = roleData.id || 'r1';
  const defaultRoleData = {
    privileged: true,
    _links: {
      self: { href: `/roles/${roleData.id}` },
      organization: { href: '/organizations/1' }
    }
  };

  return Ember.$.extend(true, defaultRoleData, roleData);
});

Ember.Test.registerHelper('createStubToken', function(app, tokenData, stubUser) {
  tokenData = tokenData || {};
  tokenData.id = tokenData.id || 'stubbed-token-id';
  const defaultTokenData = {
    access_token: 'my-token',
    token_type: 'bearer',
    expires_in: 2,
    scope: 'manage',
    type: 'token',
    _links: {
      user: { href: stubUser._links.self.href }
    }
  };

  return Ember.$.extend(true, defaultTokenData, tokenData);
});

Ember.Test.registerAsyncHelper('signIn', function(app, userData, roleData, tokenData){
  userData = createStubUser(userData);
  roleData = createStubRole(roleData);
  tokenData = createStubToken(tokenData, userData);

  stubRequest('get', `/roles/${roleData.id}`, function(request){
    return this.success(roleData);
  });

  stubRequest('get', `/users/${userData.id}/roles`, function(request){
    return this.success({
      _embedded: {
        roles: [roleData]
      }
    });
  });

  let currentUser = Ember.run(function(){
    let store = app.__container__.lookup('store:application');
    // TODO: Perhaps we could use pushPayload here?
    return store.push('user', Ember.$.extend(true, userData, {
      links: {
        sshKeys: userData._links.ssh_keys.href,
        roles: userData._links.roles.href,
        otpConfigurations: userData._links.otp_configurations.href,
        currentOtpConfiguration: userData._links.current_otp_configuration.href,
        self: userData._links.self.href
      }
    }));
  });

  let token = Ember.run(function() {
    let store = app.__container__.lookup('store:application');
    return store.push('token', {
      id: tokenData.id,
      accessToken: tokenData.access_token,
      rawPayload: JSON.stringify(tokenData),
      scope: tokenData.scope,
      links: { user: tokenData._links.user.href }
    });
  });

  stubValidSession(app, {currentUser, token});
});

Ember.Test.registerAsyncHelper('signInAndVisit', function(app, url, userData, roleData, tokenData){
  signIn(userData, roleData, tokenData);
  visit(url);
});

Ember.Test.registerHelper('expectReplacedLocation', function(app, url){
  equal(MockLocation.last(), url, `window.location replaced with "${url}"`);
});

Ember.Test.registerAsyncHelper('expectRedirectsWhenLoggedIn', function(app, url){
  stubIndexRequests();
  signInAndVisit(url);

  andThen(function(){
    equal(currentPath(), 'dashboard.requires-read-access.stack.apps.index');
  });
});

Ember.Test.registerHelper('expectTitle', function(app, title){
  equal(MockTitle.last(), title, `window.document.title updated to "${title}"`);
});

Ember.Test.registerAsyncHelper('clickNextPageLink', function(app){<commit_msg>signIn helper: don't access session directly<commit_after>import Ember from 'ember';
import { stubRequest } from 'ember-cli-fake-server';
import MockLocation from './mock-location';
import MockTitle from './mock-title';
import { stubValidSession } from './torii'; // provided by Torii

Ember.Test.registerHelper('createStubUser', function(app, userData) {
  userData = userData || {};
  userData.id = userData.id || 'user1';
  const defaultUserData = {
    name: '<NAME>',
    email: '<EMAIL>',
    verified: true,
    _links: {
      ssh_keys: { href: `/users/${userData.id}/ssh_keys` },
      roles:  { href: `/users/${userData.id}/roles` },
      otp_configurations:  { href: `/users/${userData.id}/otp_configurations` },
      current_otp_configuration: { },  // None by default
      self: { href: `/users/${userData.id}` }
    }
  };

  return Ember.$.extend(true, defaultUserData, userData);
});

Ember.Test.registerHelper('createStubRole', function(app, roleData) {
  roleData = roleData || {};
  roleData.id = roleData.id || 'r1';
  const defaultRoleData = {
    privileged: true,
    _links: {
      self: { href: `/roles/${roleData.id}` },
      organization: { href: '/organizations/1' }
    }
  };

  return Ember.$.extend(true, defaultRoleData, roleData);
});

Ember.Test.registerHelper('createStubToken', function(app, tokenData, stubUser) {
  tokenData = tokenData || {};
  tokenData.id = tokenData.id || 'stubbed-token-id';
  const defaultTokenData = {
    access_token: 'my-token',
    token_type: 'bearer',
    expires_in: 2,
    scope: 'manage',
    type: 'token',
    _links: {
      user: { href: stubUser._links.self.href }
    }
  };

  return Ember.$.extend(true, defaultTokenData, tokenData);
});

Ember.Test.registerHelper('signIn', function(app, userData, roleData, tokenData){
  userData = createStubUser(userData);
  roleData = createStubRole(roleData);
  tokenData = createStubToken(tokenData, userData);

  stubRequest('get', `/roles/${roleData.id}`, function(){
    return this.success(roleData);
  });

  stubRequest('get', `/users/${userData.id}/roles`, function(){
    return this.success({
      _embedded: {
        roles: [roleData]
      }
    });
  });

  stubRequest('get', `/users/${userData.id}`, function() {
    return this.success(userData);
  });

  stubRequest('get', '/current_token', function() {
    return this.success(tokenData);
  });

  stubRequest('get', `/tokens/${tokenData.id}`, function() {
    return this.success(tokenData);
  });
});

Ember.Test.registerAsyncHelper('signInAndVisit', function(app, url, userData, roleData, tokenData){
  signIn(userData, roleData, tokenData);
  visit(url);
});

Ember.Test.registerHelper('expectReplacedLocation', function(app, url){
  equal(MockLocation.last(), url, `window.location replaced with "${url}"`);
});

Ember.Test.registerAsyncHelper('expectRedirectsWhenLoggedIn', function(app, url){
  stubIndexRequests();
  signInAndVisit(url);

  andThen(function(){
    equal(currentPath(), 'dashboard.requires-read-access.stack.apps.index');
  });
});

Ember.Test.registerHelper('expectTitle', function(app, title){
  equal(MockTitle.last(), title, `window.document.title updated to "${title}"`);
});

Ember.Test.registerAsyncHelper('clickNextPageLink', function(app){
{'fraction_non_alphanumeric': 0.1660377358490566, 'fraction_numerical': 0.0011180992313067784, 'mean_word_length': 4.089615931721195, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5817638', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2344, 'n_words': 420}