diff --git a/frontend/src/js/controllers/profileCtrl.js b/frontend/src/js/controllers/profileCtrl.js index 5545f14cda..24dd81aaf0 100644 --- a/frontend/src/js/controllers/profileCtrl.js +++ b/frontend/src/js/controllers/profileCtrl.js @@ -177,10 +177,11 @@ }; vm.isURLValid = function(url) { - if (url === undefined || url === null) { - return true; + if (!url) { + return true; // Allow empty URLs } - return (url.length <= 200); + var urlPattern = /^(https?:\/\/)?([\w-]+\.)+[\w-]+(\/[\w-._~:/?#[\]@!$&'()*+,;=]*)?$/; + return url.length <= 200 && urlPattern.test(url); }; vm.editprofileDialog = function(ev) { @@ -227,12 +228,14 @@ vm.user.google_scholar_url = vm.user.google_scholar_url === null ? "" : vm.user.google_scholar_url; vm.user.linkedin_url = vm.user.linkedin_url === null ? "" : vm.user.linkedin_url; - if (!vm.isURLValid(vm.user[editid])) { - vm.isFormError = true; - $rootScope.notify("error", "URL length should not be greater than 200 or is in invalid format!"); - return; + if (editid === "github_url" || editid === "google_scholar_url" || editid === "linkedin_url") { + if (!vm.isURLValid(vm.user[editid])) { + vm.isFormError = true; + $rootScope.notify("error", "URL length should not be greater than 200 or is in invalid format!"); + return; + } } - + var parameters = {}; parameters.url = 'auth/user/'; parameters.method = 'PUT'; diff --git a/frontend/tests/controllers-test/profileCtrl.test.js b/frontend/tests/controllers-test/profileCtrl.test.js index d6f4ae3d9c..ba6e0dac57 100644 --- a/frontend/tests/controllers-test/profileCtrl.test.js +++ b/frontend/tests/controllers-test/profileCtrl.test.js @@ -313,4 +313,41 @@ describe('Unit tests for profile controller', function () { expect($rootScope.notify).toHaveBeenCalledWith("error", errorResponse.error); }); }); + + describe('Unit tests for isURLValid function', function () { + it('should allow empty URLs', function () { + var result = vm.isURLValid(''); + expect(result).toBeTruthy(); + + result = vm.isURLValid(null); + expect(result).toBeTruthy(); + + result = vm.isURLValid(undefined); + expect(result).toBeTruthy(); + }); + + it('should return true for valid URLs within 200 characters', function () { + var result = vm.isURLValid('https://github.com'); + expect(result).toBeTruthy(); + + result = vm.isURLValid('http://example.com/path?query=param'); + expect(result).toBeTruthy(); + + result = vm.isURLValid('https://sub.domain.example.com/long-path/to/resource?query=1&more=2'); + expect(result).toBeTruthy(); + }); + + it('should return false for invalid URLs or overly long ones', function () { + var result = vm.isURLValid('invalid-url'); + expect(result).toBeFalsy(); + + result = vm.isURLValid('htp://missing-schema.com'); + expect(result).toBeFalsy(); + + var longUrl = 'http://example.com/' + 'a'.repeat(201); + result = vm.isURLValid(longUrl); + expect(result).toBeFalsy(); + }); + }); + });