diff --git a/__tests__/functions/databases/mongoose/send.js b/__tests__/functions/databases/mongoose/send.js deleted file mode 100644 index 6590125..0000000 --- a/__tests__/functions/databases/mongoose/send.js +++ /dev/null @@ -1,4 +0,0 @@ -const mongoose = require("mongoose"); -const mongooseSend = require("../../../../functions/databases/mongoose/send"); - - diff --git a/__tests__/functions/databases/mongoose/verify.js b/__tests__/functions/databases/mongoose/verify.js deleted file mode 100644 index 1b0cb05..0000000 --- a/__tests__/functions/databases/mongoose/verify.js +++ /dev/null @@ -1,3 +0,0 @@ -describe('', () => { - -} \ No newline at end of file diff --git a/__tests__/functions/databases/postgresql/send.js b/__tests__/functions/databases/postgresql/send.js index 77387d0..542594f 100644 --- a/__tests__/functions/databases/postgresql/send.js +++ b/__tests__/functions/databases/postgresql/send.js @@ -1,14 +1,82 @@ -// const client = require("../../../../index")(process.env.SID, process.env.AUTH, { -// isPostgres: true, -// connectionURI: "postgres://student:ilovetesting@localhost/twoauthtests" -// }); +const send = require("../../../../functions/databases/postgres/send"); -// // client.create("ian", "+17604207520"); +describe("tests the pg send function", () => { + const mockSave = jest.fn(function(x) { + return x; + }); -// client -// .send("ian") -// .then(res => console.log(res)) -// .catch(err => console.log(err)); -// // describe("Tests for Postgres Send", () => { + beforeAll(() => { + mockSave.mockClear(); + }); + class FakeClient { + constructor(sidExists = true) { + this.pgConnect = function() { + return new Promise((resolve, reject) => { + resolve({ + database: { + query: function(query, values, callback) { + mockSave(); + if (sidExists) { + callback(null, { + rows: [ + { + sid: "fakesid", + phone: "1234" + } + ] + }); + } else { + callback(null, { + rows: [ + { + sid: null, + phone: "1234" + } + ] + }); + } + } + }, + done: function() { + return null; + } + }); + }); + }; + this.client = { + verify: { + services: function(sid) { + return { + verifications: { + create: function({ to, phone }) { + return new Promise((resolve, reject) => { + resolve("fakeverification"); + }); + } + } + }; + } + } + }; + this.send = send; + } + } -// // }); + it("successfully saves to a database", async () => { + const client = new FakeClient(); + const result = await client.send(); + expect(mockSave.mock.calls.length).toBe(1); + }); + + it("rejects with an error if no sid exists", async () => { + const client = new FakeClient(false); + const result = client.send(); + expect(result).rejects.toBeInstanceOf(Error); + }); + + it("successfully resolves a verification from twilio", async () => { + const client = new FakeClient(); + const result = await client.send(); + expect(result).toBe("fakeverification"); + }); +}); diff --git a/functions/databases/postgres/send.js b/functions/databases/postgres/send.js index be943f0..545d4b6 100644 --- a/functions/databases/postgres/send.js +++ b/functions/databases/postgres/send.js @@ -6,16 +6,23 @@ module.exports = function(userID) { const query = "SELECT * FROM twoauthusers WHERE userID=$1"; const values = [String(userID)]; database.query(query, values, (err, res) => { - if (err) reject(err); + if (err) { + done(); + reject(err); + } const { sid, phone } = res.rows[0]; - if (!sid) + if (!sid) { + done(); reject(new Error("SID Error: No SID exists for this user.")); - if (!phone) + } + if (!phone) { + done(); reject( new Error( "Phone Number Error: No phone number exists for this user." ) ); + } //invoke done before your resolve this promise client.verify .services(sid) @@ -34,7 +41,6 @@ module.exports = function(userID) { }); }) .catch(err => { - done(); reject(err); //"userID Error: This userID has not been created yet." });