Skip to content

Commit

Permalink
Merge branch 'phase1-branch'
Browse files Browse the repository at this point in the history
Conflicts:
	config/config.js
	config/development.env
	test/fixtures/users.json
	test/users-spec.js
	users.js
  • Loading branch information
dberesford committed Jul 9, 2015
2 parents 947ff46 + ee11ccf commit d9cc634
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 24 deletions.
1 change: 1 addition & 0 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ module.exports = function() {
// }
}
},
'recaptcha_secret_key': process.env.RECAPTCHA_SECRET_KEY,
transport: {
type: 'web',
web: {
Expand Down
5 changes: 4 additions & 1 deletion config/development.env
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ export ES_INDEX=cp-users-development
export MAIL_HOST=mailtrap.io
export MAIL_PORT=2525
export MAIL_USER=374406e9e0f3e3d16
export MAIL_PASS=4bd109f2f1c55d
export MAIL_PASS=4bd109f2f1c55d

export RECAPTCHA_SECRET_KEY=6LfVKQgTAAAAAI3dhMSRsrTbFbx7cnGr4Fy2sn5_

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"node-uuid": "1.4.3",
"optimist": "0.6.1",
"postgrator": "2.2.0",
"request": "2.58.0",
"seneca": "0.6.1",
"seneca-elasticsearch": "git://github.com/sigismondm/seneca-elasticsearch.git",
"seneca-mail": "git://github.com/iantocristian/seneca-mail.git#0.1.6",
Expand Down
50 changes: 50 additions & 0 deletions test/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

module.exports = function() {

// Utility function for local development running with boot2docker
// where we need the ip address of boot2docker instead of localhost.
// This is for accessing containerised services.
function localhost() {
if (process.env.DOCKER_HOST) {
return require('url').parse(process.env.DOCKER_HOST).hostname;
}
if (process.env.TARGETIP) {
return process.env.TARGETIP;
}
return '127.0.0.1';
}

function pgConfig() {
return {
name: process.env.POSTGRES_NAME,
host: process.env.POSTGRES_HOST || localhost(),
port: process.env.POSTGRES_PORT || 5432,
username: process.env.POSTGRES_USERNAME,
password: process.env.POSTGRES_PASSWORD
}
}

function esConfig() {
return {
connection: {
host : localhost() + ':9200',
index: process.env.ES_INDEX,
sniffOnStart: false,
sniffInterval: false
}
};
}

return {
'postgresql-store': pgConfig(),
elasticsearch: esConfig(),
'recaptcha_secret_key': '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe',
transport: {
type: 'web',
web: {
host: '0.0.0.0',
port: 10303
}
}
};
}
17 changes: 11 additions & 6 deletions test/fixtures/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,39 @@
"email": "[email protected]",
"password": "pass1",
"termsConditionsAccepted": true,
"initUserType":"attendee-u13"
"initUserType":"attendee-u13",
"g-recaptcha-response": "03AHJ_VuufpHRAc3bbYfeMunZ-nOYP5rjdSwlw7e4Btq-RGYYvCRTJJkXptbQuBwJDL0ZWQ7eHeQRoTI9iRZlakVlpVDB9rd0kYw2iNcMXG9qNNNBNv_qNjTyE4RwZ3x0zAt2aqg-LjboEqRyLqbOO032kal8wz_GGKbrykJMV0kiSdCbABlSalNHUwlP9II7nGs1me9x84owsr5ZCFkCYtQehguTm6nMe9HRq7hLbQb4hK8HuWwfqQ1z5CIuKk7el5taxNC1h4QuqWsNgGlWAv_Gqp4dJjz683kNCV2vbTlofz6FwttNZwD-mS1l4OrTCdvdX9JBcipXbjlIF1RFyBbXGvSAftp3_ajmoAjstwSdAZVtD1Whm_x8nUo_0pFp6x0n0Y1j8Ztc87oxAXswI-Yvf8JFu8Bhaw_SwAz2Qk7meR2Mvx5lKz_3IzK_b15gnmXenqamBpksv"
},
{
"name": "test2",
"email": "[email protected]",
"password": "pass2",
"termsConditionsAccepted": true,
"initUserType":"attendee-o13"
"initUserType":"attendee-o13",
"g-recaptcha-response": "03AHJ_VuufpHRAc3bbYfeMunZ-nOYP5rjdSwlw7e4Btq-RGYYvCRTJJkXptbQuBwJDL0ZWQ7eHeQRoTI9iRZlakVlpVDB9rd0kYw2iNcMXG9qNNNBNv_qNjTyE4RwZ3x0zAt2aqg-LjboEqRyLqbOO032kal8wz_GGKbrykJMV0kiSdCbABlSalNHUwlP9II7nGs1me9x84owsr5ZCFkCYtQehguTm6nMe9HRq7hLbQb4hK8HuWwfqQ1z5CIuKk7el5taxNC1h4QuqWsNgGlWAv_Gqp4dJjz683kNCV2vbTlofz6FwttNZwD-mS1l4OrTCdvdX9JBcipXbjlIF1RFyBbXGvSAftp3_ajmoAjstwSdAZVtD1Whm_x8nUo_0pFp6x0n0Y1j8Ztc87oxAXswI-Yvf8JFu8Bhaw_SwAz2Qk7meR2Mvx5lKz_3IzK_b15gnmXenqamBpksv"
},
{
"name": "test3",
"email": "[email protected]",
"password": "pass3",
"termsConditionsAccepted": true,
"initUserType":"parent-guardian"
"initUserType":"parent-guardian",
"g-recaptcha-response": "03AHJ_VuufpHRAc3bbYfeMunZ-nOYP5rjdSwlw7e4Btq-RGYYvCRTJJkXptbQuBwJDL0ZWQ7eHeQRoTI9iRZlakVlpVDB9rd0kYw2iNcMXG9qNNNBNv_qNjTyE4RwZ3x0zAt2aqg-LjboEqRyLqbOO032kal8wz_GGKbrykJMV0kiSdCbABlSalNHUwlP9II7nGs1me9x84owsr5ZCFkCYtQehguTm6nMe9HRq7hLbQb4hK8HuWwfqQ1z5CIuKk7el5taxNC1h4QuqWsNgGlWAv_Gqp4dJjz683kNCV2vbTlofz6FwttNZwD-mS1l4OrTCdvdX9JBcipXbjlIF1RFyBbXGvSAftp3_ajmoAjstwSdAZVtD1Whm_x8nUo_0pFp6x0n0Y1j8Ztc87oxAXswI-Yvf8JFu8Bhaw_SwAz2Qk7meR2Mvx5lKz_3IzK_b15gnmXenqamBpksv"
},
{
"name": "test4",
"email": "[email protected]",
"password": "pass4",
"termsConditionsAccepted": true,
"initUserType":"mentor"
"initUserType":"mentor",
"g-recaptcha-response": "03AHJ_VuufpHRAc3bbYfeMunZ-nOYP5rjdSwlw7e4Btq-RGYYvCRTJJkXptbQuBwJDL0ZWQ7eHeQRoTI9iRZlakVlpVDB9rd0kYw2iNcMXG9qNNNBNv_qNjTyE4RwZ3x0zAt2aqg-LjboEqRyLqbOO032kal8wz_GGKbrykJMV0kiSdCbABlSalNHUwlP9II7nGs1me9x84owsr5ZCFkCYtQehguTm6nMe9HRq7hLbQb4hK8HuWwfqQ1z5CIuKk7el5taxNC1h4QuqWsNgGlWAv_Gqp4dJjz683kNCV2vbTlofz6FwttNZwD-mS1l4OrTCdvdX9JBcipXbjlIF1RFyBbXGvSAftp3_ajmoAjstwSdAZVtD1Whm_x8nUo_0pFp6x0n0Y1j8Ztc87oxAXswI-Yvf8JFu8Bhaw_SwAz2Qk7meR2Mvx5lKz_3IzK_b15gnmXenqamBpksv"
},
{
"name": "test5",
"email": "[email protected]",
"password": "pass5",
"termsConditionsAccepted": true,
"initUserType":"champion"
"initUserType":"champion",
"g-recaptcha-response": "03AHJ_VuufpHRAc3bbYfeMunZ-nOYP5rjdSwlw7e4Btq-RGYYvCRTJJkXptbQuBwJDL0ZWQ7eHeQRoTI9iRZlakVlpVDB9rd0kYw2iNcMXG9qNNNBNv_qNjTyE4RwZ3x0zAt2aqg-LjboEqRyLqbOO032kal8wz_GGKbrykJMV0kiSdCbABlSalNHUwlP9II7nGs1me9x84owsr5ZCFkCYtQehguTm6nMe9HRq7hLbQb4hK8HuWwfqQ1z5CIuKk7el5taxNC1h4QuqWsNgGlWAv_Gqp4dJjz683kNCV2vbTlofz6FwttNZwD-mS1l4OrTCdvdX9JBcipXbjlIF1RFyBbXGvSAftp3_ajmoAjstwSdAZVtD1Whm_x8nUo_0pFp6x0n0Y1j8Ztc87oxAXswI-Yvf8JFu8Bhaw_SwAz2Qk7meR2Mvx5lKz_3IzK_b15gnmXenqamBpksv"
}
]
]
8 changes: 5 additions & 3 deletions test/users-spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var seneca = require('seneca')(),
config = require(__dirname + '/../config/config.js')(),
config = require(__dirname + '/config/config.js')(),
util = require('util'),
async = require('async'),
_ = require('lodash'),
Expand Down Expand Up @@ -43,6 +43,7 @@ function expect_contain_properties(actual, expected){
}

describe('Users Microservice test', function(){
this.timeout(5000);

before(function(done){
seneca.ready(function(){
Expand Down Expand Up @@ -91,13 +92,14 @@ describe('Users Microservice test', function(){

describe('Register', function(){
it('save user to db', function(done){

var user = {
"name": "test6",
"email": "[email protected]",
"password": "pass6",
"termsConditionsAccepted": true,
"initUserType": "mentor"
"initUserType": "mentor",
"g-recaptcha-response": "03AHJ_VuufpHRAc3bbYfeMunZ-nOYP5rjdSwlw7e4Btq-RGYYvCRTJJkXptbQuBwJDL0ZWQ7eHeQRoTI9iRZlakVlpVDB9rd0kYw2iNcMXG9qNNNBNv_qNjTyE4RwZ3x0zAt2aqg-LjboEqRyLqbOO032kal8wz_GGKbrykJMV0kiSdCbABlSalNHUwlP9II7nGs1me9x84owsr5ZCFkCYtQehguTm6nMe9HRq7hLbQb4hK8HuWwfqQ1z5CIuKk7el5taxNC1h4QuqWsNgGlWAv_Gqp4dJjz683kNCV2vbTlofz6FwttNZwD-mS1l4OrTCdvdX9JBcipXbjlIF1RFyBbXGvSAftp3_ajmoAjstwSdAZVtD1Whm_x8nUo_0pFp6x0n0Y1j8Ztc87oxAXswI-Yvf8JFu8Bhaw_SwAz2Qk7meR2Mvx5lKz_3IzK_b15gnmXenqamBpksv"
}

seneca.act({role: role, cmd: 'register'}, user, function(err, savedUser){
Expand Down
139 changes: 125 additions & 14 deletions users.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

var _ = require('lodash');
var async = require('async');
var request = require('request');

module.exports = function(options){
var seneca = this;
var plugin = 'cd-users';
var ENTITY_NS = 'sys/user';
var so = seneca.options();

seneca.add({role: plugin, cmd: 'load'}, cmd_load);
seneca.add({role: plugin, cmd: 'list'}, cmd_list);
Expand All @@ -15,12 +17,22 @@ module.exports = function(options){
seneca.add({role: plugin, cmd: 'get_users_by_emails'}, cmd_get_users_by_emails);
seneca.add({role: plugin, cmd: 'update'}, cmd_update);
seneca.add({role: plugin, cmd: 'get_init_user_types'}, cmd_get_init_user_types);
seneca.add({role: plugin, cmd: 'is_champion'}, cmd_is_champion);

function cmd_load(args, done) {
var seneca = this;
var id = args.id;
var userEntity = seneca.make(ENTITY_NS);
userEntity.load$(id, done);

async.waterfall([
function(done) {
seneca.make(ENTITY_NS).load$({id: args.id}, done);
},
function(user, done) {
return done(null, user.data$());
}
], done);
}

function cmd_list(args, done){
Expand All @@ -43,23 +55,63 @@ module.exports = function(options){
//TO-DO: define basic-user and cdf-admin permissions
//cdf-admin role should give user global access to the system.
var seneca = this;
args.roles = ['basic-user'];
args.mailingList = (args.mailingList) ? 1 : 0;

seneca.act({role:'user', cmd:'register'}, args, function (err, registerResponse) {
if(err) return done(err);
var user = registerResponse.user;
//Create user profile based on initial user type.
var profileData = {
userId:user.id,
email:user.email,
userType:user.initUserType.name
};
seneca.act({role:'cd-profiles', cmd:'save', profile: profileData}, function (err, profile) {
if(!args['g-recaptcha-response']){
return done(new Error('Error with captcha'));
}

var secret = so['recaptcha_secret_key'];
var captchaResponse = args['g-recaptcha-response'];

var postData = {
url: 'https://www.google.com/recaptcha/api/siteverify',
form: {
response: captchaResponse,
secret: secret
}
};

function verifyCaptcha(done){
request.post(postData, function(err, response, body){
if(err){
return done(err);
}

body = JSON.parse(body);

if(!body.success){
return done(JSON.stringify(body['error-codes']));
}

return done(null, body.success);
});
}

function registerUser(success, done){
args = _.omit(args, ['g-recaptcha-response']);

args.roles = ['basic-user'];
args.mailingList = (args.mailingList) ? 1 : 0;
seneca.act({role:'user', cmd:'register'}, args, function (err, registerResponse) {
if(err) return done(err);
done(null, registerResponse);
var user = registerResponse.user;
//Create user profile based on initial user type.
var profileData = {
userId:user.id,
email:user.email,
userType:user.initUserType.name
};
seneca.act({role:'cd-profiles', cmd:'save', profile: profileData}, function (err, profile) {
if(err) return done(err);
done(null, registerResponse);
});
});
});
}

async.waterfall([
verifyCaptcha,
registerUser
], done);
}

function cmd_promote(args, done) {
Expand Down Expand Up @@ -126,6 +178,65 @@ module.exports = function(options){
done(null, initUserTypes);
}

/**
* This function returns if true if a user is champion and it's dojos if any.
*/
function cmd_is_champion(args, done){
var seneca = this;

seneca.make(ENTITY_NS).load$({id: args.id}, function(err, user) {
if (err) {
return done(err)
}

user = user.data$();

var query = {
query: {
filtered: {
query: {
match_all: {}
},
filter: {
bool: {
must: [{
term: {userId: args.id}
}]
}
}
}
}
};

seneca.act({
role: 'cd-dojos',
cmd: 'search',
search: query,
type: 'cd_dojoleads',
user: user
}, function (err, dojoLeads) {
if (err) {
return done(err)
}

if (dojoLeads.total > 0) {
seneca.act({role: 'cd-dojos', cmd: 'my_dojos', user: user}, function (err, myDojos) {
if (err) {
return done(err)
}

return done(null, {
isChampion: true,
dojos: myDojos
});
});
} else {
return done(null, {isChampion: false});
}
});
});
}

return {
name: plugin
};
Expand Down

0 comments on commit d9cc634

Please sign in to comment.