Skip to content

Commit

Permalink
Merge pull request #2144 from ResearchHub/fix-rh-views
Browse files Browse the repository at this point in the history
fix: Add view parameters
  • Loading branch information
gzurowski authored Feb 28, 2025
2 parents b1ceb29 + 8fdf750 commit 4242327
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
54 changes: 54 additions & 0 deletions src/researchhub/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os
from unittest.mock import mock_open, patch

from django.conf import settings
from django.test import Client, TestCase
from django.urls import reverse


class ViewsTestCase(TestCase):
def setUp(self):
self.client = Client()

def test_index_view(self):
# Act
response = self.client.get(reverse("index"))

# Assert
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.content.decode("utf-8"),
"Authenticate with a token in the Authorization header.",
)

@patch("builtins.open", new_callable=mock_open, read_data='{"permissions": "test"}')
def test_permissions_view(self, mock_file):
# Arrange
expected_path = os.path.join(
settings.BASE_DIR, "static", "researchhub", "user_permissions.json"
)

# Act
response = self.client.get(reverse("permissions"))

# Assert
mock_file.assert_called_once_with(expected_path, "r")
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/json")
self.assertEqual(response.content.decode("utf-8"), '{"permissions": "test"}')

@patch("researchhub.views.views.render_to_string")
def test_robots_txt_view(self, mock_render):
# Arrange
mock_render.return_value = "User-agent: *\nDisallow: /admin/"

# Act
response = self.client.get(reverse("robots_txt"))

# Assert
mock_render.assert_called_once_with("robots.txt")
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "text/plain")
self.assertEqual(
response.content.decode("utf-8"), "User-agent: *\nDisallow: /admin/"
)
6 changes: 3 additions & 3 deletions src/researchhub/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
from researchhub.settings import BASE_DIR


def index():
def index(_):
return HttpResponse("Authenticate with a token in the Authorization header.")


def permissions():
def permissions(_):
path = os.path.join(BASE_DIR, "static", "researchhub", "user_permissions.json")
with open(path, "r") as file:
data = file.read()
return HttpResponse(content=data, content_type="application/json")


def robots_txt():
def robots_txt(_):
content = render_to_string("robots.txt")
return HttpResponse(content, content_type="text/plain")

0 comments on commit 4242327

Please sign in to comment.