def test_view_post_should_add_resource_author(self): data = {"url": "https://example.com"} request = self.factory.post(reverse("protector-resource_create"), data=data) request.user = self.user ResourceCreateView.as_view()(request) self.assertEqual(Resource.objects.first().author, self.user)
def test_view_post_should_create_resource_object(self): data = {"url": "https://example.com"} request = self.factory.post(reverse("protector-resource_create"), data=data) request.user = self.user ResourceCreateView.as_view()(request) self.assertTrue(Resource.objects.exists())
def test_view_post_should_add_resource_password(self): data = {"url": "https://example.com"} request = self.factory.post(reverse("protector-resource_create"), data=data) request.user = self.user ResourceCreateView.as_view()(request) resource = Resource.objects.first() self.assertIsNotNone(resource.password)
def test_view_post_should_add_resource_hashed_url(self): data = {"url": "https://example.com"} request = self.factory.post(reverse("protector-resource_create"), data=data) request.user = self.user ResourceCreateView.as_view()(request) resource = Resource.objects.first() self.assertTrue( resource.protected_url, reverse("protector-protected_resource", args=(resource.pk, )), )
def test_view_contains_form(self): request = self.factory.get(reverse("protector-resource_create")) request.user = self.user response = ResourceCreateView.as_view()(request) self.assertEqual(response.context_data["form"].__class__, ResourceForm)
def test_view_get_status_code(self): request = self.factory.get(reverse("protector-resource_create")) request.user = self.user response = ResourceCreateView.as_view()(request) self.assertEqual(response.status_code, 200)
def test_view_should_redirect_to_login_page_when_user_is_not_logged_in( self): request = self.factory.get(reverse("protector-resource_create")) request.user = AnonymousUser() response = ResourceCreateView.as_view()(request) self.assertEqual(response.status_code, 302)
from django.urls import path from protector.views import ( ResourceCreateView, ResourceDetailView, ResourceProtectedDetailView, DownloadFileView, ) urlpatterns = [ path("create/", ResourceCreateView.as_view(), name="protector-resource_create"), path( "detail/<pk>/", ResourceDetailView.as_view(), name="protector-resource_detail" ), path( "protected/<protected_url>", ResourceProtectedDetailView.as_view(), name="protector-protected_resource", ), path( "download/<protected_url>/", DownloadFileView.as_view(), name="protector-download", ), ]