Coverage for CIResults/tests/test_bugs_views.py: 100%

134 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-19 09:20 +0000

1from datetime import timedelta 

2from unittest.mock import patch, MagicMock 

3 

4from django.conf import settings 

5from django.test import TestCase 

6from django.utils import timezone 

7from django.urls import reverse 

8 

9from CIResults.tests.test_views import ViewMixin, UserFiltrableViewMixin, create_user_and_log_in 

10from CIResults.models import BugTracker, Bug, ReplicationScript 

11from CIResults.serializers import serialize_bug 

12 

13import json 

14 

15# HACK: Massively speed up the login primitive. We don't care about security in tests 

16settings.PASSWORD_HASHERS = ('django.contrib.auth.hashers.MD5PasswordHasher', ) 

17 

18 

19class ReplicationScriptCheckTests(TestCase): 

20 

21 def setUp(self): 

22 self.user = create_user_and_log_in(self.client, permissions=['change_replicationscript']) 

23 self.url = reverse("CIResults-replication-script-check") 

24 self.bt1 = BugTracker.objects.create(name="Tracker1", project="TEST", tracker_type="bugzilla", 

25 url="http://bar", public=True) 

26 self.bt2 = BugTracker.objects.create(name="Tracker2", project="TEST", tracker_type="jira", 

27 url="http://bar", public=True) 

28 self.kwargs = {"script": "def foo(): pass", 

29 "source_tracker": "Tracker1", 

30 "destination_tracker": "Tracker2"} 

31 

32 @patch('CIResults.bugs_views.Client') 

33 @patch('CIResults.bugtrackers.BugTrackerCommon.tracker_check_replication') 

34 def test_replication_script_check(self, t_rep_mock, c_mock): 

35 bug = Bug.objects.create(tracker=self.bt1, bug_id='2', 

36 title="You don't win friends with salad!", description="steamed hams") 

37 ser_bug = serialize_bug(bug) 

38 exp_resp = {'bugs': [ 

39 {'operation': "create", 'src_bug': ser_bug, 'dest_bug': None} 

40 ]} 

41 

42 t_rep_mock.return_value = [{"operation": "create", 'src_bug': ser_bug, 'dest_bug': None}] 

43 resp = self.client.post(self.url, self.kwargs) 

44 self.assertEqual(resp.status_code, 200) 

45 output = json.loads(resp.getvalue().decode()) 

46 self.assertEqual(output, exp_resp) 

47 

48 @patch('CIResults.bugs_views.Client') 

49 @patch('CIResults.bugtrackers.BugTrackerCommon.tracker_check_replication') 

50 def test_replication_script_check_no_resp(self, t_rep_mock, c_mock): 

51 Bug.objects.create(tracker=self.bt1, bug_id=2, 

52 title="You don't win friends with salad!", description="steamed hams") 

53 exp_resp = {'bugs': []} 

54 

55 t_rep_mock.return_value = [] 

56 resp = self.client.post(self.url, self.kwargs) 

57 self.assertEqual(resp.status_code, 200) 

58 output = json.loads(resp.getvalue().decode()) 

59 self.assertEqual(output, exp_resp) 

60 

61 @patch('CIResults.bugs_views.Client') 

62 @patch('CIResults.bugtrackers.BugTrackerCommon.tracker_check_replication') 

63 def test_replication_script_check_errors(self, t_rep_mock, c_mock): 

64 class FooError(Exception): 

65 def __init__(self, return_code, reason): 

66 self.return_code = return_code 

67 self.reason = reason 

68 

69 c_mock.UserFunctionCallError = FooError 

70 Bug.objects.create(tracker=self.bt1, bug_id=2, 

71 title="You don't win friends with salad!", description="steamed hams") 

72 

73 t_rep_mock.side_effect = FooError("foo", "foo") 

74 resp = self.client.post(self.url, self.kwargs) 

75 self.assertEqual(resp.status_code, 500) 

76 

77 t_rep_mock.side_effect = ValueError() 

78 resp = self.client.post(self.url, self.kwargs) 

79 self.assertEqual(resp.status_code, 500) 

80 

81 @patch('CIResults.bugs_views.Client') 

82 @patch('CIResults.bugtrackers.BugTrackerCommon.tracker_check_replication') 

83 def test_replication_script_check_update(self, t_rep_mock, c_mock): 

84 bug = Bug.objects.create(tracker=self.bt1, bug_id='2', 

85 title="You don't win friends with salad!", description="steamed hams") 

86 dest_bug = Bug.objects.create(tracker=self.bt2, bug_id='3', parent=bug, title="Something clever", 

87 description="Even more clever") 

88 ser_bug = serialize_bug(bug) 

89 ser_dest_bug = serialize_bug(dest_bug) 

90 exp_resp = {'bugs': [ 

91 {'operation': "update", 'src_bug': ser_bug, 'dest_bug': ser_dest_bug} 

92 ]} 

93 

94 t_rep_mock.return_value = [{"operation": "update", 'src_bug': ser_bug, 'dest_bug': ser_dest_bug}] 

95 resp = self.client.post(self.url, self.kwargs) 

96 self.assertEqual(resp.status_code, 200) 

97 output = json.loads(resp.getvalue().decode()) 

98 self.assertEqual(output, exp_resp) 

99 

100 @patch('CIResults.bugs_views.Client') 

101 def test_replication_script_check_client_errors(self, c_mock): 

102 c_mock.get_or_create_instance = MagicMock(side_effect=ValueError()) 

103 resp = self.client.post(self.url, self.kwargs) 

104 self.assertEqual(resp.status_code, 500) 

105 

106 c_mock.get_or_create_instance = MagicMock(side_effect=IOError()) 

107 resp = self.client.post(self.url, self.kwargs) 

108 self.assertEqual(resp.status_code, 500) 

109 

110 

111class MetricsOpenBugsTests(UserFiltrableViewMixin, TestCase): 

112 fixtures = ['CIResults/fixtures/bugs'] 

113 

114 # TODO: set the current time to a fixed one, add more comments 

115 

116 reverse_name = "CIResults-bug-open-list" 

117 query = "product = 'test'" 

118 

119 

120class bug_flag_for_updateTests(TestCase): 

121 def setUp(self): 

122 tracker = BugTracker.objects.create(name="BugTracker", public=True) 

123 self.bug = Bug.objects.create(tracker=tracker, bug_id="BUG_1") 

124 

125 def test_not_being_updated(self): 

126 response = self.client.post(reverse("CIResults-bug-flag-for-update", kwargs={"pk": self.bug.pk})) 

127 self.assertEqual(response.status_code, 200) 

128 

129 delta = timezone.now() - Bug.objects.get(pk=self.bug.id).flagged_as_update_pending_on 

130 self.assertLess(delta.total_seconds(), 1) 

131 

132 def test_being_updated(self): 

133 self.bug.flagged_as_update_pending_on = timezone.now() - timedelta(seconds=10) 

134 self.bug.save() 

135 

136 response = self.client.post(reverse("CIResults-bug-flag-for-update", kwargs={"pk": self.bug.pk})) 

137 self.assertEqual(response.status_code, 409) 

138 

139 delta = timezone.now() - Bug.objects.get(pk=self.bug.id).flagged_as_update_pending_on 

140 self.assertGreater(delta.total_seconds(), 1) 

141 

142 

143class ReplicationScriptTests(ViewMixin, TestCase): 

144 reverse_name = "CIResults-replication-script-list" 

145 

146 

147class ReplicationScriptCreateTests(ViewMixin, TestCase): 

148 reverse_name = "CIResults-replication-script-create" 

149 permissions_needed = ['add_replicationscript'] 

150 

151 def setUp(self): 

152 self.db_tracker = BugTracker.objects.create(name="Tracker1", project="TEST", tracker_type="bugzilla", 

153 url="http://bar", public=True) 

154 self.rep_tracker = BugTracker.objects.create(name="Tracker2", tracker_type="jira", url="http://foo", 

155 project="TEST2", public=True) 

156 

157 def test_anonymous(self): 

158 self.client.logout() 

159 response = self.client.get(self.url) 

160 self.assertRedirects(response, '/accounts/login/?next={}'.format(reverse(self.reverse_name))) 

161 

162 def test_script_create(self): 

163 create_user_and_log_in(self.client, permissions=self.permissions_needed) 

164 response = self.client.post(self.url, {'name': "foo", 'enabled': False, 'source_tracker': self.db_tracker.id, 

165 'destination_tracker': self.rep_tracker.id, 

166 'script': "def foo(): pass"}) 

167 self.assertRedirects(response, reverse("CIResults-replication-script-list")) 

168 self.assertEqual(len(ReplicationScript.objects.all()), 1) 

169 

170 

171class ReplicationScriptEditTests(ViewMixin, TestCase): 

172 reverse_name = "CIResults-replication-script-edit" 

173 

174 def __post(self): 

175 return self.client.post(self.url, {'name': "BAR", 'script': "def bar(): pass", 'source_tracker': self.bt1.id, 

176 'destination_tracker': self.bt2.id, 'enabled': 'True'}) 

177 

178 def setUp(self): 

179 self.user = create_user_and_log_in(self.client, permissions=['change_replicationscript']) 

180 self.bt1 = BugTracker.objects.create(name="Tracker1", project="TEST", tracker_type="bugzilla", 

181 url="http://bar", public=True) 

182 self.bt2 = BugTracker.objects.create(name="Tracker2", project="TEST", tracker_type="jira", 

183 url="http://bar", public=True) 

184 self.rp = ReplicationScript.objects.create(name="FOO", script="def foo(): pass", 

185 source_tracker=self.bt1, destination_tracker=self.bt2, enabled=True, 

186 created_by=self.user) 

187 self.create_time = self.rp.created_on 

188 self.view_kwargs = {'pk': self.rp.pk} 

189 

190 def test_script_history(self): 

191 response = self.__post() 

192 expected = [{'name': "FOO", 

193 'created_by': self.user.username, 

194 'created_on': self.create_time.isoformat()[:-6]+"Z", 

195 'source_tracker': "Tracker1", 

196 'destination_tracker': "Tracker2", 

197 'script': "def foo(): pass"}] 

198 rep_script = ReplicationScript.objects.first() 

199 history = rep_script.script_history 

200 self.assertRedirects(response, reverse("CIResults-replication-script-list")) 

201 self.assertEqual(history, json.dumps(expected)) 

202 

203 def test_anonymous(self): 

204 self.client.logout() 

205 response = self.client.get(self.url) 

206 self.assertRedirects(response, '/accounts/login/?next={}'.format(self.url))