Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added tests for postgres send #37

Merged
merged 6 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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