Skip to content

Commit

Permalink
Add test case for user that uses social authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
ChesterSng committed Mar 23, 2019
1 parent d20cd1b commit 87d86ee
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
18 changes: 18 additions & 0 deletions test/api/v3/integration/user/auth/POST-login-local.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,22 @@ describe('POST /user/auth/local/login', () => {
let isValidPassword = await bcryptCompare(textPassword, user.auth.local.hashed_password);
expect(isValidPassword).to.equal(true);
});

it('user uses social authentication and has no password', async () => {
await user.unset({
'auth.local.hashed_password': 1,
});

await user.sync();
expect(user.auth.local.hashed_password).to.be.undefined;

await expect(api.post(endpoint, {
username: user.auth.local.username,
password: 'any-password',
})).to.eventually.be.rejected.and.eql({
code: 401,
error: 'NotAuthorized',
message: t('invalidLoginCredentialsLong'),
});
});
});
13 changes: 13 additions & 0 deletions test/helpers/api-integration/api-classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { requester } from './requester';
import {
getDocument as getDocumentFromMongo,
updateDocument as updateDocumentInMongo,
unsetDocument as unsetDocumentInMongo,
} from '../mongo';
import {
assign,
Expand All @@ -29,6 +30,18 @@ class ApiObject {
return this;
}

async unset (options) {
if (isEmpty(options)) {
return;
}

await unsetDocumentInMongo(this._docType, this, options);

_updateLocalParameters((this, options));

return this;
}

async sync () {
let updatedDoc = await getDocumentFromMongo(this._docType, this);

Expand Down
13 changes: 13 additions & 0 deletions test/helpers/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ export async function updateDocument (collectionName, doc, update) {
});
}

// Unset a property in the database.
// Useful for testing.
export async function unsetDocument (collectionName, doc, update) {
let collection = mongoose.connection.db.collection(collectionName);

return new Promise((resolve) => {
collection.updateOne({ _id: doc._id }, { $unset: update }, (updateErr) => {
if (updateErr) throw new Error(`Error updating ${collectionName}: ${updateErr}`);
resolve();
});
});
}

export async function getDocument (collectionName, doc) {
let collection = mongoose.connection.db.collection(collectionName);

Expand Down

0 comments on commit 87d86ee

Please sign in to comment.