Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions frontend/src/js/controllers/profileCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Check warning on line 235 in frontend/src/js/controllers/profileCtrl.js

View check run for this annotation

Codecov / codecov/patch

frontend/src/js/controllers/profileCtrl.js#L232-L235

Added lines #L232 - L235 were not covered by tests
}
}

var parameters = {};
parameters.url = 'auth/user/';
parameters.method = 'PUT';
Expand Down
37 changes: 37 additions & 0 deletions frontend/tests/controllers-test/profileCtrl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

});