Merge pull request #146 from cvan/script-fixes

Fixed bugs with pre-fill and admin scripts
This commit is contained in:
Andrew Richardson 2014-03-18 15:47:28 -07:00
Родитель 76324b0e14 3f3634b482
Коммит 2a845f3b46
5 изменённых файлов: 85 добавлений и 52 удалений

Просмотреть файл

@ -520,7 +520,7 @@ function _newUserView(view, fetchMethod) {
var args = Array.prototype.slice.call(arguments, 0);
var email = req._email;
if (!email) {
res.json(403, {err: 'missing_user'});
res.json(403, {error: 'missing_user'});
done();
return;
}

Просмотреть файл

@ -24,9 +24,11 @@ var client = db.redis();
var idOrEmail = process.argv[2];
var groups = process.argv.slice(3);
var lookupMethod = userlib.getUserFromID;
var isEmail = false;
if (idOrEmail.indexOf('@') !== -1) {
lookupMethod = userlib.getUserFromEmail;
isEmail = true;
}
console.log('Attempting to add user <' + idOrEmail +
@ -34,7 +36,12 @@ console.log('Attempting to add user <' + idOrEmail +
lookupMethod(client, idOrEmail, function (err, user) {
if (err) {
return console.error(err);
if (err === 'no_such_user' && !user && isEmail) {
console.log('User <' + idOrEmail + '> does not exist, creating now');
user = userlib.newUser(client, idOrEmail);
} else {
return console.error(err);
}
}
if (!user) {

Просмотреть файл

@ -9,6 +9,7 @@ var Promise = require('es6-promise').Promise;
var db = require('../db');
var settings = require('../settings');
var settings_local = require('../settings_local');
var userlib = require('../lib/user');
var utils = require('../lib/utils');
var params = require('./prefillParameters');
@ -77,6 +78,7 @@ function run() {
delete remaining_signals[source];
if (!Object.keys(remaining_signals).length) {
clearTimeout(timeout);
client.punsubscribe();
resolve();
}
}
@ -164,7 +166,7 @@ function run() {
console.log('Finished generating users, games, purchases and friend requests.');
process.exit(0);
}).catch(function(err) {
console.error(err);
console.error('Error running prefill:', err);
process.exit(1);
});
};
@ -172,48 +174,71 @@ function run() {
/*** Prefill Logic ***/
function createUsers() {
function createUser(email) {
return postPromise(PERSONA_ENDPOINT + '/generate', {
email: email
});
};
function createUser(email) {
return postPromise(PERSONA_ENDPOINT + '/generate', {
email: email
}).then(login);
function login(emailAssertion) {
return new Promise(function(resolve, reject) {
var assertion = emailAssertion.assertion;
postPromise(API_ENDPOINT + '/user/login', {
assertion: assertion,
audience: API_ENDPOINT
}).then(function(result) {
if (result.error) {
return reject('Login failed: ' + result.error);
}
resolve({
email: result.settings.email,
token: result.token,
username: result.public.username,
id: result.public.id
});
});
var assertion = emailAssertion.assertion;
return postPromise(API_ENDPOINT + '/user/login', {
assertion: assertion,
audience: API_ENDPOINT
}).then(function(result) {
return {
email: result.settings.email,
token: result.token,
username: result.public.username,
id: result.public.id
};
});
};
};
function createUsers() {
return Promise.all(_.times(params.numUsers, function(i) {
return createUser('test' + i + '@test.com').then(login);
return createUser('test' + i + '@test.com');
}));
}
function createTestDeveloper() {
var email = 'test_developer' + Math.round(Math.random() * 1000) + '@test.com';
return createUser(email).then(function(user) {
return new Promise(function(resolve, reject) {
// The one thing we can't simulate through real API calls is updating
// permissions, since we require an existing admin to do that. So here
// we'll cheat and access the user lib directly.
userlib.updateUser(client, user.id, {
permissions: {
developer: true
}
}, function(err, newUserData) {
if (err) {
return reject(err);
}
resolve(user);
});
});
});
}
function createGames() {
var default_params = {
homepage_url: 'http://fake.game',
icons: '128',
screenshots: 'yes'
};
return Promise.all(params.games.map(function(game) {
return postPromise(API_ENDPOINT + '/game/submit',
_.defaults(game, default_params));
}));
return createTestDeveloper().then(function(devUser) {
return Promise.all(params.games.map(function(game) {
var params = _.defaults(game, default_params);
params._user = devUser.token;
return postPromise(API_ENDPOINT + '/game/submit', params, true)
.then(function(result) {
return result;
});
}));
});
}
function purchaseGames(userSSAs, gameSlugs) {
@ -248,18 +273,16 @@ function createFriends(users) {
_user: user.token,
recipient: recipient.id
}).then(function(result) {
if (result.error) {
if (_.contains(['already_friends', 'already_requested'], result.error)) {
console.log('Friend request warning:', result.error);
return resolve({});
}
return reject(result.error);
}
resolve({
user: user,
recipient: recipient
});
}).catch(function(err) {
if (_.contains(['already_friends', 'already_requested'], err)) {
console.log('Friend request warning:', err);
return resolve({});
}
reject(err);
});
});
}
@ -284,11 +307,9 @@ function createFriends(users) {
_user: friendRequest.recipient.token,
acceptee: friendRequest.user.id
}).then(function(result) {
if (result.error) {
reject('Friend accept failed: ' + result.error);
return;
}
resolve(done());
}).catch(function(err) {
reject('Friend accept failed: ' + err);
});
});
}
@ -302,17 +323,22 @@ function createFriends(users) {
}
// Helper function that returns a promise for a post
function postPromise(url, form) {
function postPromise(url, form, asJson) {
return new Promise(function(resolve, reject) {
request.post({
url: url,
form: form
form: asJson ? null : form,
json: asJson ? form : null
}, function(err, resp, body) {
if (err) {
reject(err);
return;
return reject(err);
}
resolve(JSON.parse(body));
// request.js converts response body to JSON when request body is JSON
var json = typeof body === 'object' ? body : JSON.parse(body);
if (json.error) {
return reject(json.error);
}
resolve(json);
});
});
}

Просмотреть файл

@ -34,7 +34,7 @@ module.exports = function(server) {
summary: 'Submission'
},
}, userlib.userDataView(function(user, client, done, req, res) {
if (!user.permissions || (!user.permissions.admin && !user.permissions.dev)) {
if (!user.permissions || (!user.permissions.admin && !user.permissions.developer)) {
res.json(403, {error: 'bad_permission'});
return done();
}
@ -66,7 +66,7 @@ module.exports = function(server) {
summary: 'Edit game details'
}
}, userlib.userDataView(function(user, client, done, req, res) {
if (!user.permissions || (!user.permissions.admin && !user.permissions.dev)) {
if (!user.permissions || (!user.permissions.admin && !user.permissions.developer)) {
res.json(403, {error: 'bad_permission'});
return done();
}

Просмотреть файл

@ -4,7 +4,7 @@ var userlib = require('../../lib/user');
module.exports = function(server) {
// Sample usage:
// % curl -X POST 'http://localhost:5000/user/acl' -d 'id=1&dev=1&reviewer=1&admin=0'
// % curl -X POST 'http://localhost:5000/user/acl' -d 'id=1&developer=1&reviewer=1&admin=0'
server.post({
url: '/user/acl',
validation: {
@ -16,7 +16,7 @@ module.exports = function(server) {
description: 'User ID to change permissions for',
isRequired: true
},
dev: {
developer: {
description: 'Whether or not user should have developer permissions',
isRequired: false
},
@ -54,7 +54,7 @@ module.exports = function(server) {
userlib.updateUser(client, userID, {
permissions: {
developer: !!+POST.dev,
developer: !!+POST.developer,
reviewer: !!+POST.reviewer,
admin: !!+POST.admin
}