Skip to content

Commit 1a09d0d

Browse files
committed
sentry - middleware - campaign - called_by_creator tests
1 parent a2c03b7 commit 1a09d0d

File tree

1 file changed

+155
-12
lines changed

1 file changed

+155
-12
lines changed

sentry/src/middleware/campaign.rs

Lines changed: 155 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,16 @@ where
8080
let campaign_context = request
8181
.extensions()
8282
.get::<ChainOf<Campaign>>()
83-
.expect("We must have a campaign in extensions");
83+
.expect("We must have a Campaign in extensions");
8484

8585
let auth = request
8686
.extensions()
8787
.get::<Auth>()
88-
.expect("request should have session");
88+
.expect("request should have an Authentication");
8989

9090
if auth.uid.to_address() != campaign_context.context.creator {
9191
return Err(ResponseError::Forbidden(
92-
"Request not sent by campaign creator".to_string(),
92+
"Request not sent by Campaign's creator".to_string(),
9393
));
9494
}
9595

@@ -107,8 +107,11 @@ mod test {
107107
};
108108
use tower::Service;
109109

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+
};
112115

113116
use crate::{
114117
db::{insert_campaign, insert_channel},
@@ -122,16 +125,16 @@ mod test {
122125
let app_guard = setup_dummy_app().await;
123126
let app = Arc::new(app_guard.app);
124127

125-
let build_request = |id: CampaignId| {
128+
let campaign_context = CAMPAIGNS[0].clone();
129+
130+
let build_request = || {
126131
Request::builder()
127-
.uri(format!("/{id}/test"))
132+
.uri(format!("/{id}/test", id = campaign_context.context.id))
128133
.extension(app.clone())
129134
.body(Body::empty())
130135
.expect("Should build Request")
131136
};
132137

133-
let campaign_context = CAMPAIGNS[0].clone();
134-
135138
async fn handle(
136139
Extension(campaign_context): Extension<ChainOf<Campaign>>,
137140
Path((id, another)): Path<(CampaignId, String)>,
@@ -147,7 +150,7 @@ mod test {
147150

148151
// bad CampaignId
149152
{
150-
let mut request = build_request(campaign_context.context.id);
153+
let mut request = build_request();
151154
*request.uri_mut() = "/WrongCampaignId".parse().unwrap();
152155

153156
let response = router
@@ -164,7 +167,7 @@ mod test {
164167

165168
// non-existent Campaign
166169
{
167-
let request = build_request(campaign_context.context.id);
170+
let request = build_request();
168171

169172
let response = router
170173
.call(request)
@@ -187,7 +190,7 @@ mod test {
187190
.await
188191
.expect("Should insert Campaign"));
189192

190-
let request = build_request(campaign_context.context.id);
193+
let request = build_request();
191194

192195
let response = router
193196
.call(request)
@@ -197,4 +200,144 @@ mod test {
197200
assert_eq!(response.status(), StatusCode::OK);
198201
}
199202
}
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+
}
200343
}

0 commit comments

Comments
 (0)