diff --git a/__tests__/gitlab.unit.test.js b/__tests__/gitlab.unit.test.js index 5159e0b2e705e9f74e84fda093f70deb0a7177a8..af8cf6de1c3eecdd199a10a1268e4c1cf56e50a7 100644 --- a/__tests__/gitlab.unit.test.js +++ b/__tests__/gitlab.unit.test.js @@ -4,7 +4,7 @@ const gitlab = require('../routes/gitlab') describe('GitLab API', () => { test('returns an existing gitlab user by an email address', async () => { - let user = await gitlab.getUserByEmail('putavaliduseremailaddress@here.com') + let user = await gitlab.getUserByEmail('litehon958@whipjoy.com') expect(user).not.toBeNull() }) test('returns an undefined user', async () => { @@ -13,7 +13,7 @@ describe('GitLab API', () => { }) test('returns users project', async () => { - let userProjects = await gitlab.getUserProjects('put a valid user id in integer here') + let userProjects = await gitlab.getUserProjects(136) expect(userProjects).toBeDefined() }) test('returns undefined projects, due to non-existing gitlab user ID', async () => { diff --git a/__tests__/method.unit.test.js b/__tests__/method.unit.test.js index 9ad00d8e01af125cb81c14c620695aa8c24ee42e..8dde44c8b285a127ac8fc8d5709db76982c98c4c 100644 --- a/__tests__/method.unit.test.js +++ b/__tests__/method.unit.test.js @@ -3,7 +3,7 @@ const methods = require('../routes/methods') describe("DB methohds test", () => { it('returns a user from DB by email', done => { - methods.getUserByEmail('rosanny.sihombing@hft-stuttgart.de', function(resp, err){ + methods.getUserByEmail('litehon958@whipjoy.com', function(resp, err){ try { expect(resp).not.toBeNull() expect(err).toBeNull() diff --git a/routes/gitlab.js b/routes/gitlab.js index 8766d9177b802168e99284a9d05bff77f65f04ff..9e82a8a0f064a862656c1d7551916a9088ff8728 100644 --- a/routes/gitlab.js +++ b/routes/gitlab.js @@ -14,10 +14,7 @@ var gitlab = { headers: { 'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects} }) - .then(res => userData = { - id: res.data[0].id, - username: res.data[0].username - }) + .then(res => res.data[0]) .catch(err => console.error(err)) }, createNewPages: async function(newPagesData, newLogoFile, template) { @@ -72,8 +69,10 @@ var gitlab = { getUserProjects: async function(gitlabUserId) { return axios({ method: 'get', - url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/users/'+gitlabUserId+'/projects?private_token='+ - config.gitlab.token_readWriteProjects+'&owned=true&simple=true&visibility=public' + url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/users/'+gitlabUserId+'/projects?owned=true&visibility=public', + headers: { + 'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects + } }) .then(res => res.data) .catch(err => console.error(err)) @@ -81,11 +80,13 @@ var gitlab = { getProjectById: async function(projectId) { return axios({ method: 'get', - url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/'+projectId+'?private_token='+ - config.gitlab.token_readWriteProjects + url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/'+projectId, + headers: { + 'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects + } }) .then(res => res.data) - .catch(err => console.error(err)) + .catch(err => console.error(err.response.status)) }, getProjectPipelineLatestStatus: async function(projectId) { return axios({ diff --git a/routes/methods.js b/routes/methods.js index d3d3f69c029b562775f40c36cf4e710377e008a0..625734291559e3fcb5cb26fd3f61ab74452ae11b 100644 --- a/routes/methods.js +++ b/routes/methods.js @@ -228,7 +228,11 @@ var methods = { getGitlabId: async function(userId) { try { let rows = await dbconn.user.promise().query('SELECT gu.gitlab_userId FROM user_gitlab gu, user u WHERE u.id = "' +userId+'" and gu.user_id = u.id') - return rows[0][0].gitlab_userId + if (rows[0][0]) { + return rows[0][0].gitlab_userId + } else { + return null + } } catch(err) { console.error(err) diff --git a/routes/routes-account.js b/routes/routes-account.js index 24502b7ad61612d3424429ceeb305f66778da7ae..0244ea77fd7dd1312c32d1cb19851f763267f33d 100644 --- a/routes/routes-account.js +++ b/routes/routes-account.js @@ -118,7 +118,9 @@ module.exports = function (app, config, passport, i18n) { ) let userGitlabId = await methods.getGitlabId(loggedInUser.id) - loggedInUser.setGitlabUserId(userGitlabId) + if (userGitlabId) { + loggedInUser.setGitlabUserId(userGitlabId) + } return loggedInUser } @@ -192,8 +194,10 @@ module.exports = function (app, config, passport, i18n) { if(loggedInUser.getGitlabUserId()) { // for users who have activated their gitlab account let userProjects = await gitlab.getUserProjects(loggedInUser.getGitlabUserId()) - if (!userProjects) - res.status(500).send("something went wrong :/") + if (!userProjects) { + console.error("something went wrong") + res.status(500).render(lang+'/500', { error: "something went wrong" }) + } for (project in userProjects) { if (userProjects[project].tag_list.includes('website')) { @@ -217,19 +221,25 @@ module.exports = function (app, config, passport, i18n) { }) } else { // for users who have not activated their gitlab account yet let gitlabUser = await gitlab.getUserByEmail(loggedInUser.getEmail()) - // RS: if error, then what? - let gitlabActivationData = { - user_id: loggedInUser.getId(), - gitlab_userId: gitlabUser.id} - // RS: update to await - methods.addGitlabUser(gitlabActivationData, function(err){ - if(err) { - res.status(500).render(lang+'/500', { error: err }) - } else { - loggedInUser.setGitlabUserId(gitlabActivationData.gitlab_userId) - res.redirect('/account/services') - } - }) + if (!gitlabUser) { + res.render(lang+'/account/services', { + user: loggedInUser, + gitlabRepos: null, + gitlabPages: null + }) + } else { + let gitlabActivationData = { + user_id: loggedInUser.getId(), + gitlab_userId: gitlabUser.id} + // RS: update to await? + methods.addGitlabUser(gitlabActivationData, function(err){ + if(err) { + res.status(500).render(lang+'/500', { error: err }) + } else { + res.redirect('/account/services') + } + }) + } } } } @@ -471,7 +481,7 @@ module.exports = function (app, config, passport, i18n) { let loggedInUser = await getLoggedInUserData(req.user.email) let gitlabUser = await gitlab.getUserByEmail(loggedInUser.getEmail()) if (!gitlabUser) { // no user found - res.redirect('/account/service') + res.redirect('/account/services') } else { res.render(lang+'/account/newInformation', { user: loggedInUser,