"use strict"; var should = require('should'); var assert = require('assert'); var db = require('../units/db-connector').dbConnection; var dbAccount = require('../units/db-accounts')(db); var exec = require('child_process').exec; function prepare_db(next) { exec('psql -Upostgres -d oBrand -f ./rebuild.sql', function(err) { if (err !== null) { console.log('exec error: ' + err); } }); } describe('Accounts DB', function() { before(function() { console.log('Prepare database'); prepare_db(function(err) { if (err) { console.log('Problem setting up database'); } }); }); it('should connect to Postgres using promises', function() { return dbAccount.connectPGP().then(function(data) { assert.equal(data[0].number, 1); }); }); it('should not add user with no data', function() { return dbAccount.addNewAccount({}).then(function() { }) .catch(function(err) { assert.notEqual(err, null); }); }); it('should not add user with no details', function() { return dbAccount.addNewAccount({ username: '', password: '', email: '' }).then(function(data) { }) .catch(function(err) { assert.notEqual(err, null); err.should.have.property('code', 1000); }); }); it('should not add an empty name', function() { return dbAccount.addNewAccount({ username: '', password: 'b0b5p455w0rd', email: 'bob@tim.com' }).then(function(data) { }) .catch(function(err) { err.should.have.property('code', 1000); }); }); it('should not add user with missing password', function() { return dbAccount.addNewAccount({ username: 'bob', password: '', email: 'bob@tim.com' }).then(function(data) { }) .catch(function(err) { err.should.have.property('code', 1000); }); }); it('should not add user with missing email', function() { return dbAccount.addNewAccount({ username: 'bob', password: 'b0b5p455w0rd', email: '' }).then(function(data) { }) .catch(function(err) { err.should.have.property('code', 1000); }); }); it('should not add user with invalid email', function() { return dbAccount.addNewAccount({ username: 'bob', password: 'b0b5p455w0rd', email: 'tim' }).then(function(data) { }) .catch(function(err) { err.should.have.property('code', 1001); }); }); it('should add a new user', function() { return dbAccount.addNewAccount({ username: 'bob', password: 'b0b5p455w0rd', email: 'bob@tim.com' }) .then(function(data) { should(data.reply).equal('user added'); }) .catch(function(err) { console.log(err); err.should.not.be.ok(); }); }); it('should not add a duplicate user', function() { return dbAccount.addNewAccount({ username: 'bob', password: 'b0b5p455w0rd', email: 'bob@tim.com' }) .then(function(data) { should(data.reply).equal('user added'); }) .catch(function(err) { console.log(err); err.should.have.property('detail', 'Key (username)=(bob) already exists.'); }); }); it('should find a valid account', function() { return dbAccount.findAccount({ password: 'b0b5p455w0rd', email: 'bob@tim.com' }) .then(function(data) { data.should.have.property('username','bob'); }) .catch(function(err) { console.log(err); err.should.be.ok(); }); }); it('should not allow login with incorrect password', function() { return dbAccount.findAccount({password: 'bobspassword', email: 'bob@tim.com'}) .then(function(data) { data.should.have.property('username','bob'); }) .catch(function(err) { err.should.have.property('code', 1004); }); }); it('should not allow login with incorrect email', function() { return dbAccount.findAccount({password: 'b0b5p455w0rd', email: 'tim@bob.com'}) .then(function(data) { data.should.have.property('username','bob'); }) .catch(function(err) { err.should.have.property('code', 1004); }); }); it('should return account details for a valid account', function() { return dbAccount.findAccount({ password: 'b0b5p455w0rd', email: 'bob@tim.com' }) .then(function(data) { return dbAccount.sqlGetAccountDetails(data.uid) .then(function(wantedData){ console.log(wantedData); wantedData.should.have.property('username','bob'); }); }) .catch(function(err) { console.log(err); err.should.be.ok(); }); }); it('should return account details for a valid account', function() { return dbAccount.sqlGetAccountDetails('DEADBEEFDEADBEEF') .then(function(wantedData){ wantedData.should.have.property('username','bob'); }).catch(function(err) { console.log(err); err.should.be.ok(); }); }); it('should insert profile details', function() { let profileData = { forename:'Bob', surname:'Bobbington', gender:0, dob:'1974-10-24', bio:'Sharks have a week dedicated to him' }; return dbAccount.findAccount({ password: 'b0b5p455w0rd', email: 'bob@tim.com' }) .then(function(data) { profileData.uid = data.uid; return dbAccount.addInsertProfile(profileData) .then(function(sqlResponse){ sqlResponse.should.be.ok(); }); }) .catch(function(err) { console.log(err); err.should.not.be.ok(); }); }); it('should insert update details', function() { let profileData = { forename:'Tim', surname:'Timmington', gender:0, dob:'2000-01-01', bio:'He is forbidden from walking through cemetaries, because of that one incident where he raised the dead' }; return dbAccount.findAccount({ password: 'b0b5p455w0rd', email: 'bob@tim.com' }) .then(function(data) { profileData.uid = data.uid; return dbAccount.addInsertProfile(profileData) .then(function(sqlResponse){ sqlResponse.should.be.ok(); }); }) .catch(function(err) { console.log(err); err.should.not.be.ok(); }); }); it('should return profile details', function() { return dbAccount.findAccount({ password: 'b0b5p455w0rd', email: 'bob@tim.com' }) .then(function(data) { return dbAccount.sqlGetProfile(data.uid) .then(function(sqlResponse){ console.log(sqlResponse); sqlResponse.should.have.property('forename','Tim'); }); }) .catch(function(err) { console.log(err); err.should.not.be.ok(); }); }); it('should add Martin', function() { return dbAccount.addNewAccount({ username: 'Martin ', password: 'MPReoa43', email: 'martind2000@gmail.com' }) .then(function(data) { should(data.reply).equal('user added'); }) .catch(function(err) { console.log(err); err.should.not.be.ok(); }); }); it('should add Default', function() { return dbAccount.addNewAccount({ username: 'Default', password: 'password', email: 'm@g.com' }) .then(function(data) { should(data.reply).equal('user added'); }) .catch(function(err) { console.log(err); err.should.not.be.ok(); }); }); // TEST END HERE });