Merge branch 'master' into asset-graph-api

This commit is contained in:
Kiwi
2017-08-29 21:04:30 +02:00
committed by GitHub
91 changed files with 1757 additions and 1708 deletions
+6 -6
View File
@@ -51,12 +51,12 @@ describe('graph.Context', () => {
id: '1',
name: 'Tag',
})
.then(() => {
throw new Error('should not reach this point');
})
.catch((err) => {
expect(err).to.be.equal(errors.ErrNotAuthorized);
});
.then(() => {
throw new Error('should not reach this point');
})
.catch((err) => {
expect(err).to.be.equal(errors.ErrNotAuthorized);
});
});
});
});
+16 -16
View File
@@ -181,24 +181,24 @@ describe('graph.mutations.createComment', () => {
body
}
})
.then(({data, errors}) => {
expect(errors).to.be.undefined;
expect(data.createComment).to.have.property('comment').not.null;
expect(data.createComment.comment).to.have.property('status', status);
expect(data.createComment).to.have.property('errors').null;
.then(({data, errors}) => {
expect(errors).to.be.undefined;
expect(data.createComment).to.have.property('comment').not.null;
expect(data.createComment.comment).to.have.property('status', status);
expect(data.createComment).to.have.property('errors').null;
return ActionModel.find({
item_id: data.createComment.comment.id,
action_type: 'FLAG'
return ActionModel.find({
item_id: data.createComment.comment.id,
action_type: 'FLAG'
});
})
.then((actions) => {
if (flagged) {
expect(actions).to.have.length(1);
} else {
expect(actions).to.have.length(0);
}
});
})
.then((actions) => {
if (flagged) {
expect(actions).to.have.length(1);
} else {
expect(actions).to.have.length(0);
}
});
});
});
+6 -6
View File
@@ -25,9 +25,9 @@ describe('/api/v1/account/username', () => {
.set(passport.inject({id: '456', roles: ['ADMIN']}));
const res = await chai.request(app)
.put('/api/v1/account/username')
.set(passport.inject({id: mockUser.id, roles: []}))
.send({username: 'MojoJojo'});
.put('/api/v1/account/username')
.set(passport.inject({id: mockUser.id, roles: []}))
.send({username: 'MojoJojo'});
expect(res).to.have.status(204);
});
@@ -38,9 +38,9 @@ describe('/api/v1/account/username', () => {
.set(passport.inject({id: '456', roles: ['ADMIN']}));
let res = chai.request(app)
.put('/api/v1/account/username')
.set(passport.inject({id: 'wrongid', roles: []}))
.send({username: 'MojoJojo'});
.put('/api/v1/account/username')
.set(passport.inject({id: 'wrongid', roles: []}))
.send({username: 'MojoJojo'});
return expect(res).to.eventually.be.rejected;
});
+10 -13
View File
@@ -25,14 +25,11 @@ const SettingsService = require('../../../../../services/settings');
describe('/api/v1/auth/local', () => {
let mockUser;
beforeEach(() => {
beforeEach(async () => {
const settings = {requireEmailConfirmation: false, wordlist: {banned: ['bad'], suspect: ['naughty']}};
return SettingsService.init(settings).then(() => {
return UsersService.createLocalUser('maria@gmail.com', 'password!', 'Maria')
.then((user) => {
mockUser = user;
});
});
await SettingsService.init(settings);
mockUser = await UsersService.createLocalUser('maria@gmail.com', 'password!', 'Maria');
});
describe('email confirmation disabled', () => {
@@ -53,12 +50,12 @@ describe('/api/v1/auth/local', () => {
it('should not send back the user on a unsuccessful login', () => {
return chai.request(app)
.post('/api/v1/auth/local')
.send({email: 'maria@gmail.com', password: 'password!3'})
.catch((err) => {
expect(err).to.not.be.null;
expect(err.response).to.have.status(401);
expect(err.response.body).to.have.property('message', 'not authorized');
});
.send({email: 'maria@gmail.com', password: 'password!3'})
.catch((err) => {
expect(err).to.not.be.null;
expect(err.response).to.have.status(401);
expect(err.response.body).to.have.property('message', 'not authorized');
});
});
});
+9 -9
View File
@@ -20,9 +20,9 @@ describe('/api/v1/users/:user_id/email/confirm', () => {
beforeEach(() => SettingsService.init(settings).then(() => {
return UsersService.createLocalUser('ana@gmail.com', '123321123', 'Ana');
})
.then((user) => {
mockUser = user;
}));
.then((user) => {
mockUser = user;
}));
describe('#post', () => {
it('should send an email when we hit the endpoint', () => {
@@ -56,9 +56,9 @@ describe('/api/v1/users/:user_id/actions', () => {
beforeEach(() => SettingsService.init(settings).then(() => {
return UsersService.createLocalUser('ana@gmail.com', '123321123', 'Ana');
})
.then((user) => {
mockUser = user;
}));
.then((user) => {
mockUser = user;
}));
describe('#post', () => {
it('it should update actions', () => {
@@ -82,9 +82,9 @@ describe('/api/v1/users/:user_id/username-enable', () => {
beforeEach(() => SettingsService.init(settings).then(() => {
return UsersService.createLocalUser('ana@gmail.com', '123321123', 'Ana');
})
.then((user) => {
mockUser = user;
}));
.then((user) => {
mockUser = user;
}));
describe('#post', () => {
it('it should enable a user to edit their username', () => {
+56 -93
View File
@@ -2,64 +2,58 @@ const UsersService = require('../../../services/users');
const SettingsService = require('../../../services/settings');
const chai = require('chai');
chai.use(require('chai-as-promised'));
const expect = chai.expect;
describe('services.UsersService', () => {
let mockUsers;
beforeEach(() => {
beforeEach(async () => {
const settings = {id: '1', moderation: 'PRE', wordlist: {banned: ['bad words'], suspect: ['suspect words']}};
return SettingsService.init(settings).then(() => {
return UsersService.createLocalUsers([{
email: 'stampi@gmail.com',
username: 'Stampi',
password: '1Coral!-'
}, {
email: 'sockmonster@gmail.com',
username: 'Sockmonster',
password: '2Coral!2'
}, {
email: 'marvel@gmail.com',
username: 'Marvel',
password: '3Coral!3'
}]).then((users) => {
mockUsers = users;
});
});
await SettingsService.init(settings);
mockUsers = await UsersService.createLocalUsers([{
email: 'stampi@gmail.com',
username: 'Stampi',
password: '1Coral!-'
}, {
email: 'sockmonster@gmail.com',
username: 'Sockmonster',
password: '2Coral!2'
}, {
email: 'marvel@gmail.com',
username: 'Marvel',
password: '3Coral!3'
}]);
});
describe('#findById()', () => {
it('should find a user by id', () => {
return UsersService
.findById(mockUsers[0].id)
.then((user) => {
expect(user).to.have.property('username', 'Stampi');
});
it('should find a user by id', async () => {
const user = await UsersService.findById(mockUsers[0].id);
expect(user).to.have.property('username', 'Stampi');
});
});
describe('#findByIdArray()', () => {
it('should find an array of users from an array of ids', () => {
it('should find an array of users from an array of ids', async () => {
const ids = mockUsers.map((user) => user.id);
return UsersService.findByIdArray(ids).then((result) => {
expect(result).to.have.length(3);
});
const users = await UsersService.findByIdArray(ids);
expect(users).to.have.length(3);
});
});
describe('#findPublicByIdArray()', () => {
it('should find an array of users from an array of ids', () => {
it('should find an array of users from an array of ids', async () => {
const ids = mockUsers.map((user) => user.id);
return UsersService.findPublicByIdArray(ids).then((result) => {
expect(result).to.have.length(3);
const sorted = result.sort((a, b) => {
if(a.username < b.username) {return -1;}
if(a.username > b.username) {return 1;}
return 0;
});
expect(sorted[0]).to.have.property('username', 'Marvel');
const users = await UsersService.findPublicByIdArray(ids);
expect(users).to.have.length(3);
const sorted = users.sort((a, b) => {
if(a.username < b.username) {return -1;}
if(a.username > b.username) {return 1;}
return 0;
});
expect(sorted[0]).to.have.property('username', 'Marvel');
});
});
@@ -82,56 +76,43 @@ describe('services.UsersService', () => {
username: 'StampiTheSecond',
password: '1Coralito!'
}])
.then((user) => {
expect(user).to.be.null;
})
.catch((error) => {
expect(error).to.not.be.null;
});
.then((user) => {
expect(user).to.be.null;
})
.catch((error) => {
expect(error).to.not.be.null;
});
});
});
describe('#createEmailConfirmToken', () => {
it('should create a token for a valid user', () => {
return UsersService
.createEmailConfirmToken(mockUsers[0].id, mockUsers[0].profiles[0].id)
.then((token) => {
expect(token).to.not.be.null;
});
it('should create a token for a valid user', async () => {
const token = await UsersService.createEmailConfirmToken(mockUsers[0].id, mockUsers[0].profiles[0].id);
expect(token).to.not.be.null;
});
it('should not create a token for a user already verified', () => {
return UsersService
.createEmailConfirmToken(mockUsers[0].id, mockUsers[0].profiles[0].id)
.then((token) => {
expect(token).to.not.be.null;
it('should not create a token for a user already verified', async () => {
const token = await UsersService.createEmailConfirmToken(mockUsers[0].id, mockUsers[0].profiles[0].id);
expect(token).to.not.be.null;
return UsersService.verifyEmailConfirmation(token);
})
.then(() => {
return UsersService.createEmailConfirmToken(mockUsers[0].id, mockUsers[0].profiles[0].id);
})
.catch((err) => {
expect(err).to.have.property('message', 'email address already confirmed');
});
await UsersService.verifyEmailConfirmation(token);
return expect(UsersService.createEmailConfirmToken(mockUsers[0].id, mockUsers[0].profiles[0].id)).to.eventually.be.rejected;
});
});
describe('#verifyEmailConfirmation', () => {
it('should correctly validate a valid token', () => {
return UsersService
.createEmailConfirmToken(mockUsers[0].id, mockUsers[0].profiles[0].id)
.then((token) => {
expect(token).to.not.be.null;
it('should correctly validate a valid token', async () => {
const token = await UsersService.createEmailConfirmToken(mockUsers[0].id, mockUsers[0].profiles[0].id);
expect(token).to.not.be.null;
return UsersService.verifyEmailConfirmation(token);
});
return expect(UsersService.verifyEmailConfirmation(token)).to.eventually.not.be.rejected;
});
it('should correctly reject an invalid token', () => {
it('should correctly reject an invalid token', async () => {
return UsersService
.verifyEmailConfirmation('cats')
.catch((err) => {
@@ -265,31 +246,13 @@ describe('services.UsersService', () => {
});
});
it('should return an error if canEditName is false', (done) => {
UsersService
.editName(mockUsers[0].id, 'Jojo')
.then(() => UsersService.findById(mockUsers[0].id))
.then(() => {
done(new Error('Error expected'));
})
.catch((err) => {
expect(err).to.be.ok;
done();
});
it('should return an error if canEditName is false', async () => {
return expect(UsersService.editName(mockUsers[0].id, 'Jojo')).to.eventually.be.rejected;
});
it('should return an error if the username is already taken', (done) => {
UsersService
.toggleNameEdit(mockUsers[0].id, true)
.then(() => UsersService.editName(mockUsers[0].id, 'Marvel'))
.then(() => UsersService.findById(mockUsers[0].id))
.then(() => {
done(new Error('Error expected'));
})
.catch((err) => {
expect(err).to.be.ok;
done();
});
it('should return an error if the username is already taken', async () => {
await UsersService.toggleNameEdit(mockUsers[0].id, true);
return expect(UsersService.editName(mockUsers[0].id, 'Marvel')).to.eventually.be.rejected;
});
it('should not allow non-alphanumeric characters in usernames', () => {