|
6 | 6 |
|
7 | 7 | import freezegun
|
8 | 8 | from django.core.management import call_command
|
9 |
| -from django.test import TestCase |
| 9 | +from django.test import TestCase, TransactionTestCase |
10 | 10 |
|
11 | 11 | from auditlog_tests.models import SimpleModel
|
12 | 12 |
|
@@ -110,3 +110,82 @@ def test_before_date(self):
|
110 | 110 | out, "Deleted 1 objects.", msg="Output shows deleted 1 object."
|
111 | 111 | )
|
112 | 112 | self.assertEqual(err, "", msg="No stderr")
|
| 113 | + |
| 114 | + |
| 115 | +class AuditlogFlushWithTruncateTest(TransactionTestCase): |
| 116 | + def setUp(self): |
| 117 | + input_patcher = mock.patch("builtins.input") |
| 118 | + self.mock_input = input_patcher.start() |
| 119 | + self.addCleanup(input_patcher.stop) |
| 120 | + |
| 121 | + def make_object(self): |
| 122 | + return SimpleModel.objects.create(text="I am a simple model.") |
| 123 | + |
| 124 | + def call_command(self, *args, **kwargs): |
| 125 | + outbuf = StringIO() |
| 126 | + errbuf = StringIO() |
| 127 | + call_command("auditlogflush", *args, stdout=outbuf, stderr=errbuf, **kwargs) |
| 128 | + return outbuf.getvalue().strip(), errbuf.getvalue().strip() |
| 129 | + |
| 130 | + def test_flush_with_both_truncate_and_before_date_options(self): |
| 131 | + obj = self.make_object() |
| 132 | + self.assertEqual(obj.history.count(), 1, msg="There is one log entry.") |
| 133 | + out, err = self.call_command("--truncate", "--before-date=2000-01-01") |
| 134 | + |
| 135 | + self.assertEqual(obj.history.count(), 1, msg="There is still one log entry.") |
| 136 | + self.assertEqual( |
| 137 | + out, |
| 138 | + "Truncate deletes all log entries and can not be passed with before-date.", |
| 139 | + msg="Output shows error", |
| 140 | + ) |
| 141 | + self.assertEqual(err, "", msg="No stderr") |
| 142 | + |
| 143 | + def test_flush_with_truncate_and_yes(self): |
| 144 | + obj = self.make_object() |
| 145 | + self.assertEqual(obj.history.count(), 1, msg="There is one log entry.") |
| 146 | + out, err = self.call_command("--truncate", "--y") |
| 147 | + |
| 148 | + self.assertEqual(obj.history.count(), 0, msg="There is no log entry.") |
| 149 | + self.assertEqual( |
| 150 | + out, |
| 151 | + "Truncated log entry table.", |
| 152 | + msg="Output shows table gets truncate", |
| 153 | + ) |
| 154 | + self.assertEqual(err, "", msg="No stderr") |
| 155 | + |
| 156 | + def test_flush_with_truncate_with_input_yes(self): |
| 157 | + obj = self.make_object() |
| 158 | + self.assertEqual(obj.history.count(), 1, msg="There is one log entry.") |
| 159 | + self.mock_input.return_value = "Y\n" |
| 160 | + out, err = self.call_command("--truncate") |
| 161 | + |
| 162 | + self.assertEqual(obj.history.count(), 0, msg="There is no log entry.") |
| 163 | + self.assertEqual( |
| 164 | + out, |
| 165 | + "This action will clear all log entries from the database.\nTruncated log entry table.", |
| 166 | + msg="Output shows warning and table gets truncate", |
| 167 | + ) |
| 168 | + self.assertEqual(err, "", msg="No stderr") |
| 169 | + |
| 170 | + @mock.patch( |
| 171 | + "django.db.connection.vendor", |
| 172 | + new_callable=mock.PropertyMock(return_value="unknown"), |
| 173 | + ) |
| 174 | + @mock.patch( |
| 175 | + "django.db.connection.display_name", |
| 176 | + new_callable=mock.PropertyMock(return_value="Unknown"), |
| 177 | + ) |
| 178 | + def test_flush_with_truncate_for_unsupported_database_vendor( |
| 179 | + self, mocked_vendor, mocked_db_name |
| 180 | + ): |
| 181 | + obj = self.make_object() |
| 182 | + self.assertEqual(obj.history.count(), 1, msg="There is one log entry.") |
| 183 | + out, err = self.call_command("--truncate", "--y") |
| 184 | + |
| 185 | + self.assertEqual(obj.history.count(), 1, msg="There is still one log entry.") |
| 186 | + self.assertEqual( |
| 187 | + out, |
| 188 | + "Database Unknown does not support truncate statement.", |
| 189 | + msg="Output shows error", |
| 190 | + ) |
| 191 | + self.assertEqual(err, "", msg="No stderr") |
0 commit comments