-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' of github.com:two-factor/two-factor into dev
- Loading branch information
Showing
5 changed files
with
146 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const mongoose = require("mongoose"); | ||
const mongooseSend = require("../../../../functions/databases/mongoose/send"); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +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(isError) { | ||
this.users = {}; | ||
this.isError = isError; | ||
this.pgConnect = function () { | ||
return new Promise((resolve, reject) => { | ||
resolve({ | ||
database: { | ||
query: (query, values) => new Promise((resolve, reject) => { | ||
resolve('fakeUser'); | ||
}), | ||
}, | ||
done: () => null, | ||
}); | ||
}); | ||
}; | ||
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'); | ||
}) | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |