Browse Source

All tests are passing now without Bookshelf.

bookshelf-removal
Zed A. Shaw 2 months ago
parent
commit
a4e08c31dc
9 changed files with 70 additions and 75 deletions
  1. +1
    -1
      __tests__/ui/login.spec.js
  2. +2
    -2
      __tests__/ui/modules.spec.js
  3. +6
    -6
      __tests__/ui/register.spec.js
  4. +4
    -1
      __tests__/utils.js
  5. +1
    -1
      lib/models.js
  6. +44
    -44
      scripts/services/auth.js
  7. +3
    -10
      src/routes/auth/verify.js
  8. +7
    -8
      src/routes/register/index.json.js
  9. +2
    -2
      src/routes/user/index.json.js

+ 1
- 1
__tests__/ui/login.spec.js View File

@@ -8,7 +8,7 @@ const host = require('@lib/secrets').root_url;

afterAll(async () => {
t.close()
await User.delete_by_email(main_user.email, false);
await User.delete_by_email(main_user.email);
knex.destroy();
});


+ 2
- 2
__tests__/ui/modules.spec.js View File

@@ -1,13 +1,13 @@
const t = require('@lib/testing');
const host = require('@lib/secrets').root_url;
const {User, Auth, knex} = require('@lib/models');
const {User, knex} = require('@lib/models');
const utils = require('../utils');

const main_user = t.fake_person();

afterAll(async () => {
t.close();
await User.delete_by_email(main_user.email, false);
await User.delete_by_email(main_user.email);
knex.destroy();
});


+ 6
- 6
__tests__/ui/register.spec.js View File

@@ -1,6 +1,6 @@

const t = require('@lib/testing');
const {User, Auth, knex} = require('@lib/models');
const {User, knex} = require('@lib/models');
const host = require('@lib/secrets').root_url;
const utils = require('../utils');

@@ -31,17 +31,17 @@ it('Can register for an account after soft login', async () => {
await page.click(t.sel('send-emails'));
await page.click(t.sel('account-submit'));

let updated = await User.find_and_validate(main_user.email, false);
while(updated.user.get('send_emails') == true) {
let updated = await User.find_and_validate(main_user.email);
while(updated.user.send_emails == true) {
console.log("User hasn't been set yet.", updated.user.full_name);
await t.sleep(1000); /// so dumb, but have to wait for this to finish
updated = await User.find_and_validate(main_user.email, false);
updated = await User.find_and_validate(main_user.email);
}

expect(updated.user).toBeDefined();
expect(updated.is_valid).toBe(true);
expect(updated.user.get('full_name')).toBe(main_user.name + ' Updated');
expect(updated.user.get('send_emails')).toBe(false);
expect(updated.user.full_name).toBe(main_user.name + ' Updated');
expect(updated.user.send_emails).toBe(false);
});

it('Can register off the index.', async () => {

+ 4
- 1
__tests__/utils.js View File

@@ -1,4 +1,5 @@
const t = require('@lib/testing');
const { log } = require('@lib/logging');
const {User, Auth, knex} = require('@lib/models');

/* Common operations common to this test suite. */
@@ -41,9 +42,11 @@ exports.register = async (main_user, page) => {
await page.click(t.sel('tos-agree'));
await page.click(t.sel('register-submit'));

log.debug("reg wait for dashboard-page");
await page.waitForSelector(t.sel('dashboard-page'));

let {user, is_valid} = await User.find_and_validate(main_user.email, true);
log.debug("reg find_and_validate");
let {user, is_valid} = await User.find_and_validate(main_user.email);

expect(is_valid).toBe(true);


+ 1
- 1
lib/models.js View File

@@ -148,7 +148,7 @@ class User extends Model.from_table('users') {
assert(attr_only === false, "This parameter is no longer used.");
assert(email, `Valid email required: ${email}`);

let user = await knex.table('users').first().where({email});
let user = await knex('users').first().where({email});

// TODO: why am I getting by email then checking the email? do more validation
let is_valid = user ? user.email === email && user.verified : false;

+ 44
- 44
scripts/services/auth.js View File

@@ -35,72 +35,72 @@ const unsubscribe = new Queue('unsubscribe', secrets.bull_config);

auth_request.process(async (job) => {
try {
log.debug("Received job", job.id);
let authtoken;
let options;
log.debug("Received job", job.id);
let authtoken;
let options;

// lookup the person in the user's table, and if they aren't there then
// send a registration email instead of a login email
let {user, is_valid} = await User.find_and_validate(job.data.to)
log.debug("Processing auth_request user", user, "is_valid", is_valid);
// lookup the person in the user's table, and if they aren't there then
// send a registration email instead of a login email
let {user, is_valid} = await User.find_and_validate(job.data.to)
log.debug("Processing auth_request user", user, "is_valid", is_valid);

// TODO: lots of duplicate here
if(is_valid === true) {
log.debug("User", job.data.to, "is valid sending an authentication email.");
auth = await Auth.create(job.data.to, job.data.ip_addr, false);
authtoken = auth.get('token');
assert(auth, "No auth token returned on create for valid user.");
log.debug("Auth token is", authtoken);
options = gen_login_email(job.data.to, authtoken);
} else {
log.debug("User", job.data.to, "is NOT valid sending a REGISTRATION email.");
auth = await Auth.create(job.data.to, job.data.ip_addr, true);
authtoken = auth.get('token');
assert(auth, "No auth token returned on create for valid user.");
log.debug("Auth token is", authtoken);
options = gen_reg_email(job.data.to, authtoken);
}
// TODO: lots of duplicate here
if(is_valid === true) {
log.debug("User", job.data.to, "is valid sending an authentication email.");
auth = await Auth.create(job.data.to, job.data.ip_addr, false);
authtoken = auth.token;
assert(auth, "No auth token returned on create for valid user.");
log.debug("Auth token is", authtoken);
options = gen_login_email(job.data.to, authtoken);
} else {
log.debug("User", job.data.to, "is NOT valid sending a REGISTRATION email.");
auth = await Auth.create(job.data.to, job.data.ip_addr, true);
authtoken = auth.token;
assert(auth, "No auth token returned on create for valid user.");
log.debug("Auth token is", authtoken);
options = gen_reg_email(job.data.to, authtoken);
}

transporter.sendMail(options, (error, info) => {
if (error) {
log.error(error);
} else {
log.debug(info.envelope);
log.debug(info.messageId);
info.message.pipe(process.stdout);
}
});
transporter.sendMail(options, (error, info) => {
if (error) {
log.error(error);
} else {
log.debug(info.envelope);
log.debug(info.messageId);
info.message.pipe(process.stdout);
}
});
} catch (error) {
log.error(error, "auth_request");
log.error(error, "auth_request");
}
});

delete_auth.process(async (job) => {
try {
log.info("Received delete request", job.data.email);
await Auth.destroy_by_email(job.data.email, false)
log.info("Received delete request", job.data.email);
await Auth.destroy_by_email(job.data.email);
} catch (error) {
log.error(error, "delete_auth");
log.error(error, "delete_auth");
}
});

subscribe.process(async (job) => {
try {
log.info("Received subscribe for user", job.data.email);
log.debug("Job data for subscribe", job);
await train.subscribe(job.data.email);
log.info("Received subscribe for user", job.data.email);
log.debug("Job data for subscribe", job);
await train.subscribe(job.data.email);
} catch (error) {
log.error(error, "subscribe");
log.error(error, "subscribe");
}
});

unsubscribe.process(async (job) => {
try {
log.info("Received unsubscribe for user", job.data.email);
await train.unsubscribe(job.data.email);
log.info("Received unsubscribe for user", job.data.email);
await train.unsubscribe(job.data.email);
} catch (error) {
log.error(error, "unsubscribe");
done(error);
log.error(error, "unsubscribe");
done(error);
}
});


+ 3
- 10
src/routes/auth/verify.js View File

@@ -2,13 +2,6 @@ import { delete_authmail } from 'mq';
import { Auth, User} from '../../../lib/models';
import { log } from 'logging';


// temporary until I can swap it all out
export async function get_auth_token(token) {
let auth = await Auth.find_by_token(token);
return auth ? auth.attributes : undefined;
}

export async function post(req, res) {
const user = req.body;
const ip_addr = req.headers['x-forwarded-for'] || req.connection.localAddress;
@@ -18,11 +11,11 @@ export async function post(req, res) {

if(user.email === undefined) {
res.statusCode = 403;
res.end(JSON.stringify({message: "Email required."}));
log.debug("Email not given by user", user);
res.end(JSON.stringify({message: "Email required."}));
} else {
// get the user's auth token by email and token in the database
let expected_auth = await get_auth_token(user.token);
let expected_auth = await await Auth.find_by_token(user.token);
log.debug("Auth in the database is", expected_auth);

if(expected_auth === undefined) {
@@ -42,7 +35,7 @@ export async function post(req, res) {

if(user.registered) {
// look them up in the database and return that
let valid = await User.find_and_validate(user.email, true);
let valid = await User.find_and_validate(user.email);
// went through validating this auth, so we're kind of doing it again
if(valid.is_valid) {
log.debug("User", user, "test user", valid);

+ 7
- 8
src/routes/register/index.json.js View File

@@ -1,5 +1,5 @@
import { log } from 'logging'
const { User, Auth } = require('../../../lib/models');
const { User } = require('../../../lib/models');
import {subscribe_user} from 'mq';

async function register_user(user, full_name, send_emails, tos_agree)
@@ -10,9 +10,9 @@ async function register_user(user, full_name, send_emails, tos_agree)

let new_user = await User.register(user.email, full_name, send_emails, tos_agree);

if(new_user.get('send_emails')) {
if(new_user.send_emails) {
log.debug("Sending Queue the subscribe to train request.");
subscribe_user(new_user.get('email'));
subscribe_user(new_user.email);
}

return new_user;
@@ -35,13 +35,12 @@ export async function post(req, res) {
log.error(`User ${found.user.email} already exists.`, found.user.attributes);

// need to update their settings as well as set them registered
found.user.set('registered', true);
found.user.save({}, {method: "update"});

res.end(JSON.stringify({user: found.user.attributes}));
let count = User.update({id: found.user.id}, { registered: true});
log.debug("User update count after register is", count);
res.end(JSON.stringify({user: found.user}));
} else {
const updated_user = await register_user(user, full_name, send_emails, tos_agree);
res.end(JSON.stringify({user: updated_user.attributes}));
res.end(JSON.stringify({user: updated_user}));
}
} catch(error) {
log.error(error, "register post");

+ 2
- 2
src/routes/user/index.json.js View File

@@ -5,8 +5,8 @@ export async function post(req, res) {
if(req.session.user && req.session.user.verified) {
log.debug("Received user", req.body);
let new_user = await User.update_by_email(req.session.user.email, req.body.full_name, req.body.send_emails);
req.session.user.full_name = new_user.get('full_name');
req.session.user.send_emails = new_user.get('send_emails');
req.session.user.full_name = new_user.full_name;
req.session.user.send_emails = new_user.send_emails;
res.end(JSON.stringify({message: "OK"}));
} else {
log.error("Attempt to access API without auth.");

Loading…
Cancel
Save