@@ -80,16 +80,16 @@ where
80
80
let campaign_context = request
81
81
. extensions ( )
82
82
. get :: < ChainOf < Campaign > > ( )
83
- . expect ( "We must have a campaign in extensions" ) ;
83
+ . expect ( "We must have a Campaign in extensions" ) ;
84
84
85
85
let auth = request
86
86
. extensions ( )
87
87
. get :: < Auth > ( )
88
- . expect ( "request should have session " ) ;
88
+ . expect ( "request should have an Authentication " ) ;
89
89
90
90
if auth. uid . to_address ( ) != campaign_context. context . creator {
91
91
return Err ( ResponseError :: Forbidden (
92
- "Request not sent by campaign creator" . to_string ( ) ,
92
+ "Request not sent by Campaign's creator" . to_string ( ) ,
93
93
) ) ;
94
94
}
95
95
@@ -107,8 +107,11 @@ mod test {
107
107
} ;
108
108
use tower:: Service ;
109
109
110
- use adapter:: Dummy ;
111
- use primitives:: { test_util:: CAMPAIGNS , Campaign , ChainOf } ;
110
+ use adapter:: { ethereum:: test_util:: GANACHE_1 , Dummy } ;
111
+ use primitives:: {
112
+ test_util:: { CAMPAIGNS , CREATOR , IDS , PUBLISHER } ,
113
+ Campaign , ChainOf ,
114
+ } ;
112
115
113
116
use crate :: {
114
117
db:: { insert_campaign, insert_channel} ,
@@ -122,16 +125,16 @@ mod test {
122
125
let app_guard = setup_dummy_app ( ) . await ;
123
126
let app = Arc :: new ( app_guard. app ) ;
124
127
125
- let build_request = |id : CampaignId | {
128
+ let campaign_context = CAMPAIGNS [ 0 ] . clone ( ) ;
129
+
130
+ let build_request = || {
126
131
Request :: builder ( )
127
- . uri ( format ! ( "/{id}/test" ) )
132
+ . uri ( format ! ( "/{id}/test" , id = campaign_context . context . id ) )
128
133
. extension ( app. clone ( ) )
129
134
. body ( Body :: empty ( ) )
130
135
. expect ( "Should build Request" )
131
136
} ;
132
137
133
- let campaign_context = CAMPAIGNS [ 0 ] . clone ( ) ;
134
-
135
138
async fn handle (
136
139
Extension ( campaign_context) : Extension < ChainOf < Campaign > > ,
137
140
Path ( ( id, another) ) : Path < ( CampaignId , String ) > ,
@@ -147,7 +150,7 @@ mod test {
147
150
148
151
// bad CampaignId
149
152
{
150
- let mut request = build_request ( campaign_context . context . id ) ;
153
+ let mut request = build_request ( ) ;
151
154
* request. uri_mut ( ) = "/WrongCampaignId" . parse ( ) . unwrap ( ) ;
152
155
153
156
let response = router
@@ -164,7 +167,7 @@ mod test {
164
167
165
168
// non-existent Campaign
166
169
{
167
- let request = build_request ( campaign_context . context . id ) ;
170
+ let request = build_request ( ) ;
168
171
169
172
let response = router
170
173
. call ( request)
@@ -187,7 +190,7 @@ mod test {
187
190
. await
188
191
. expect( "Should insert Campaign" ) ) ;
189
192
190
- let request = build_request ( campaign_context . context . id ) ;
193
+ let request = build_request ( ) ;
191
194
192
195
let response = router
193
196
. call ( request)
@@ -197,4 +200,144 @@ mod test {
197
200
assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
198
201
}
199
202
}
203
+
204
+ #[ tokio:: test]
205
+ async fn test_called_by_creator ( ) {
206
+ let app_guard = setup_dummy_app ( ) . await ;
207
+ let app = Arc :: new ( app_guard. app ) ;
208
+
209
+ let campaign_context = CAMPAIGNS [ 0 ] . clone ( ) ;
210
+
211
+ // insert Channel
212
+ insert_channel ( & app. pool , & campaign_context. of_channel ( ) )
213
+ . await
214
+ . expect ( "Should insert Channel" ) ;
215
+ // insert Campaign
216
+ assert ! ( insert_campaign( & app. pool, & campaign_context. context)
217
+ . await
218
+ . expect( "Should insert Campaign" ) ) ;
219
+
220
+ let build_request = |auth : Auth | {
221
+ Request :: builder ( )
222
+ . extension ( app. clone ( ) )
223
+ . extension ( campaign_context. clone ( ) )
224
+ . extension ( auth)
225
+ . body ( Body :: empty ( ) )
226
+ . expect ( "Should build Request" )
227
+ } ;
228
+
229
+ async fn handle ( Extension ( _campaign_context) : Extension < ChainOf < Campaign > > ) -> String {
230
+ "Ok" . into ( )
231
+ }
232
+
233
+ let mut router = Router :: new ( )
234
+ . route ( "/" , get ( handle) )
235
+ . layer ( from_fn ( called_by_creator :: < Dummy , _ > ) ) ;
236
+
237
+ // Not the Creator - Forbidden
238
+ {
239
+ let not_creator = Auth {
240
+ era : 1 ,
241
+ uid : IDS [ & PUBLISHER ] ,
242
+ chain : campaign_context. chain . clone ( ) ,
243
+ } ;
244
+ assert_ne ! (
245
+ not_creator. uid. to_address( ) ,
246
+ campaign_context. context. creator,
247
+ "The Auth address should not be the Campaign creator for this test!"
248
+ ) ;
249
+ let request = build_request ( not_creator) ;
250
+
251
+ let response = router
252
+ . call ( request)
253
+ . await
254
+ . expect ( "Should make request to Router" ) ;
255
+
256
+ assert_eq ! ( response. status( ) , StatusCode :: FORBIDDEN ) ;
257
+ }
258
+
259
+ // The Campaign Creator - Ok
260
+ {
261
+ let the_creator = Auth {
262
+ era : 1 ,
263
+ uid : IDS [ & campaign_context. context . creator ] ,
264
+ chain : campaign_context. chain . clone ( ) ,
265
+ } ;
266
+
267
+ assert_eq ! (
268
+ the_creator. uid. to_address( ) ,
269
+ campaign_context. context. creator,
270
+ "The Auth address should be the Campaign creator for this test!"
271
+ ) ;
272
+ let request = build_request ( the_creator) ;
273
+
274
+ let response = router
275
+ . call ( request)
276
+ . await
277
+ . expect ( "Should make request to Router" ) ;
278
+
279
+ assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
280
+ }
281
+ }
282
+
283
+ #[ tokio:: test]
284
+ #[ should_panic]
285
+ async fn test_called_by_creator_no_auth ( ) {
286
+ let app_guard = setup_dummy_app ( ) . await ;
287
+ let app = Arc :: new ( app_guard. app ) ;
288
+
289
+ let campaign_context = CAMPAIGNS [ 0 ] . clone ( ) ;
290
+
291
+ async fn handle ( Extension ( _campaign_context) : Extension < ChainOf < Campaign > > ) -> String {
292
+ "Ok" . into ( )
293
+ }
294
+
295
+ let mut router = Router :: new ( )
296
+ . route ( "/" , get ( handle) )
297
+ . layer ( from_fn ( called_by_creator :: < Dummy , _ > ) ) ;
298
+
299
+ // No Auth - Unauthorized
300
+ let request = Request :: builder ( )
301
+ . extension ( app. clone ( ) )
302
+ . extension ( campaign_context. clone ( ) )
303
+ . body ( Body :: empty ( ) )
304
+ . expect ( "Should build Request" ) ;
305
+
306
+ let _response = router
307
+ . call ( request)
308
+ . await
309
+ . expect ( "Should make request to Router" ) ;
310
+ }
311
+
312
+ #[ tokio:: test]
313
+ #[ should_panic]
314
+ async fn test_called_by_creator_no_campaign ( ) {
315
+ let app_guard = setup_dummy_app ( ) . await ;
316
+ let app = Arc :: new ( app_guard. app ) ;
317
+
318
+ let auth = Auth {
319
+ era : 1 ,
320
+ uid : IDS [ & CREATOR ] ,
321
+ chain : GANACHE_1 . clone ( ) ,
322
+ } ;
323
+
324
+ let request = Request :: builder ( )
325
+ . extension ( app. clone ( ) )
326
+ . extension ( auth)
327
+ . body ( Body :: empty ( ) )
328
+ . expect ( "Should build Request" ) ;
329
+
330
+ async fn handle ( Extension ( _campaign_context) : Extension < ChainOf < Campaign > > ) -> String {
331
+ "Ok" . into ( )
332
+ }
333
+
334
+ let mut router = Router :: new ( )
335
+ . route ( "/" , get ( handle) )
336
+ . layer ( from_fn ( called_by_creator :: < Dummy , _ > ) ) ;
337
+
338
+ let _response = router
339
+ . call ( request)
340
+ . await
341
+ . expect ( "Should make request to Router" ) ;
342
+ }
200
343
}
0 commit comments