Skip to content

Commit 7afc932

Browse files
authored
Redirect private task OAuth failures to dashboard (#1456)
* Redirect private task OAuth failures to dashboard * Fix private task OAuth invalid-code test
1 parent 41c745b commit 7afc932

2 files changed

Lines changed: 29 additions & 11 deletions

File tree

src/app/controllers/user/user.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ export const createPrivateTask = async (req: any, res: any) => {
126126
const { url, code, userId } = req.query
127127
const githubClientId = secrets.github.id
128128
const githubClientSecret = secrets.github.secret
129+
const redirectPrivateTaskError = (message?: string) => {
130+
const encodedError = encodeURIComponent(message || 'We could not import the issue.')
131+
return res.redirect(
132+
`${process.env.FRONTEND_HOST}/#/profile?createTaskError=true&message=${encodedError}`
133+
)
134+
}
129135
try {
130136
const response = await requestPromise({
131137
method: 'POST',
@@ -164,15 +170,12 @@ export const createPrivateTask = async (req: any, res: any) => {
164170
const isRateLimit =
165171
String(errorStatus) === '403' || /rate limit exceeded/i.test(errorMessage || '')
166172
const finalError = isRateLimit ? 'API limit reached, please try again later.' : errorMessage
167-
const encodedError = encodeURIComponent(finalError || 'We could not import the issue.')
168-
return res.redirect(
169-
`${process.env.FRONTEND_HOST}/#/profile?createTaskError=true&message=${encodedError}`
170-
)
173+
return redirectPrivateTaskError(finalError)
171174
}
172175
}
173-
return res.status(response.access_token ? 200 : 401).send(response)
176+
return redirectPrivateTaskError(response?.error_description || response?.error)
174177
} catch (e: any) {
175-
return res.status(401).send(e)
178+
return redirectPrivateTaskError(e?.message || e?.error?.message)
176179
}
177180
}
178181

test/api/task/taskCrud.test.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ const nockAuthLimitExceeded = () => {
6363
.reply(200, getSingleRepo.repo)
6464
}
6565

66+
const nockAuthInvalidCode = () => {
67+
nock('https://github.com')
68+
.persist()
69+
.post('/login/oauth/access_token/', { code: 'eb518274e906c68580f7' })
70+
.basicAuth({ user: secrets.github.id, pass: secrets.github.secret })
71+
.reply(200, {
72+
error: 'bad_verification_code',
73+
error_description: 'The code passed is incorrect or expired.'
74+
})
75+
}
76+
6677
describe('Task CRUD', () => {
6778
const createTask = async (authorizationHeader: string, params?: any) => {
6879
const res = await agent
@@ -236,16 +247,20 @@ describe('Task CRUD', () => {
236247
expect(mailSpySuccess).to.have.been.called()
237248
})
238249

239-
xit('should receive code on the platform from github auth to the redirected url for private tasks but invalid code', async () => {
250+
it('should redirect to profile with an error when private task auth returns an invalid code', async () => {
251+
nockAuthInvalidCode()
240252
const res = await agent
241253
.get(
242254
'/callback/github/private/?userId=1&url=https%3A%2F%2Fgithub.com%2Falexanmtz%2Ffestifica%2Fissues%2F1&code=eb518274e906c68580f7'
243255
)
244-
.expect(401)
256+
.expect(302)
245257

246-
expect(res.statusCode).to.equal(401)
247-
expect(res.body.error).to.equal('bad_verification_code')
248-
expect(res.body).to.exist
258+
expect(res.statusCode).to.equal(302)
259+
expect(res.headers.location).to.equal(
260+
`${process.env.FRONTEND_HOST}/#/profile?createTaskError=true&message=${encodeURIComponent(
261+
'The code passed is incorrect or expired.'
262+
)}`
263+
)
249264
})
250265

251266
it('should receive code on the platform from github auth to the redirected url for private tasks with a valid code', async () => {

0 commit comments

Comments
 (0)