diff --git a/packages/rocketchat-api/server/v1/users.js b/packages/rocketchat-api/server/v1/users.js index b871381b81be..2d8e9b9003a7 100644 --- a/packages/rocketchat-api/server/v1/users.js +++ b/packages/rocketchat-api/server/v1/users.js @@ -416,3 +416,11 @@ RocketChat.API.v1.addRoute('users.forgotPassword', { authRequired: false }, { return RocketChat.API.v1.failure('User not found'); } }); + +RocketChat.API.v1.addRoute('users.getUsernameSuggestion', { authRequired: true }, { + get() { + const result = Meteor.runAsUser(this.userId, () => Meteor.call('getUsernameSuggestion')); + + return RocketChat.API.v1.success({ result }); + } +}); diff --git a/tests/end-to-end/api/01-users.js b/tests/end-to-end/api/01-users.js index 6eae83d7e99c..088ec84c4bdb 100644 --- a/tests/end-to-end/api/01-users.js +++ b/tests/end-to-end/api/01-users.js @@ -617,4 +617,54 @@ describe('[Users]', function() { .end(done); }); }); + + describe('[/users.getUsernameSuggestion]', () => { + const testUsername = `test${ +new Date() }`; + let targetUser; + let userCredentials; + it('register a new user...', (done) => { + request.post(api('users.register')) + .set(credentials) + .send({ + email: `${ testUsername }.@teste.com`, + username: `${ testUsername }test`, + name: testUsername, + pass: password + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + targetUser = res.body.user; + }) + .end(done); + }); + it('Login...', (done) => { + request.post(api('login')) + .send({ + user: targetUser.username, + password + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + userCredentials = {}; + userCredentials['X-Auth-Token'] = res.body.data.authToken; + userCredentials['X-User-Id'] = res.body.data.userId; + }) + .end(done); + }); + + it('should return an username suggestion', (done) => { + request.get(api('users.getUsernameSuggestion')) + .set(userCredentials) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body.result).to.be.equal(testUsername); + }) + .end(done); + }); + + }); });