10
10
from jbi .app import app
11
11
from jbi .environment import Settings
12
12
from jbi .models import Action , Actions , BugzillaWebhookComment , BugzillaWebhookRequest
13
+ from tests .fixtures .factories import (
14
+ action_factory ,
15
+ bug_factory ,
16
+ webhook_event_factory ,
17
+ webhook_factory ,
18
+ webhook_user_factory ,
19
+ )
13
20
14
21
15
22
@pytest .fixture
@@ -27,207 +34,95 @@ def settings():
27
34
@pytest .fixture (autouse = True )
28
35
def mocked_bugzilla ():
29
36
with mock .patch ("jbi.services.rh_bugzilla.Bugzilla" ) as mocked_bz :
30
- yield mocked_bz
37
+ yield mocked_bz ()
31
38
32
39
33
40
@pytest .fixture (autouse = True )
34
41
def mocked_jira ():
35
42
with mock .patch ("jbi.services.Jira" ) as mocked_jira :
36
- yield mocked_jira
43
+ yield mocked_jira ()
37
44
38
45
39
46
@pytest .fixture
40
- def webhook_create_example (mocked_bugzilla ) -> BugzillaWebhookRequest :
41
- webhook_payload = BugzillaWebhookRequest .parse_obj (
42
- {
43
- "bug" : {
44
- "assigned_to" :
"[email protected] " ,
45
- "comment" : None ,
46
- "component" : "General" ,
47
-
48
- "flags" : [],
49
- "id" : 654321 ,
50
- "is_private" : False ,
51
- "keywords" : [],
52
- "priority" : "" ,
53
- "product" : "JBI" ,
54
- "resolution" : "" ,
55
- "see_also" : [],
56
- "severity" : "--" ,
57
- "status" : "NEW" ,
58
- "summary" : "JBI Test" ,
59
- "type" : "defect" ,
60
- "whiteboard" : "devtest" ,
61
- },
62
- "event" : {
63
- "action" : "create" ,
64
- "changes" : None ,
65
- "routing_key" : "bug.create" ,
66
- "target" : "bug" ,
67
- "time" : "2022-03-23T20:10:17.495000+00:00" ,
68
- "user" : {
69
- "id" : 123456 ,
70
-
71
- "real_name" : "Nobody [ :nobody ]" ,
72
- },
73
- },
74
- "webhook_id" : 34 ,
75
- "webhook_name" : "local-test" ,
76
- }
77
- )
78
-
79
- mocked_bugzilla ().getbug .return_value = webhook_payload .bug
80
- mocked_bugzilla ().get_comments .return_value = {
81
- "bugs" : {"654321" : {"comments" : [{"text" : "Initial comment" }]}}
82
- }
47
+ def webhook_create_example () -> BugzillaWebhookRequest :
48
+ webhook_payload = webhook_factory ()
83
49
84
50
return webhook_payload
85
51
86
52
87
53
@pytest .fixture
88
- def webhook_comment_example (webhook_create_example ) -> BugzillaWebhookRequest :
89
- webhook_comment_example : BugzillaWebhookRequest = webhook_create_example
90
- webhook_comment_example .event .target = "comment"
91
- webhook_comment_example .
event .
user .
login = "[email protected] " # type: ignore
92
- assert webhook_comment_example .bug
93
- webhook_comment_example .bug .comment = BugzillaWebhookComment .parse_obj (
94
- {"number" : 2 , "body" : "hello" }
54
+ def webhook_comment_example () -> BugzillaWebhookRequest :
55
+ user = webhook_user_factory (
login = "[email protected] " )
56
+ comment = BugzillaWebhookComment .parse_obj ({"number" : 2 , "body" : "hello" })
57
+ bug = bug_factory (
58
+ see_also = ["https://mozilla.atlassian.net/browse/JBI-234" ],
59
+ comment = comment ,
95
60
)
96
- webhook_comment_example . bug . see_also = [
97
- "https://mozilla.atlassian.net/browse/JBI-234"
98
- ]
99
- return webhook_comment_example
61
+ event = webhook_event_factory ( target = "comment" , user = user )
62
+ webhook_payload = webhook_factory ( bug = bug , event = event )
63
+
64
+ return webhook_payload
100
65
101
66
102
67
@pytest .fixture
103
- def webhook_private_comment_example (
104
- webhook_create_example , mocked_bugzilla
105
- ) -> BugzillaWebhookRequest :
106
- webhook_comment_example : BugzillaWebhookRequest = webhook_create_example
107
- webhook_comment_example .event .target = "comment"
108
- webhook_comment_example .
event .
user .
login = "[email protected] " # type: ignore
109
- assert webhook_comment_example .bug
110
- webhook_comment_example .bug .comment = BugzillaWebhookComment .parse_obj (
111
- {"id" : 344 , "number" : 2 , "is_private" : True }
68
+ def webhook_private_comment_example () -> BugzillaWebhookRequest :
69
+ user = webhook_user_factory (
login = "[email protected] " )
70
+ event = webhook_event_factory (target = "comment" , user = user )
71
+ bug = bug_factory (
72
+ comment = {"id" : 344 , "number" : 2 , "is_private" : True },
73
+ see_also = ["https://mozilla.atlassian.net/browse/JBI-234" ],
112
74
)
113
- webhook_comment_example .bug .see_also = [
114
- "https://mozilla.atlassian.net/browse/JBI-234"
115
- ]
116
-
117
- # Call to Bugzilla returns the actual bug comments.
118
- mocked_bugzilla ().get_comments .return_value = {
119
- "bugs" : {
120
- str (webhook_comment_example .bug .id ): {
121
- "comments" : [
122
- {
123
- "id" : 343 ,
124
- "text" : "not this one" ,
125
- "bug_id" : webhook_comment_example .bug .id ,
126
- "count" : 1 ,
127
- "is_private" : True ,
128
-
129
- },
130
- {
131
- "id" : 344 ,
132
- "text" : "hello" ,
133
- "bug_id" : webhook_comment_example .bug .id ,
134
- "count" : 2 ,
135
- "is_private" : True ,
136
-
137
- },
138
- {
139
- "id" : 345 ,
140
- "text" : "or this one" ,
141
- "bug_id" : webhook_comment_example .bug .id ,
142
- "count" : 3 ,
143
- "is_private" : True ,
144
-
145
- },
146
- ]
147
- }
148
- },
149
- "comments" : {},
150
- }
151
-
152
- return webhook_comment_example
75
+ webhook_payload = webhook_factory (bug = bug , event = event )
76
+ return webhook_payload
153
77
154
78
155
79
@pytest .fixture
156
- def webhook_create_private_example (
157
- webhook_create_example , mocked_bugzilla
158
- ) -> BugzillaWebhookRequest :
159
- private_bug = webhook_create_example .bug
160
- private_bug .is_private = True
161
- # Call to Bugzilla returns the actual bug object.
162
- mocked_bugzilla ().getbug .return_value = private_bug
163
-
164
- # But webhook payload only contains private attribute.
165
- webhook_create_private_example = BugzillaWebhookRequest .parse_obj (
166
- {
167
- ** webhook_create_example .dict (),
168
- "bug" : {"id" : private_bug .id , "is_private" : True },
169
- }
80
+ def webhook_create_private_example () -> BugzillaWebhookRequest :
81
+ return webhook_factory (
82
+ event = webhook_event_factory (),
83
+ bug = {"id" : 654321 , "is_private" : True },
170
84
)
171
- return webhook_create_private_example
172
85
173
86
174
87
@pytest .fixture
175
- def webhook_modify_example (webhook_create_example ) -> BugzillaWebhookRequest :
176
- webhook_modify_example : BugzillaWebhookRequest = webhook_create_example
177
- assert webhook_modify_example .bug
178
- webhook_modify_example .bug .see_also = [
179
- "https://mozilla.atlassian.net/browse/JBI-234"
180
- ]
181
-
182
- webhook_modify_example .event .action = "modify"
183
- webhook_modify_example .event .routing_key = "bug.modify:status"
184
- return webhook_modify_example
88
+ def webhook_modify_example () -> BugzillaWebhookRequest :
89
+ bug = bug_factory (see_also = ["https://mozilla.atlassian.net/browse/JBI-234" ])
90
+ event = webhook_event_factory (action = "modify" , routing_key = "bug.modify:status" )
91
+ webhook_payload = webhook_factory (bug = bug , event = event )
92
+ return webhook_payload
185
93
186
94
187
95
@pytest .fixture
188
- def webhook_change_status_assignee (webhook_modify_example ):
189
- payload = webhook_modify_example .dict ()
190
- payload ["event" ]["routing_key" ] = "bug.modify"
191
- payload ["event" ]["changes" ] = [
192
- {"field" : "status" , "removed" : "OPEN" , "added" : "FIXED" },
96
+ def webhook_change_status_assignee ():
97
+ changes = [
98
+ {
99
+ "field" : "status" ,
100
+ "removed" : "OPEN" ,
101
+ "added" : "FIXED" ,
102
+ },
193
103
{
194
104
"field" : "assignee" ,
195
105
196
106
197
107
},
198
108
]
199
- return BugzillaWebhookRequest .parse_obj (payload )
109
+ event = webhook_event_factory (routing_key = "bug.modify" , changes = changes )
110
+ webhook_payload = webhook_factory (event = event )
111
+ return webhook_payload
200
112
201
113
202
114
@pytest .fixture
203
- def webhook_modify_private_example (
204
- webhook_modify_example , mocked_bugzilla
205
- ) -> BugzillaWebhookRequest :
206
- private_bug = webhook_modify_example .bug
207
- private_bug .is_private = True
208
- # Call to Bugzilla returns the actual bug object.
209
- mocked_bugzilla ().getbug .return_value = private_bug
210
-
211
- # But webhook payload only contains private attribute.
212
- webhook_modify_private_example = BugzillaWebhookRequest .parse_obj (
213
- {
214
- ** webhook_modify_example .dict (),
215
- "bug" : {"id" : private_bug .id , "is_private" : True },
216
- }
115
+ def webhook_modify_private_example () -> BugzillaWebhookRequest :
116
+ event = webhook_event_factory (action = "modify" , routing_key = "bug.modify:status" )
117
+ webhook_payload = webhook_factory (
118
+ bug = {"id" : 654321 , "is_private" : True }, event = event
217
119
)
218
- return webhook_modify_private_example
120
+ return webhook_payload
219
121
220
122
221
123
@pytest .fixture
222
124
def action_example () -> Action :
223
- return Action .parse_obj (
224
- {
225
- "whiteboard_tag" : "devtest" ,
226
-
227
- "description" : "test config" ,
228
- "module" : "tests.fixtures.noop_action" ,
229
- }
230
- )
125
+ return action_factory ()
231
126
232
127
233
128
@pytest .fixture
0 commit comments