|
| 1 | +import os |
| 2 | +import requests |
| 3 | +import unittest |
| 4 | +import time |
| 5 | + |
| 6 | +ad_payload = { |
| 7 | + "ad_unit_targeted": [ |
| 8 | + "ADU20240429114506248" |
| 9 | + ], |
| 10 | + "adid": "AD12345", |
| 11 | + "adname": "Integration Test", |
| 12 | + "adpriority": 8, |
| 13 | + "adstate": "ACTIVE", |
| 14 | + "adtype": "GUARANTEED", |
| 15 | + "advertiserid": "A20240428210030849", |
| 16 | + "bidinfo": { |
| 17 | + "bid": 1, |
| 18 | + "bidType": "CPM" |
| 19 | + }, |
| 20 | + "budget": { |
| 21 | + "currencyCode": "USD", |
| 22 | + "dailyBudget": 10, |
| 23 | + "totalBudget": 10 |
| 24 | + }, |
| 25 | + "campaignid": "C20240428150217896", |
| 26 | + "createdat": "Sun, 17 Mar 2024 12:46:56 GMT", |
| 27 | + |
| 28 | + "creativeid": "CR20240420110525228", |
| 29 | + "enddate": "2025-03-17T13:00:00", |
| 30 | + "frequencycaps": { |
| 31 | + "frequencyCapList": [ |
| 32 | + { |
| 33 | + "entityType": "CAMPAIGN", |
| 34 | + "eventType": "CSC", |
| 35 | + "timeFrame": { |
| 36 | + "maxCount": 0, |
| 37 | + "timeWindowType": "HOUR", |
| 38 | + "value": 0 |
| 39 | + }, |
| 40 | + "userCap": True |
| 41 | + } |
| 42 | + ] |
| 43 | + }, |
| 44 | + "landingurl": "nike.com", |
| 45 | + "startdate": "2024-04-17T13:00:00", |
| 46 | + "updatedat": "Sun, 17 Mar 2024 12:46:56 GMT", |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | +ad_serve_payload = { |
| 51 | + "dpl": "eyJ1c2VySWQiOiJVMjIwOTA4MTU0MjE2MTUwNzUwMTUzOCIsImFkVW5pdElkIjoiQURVMTIzNDUiLCJhZFVuaXROYW1lIjoiV2ViUGFnZTFUb3AiLCJsb2NhbGUiOiJlbiIsImFzcGVjdFJhdGlvIjoiMTZ4OSJ9", |
| 52 | + "id": "c2cd389c-b6e8-4198-8850-9cc14305d407", |
| 53 | + "imp": [ |
| 54 | + { |
| 55 | + "bidfloorcur": "INR", |
| 56 | + "id": "877d9821-fc34-4f2c-b63a-28397bea8308-1", |
| 57 | + "native": { |
| 58 | + "request": {"ver": "1.2", "assets": [{"id": 1, "required": 1, "img": {"type": 3, "w": 1280, "h": 720}}]}, |
| 59 | + "ver": "1.2" |
| 60 | + } |
| 61 | + } |
| 62 | + ], |
| 63 | + "user": { |
| 64 | + "id": "b300a5a01e981519bccfdb9093407f9fe0c22d31dc16823a450974c7e4668d49" |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | +class TestAPI(unittest.TestCase): |
| 69 | + # Fixture to retrieve the API hostname from environment variables |
| 70 | + def setUp(self): |
| 71 | + self.ad_manager_host = os.environ.get('AD_MANAGER_HOST') |
| 72 | + self.ad_server_host = os.environ.get('AD_SERVER_HOST') |
| 73 | + |
| 74 | + def test_workflow(self): |
| 75 | + |
| 76 | + ad_creation_endpoint = '/ad' |
| 77 | + cache_refresh_endpoint = '/cache' |
| 78 | + |
| 79 | + # Make a POST request to create the ad |
| 80 | + ad_response = requests.post(self.ad_manager_host + ad_creation_endpoint, json=ad_payload) |
| 81 | + |
| 82 | + # Assert the response status code is 200 (or any other expected status code) |
| 83 | + self.assertEqual(ad_response.status_code, 201, f"Expected status code 200, but got {ad_response.status_code}") |
| 84 | + response_data = ad_response.json() |
| 85 | + ad_id = response_data['ad']['adid'] |
| 86 | + |
| 87 | + query_params_activate_ad = { |
| 88 | + 'ad_id': ad_id, |
| 89 | + 'state': 'ACTIVE' |
| 90 | + } |
| 91 | + |
| 92 | + ad_update_response = requests.patch(self.ad_manager_host + ad_creation_endpoint, params=query_params_activate_ad) |
| 93 | + self.assertEqual(ad_update_response.status_code, 200, f"Expected status code 200, but got {ad_update_response.status_code}") |
| 94 | + |
| 95 | + time.sleep(5) |
| 96 | + |
| 97 | + requests.get(self.ad_manager_host + cache_refresh_endpoint + '/campaigns') |
| 98 | + cache_response = requests.get(self.ad_manager_host + cache_refresh_endpoint + '/ads') |
| 99 | + self.assertEqual(cache_response.status_code, 200, f"Expected status code 200, but got {cache_response.status_code}") |
| 100 | + cache_response_data = cache_response.json() |
| 101 | + self.assertTrue(ad_id in cache_response_data["C20240428150217896"], f"Expected adid {ad_id} in cache, but not in cache") |
| 102 | + |
| 103 | + time.sleep(5) |
| 104 | + |
| 105 | + query_params_ad_serve = { |
| 106 | + 'adunit_id': 'ADU20240429114506248', |
| 107 | + 'publisher_id': 'P20240429114437478' |
| 108 | + } |
| 109 | + ad_serve_response = requests.post(self.ad_server_host + '/adserve', params=query_params_ad_serve, json=ad_serve_payload) |
| 110 | + |
| 111 | + |
| 112 | + query_params_deactivate_ad = { |
| 113 | + 'ad_id': ad_id, |
| 114 | + 'state': 'INACTIVE' |
| 115 | + } |
| 116 | + |
| 117 | + ad_update_response = requests.patch(self.ad_manager_host + ad_creation_endpoint, params=query_params_deactivate_ad) |
| 118 | + self.assertEqual(ad_update_response.status_code, 200, f"Expected status code 200, but got {ad_update_response.status_code}") |
| 119 | + |
| 120 | + time.sleep(5) |
| 121 | + cache_response = requests.get(self.ad_manager_host + cache_refresh_endpoint + '/ads') |
| 122 | + self.assertEqual(cache_response.status_code, 200, f"Expected status code 200, but got {cache_response.status_code}") |
| 123 | + cache_response_data = cache_response.json() |
| 124 | + self.assertTrue("C20240428150217896" not in cache_response_data, f"Expected adid {ad_id} to be inactive, but found in cache") |
| 125 | + |
| 126 | + self.assertEqual(ad_serve_response.status_code, 200, f"Expected status code 200, but got {ad_serve_response.status_code}") |
| 127 | + self.assertEqual(ad_serve_response.json()['bid'][0]['adid'], ad_id, f"Expected adid {ad_id} not present in response") |
| 128 | + |
| 129 | + clickURL = ad_serve_response.json()['bid'][0]['ext']['clickUrl'] |
| 130 | + renderURL = ad_serve_response.json()['bid'][0]['ext']['renderUrl'] |
| 131 | + |
| 132 | + click_response = requests.get(clickURL) |
| 133 | + self.assertEqual(click_response.status_code, 200, f"Expected status code 200, but got {click_response.status_code}") |
| 134 | + |
| 135 | + render_response = requests.get(renderURL) |
| 136 | + self.assertEqual(render_response.status_code, 200, f"Expected status code 200, but got {render_response.status_code}") |
| 137 | + |
| 138 | + time.sleep(5) |
| 139 | + report_response = requests.get(self.ad_manager_host + '/reports/' + ad_id) |
| 140 | + self.assertEqual(report_response.status_code, 200, f"Expected status code 200, but got {report_response.status_code}") |
| 141 | + |
| 142 | + report_response = report_response.json() |
| 143 | + print(report_response) |
| 144 | + self.assertEqual(report_response[0]['_id'], ad_id, f"Expected adid {ad_id} not present in response") |
| 145 | + self.assertEqual(report_response[0]['click'], 1, f"Expected clicks 1, but got {report_response[0]['click']}") |
| 146 | + self.assertEqual(report_response[0]['render'], 1, f"Expected impressions 1, but got {report_response[0]['render']}") |
| 147 | + |
| 148 | + |
0 commit comments