fix(api): catch invalid ms-username url (#60402)

Co-authored-by: Tom <20648924+moT01@users.noreply.github.com>
This commit is contained in:
Shaun Hamilton
2025-05-19 12:53:24 +02:00
committed by GitHub
parent ac2efd4877
commit 8558d0b1f1
3 changed files with 82 additions and 63 deletions
+43 -20
View File
@@ -973,21 +973,15 @@ Thanks and regards,
});
it('handles invalid transcript urls', async () => {
mockedFetch.mockImplementationOnce(() =>
Promise.resolve({
ok: false
})
);
const response = await superPost('/user/ms-username').send({
msTranscriptUrl: 'https://www.example.com'
});
expect(response.body).toStrictEqual({
type: 'error',
message: 'flash.ms.transcript.link-err-2'
message: 'flash.ms.transcript.link-err-1'
});
expect(response.statusCode).toBe(404);
expect(response.statusCode).toBe(400);
});
it('handles the case that MS does not return a username', async () => {
@@ -999,7 +993,8 @@ Thanks and regards,
);
const response = await superPost('/user/ms-username').send({
msTranscriptUrl: 'https://www.example.com'
msTranscriptUrl:
'https://learn.microsoft.com/en-us/users/not/transcript/8u6ert43q1p'
});
expect(response.body).toStrictEqual({
@@ -1029,7 +1024,8 @@ Thanks and regards,
});
const response = await superPost('/user/ms-username').send({
msTranscriptUrl: 'https://www.example.com'
msTranscriptUrl:
'https://learn.microsoft.com/en-us/users/mot01/transcript/8wert4'
});
expect(response.body).toStrictEqual({
@@ -1052,7 +1048,8 @@ Thanks and regards,
})
);
const response = await superPost('/user/ms-username').send({
msTranscriptUrl: 'https://www.example.com'
msTranscriptUrl:
'https://learn.microsoft.com/en-us/users/mot01/transcript/8ert43q'
});
expect(response.body).toStrictEqual({
@@ -1074,7 +1071,8 @@ Thanks and regards,
);
await superPost('/user/ms-username').send({
msTranscriptUrl: 'https://www.example.com'
msTranscriptUrl:
'https://learn.microsoft.com/en-us/users/mot01/transcript/12345'
});
const linkedAccount =
@@ -1122,10 +1120,12 @@ Thanks and regards,
});
await superPost('/user/ms-username').send({
msTranscriptUrl: 'https://www.example.com'
msTranscriptUrl:
'https://learn.microsoft.com/en-us/users/mot01/transcript/8u6awert43q1plo'
});
await superPost('/user/ms-username').send({
msTranscriptUrl: 'https://www.example.com'
msTranscriptUrl:
'https://learn.microsoft.com/en-us/users/mot01/transcript/8u6awert43q1plo'
});
const linkedAccounts =
@@ -1311,18 +1311,41 @@ describe('Microsoft helpers', () => {
const urlWithQueryParamsAndSlash = `${urlWithSlash}?foo=bar`;
it('should extract the transcript id from the url', () => {
expect(getMsTranscriptApiUrl(urlWithoutSlash)).toBe(expectedUrl);
expect(getMsTranscriptApiUrl(urlWithoutSlash)).toEqual({
error: null,
data: expectedUrl
});
});
it('should handle trailing slashes', () => {
expect(getMsTranscriptApiUrl(urlWithSlash)).toBe(expectedUrl);
expect(getMsTranscriptApiUrl(urlWithSlash)).toEqual({
error: null,
data: expectedUrl
});
});
it('should ignore query params', () => {
expect(getMsTranscriptApiUrl(urlWithQueryParams)).toBe(expectedUrl);
expect(getMsTranscriptApiUrl(urlWithQueryParamsAndSlash)).toBe(
expectedUrl
);
expect(getMsTranscriptApiUrl(urlWithQueryParams)).toEqual({
error: null,
data: expectedUrl
});
expect(getMsTranscriptApiUrl(urlWithQueryParamsAndSlash)).toEqual({
error: null,
data: expectedUrl
});
});
it('should return an error for invalid URLs', () => {
const validBadUrl = 'https://www.example.com/invalid-url';
expect(getMsTranscriptApiUrl(validBadUrl)).toEqual({
error: expect.any(String),
data: null
});
const invalidUrl = ' ';
expect(getMsTranscriptApiUrl(invalidUrl)).toEqual({
error: expect.any(String),
data: null
});
});
});
});