|
| 1 | +""" |
| 2 | +Tests for network-aware navbar links. |
| 3 | +
|
| 4 | +On a network host (up.profile.hcommons.org) or network path prefix, |
| 5 | +the community links (news feed/activity, groups, sites) point at the |
| 6 | +network's own Commons domain (up.hcommons.org). Works, Help & Support, |
| 7 | +KC Organizations, About and the Team Blog never follow the network — |
| 8 | +including KC Organizations, which lives on the default domain and must |
| 9 | +be excluded by key, not by hostname. |
| 10 | +""" |
| 11 | + |
| 12 | +import json |
| 13 | +import time |
| 14 | + |
| 15 | +from django.test import RequestFactory |
| 16 | +from django.test import TestCase |
| 17 | +from django.test import override_settings |
| 18 | + |
| 19 | +from knowledge_commons_profiles.newprofile.context_processors import nav_links |
| 20 | +from knowledge_commons_profiles.newprofile.models import Profile |
| 21 | + |
| 22 | +NAV_SETTINGS = { |
| 23 | + "NAV_NEWS_FEED_URL": "https://hcommons.org/activity/", |
| 24 | + "NAV_GROUPS_URL": "https://hcommons.org/groups/", |
| 25 | + "NAV_SITES_URL": "https://hcommons.org/sites/", |
| 26 | + "NAV_WORKS_URL": "https://works.hcommons.org/", |
| 27 | + "NAV_SUPPORT_URL": "https://support.hcommons.org/", |
| 28 | + "NAV_ORGANIZATIONS_URL": "https://hcommons.org/societies/", |
| 29 | + "NAV_ABOUT_URL": "https://sustaining.hcommons.org/", |
| 30 | + "NAV_BLOG_URL": "https://team.hcommons.org/", |
| 31 | + "NAV_DEFAULT_DOMAIN": "hcommons.org", |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +@override_settings(**NAV_SETTINGS) |
| 36 | +class NetworkAwareNavLinksTests(TestCase): |
| 37 | + def _nav_for_network(self, slug, session=None): |
| 38 | + request = RequestFactory().get("/members/") |
| 39 | + request.network_slug = slug |
| 40 | + request.network = slug |
| 41 | + request.session = session if session is not None else {} |
| 42 | + return nav_links(request) |
| 43 | + |
| 44 | + def test_community_links_follow_the_network(self): |
| 45 | + urls = self._nav_for_network("up") |
| 46 | + self.assertEqual( |
| 47 | + urls["NAV_NEWS_FEED_URL"], "https://up.hcommons.org/activity/" |
| 48 | + ) |
| 49 | + self.assertEqual( |
| 50 | + urls["NAV_GROUPS_URL"], "https://up.hcommons.org/groups/" |
| 51 | + ) |
| 52 | + self.assertEqual( |
| 53 | + urls["NAV_SITES_URL"], "https://up.hcommons.org/sites/" |
| 54 | + ) |
| 55 | + |
| 56 | + def test_fixed_links_never_follow_the_network(self): |
| 57 | + urls = self._nav_for_network("stemedplus") |
| 58 | + self.assertEqual( |
| 59 | + urls["NAV_WORKS_URL"], "https://works.hcommons.org/" |
| 60 | + ) |
| 61 | + self.assertEqual( |
| 62 | + urls["NAV_SUPPORT_URL"], "https://support.hcommons.org/" |
| 63 | + ) |
| 64 | + # KC Organizations lives ON the default domain and must still |
| 65 | + # not follow the network |
| 66 | + self.assertEqual( |
| 67 | + urls["NAV_ORGANIZATIONS_URL"], "https://hcommons.org/societies/" |
| 68 | + ) |
| 69 | + self.assertEqual( |
| 70 | + urls["NAV_ABOUT_URL"], "https://sustaining.hcommons.org/" |
| 71 | + ) |
| 72 | + self.assertEqual(urls["NAV_BLOG_URL"], "https://team.hcommons.org/") |
| 73 | + |
| 74 | + def test_no_network_leaves_links_unchanged(self): |
| 75 | + urls = self._nav_for_network(None) |
| 76 | + self.assertEqual( |
| 77 | + urls["NAV_NEWS_FEED_URL"], "https://hcommons.org/activity/" |
| 78 | + ) |
| 79 | + self.assertEqual( |
| 80 | + urls["NAV_GROUPS_URL"], "https://hcommons.org/groups/" |
| 81 | + ) |
| 82 | + |
| 83 | + def test_network_beats_referer_session_domain(self): |
| 84 | + session = { |
| 85 | + "nav_network_domain": "msu.edu", |
| 86 | + "nav_network_domain_ts": time.time(), |
| 87 | + } |
| 88 | + urls = self._nav_for_network("up", session=session) |
| 89 | + self.assertEqual( |
| 90 | + urls["NAV_NEWS_FEED_URL"], "https://up.hcommons.org/activity/" |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +@override_settings( |
| 95 | + **NAV_SETTINGS, |
| 96 | + ALLOWED_HOSTS=["*"], |
| 97 | + KNOWN_SOCIETY_MAPPINGS={"stemedplus": "STEMED+"}, |
| 98 | + NETWORK_DISPLAY_NAMES={ |
| 99 | + "up": "Association of University Presses", |
| 100 | + "stemed+": "STEM Ed+", |
| 101 | + }, |
| 102 | + NETWORK_SUBDOMAIN_BASE_DOMAINS=["profile.hcommons-dev.org"], |
| 103 | + NETWORK_SUBDOMAIN_IGNORED=["www"], |
| 104 | +) |
| 105 | +class NetworkNavRenderingTests(TestCase): |
| 106 | + def setUp(self): |
| 107 | + Profile.objects.create( |
| 108 | + username="alice", |
| 109 | + name="Alice", |
| 110 | + is_member_of=json.dumps({"UP": True}), |
| 111 | + ) |
| 112 | + |
| 113 | + def test_subdomain_page_renders_network_nav_links(self): |
| 114 | + response = self.client.get( |
| 115 | + "/members/", headers={"host": "up.profile.hcommons-dev.org"} |
| 116 | + ) |
| 117 | + self.assertEqual(response.status_code, 200) |
| 118 | + self.assertContains(response, "https://up.hcommons.org/activity/") |
| 119 | + self.assertContains(response, "https://up.hcommons.org/groups/") |
| 120 | + self.assertContains(response, "https://works.hcommons.org/") |
| 121 | + self.assertContains(response, "https://hcommons.org/societies/") |
0 commit comments