<commit_before>/*
 * Script to load test/dev data in the application.
 */

var server = require('../server')
  , model = require('../model')
  , CLB = require('nodetk/orchestration/callbacks')
  , tkfs = require('nodetk/fs')
  , fs = require('fs')
  , R = model.RFactory()
  , config = require('../lib/config_loader').get_config()

  // indexes:
  , email2user = {}
  , name2client = {}
  ;

var DEBUG = false;


var clear_collections = function(callback) {
  /* Erase all the data (delete the store files) and call callback.
   */
  var collections = [R.Client, R.Grant, R.User, R.Authorization];
  var waiter = CLB.get_waiter(collections.length, function() {
    callback && callback();
  });
  collections.forEach(function(collection) {
    collection.remove(waiter);
  });
};

var load_users = function(callback) {
  /* Load end users data in store.
   */
  var emails = [
    '<EMAIL>',
    '<EMAIL>',
    '<EMAIL>',
    '<EMAIL>'
  ];

  if (config.hash_lib == "bcrypt") {
    var password = <PASSWORD>";
  } // password = <PASSWORD> (hashed using bcrypt)
  else if (config.hash_lib == "crypto"){
    var password = "<PASSWORD>";
  } // password = <PASSWORD> (hashed using sha256)

  var users = emails.map(function(email) {
    var user =  new R.User({
      email: email,
      password: password,
      confirmed: 1,
    });
    email2user[user.email] = user;
    return user;
  });
  R.save(users, callback, function(err) {
    throw err;
  });
};


var load_clients = function(callback) {
  /* Load the client applications in store.
   */
  var clients = [
    // name, redirect_uri
    [config.oauth2_client.name, config.oauth2_client.client.redirect_uri],
    ["errornot", 'http://127.0.0.1:8888/login'],
    ["text_server", 'http://127.0.0.1:5000/oauth2/process'],
    ["test_client", 'http://127.0.0.1:7070/login/process/'],
    ["geeks", 'http://127.0.0.1:3000/oauth2/process'],
    ['trac', 'http://localhost:8080/trac_env_test/auth_server_process'],
    ['local_redishttp', 'http://localhost:3000/oauth2/process']
  ];
  clients = clients.map(function(t) {
    var client = new R.Client({
      name: t[0],
      redirect_uri: t[1],
      secret: 'some secret string'
    });
    name2client[client.name] = client;
    return client;
  });
  R.save(clients, function() {
    config.oauth2_client.client_id = name2client[config.oauth2_client.name].id;
    DEBUG && console.log('test_client id:', name2client['test_client'].id);
    callback()
  }, function(err) {
    throw err;
  });
};


var load_authorizations = function(callback) {
  /* Load authorizations in DB.
   */
  var auths = [
   // user email , client name, context, roles
    ['<EMAIL>', config.oauth2_client.name,
                          config.oauth2_client.name, ['admin']],
    ['<EMAIL>', 'errornot', 'errornot', ['user', 'admin']],
    ['<EMAIL>', 'errornot', 'text_server', ['user', 'admin']],
    ['<EMAIL>', 'errornot', 'auth_server', ['user', 'admin']],
    ['<EMAIL>', 'text_server', 'auth_server', ['user', 'admin']],
    ['<EMAIL>', 'text_server', 'text_server', ['user', 'admin']],
    ['<EMAIL>', 'geeks', '/', ['user', 'admin']],
    ['<EMAIL>', config.oauth2_client.name,
                          config.oauth2_client.name, ['admin']],
    ['<EMAIL>', 'errornot', 'errornot', ['user', 'admin']],
    ['<EMAIL>', 'errornot', 'text_server', ['user', 'admin']],
    ['<EMAIL>', 'errornot', 'auth_server', ['user', 'admin']],
    ['<EMAIL>', 'text_server', 'auth_server', ['user', 'admin']],
    ['<EMAIL>', 'text_server', 'text_server', ['user', 'admin']],
    ['<EMAIL>', 'geeks', '/', ['user', 'admin']],
    ['<EMAIL>', 'local_redishttp', '/', ['user', 'admin']],
    ['<EMAIL>', 'local_redishttp', '/redis', ['user', 'admin']],
    ['<EMAIL>', 'local_redishttp', '/vote', ['user', 'admin']],
  ];
  auths = auths.map(function(auth) {
    return new R.Authorization({
      email: auth[0],
      client: name2client[auth[1]],
      context: auth[2],
      roles: auth[3]
    })
  });
  R.save(auths, callback, function(err) {
    throw err;
  });
};


var run = exports.run = function(callback) {
  clear_collections(function() {
    var waiter = CLB.get_waiter(2, function() {
      load_authorizations(callback);
    });
    load_users(waiter);
    load_clients(waiter);
  });
};


if(process.argv[1] == __filename) {
  DEBUG = true;
  console.log('Reset data in DB...');
  run(function() {
    process.exit()
  });
}

<commit_msg>Cleanup test data.<commit_after>/*
 * Script to load test/dev data in the application.
 */

var server = require('../server')
  , model = require('../model')
  , CLB = require('nodetk/orchestration/callbacks')
  , tkfs = require('nodetk/fs')
  , fs = require('fs')
  , R = model.RFactory()
  , config = require('../lib/config_loader').get_config()

  // indexes:
  , email2user = {}
  , name2client = {}
  ;

var DEBUG = false;


var clear_collections = function(callback) {
  /* Erase all the data (delete the store files) and call callback.
   */
  var collections = [R.Client, R.Grant, R.User, R.Authorization];
  var waiter = CLB.get_waiter(collections.length, function() {
    callback && callback();
  });
  collections.forEach(function(collection) {
    collection.remove(waiter);
  });
};

var load_users = function(callback) {
  /* Load end users data in store.
   */
  var emails = [
    '<EMAIL>',
    '<EMAIL>',
    '<EMAIL>'
  ];

  if (config.hash_lib == "bcrypt") {
    var password = <PASSWORD>";
  } // password = <PASSWORD>4 (hashed using bcrypt)
  else if (config.hash_lib == "crypto"){
    var password = "<PASSWORD>";
  } // password = <PASSWORD>4 (hashed using sha256)

  var users = emails.map(function(email) {
    var user =  new R.User({
      email: email,
      password: password,
      confirmed: 1,
    });
    email2user[user.email] = user;
    return user;
  });
  R.save(users, callback, function(err) {
    throw err;
  });
};


var load_clients = function(callback) {
  /* Load the client applications in store.
   */
  var clients = [
    // name, redirect_uri
    [config.oauth2_client.name, config.oauth2_client.client.redirect_uri],
    ["errornot", 'http://127.0.0.1:8888/login'],
    ["text_server", 'http://127.0.0.1:5000/oauth2/process'],
    ["test_client", 'http://127.0.0.1:7070/login/process/'],
    ["geeks", 'http://127.0.0.1:3000/oauth2/process'],
    ['trac', 'http://localhost:8080/trac_env_test/auth_server_process']
  ];
  clients = clients.map(function(t) {
    var client = new R.Client({
      name: t[0],
      redirect_uri: t[1],
      secret: 'some secret string'
    });
    name2client[client.name] = client;
    return client;
  });
  R.save(clients, function() {
    config.oauth2_client.client_id = name2client[config.oauth2_client.name].id;
    DEBUG && console.log('test_client id:', name2client['test_client'].id);
    callback()
  }, function(err) {
    throw err;
  });
};


var load_authorizations = function(callback) {
  /* Load authorizations in DB.
   */
  var auths = [
   // user email , client name, context, roles
    ['<EMAIL>', config.oauth2_client.name,
                          config.oauth2_client.name, ['admin']],
    ['<EMAIL>', 'errornot', 'errornot', ['user', 'admin']],
    ['<EMAIL>', 'errornot', 'text_server', ['user', 'admin']],
    ['<EMAIL>', 'errornot', 'auth_server', ['user', 'admin']],
    ['<EMAIL>', 'text_server', 'auth_server', ['user', 'admin']],
    ['<EMAIL>', 'text_server', 'text_server', ['user', 'admin']],
    ['<EMAIL>', 'geeks', '/', ['user', 'admin']]
  ];
  auths = auths.map(function(auth) {
    return new R.Authorization({
      email: auth[0],
      client: name2client[auth[1]],
      context: auth[2],
      roles: auth[3]
    })
  });
  R.save(auths, callback, function(err) {
    throw err;
  });
};


var run = exports.run = function(callback) {
  clear_collections(function() {
    var waiter = CLB.get_waiter(2, function() {
      load_authorizations(callback);
    });
    load_users(waiter);
    load_clients(waiter);
  });
};


if(process.argv[1] == __filename) {
  DEBUG = true;
  console.log('Reset data in DB...');
  run(function() {
    process.exit()
  });
}


{'fraction_non_alphanumeric': 0.1994344030493053, 'fraction_numerical': 0.01856633468584778, 'mean_word_length': 3.2497387669801463, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, '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': '7187553', 'n_tokens_mistral': 2923, 'n_tokens_neox': 2878, 'n_words': 572}