From 5344b4d0ce0c8b0c97918ae073c1799dc3997702 Mon Sep 17 00:00:00 2001 From: Giuseppe Zep Valentino Date: Mon, 8 Jul 2019 16:37:37 -0400 Subject: [PATCH 1/2] added the postgresql logic --- functions/databases/postgres/verify.js | 51 ++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/functions/databases/postgres/verify.js b/functions/databases/postgres/verify.js index 8dc374a..f31dab9 100644 --- a/functions/databases/postgres/verify.js +++ b/functions/databases/postgres/verify.js @@ -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 }; From ca6e860f473ee2b2eb1b038665d4bde2b9082fc2 Mon Sep 17 00:00:00 2001 From: Sierra Date: Mon, 8 Jul 2019 18:24:41 -0400 Subject: [PATCH 2/2] mongoose create test --- .../functions/databases/mongoose/create.js | 58 +++++++++++++++++++ .../functions/databases/mongoose/send.js | 4 ++ 2 files changed, 62 insertions(+) diff --git a/__tests__/functions/databases/mongoose/create.js b/__tests__/functions/databases/mongoose/create.js index e69de29..9d4ea26 100644 --- a/__tests__/functions/databases/mongoose/create.js +++ b/__tests__/functions/databases/mongoose/create.js @@ -0,0 +1,58 @@ +const mongoose = require('mongoose'); +const mongooseCreate = require ('../../../../functions/databases/mongoose/create'); + +describe('tests the create function', () => { + class FakeClient { + constructor( + AccSID, + AuthToken + ) { + this.AccSID = AccSID; + this.AuthToken = AuthToken; + this.client = { + verify: { + services: { + create: () => { + const isError = this.isError; + return new Promise((resolve, reject) => { + if (isError) { + reject(new Error("fake error message")); + } + resolve({ + sid: "fakeSid" + }); + }); + } + } + } + }; + this.TwoAuthUser = { + create: ({ + userID, + sid, + phone + }) => { + return new Promise((resolve, reject) => { + resolve(5) + }) + } + }; + this.create = mongooseCreate; + } + } + + it('generates a user with the right sid', async () => { + const newClient = new FakeClient(); + // create a user using client.create() + let result = await newClient.create('ian', '+17604307620') + expect(result).toBe(5) + // query database for that user + }); + + it('passes error message on rejection', () => { + const newClient = new FakeClient(); + let users = newClient.users; + newClient.create('ian', '+17604307620').catch(err => { expect(err instanceof Error).toBeTruthy(); + }); + }); +}); \ No newline at end of file diff --git a/__tests__/functions/databases/mongoose/send.js b/__tests__/functions/databases/mongoose/send.js index e69de29..6590125 100644 --- a/__tests__/functions/databases/mongoose/send.js +++ b/__tests__/functions/databases/mongoose/send.js @@ -0,0 +1,4 @@ +const mongoose = require("mongoose"); +const mongooseSend = require("../../../../functions/databases/mongoose/send"); + +