Skip to content

Commit

Permalink
[change] Use relative URLs in notification widget #249
Browse files Browse the repository at this point in the history
JS code to implement this change + one selenium test.

Closes: #249

---------

Co-authored-by: Federico Capoano <f.capoano@openwisp.io>
  • Loading branch information
Dhanus3133 and nemesifier committed Apr 22, 2024
1 parent 22e6b1e commit af4f504
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ jobs:
SAMPLE_APP=1 ./runtests.py
coverage run runtests.py --parallel
coverage combine
env:
SELENIUM_HEADLESS: 1

- name: Upload Coverage
run: coveralls --service=github
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,9 @@ function notificationWidget($) {
}

function notificationListItem(elem) {
let klass,
datetime = dateTimeStampToDateTimeLocaleString(new Date(elem.timestamp));
let klass;
const datetime = dateTimeStampToDateTimeLocaleString(new Date(elem.timestamp)),
target_url = new URL(elem.target_url);

if (!notificationReadStatus.has(elem.id)) {
if (elem.unread) {
Expand All @@ -207,8 +208,23 @@ function notificationWidget($) {
}
klass = notificationReadStatus.get(elem.id);

// Used to convert absolute URLs in notification messages to relative paths
function convertMessageWithRelativeURL(htmlString) {
const parser = new DOMParser(),
doc = parser.parseFromString(htmlString, 'text/html'),
links = doc.querySelectorAll('a');
links.forEach((link) => {
let url = link.getAttribute('href');
if (url) {
url = new URL(url);
link.setAttribute('href', url.pathname);
}
});
return doc.body.innerHTML;
}

return `<div class="ow-notification-elem ${klass}" id=ow-${elem.id}
data-location="${elem.target_url}" role="link" tabindex="0">
data-location="${target_url.pathname}" role="link" tabindex="0">
<div class="ow-notification-inner">
<div class="ow-notification-meta">
<div class="ow-notification-level-wrapper">
Expand All @@ -217,7 +233,7 @@ function notificationWidget($) {
</div>
<div class="ow-notification-date">${datetime}</div>
</div>
${elem.message}
${convertMessageWithRelativeURL(elem.message)}
</div>
</div>`;
}
Expand Down
56 changes: 56 additions & 0 deletions openwisp_notifications/tests/test_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

from openwisp_notifications.swapper import load_model
from openwisp_notifications.utils import _get_object_link
from openwisp_users.tests.utils import TestOrganizationMixin
from openwisp_utils.test_selenium_mixins import SeleniumTestMixin

Notification = load_model('Notification')


class TestWidget(
SeleniumTestMixin,
TestOrganizationMixin,
StaticLiveServerTestCase,
):
serve_static = True

def setUp(self):
self.admin = self._create_admin(
username=self.admin_username, password=self.admin_password
)

def test_notification_relative_link(self):
self.login()
operator = super()._create_operator()
data = dict(
email_subject='Test Email subject',
url='http://localhost:8000/admin/',
)
notification = Notification.objects.create(
actor=self.admin,
recipient=self.admin,
description='Test Notification Description',
verb='Test Notification',
action_object=operator,
target=operator,
data=data,
)
self.web_driver.implicitly_wait(10)
WebDriverWait(self.web_driver, 10).until(
EC.visibility_of_element_located((By.ID, 'openwisp_notifications'))
)
self.web_driver.find_element(By.ID, 'openwisp_notifications').click()
WebDriverWait(self.web_driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'ow-notification-elem'))
)
notification_elem = self.web_driver.find_element(
By.CLASS_NAME, 'ow-notification-elem'
)
data_location_value = notification_elem.get_attribute('data-location')
self.assertEqual(
data_location_value, _get_object_link(notification, 'target', False)
)
2 changes: 1 addition & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
openwisp-utils[qa] @ https://github.com/openwisp/openwisp-utils/tarball/master
openwisp-utils[qa,selenium] @ https://github.com/openwisp/openwisp-utils/tarball/master
django-cors-headers~=4.0.0
django-redis~=5.2.0
channels_redis~=4.1.0
Expand Down

0 comments on commit af4f504

Please sign in to comment.