|
| 1 | +from django.contrib.auth.models import User |
| 2 | +from django.test import TestCase |
| 3 | +from django.urls import reverse |
| 4 | +from django_dynamic_fixture import get |
| 5 | + |
| 6 | +from readthedocs.projects.models import Project |
| 7 | + |
| 8 | + |
| 9 | +class TestHistoricalModels(TestCase): |
| 10 | + |
| 11 | + def setUp(self): |
| 12 | + self.user = get(User) |
| 13 | + self.project = get(Project, users=[self.user]) |
| 14 | + self.client.force_login(self.user) |
| 15 | + |
| 16 | + def test_extra_historical_fields_with_request(self): |
| 17 | + self.assertEqual(self.project.history.count(), 1) |
| 18 | + r = self.client.post( |
| 19 | + reverse('projects_edit', args=[self.project.slug]), |
| 20 | + data={ |
| 21 | + 'name': 'Changed!', |
| 22 | + 'repo': 'https://github.com/readthedocs/readthedocs', |
| 23 | + 'repo_type': self.project.repo_type, |
| 24 | + 'language': self.project.language, |
| 25 | + }, |
| 26 | + HTTP_USER_AGENT='Firefox', |
| 27 | + ) |
| 28 | + self.assertEqual(r.status_code, 302) |
| 29 | + self.assertEqual(self.project.history.count(), 2) |
| 30 | + history = self.project.history.first() |
| 31 | + self.assertEqual(history.name, 'Changed!') |
| 32 | + self.assertEqual(history.history_user, self.user) |
| 33 | + self.assertEqual(history.extra_history_user_id, self.user.id) |
| 34 | + self.assertEqual(history.extra_history_user_username, self.user.username) |
| 35 | + self.assertEqual(history.extra_history_ip, '127.0.0.1') |
| 36 | + self.assertEqual(history.extra_history_browser, 'Firefox') |
| 37 | + |
| 38 | + def test_extra_historical_fields_outside_request(self): |
| 39 | + self.assertEqual(self.project.history.count(), 1) |
| 40 | + self.project.name = 'Changed!' |
| 41 | + self.project.save() |
| 42 | + self.assertEqual(self.project.history.count(), 2) |
| 43 | + history = self.project.history.first() |
| 44 | + self.assertEqual(history.name, 'Changed!') |
| 45 | + self.assertIsNone(history.history_user) |
| 46 | + self.assertIsNone(history.extra_history_user_id) |
| 47 | + self.assertIsNone(history.extra_history_user_username) |
| 48 | + self.assertIsNone(history.extra_history_ip) |
| 49 | + self.assertIsNone(history.extra_history_browser) |
0 commit comments