forked from mozilla/blurts-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use test-blurts db with fixture data for db tests
cover "public" DB methods with tests
- Loading branch information
1 parent
f806dd1
commit 4be9673
Showing
11 changed files
with
210 additions
and
34 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
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
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,32 @@ | ||
"use strict"; | ||
|
||
const getSha1 = require("../../sha1-utils"); | ||
|
||
|
||
exports.seed = function(knex) { | ||
// Deletes ALL existing entries | ||
return knex("subscribers").del() | ||
.then(function () { | ||
// Inserts seed entries | ||
return knex("subscribers").insert([ | ||
{ | ||
sha1: getSha1("firefoxaccount@test.com"), | ||
email: "firefoxaccount@test.com", | ||
verification_token: "", | ||
verified: true, | ||
}, | ||
{ | ||
sha1: getSha1("unverifiedemail@test.com"), | ||
email: "unverifiedemail@test.com", | ||
verification_token: "0e2cb147-2041-4e5b-8ca9-494e773b2cf0", | ||
verified: false, | ||
}, | ||
{ | ||
sha1: getSha1("verifiedemail@test.com"), | ||
email: "verifiedemail@test.com", | ||
verification_token: "54010800-6c3c-4186-971a-76dc92874941", | ||
verified: true, | ||
}, | ||
]); | ||
}); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
Binary file not shown.
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,73 @@ | ||
"use strict"; | ||
|
||
const test = require("tape-async"); | ||
|
||
const DB = require("../db/DB"); | ||
const getSha1 = require("../sha1-utils"); | ||
|
||
test("getSubscriberByEmailAndToken accepts email and token and returns subscriber", async t => { | ||
const testEmail = "unverifiedemail@test.com"; | ||
const testToken = "0e2cb147-2041-4e5b-8ca9-494e773b2cf0"; | ||
const subscriber = await DB.getSubscriberByEmailAndToken(testEmail, testToken); | ||
|
||
t.ok(subscriber.email === testEmail); | ||
t.ok(subscriber.verification_token === testToken); | ||
}); | ||
|
||
test("getSubscribersByHashes accepts hashes and only returns verified subscribers", async t => { | ||
const testHashes = [ | ||
"firefoxaccount@test.com", | ||
"unverifiedemail@test.com", | ||
"verifiedemail@test.com", | ||
].map(email => getSha1(email)); | ||
const subscribers = await DB.getSubscribersByHashes(testHashes); | ||
for (const subscriber of subscribers) { | ||
t.ok(subscriber.verified); | ||
} | ||
}); | ||
|
||
test("addSubscriberUnverifiedEmailHash accepts email and returns unverified subscriber with sha1 hash and verification token", async t => { | ||
const testEmail = "test@test.com"; | ||
// https://stackoverflow.com/a/13653180 | ||
const uuidRE = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; | ||
|
||
const subscriber = await DB.addSubscriberUnverifiedEmailHash(testEmail); | ||
t.ok(subscriber.sha1 === getSha1(testEmail)); | ||
t.ok(uuidRE.test(subscriber.verification_token)); | ||
t.notOk(subscriber.verified); | ||
}); | ||
|
||
test("verifyEmailHash accepts token and email and returns verified subscriber", async t => { | ||
const testEmail = "verifyEmailHash@test.com"; | ||
|
||
const unverifiedSubscriber = await DB.addSubscriberUnverifiedEmailHash(testEmail); | ||
t.notOk(unverifiedSubscriber.verified); | ||
|
||
const verifiedSubscriber = await DB.verifyEmailHash(unverifiedSubscriber.verification_token, unverifiedSubscriber.email); | ||
t.ok(verifiedSubscriber.sha1 === getSha1(testEmail)); | ||
t.ok(verifiedSubscriber.verified); | ||
}); | ||
|
||
test("addSubscriber accepts email and returns verified subscriber", async t => { | ||
const testEmail = "newFirefoxAccount@test.com"; | ||
|
||
const verifiedSubscriber = await DB.addSubscriber(testEmail); | ||
|
||
t.ok(verifiedSubscriber.email === testEmail); | ||
t.ok(verifiedSubscriber.verified); | ||
t.ok(verifiedSubscriber.sha1 === getSha1(testEmail)); | ||
}); | ||
|
||
test("removeSubscriber accepts email and removes the email address", async t => { | ||
const testEmail = "removingFirefoxAccount@test.com"; | ||
|
||
const verifiedSubscriber = await DB.addSubscriber(testEmail); | ||
const removedSubscriber = await DB.removeSubscriber(verifiedSubscriber.email); | ||
console.log("removedSubscriber: ", removedSubscriber); | ||
|
||
t.ok(removedSubscriber.email === null); | ||
}); | ||
|
||
test("teardown", async t => { | ||
DB.destroyConnection(); | ||
}); |
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,19 +1,25 @@ | ||
"use strict"; | ||
|
||
const test = require("tape"); | ||
const test = require("tape-async"); | ||
const getSha1 = require("../sha1-utils"); | ||
|
||
|
||
function isHexString(hashDigest) { | ||
for (const character of hashDigest) { | ||
if (parseInt(character, 16).toString(16) != character.toLowerCase()) { | ||
if (parseInt(character, 16).toString(16) !== character.toLowerCase()) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
test("getSha1 returns hex digest", function (t) { | ||
t.plan(1); | ||
test("getSha1 returns hex digest", t => { | ||
t.ok(isHexString(getSha1("test@test.com"))); | ||
t.end(); | ||
}); | ||
|
||
test("tape-async example", async t => { | ||
t.ok(isHexString(getSha1("test@test.com"))); | ||
const a = await Promise.resolve(42); | ||
t.equal(a, 42); | ||
}); |