Skip to content

Commit

Permalink
add jest test for postgresCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
Dnaganog committed Jul 8, 2019
1 parent c5734a1 commit 6c26c6f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 35 deletions.
55 changes: 28 additions & 27 deletions __tests__/functions/databases/postgresql/create.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
// const client = require('../../../../index')(process.env.SID, process.env.AUTH, {
// isPostgres: true,
// appName: 'testApp',
// connectionURI: 'postgres://postgres:yellowjacket@localhost/twoauthtest',
// });

// // client.create("ian", "+17604207520");

// client
// .create('ian', '+12016750593')
// .then(res => console.log(res))
// .catch(err => console.log(err));
// // describe("Tests for Postgres Send", () => {

// // });

const create = require("../../../../functions/databases/postgres/create");
describe('tests the pg create method', () => {
class FakeClient {
constructor() {
constructor(isError) {
this.users = {};
this.isError = isError;
this.pgConnect = function () {
return new Promise((resolve, reject) => {
resolve({
query: (query, values) => new Promise((resolve, reject) => {
resolve('fakeUser');
}),
database: {
query: (query, values) => new Promise((resolve, reject) => {
resolve('fakeUser');
}),
},
done: () => null,
});
});
};
}
this.client = {
verify: {
services: {
create: ({})
}
}
this.client = {
verify: {
services: {
create: () => new Promise((resolve, reject) => {
resolve({ sid: 'testSID' });
}),
},
},
};
this.create = create;
}
}

it("generates a postgres row with the correct sid", () => {
const fakeClient = new FakeClient(false);
let database = fakeClient.pgConnect();
return fakeClient.create('fakeUser', '+11231231234').then((user) => {
expect(user).toEqual('fakeUser');
})
})
});
51 changes: 43 additions & 8 deletions functions/databases/postgres/verify.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
module.exports = function(userID, phone) {
module.exports = function(userID, code) {
const { pgConnect } = this;
return new Promise((resolve, reject) => {
this.pgConnect()
pgConnect()
.then(({ database, done }) => {
// pgClient.query... blah blah logic
//invoke done before your resolve this promise
done();
//Query to just check if the user was created
const query = "SELECT * FROM twoauthusers WHERE userid=$1";
const values = [String(userID)];

database
.query(query, values)
.then(res => {
const { sid, phone } = res.rows[0];

if (!sid)
reject(new Error("SID Error: No SID exists for this user."));
if (!phone)
reject(
new Error(
"Phone Number Error: No phone number exists for this user."
)
);

return this.client.verify
.services(sid)
.verificationChecks.create({
to: phone,
code
})
.then(verification => {
if (verification.status === "approved") resolve(true);
resolve(false);
})
.catch(err => {
done();
resolve(false);
});
})
.catch(err => {
done();
reject(new Error("Could not find Database at Connection URI."));
});
})
.catch((err, done) => {
//invoke done before you reject
.catch(err => {
done();
reject(err);
reject(new Error("Could not find Database at Connection URI."));
});
});
//invoke done before you reject
};

0 comments on commit 6c26c6f

Please sign in to comment.