Skip to content

Commit 098b373

Browse files
Akshat453Akshat SinghRishabhJain2018gautamjajoo
authored
Frontend: Display proper error message while updating profile (#4483)
* Changed Error Message * Changed Error Message * Changed Error Message * Changed Error Message * Changed Error Message * Changed Error Message * Changed Error Message * Test Added --------- Co-authored-by: Akshat Singh <[email protected]> Co-authored-by: Rishabh Jain <[email protected]> Co-authored-by: Gautam Jajoo <[email protected]>
1 parent d2df210 commit 098b373

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

frontend/src/js/controllers/profileCtrl.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,11 @@
177177
};
178178

179179
vm.isURLValid = function(url) {
180-
if (url === undefined || url === null) {
181-
return true;
180+
if (!url) {
181+
return true; // Allow empty URLs
182182
}
183-
return (url.length <= 200);
183+
var urlPattern = /^(https?:\/\/)?([\w-]+\.)+[\w-]+(\/[\w-._~:/?#[\]@!$&'()*+,;=]*)?$/;
184+
return url.length <= 200 && urlPattern.test(url);
184185
};
185186

186187
vm.editprofileDialog = function(ev) {
@@ -382,4 +383,4 @@
382383
};
383384
}
384385

385-
})();
386+
})();

frontend/tests/controllers-test/profileCtrl.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,41 @@ describe('Unit tests for profile controller', function () {
313313
expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse.error);
314314
});
315315
});
316+
317+
describe('Unit tests for isURLValid function', function () {
318+
it('should allow empty URLs', function () {
319+
var result = vm.isURLValid('');
320+
expect(result).toBeTruthy();
321+
322+
result = vm.isURLValid(null);
323+
expect(result).toBeTruthy();
324+
325+
result = vm.isURLValid(undefined);
326+
expect(result).toBeTruthy();
327+
});
328+
329+
it('should return true for valid URLs within 200 characters', function () {
330+
var result = vm.isURLValid('https://github.com');
331+
expect(result).toBeTruthy();
332+
333+
result = vm.isURLValid('http://example.com/path?query=param');
334+
expect(result).toBeTruthy();
335+
336+
result = vm.isURLValid('https://sub.domain.example.com/long-path/to/resource?query=1&more=2');
337+
expect(result).toBeTruthy();
338+
});
339+
340+
it('should return false for invalid URLs or overly long ones', function () {
341+
var result = vm.isURLValid('invalid-url');
342+
expect(result).toBeFalsy();
343+
344+
result = vm.isURLValid('htp://missing-schema.com');
345+
expect(result).toBeFalsy();
346+
347+
var longUrl = 'http://example.com/' + 'a'.repeat(201);
348+
result = vm.isURLValid(longUrl);
349+
expect(result).toBeFalsy();
350+
});
351+
});
352+
316353
});

0 commit comments

Comments
 (0)