Skip to content

Commit

Permalink
fix(importer): Fix navigation and login after importing user data loc…
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienjoly committed Nov 1, 2020
1 parent 9733af2 commit 6a1b503
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 40 deletions.
19 changes: 19 additions & 0 deletions app/controllers/testing/refresh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// A private controller to refresh the user cache after users were inserted manually into the test db.

const mongodb = require('../../models/mongodb.js');

exports.controller = async function (request, getParams, response) {
request.logToConsole('refresh.controller', request.method);
if (request.method.toLowerCase() !== 'post') {
return response.badRequest();
}
if (process.appParams.mongoDbDatabase !== 'openwhyd_test') {
return response.forbidden(new Error('allowed on test database only'));
}
try {
await mongodb.initCollections({ addTestData: false });
response.renderJSON({ ok: true });
} catch (err) {
response.renderJSON({ error: err.message });
}
};
9 changes: 3 additions & 6 deletions docs/INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,11 @@ $ npm run docker:test # will run the automated tests: unit and e
If you want to import some user data from openwhyd.org into your local/test database, you can use the following script:

```sh
$ npm run docker:seed # will clear the database and create the admin user
$ node scripts/import-from-prod.js # will import 21 posts from https://openwhyd.org/test
$ npm run docker:seed # will clear the database
$ node scripts/import-from-prod.js test # will import 21 posts from https://openwhyd.org/test
```

After that, you will be able to sign in as an administrator using the following credentials:

- username: `admin`
- password: `admin`
After that, you will be able to sign in as an administrator using the credentials returned by the script.

The data imported can be seen from http://localhost:8080/all

Expand Down
79 changes: 45 additions & 34 deletions scripts/import-from-prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ const ObjectID = (id) => mongodb.ObjectID.createFromHexString(id);
// Parameters
const url = 'mongodb://localhost:27117';
const dbName = 'openwhyd_test';
const username = process.env[2] || 'test'; // default profile: https://openwhyd.org/test
const username = process.argv[2] || 'test'; // default profile: https://openwhyd.org/test
const password = {
plain: 'admin',
md5: '21232f297a57a5a743894a0e4a801fc3',
};

const connectToDb = ({ url, dbName }) =>
new Promise((resolve, reject) =>
Expand All @@ -23,59 +27,66 @@ const connectToDb = ({ url, dbName }) =>
})
);

const fetchUserProfile = ({ username }) =>
new Promise((resolve, reject) => {
const url = `https://openwhyd.org/api/user/${username}?format=json`;
console.log(`fetching profile from ${url} ...`);
request(url, (err, _, body) =>
err ? reject(err) : resolve(JSON.parse(body))
);
});

const fetchUserData = ({ username }) =>
new Promise((resolve, reject) => {
const url = `https://openwhyd.org/${username}?format=json`;
console.log(`fetching tracks from ${url} ...`);
request(url, function (error, response, body) {
if (error) {
reject(error);
} else {
resolve({
posts: JSON.parse(body).map((post) => ({
...post,
_id: ObjectID(post._id),
})),
});
}
});
request(url, (err, _, body) =>
err
? reject(err)
: resolve({
posts: JSON.parse(body).map((post) => ({
...post,
_id: ObjectID(post._id),
})),
})
);
});

function genUserFromPost({ post }) {
return {
_id: ObjectID(post.uId),
id: post.uId,
name: post.uNm,
};
}

const insertUser = ({ db, user }) =>
const upsertUser = ({ db, user }) =>
new Promise((resolve, reject) => {
db.collection('user').insertOne(user, function (err, r) {
if (err) reject(err);
else resolve();
});
const { _id, ...userData } = user;
db.collection('user').updateOne(
{ _id: ObjectID(_id) },
{ $set: { ...userData, pwd: password.md5 } },
{ upsert: true },
(err) => (err ? reject(err) : resolve())
);
});

const insertPosts = ({ db, posts }) =>
new Promise((resolve, reject) => {
db.collection('post').insertMany(posts, function (err, r) {
if (err) reject(err);
else resolve();
});
db.collection('post').insertMany(posts, (err) =>
err ? reject(err) : resolve()
);
});

(async () => {
console.log(`connecting to ${url}/${dbName} ...`);
const { db, client } = await connectToDb({ url, dbName });
const user = await fetchUserProfile({ username });
await upsertUser({ db, user });
const { posts } = await fetchUserData({ username }); // or require(`./../${username}.json`);
console.log(`imported ${posts.length} posts`);
const user = genUserFromPost({ post: posts[0] });
// console.log('genUserFromPost =>', user);
// await insertUser({ db, user }); // may cause E11000 duplicate key error collection: openwhyd_test.user, after seeding the db
console.log('inserted user');
await insertPosts({ db, posts });
// refresh openwhyd's in-memory cache of users, to allow this user to login
await new Promise((resolve, reject) =>
request.post('http://localhost:8080/testing/refresh', (err) =>
err ? reject(err) : resolve()
)
);
client.close();
console.log(`inserted user => http://localhost:8080/${username}`);
console.log(`login id: ${username}, password: ${password.plain}`);
})().catch((err) => {
console.error(err);
process.exit(1);
Expand Down

0 comments on commit 6a1b503

Please sign in to comment.