From 2a084f63a0d203af2ca00f4e7861943f411d11fb Mon Sep 17 00:00:00 2001 From: Rosanny Date: Wed, 21 Apr 2021 10:58:43 +0200 Subject: [PATCH 1/2] update test unit --- __tests__/gitlab.unit.test.js | 4 ++-- __tests__/method.unit.test.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/__tests__/gitlab.unit.test.js b/__tests__/gitlab.unit.test.js index 5159e0b2..af8cf6de 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 9ad00d8e..8dde44c8 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() -- GitLab From c39c509070221c4a4d76394ce8c1bc1a428e9140 Mon Sep 17 00:00:00 2001 From: Rosanny Date: Wed, 21 Apr 2021 11:16:33 +0200 Subject: [PATCH 2/2] handle new gitlab users --- routes/gitlab.js | 19 +++++++++-------- routes/methods.js | 6 +++++- routes/routes-account.js | 44 ++++++++++++++++++++++++---------------- 3 files changed, 42 insertions(+), 27 deletions(-) diff --git a/routes/gitlab.js b/routes/gitlab.js index 8766d917..9e82a8a0 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 d3d3f69c..62573429 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 24502b7a..0244ea77 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, -- GitLab