Skip to content

Commit

Permalink
Merge pull request #37 from two-factor/testsend
Browse files Browse the repository at this point in the history
added tests for postgres send
  • Loading branch information
Dnaganog committed Jul 9, 2019
2 parents 6eca9a4 + 049777c commit 3a4ece2
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 22 deletions.
4 changes: 0 additions & 4 deletions __tests__/functions/databases/mongoose/send.js

This file was deleted.

3 changes: 0 additions & 3 deletions __tests__/functions/databases/mongoose/verify.js

This file was deleted.

90 changes: 79 additions & 11 deletions __tests__/functions/databases/postgresql/send.js
Original file line number Diff line number Diff line change
@@ -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");
});
});
14 changes: 10 additions & 4 deletions functions/databases/postgres/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -34,7 +41,6 @@ module.exports = function(userID) {
});
})
.catch(err => {
done();
reject(err);
//"userID Error: This userID has not been created yet."
});
Expand Down

0 comments on commit 3a4ece2

Please sign in to comment.