@@ -163,7 +163,7 @@ pub fn get_unit_html_with_events(
163163 . iter ( )
164164 . map ( |validator| {
165165 let fetch_url = format ! (
166- "{}/campaign/{}/events?pubAddr={}" ,
166+ "{}/v5/ campaign/{}/events?pubAddr={}" ,
167167 validator. url, campaign_id, options. publisher_addr
168168 ) ;
169169
@@ -201,7 +201,13 @@ pub fn get_unit_html_with_events(
201201#[ cfg( test) ]
202202mod test {
203203 use super :: * ;
204- use adex_primitives:: test_util:: DUMMY_IPFS ;
204+ use adex_primitives:: {
205+ config:: GANACHE_CONFIG ,
206+ test_util:: { DUMMY_CAMPAIGN , DUMMY_IPFS , PUBLISHER } ,
207+ util:: ApiUrl ,
208+ } ;
209+ use scraper:: { Html , Selector } ;
210+ use std:: collections:: HashSet ;
205211
206212 fn get_ad_unit ( media_mime : & str ) -> AdUnit {
207213 AdUnit {
@@ -231,6 +237,188 @@ mod test {
231237 assert_eq ! ( "http://123" . to_string( ) , normalize_url( "http://123" ) ) ;
232238 }
233239
240+ #[ test]
241+ fn getting_unit_html ( ) {
242+ let size_1 = Size {
243+ width : 480 ,
244+ height : 480 ,
245+ } ;
246+
247+ let size_2 = Size {
248+ width : 920 ,
249+ height : 160 ,
250+ } ;
251+
252+ let video_unit = AdUnit {
253+ ipfs : DUMMY_IPFS [ 0 ] ,
254+ media_url : "" . into ( ) ,
255+ media_mime : "video/avi" . into ( ) ,
256+ target_url : "https://ambire.com?utm_source=adex_PUBHOSTNAME" . into ( ) ,
257+ } ;
258+
259+ let image_unit = AdUnit {
260+ ipfs : DUMMY_IPFS [ 1 ] ,
261+ media_url : "" . into ( ) ,
262+ media_mime : "image/jpeg" . into ( ) ,
263+ target_url : "https://ambire.com?utm_source=adex_PUBHOSTNAME" . into ( ) ,
264+ } ;
265+
266+ // Test for first size, correct link, video inside link
267+ {
268+ let unit = get_unit_html ( Some ( size_1) , & video_unit, "https://adex.network" , "" , "" ) ;
269+ let fragment = Html :: parse_fragment ( & unit) ;
270+
271+ let div_selector = Selector :: parse ( "div" ) . unwrap ( ) ;
272+ let div = fragment
273+ . select ( & div_selector)
274+ . next ( )
275+ . expect ( "There should be a div" ) ;
276+
277+ let anchor_selector = Selector :: parse ( "div>a" ) . unwrap ( ) ;
278+ let anchor = fragment
279+ . select ( & anchor_selector)
280+ . next ( )
281+ . expect ( "There should be an anchor" ) ;
282+
283+ let video_selector = Selector :: parse ( "div>a>video" ) . unwrap ( ) ;
284+ let video = fragment
285+ . select ( & video_selector)
286+ . next ( )
287+ . expect ( "There should be a video" ) ;
288+
289+ assert_eq ! ( "div" , div. value( ) . name( ) ) ;
290+
291+ assert_eq ! ( "a" , anchor. value( ) . name( ) ) ;
292+ assert_eq ! (
293+ Some ( "https://ambire.com?utm_source=AdEx+(https://adex.network)" ) ,
294+ anchor. value( ) . attr( "href" )
295+ ) ;
296+
297+ assert_eq ! ( "video" , video. value( ) . name( ) ) ;
298+ assert_eq ! ( Some ( "480" ) , video. value( ) . attr( "width" ) ) ;
299+ assert_eq ! ( Some ( "480" ) , video. value( ) . attr( "height" ) ) ;
300+ }
301+ // Test for another size
302+ {
303+ let unit = get_unit_html ( Some ( size_2) , & video_unit, "https://adex.network" , "" , "" ) ;
304+ let fragment = Html :: parse_fragment ( & unit) ;
305+
306+ let video_selector = Selector :: parse ( "div>a>video" ) . unwrap ( ) ;
307+ let video = fragment
308+ . select ( & video_selector)
309+ . next ( )
310+ . expect ( "There should be a video" ) ;
311+
312+ assert_eq ! ( "video" , video. value( ) . name( ) ) ;
313+ assert_eq ! ( Some ( "920" ) , video. value( ) . attr( "width" ) ) ;
314+ assert_eq ! ( Some ( "160" ) , video. value( ) . attr( "height" ) ) ;
315+ }
316+
317+ // Test for image ad_unit
318+ {
319+ let unit = get_unit_html ( Some ( size_1) , & image_unit, "https://adex.network" , "" , "" ) ;
320+ let fragment = Html :: parse_fragment ( & unit) ;
321+
322+ let image_selector = Selector :: parse ( "div>a>*" ) . unwrap ( ) ;
323+ let image = fragment
324+ . select ( & image_selector)
325+ . next ( )
326+ . expect ( "There should be an image" ) ;
327+
328+ assert_eq ! ( "img" , image. value( ) . name( ) ) ;
329+ }
330+
331+ // Test for another hostname
332+ {
333+ let unit = get_unit_html (
334+ Some ( size_1) ,
335+ & video_unit,
336+ "https://adsense.google.com" ,
337+ "" ,
338+ "" ,
339+ ) ;
340+ let fragment = Html :: parse_fragment ( & unit) ;
341+
342+ let anchor_selector = Selector :: parse ( "div>a" ) . unwrap ( ) ;
343+ let anchor = fragment
344+ . select ( & anchor_selector)
345+ . next ( )
346+ . expect ( "There should be a second anchor" ) ;
347+
348+ assert_eq ! ( "a" , anchor. value( ) . name( ) ) ;
349+ assert_eq ! (
350+ Some ( "https://ambire.com?utm_source=AdEx+(https://adsense.google.com)" ) ,
351+ anchor. value( ) . attr( "href" )
352+ ) ;
353+ }
354+ }
355+
356+ #[ test]
357+ fn getting_unit_html_with_events ( ) {
358+ let whitelisted_tokens = GANACHE_CONFIG
359+ . chains
360+ . values ( )
361+ . flat_map ( |chain| chain. tokens . values ( ) . map ( |token| token. address ) )
362+ . collect :: < HashSet < _ > > ( ) ;
363+
364+ let market_url = ApiUrl :: parse ( "https://market.adex.network" ) . expect ( "should parse" ) ;
365+ let validator_1_url = ApiUrl :: parse ( "https://tom.adex.network" ) . expect ( "should parse" ) ;
366+ let validator_2_url = ApiUrl :: parse ( "https://jerry.adex.network" ) . expect ( "should parse" ) ;
367+ let options = Options {
368+ market_url,
369+ market_slot : DUMMY_IPFS [ 0 ] ,
370+ publisher_addr : * PUBLISHER ,
371+ // All passed tokens must be of the same price and decimals, so that the amounts can be accurately compared
372+ whitelisted_tokens,
373+ size : Some ( Size :: new ( 300 , 100 ) ) ,
374+ navigator_language : Some ( "bg" . into ( ) ) ,
375+ disabled_video : false ,
376+ disabled_sticky : false ,
377+ validators : vec ! [ validator_1_url, validator_2_url] ,
378+ } ;
379+ let ad_unit = AdUnit {
380+ ipfs : DUMMY_IPFS [ 0 ] ,
381+ media_url : "" . into ( ) ,
382+ media_mime : "video/avi" . into ( ) ,
383+ target_url : "https://ambire.com?utm_source=adex_PUBHOSTNAME" . into ( ) ,
384+ } ;
385+ let campaign_id = DUMMY_CAMPAIGN . id ;
386+ let validators = DUMMY_CAMPAIGN . validators . clone ( ) ;
387+ // Test with events
388+ {
389+ let unit_with_events = get_unit_html_with_events (
390+ & options,
391+ & ad_unit,
392+ "https://adex.network" ,
393+ campaign_id,
394+ & validators,
395+ false ,
396+ ) ;
397+ let fragment = Html :: parse_fragment ( & unit_with_events) ;
398+
399+ let anchor_selector = Selector :: parse ( "div>a" ) . unwrap ( ) ;
400+ let anchor = fragment
401+ . select ( & anchor_selector)
402+ . next ( )
403+ . expect ( "There should be a second anchor" ) ;
404+
405+ let video_selector = Selector :: parse ( "div>a>video" ) . unwrap ( ) ;
406+ let video = fragment
407+ . select ( & video_selector)
408+ . next ( )
409+ . expect ( "There should be a video" ) ;
410+
411+ let expected_onclick: & str = & format ! ( "var fetchOpts = {{ method: 'POST', headers: {{ 'content-type': 'application/json' }}, body: {{'events':[{{'type':'CLICK','publisher':'{}','adUnit':'{}','adSlot':'{}','referrer':'document.referrer'}}]}} }}; fetch('{}/v5/campaign/{}/events?pubAddr={}', fetchOpts); fetch('{}/v5/campaign/{}/events?pubAddr={}', fetchOpts)" , options. publisher_addr, ad_unit. ipfs, options. market_slot, validators[ 0 ] . url, campaign_id, options. publisher_addr, validators[ 1 ] . url, campaign_id, options. publisher_addr) ;
412+ assert_eq ! ( Some ( expected_onclick) , anchor. value( ) . attr( "onclick" ) ) ;
413+
414+ let expected_onloadeddata: & str = & format ! ( "setTimeout(function() {{ var fetchOpts = {{ method: 'POST', headers: {{ 'content-type': 'application/json' }}, body: {{'events':[{{'type':'IMPRESSION','publisher':'{}','adUnit':'{}','adSlot':'{}','referrer':'document.referrer'}}]}} }}; fetch('{}/v5/campaign/{}/events?pubAddr={}', fetchOpts); fetch('{}/v5/campaign/{}/events?pubAddr={}', fetchOpts) }}, 8000)" , options. publisher_addr, ad_unit. ipfs, options. market_slot, validators[ 0 ] . url, campaign_id, options. publisher_addr, validators[ 1 ] . url, campaign_id, options. publisher_addr) ;
415+ assert_eq ! (
416+ Some ( expected_onloadeddata) ,
417+ video. value( ) . attr( "onloadeddata" )
418+ ) ;
419+ }
420+ }
421+
234422 mod randomized_sort_pos {
235423
236424 use super :: * ;
0 commit comments