From 18b5e40c17057dd089ffb530e4c3ebb4d41a8b9a Mon Sep 17 00:00:00 2001 From: Tyler Iams Date: Wed, 3 Jul 2019 16:36:44 -0700 Subject: [PATCH 01/16] not sure how to add 'salesforce' to test_staff.StaffViewsTest.databases --- missionbit/settings.py | 10 ++++++++++ tests/test_staff.py | 40 ++++++++++++++++++---------------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/missionbit/settings.py b/missionbit/settings.py index b2b2d37..9e27994 100644 --- a/missionbit/settings.py +++ b/missionbit/settings.py @@ -11,6 +11,7 @@ """ import dj_database_url from dotenv import load_dotenv +import sys load_dotenv() import os @@ -104,6 +105,15 @@ DATABASE_ROUTERS = ["salesforce.router.ModelRouter"] +if 'test' in sys.argv: + DATABASES['salesforce'] = { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': 'test_sfdb', + 'MIRROR': 'salesforce', + } + + + # Password validation # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators diff --git a/tests/test_staff.py b/tests/test_staff.py index a7c24d1..2584696 100644 --- a/tests/test_staff.py +++ b/tests/test_staff.py @@ -1,13 +1,13 @@ from django.test import TestCase, RequestFactory from staff.views import * -from django.contrib.auth.models import User +from django.contrib.auth.models import User as DjangoUser from django.urls import reverse from rest_framework import status +from missionbit.settings import * -""" class BaseTestCase(TestCase): def create_authenticated_staff_user(self): - user = User.objects.create_user( + user = DjangoUser.objects.create_user( username="testuser", email="test@email.com", first_name="testfirst", @@ -20,7 +20,7 @@ def create_authenticated_staff_user(self): return user def create_authenticated_nonstaff_user(self): - user = User.objects.create_user( + user = DjangoUser.objects.create_user( username="otherstafftestuser", email="othertest@email.com", first_name="othertestfirst", @@ -35,32 +35,28 @@ def create_authenticated_nonstaff_user(self): class StaffViewsTest(BaseTestCase): def test_staff(self): - request = RequestFactory().get(reverse("home-home")) - request.user = self.create_authenticated_nonstaff_user() - response = staff(request) - self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) - request.user = self.create_authenticated_staff_user() - response = staff(request) + self.client.force_login(self.create_authenticated_staff_user()) + response = self.client.get(reverse("staff")) self.assertEqual(response.status_code, status.HTTP_200_OK) + self.client.force_login(self.create_authenticated_nonstaff_user()) + self.assertRaises(PermissionError) def test_user_management(self): - request = RequestFactory().get(reverse("home-home")) - request.user = self.create_authenticated_nonstaff_user() - response = user_management(request) - self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) - request.user = self.create_authenticated_staff_user() - response = user_management(request) + self.client.force_login(self.create_authenticated_staff_user()) + response = self.client.get(reverse("user_management")) self.assertEqual(response.status_code, status.HTTP_200_OK) + self.client.force_login(self.create_authenticated_nonstaff_user()) + self.assertRaises(PermissionError) def test_create_staff_user(self): - request = RequestFactory().get(reverse("home-home")) - request.user = self.create_authenticated_nonstaff_user() - response = create_staff_user(request) - self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) - request.user = self.create_authenticated_staff_user() - response = create_staff_user(request) + self.client.force_login(self.create_authenticated_staff_user()) + response = self.client.get(reverse("create_staff_user")) self.assertEqual(response.status_code, status.HTTP_200_OK) + self.client.force_login(self.create_authenticated_nonstaff_user()) + self.assertRaises(PermissionError) + + """ def test_create_teacher_user(self): request = RequestFactory().get(reverse("home-home")) request.user = self.create_authenticated_nonstaff_user() From 5fc35e7dc53da6dc60f7c8115d04f78f81bd71ee Mon Sep 17 00:00:00 2001 From: Tyler Iams Date: Thu, 4 Jul 2019 08:51:52 -0700 Subject: [PATCH 02/16] adding assertTemplateUsed --- home/templates/home/home.html | 2 +- missionbit/settings.py | 7 ------- tests/test_staff.py | 5 +++++ 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/home/templates/home/home.html b/home/templates/home/home.html index b3cc468..2973286 100644 --- a/home/templates/home/home.html +++ b/home/templates/home/home.html @@ -10,7 +10,7 @@ -