def test_datetime_and_field_should_work(self): original_timestamp = timezone.now() - timedelta(hours=1) transaction = TransactionFactory(timestamp=original_timestamp) self.selenium.get( self.live_server_url + f"/admin/market/transaction/{transaction.id}/change/") self.assertIn(CONFIRM_CHANGE, self.selenium.page_source) # Set date via text input date_input = self.selenium.find_element(By.ID, "id_date") date_input.clear() date_input.send_keys("2021-01-01") self.assertEqual(date_input.get_attribute("value"), "2021-01-01") # Set timestamp via text input timestamp_date = self.selenium.find_element(By.ID, "id_timestamp_0") timestamp_date.clear() timestamp_date.send_keys(str(timezone.now().date())) timestamp_time = self.selenium.find_element(By.ID, "id_timestamp_1") timestamp_time.clear() timestamp_time.send_keys(str(timezone.now().time())) # Click save and continue self.selenium.find_element(By.NAME, "_continue").click() # Click Yes I'm Sure on confirmation page self.assertIn("Confirm", self.selenium.page_source) self.selenium.find_element(By.NAME, "_continue").click() transaction.refresh_from_db() self.assertEqual(str(transaction.date), "2021-01-01") self.assertTrue(transaction.timestamp > original_timestamp)
def test_confirm_change_of_datetime_and_date_field(self): transaction = TransactionFactory() original_date = transaction.date original_timestamp = transaction.timestamp data = { "id": transaction.id, "date": "2021-01-01", "timestamp_0": "2021-01-01", "timestamp_1": "12:30:00", "currency": "USD", "shop": transaction.shop.id, "total": 0, "_confirm_change": True, "csrfmiddlewaretoken": "fake token", "_continue": True, } response = self.client.post( f"/admin/market/transaction/{transaction.id}/change/", data ) # Ensure not redirected (confirmation page does not redirect) self.assertEqual(response.status_code, 200) expected_templates = [ "admin/market/transaction/change_confirmation.html", "admin/market/change_confirmation.html", "admin/change_confirmation.html", ] self.assertEqual(response.template_name, expected_templates) self._assertSubmitHtml( rendered_content=response.rendered_content, save_action="_continue" ) # Hasn't changed item yet transaction.refresh_from_db() self.assertEqual(transaction.date, original_date) self.assertEqual(transaction.timestamp, original_timestamp) # Selecting to "Yes, I'm sure" on the confirmation page # Would post to the same endpoint del data["_confirm_change"] response = self.client.post( f"/admin/market/transaction/{transaction.id}/change/", data ) # will show the change page for this transaction self.assertEqual(response.status_code, 302) self.assertEqual( response.url, f"/admin/market/transaction/{transaction.id}/change/" ) # Should not be the confirmation page, we already confirmed change self.assertNotEqual(response.templates, expected_templates) self.assertEqual(Transaction.objects.count(), 1) transaction.refresh_from_db() self.assertEqual(str(transaction.date), "2021-01-01") self.assertEqual(str(transaction.timestamp.date()), "2021-01-01") self.assertEqual(str(transaction.timestamp.time()), "12:30:00")