最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

python - Django LiveServerTestCase live server shuts down prematurely - Stack Overflow

matteradmin4PV0评论

I've been trying to run some functional tests (LiveServerTestCase) with Selenium for a Django app. For demonstration it's a note taking app. When I'm trying to simulate note creation, it requires authentication, so in the setup it involves going into admin dashboard, inserting credentials into the form fields, then submitting.

After that, I programmatically redirect to the page where I am supposed to write notes, but as soon as I click on submit, it induces a forbidden error. It says that the instance running the test is an AnonymousUser, ergo, the submission does not follow through. Do keep in mind that submission is in the end of the testcase, so I can't help but presuppose that the server ends prematurely. Is there a flag or configuration that I'm missing as to why this occurs?

An example code of the testcase:

class NoteTakingTest(StaticLiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        chrome_options = Options()        
        cls.selenium = WebDriver(options=chrome_options)
        cls.selenium.implicitly_wait(10)

    @classmethod
    def tearDownClass(cls):
        cls.selenium.quit()
        return super().tearDownClass()

    def setUp(self):
        super().setUp()        
        user = User.objects.create_superuser(email="[email protected]", password="admin")

        self.selenium.get(f"{self.live_server_url}/admin/login")        
        email = self.selenium.find_element(By.ID, "id_username")
        password = self.selenium.find_element(By.ID, "id_password")
        email.send_keys("[email protected]")
        password.send_keys("admin")
        form = self.selenium.find_element(By.ID, "login-form")
        form.submit()


    def access(self, url):        
        self.selenium.get(self.live_server_url + url)

    def test_pipeline_page_access(self):        
        pipeline_url = reverse("notes")
        self.access(pipeline_url)
        self.assertFalse(self.selenium.title.startswith("404"))
        
        wait = WebDriverWait(self.selenium, 10)

        submit_button = wait.until(EC.visibility_of_element_located((By.ID, f'note-submit-btn')))
        self.selenium.execute_script("arguments[0].scrollIntoView();", submit_button)
        # Clicking on the submit button triggers a POST request to /notes
        submit_button.click()

An example code of the testcase:

class NoteViewSet(viewsets.ModelViewSet):
    """
    A viewset that provides the standard actions
    """
    serializer_class = NoteSerializer
    permission_classes = (permissions.IsAuthenticated,)

    def perform_create(self, serializer):
        if not self.request.user.is_authenticated:
            raise PermissionDenied("You are not authorized to create a note.")        
        
        updated_object = serializer.save()
Post a comment

comment list (0)

  1. No comments so far