|
| 1 | +import json |
| 2 | +import unittest |
| 3 | +from urllib.parse import urlparse, parse_qs |
| 4 | +from pytest_httpserver import HTTPServer |
| 5 | +from linebot.v3.messaging import ( |
| 6 | + Configuration, |
| 7 | + ApiClient, |
| 8 | + MessagingApi, |
| 9 | + CouponCreateRequest, |
| 10 | + LotteryAcquisitionConditionRequest, |
| 11 | + CouponDiscountRewardRequest, |
| 12 | + DiscountFixedPriceInfoRequest, |
| 13 | + CouponMessage |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class TestCouponAPI(unittest.TestCase): |
| 18 | + |
| 19 | + def test_coupon_create(self): |
| 20 | + expected_coupon_id = "COUPON123" |
| 21 | + expected_response_create = { |
| 22 | + "couponId": expected_coupon_id |
| 23 | + } |
| 24 | + expected_body = { |
| 25 | + "acquisitionCondition": { |
| 26 | + "type": "lottery", |
| 27 | + "lotteryProbability": 50, |
| 28 | + "maxAcquireCount": 1000 |
| 29 | + }, |
| 30 | + "barcodeImageUrl": "https://example.com/barcode.png", |
| 31 | + "couponCode": "UNIQUECODE123", |
| 32 | + "description": "Get 100 Yen off your purchase", |
| 33 | + "endTimestamp": 1700000000, |
| 34 | + "imageUrl": "https://example.com/image.png", |
| 35 | + "maxUseCountPerTicket": 1, |
| 36 | + "startTimestamp": 1600000000, |
| 37 | + "title": "100 Yen OFF", |
| 38 | + "usageCondition": "Minimum purchase of 500 Yen", |
| 39 | + "reward": { |
| 40 | + "type": "discount", |
| 41 | + "priceInfo": { |
| 42 | + "type": "fixed", |
| 43 | + "fixedAmount": 100 |
| 44 | + } |
| 45 | + }, |
| 46 | + "visibility": "PUBLIC", |
| 47 | + "timezone": "ASIA_TOKYO" |
| 48 | + } |
| 49 | + |
| 50 | + with HTTPServer() as httpserver: |
| 51 | + httpserver.expect_request( |
| 52 | + uri="/v2/bot/coupon", |
| 53 | + method="POST", |
| 54 | + ).respond_with_json( |
| 55 | + expected_response_create, |
| 56 | + status=200 |
| 57 | + ) |
| 58 | + |
| 59 | + configuration = Configuration( |
| 60 | + access_token="dummy-channel-access-token", |
| 61 | + host=httpserver.url_for("/") |
| 62 | + ) |
| 63 | + |
| 64 | + with ApiClient(configuration) as api_client: |
| 65 | + line_bot_api = MessagingApi(api_client) |
| 66 | + |
| 67 | + req = CouponCreateRequest( |
| 68 | + acquisitionCondition=LotteryAcquisitionConditionRequest( |
| 69 | + lotteryProbability=50, |
| 70 | + maxAcquireCount=1000 |
| 71 | + ), |
| 72 | + barcodeImageUrl="https://example.com/barcode.png", |
| 73 | + couponCode="UNIQUECODE123", |
| 74 | + description="Get 100 Yen off your purchase", |
| 75 | + endTimestamp=1700000000, |
| 76 | + imageUrl="https://example.com/image.png", |
| 77 | + maxUseCountPerTicket=1, |
| 78 | + startTimestamp=1600000000, |
| 79 | + title="100 Yen OFF", |
| 80 | + usageCondition="Minimum purchase of 500 Yen", |
| 81 | + reward=CouponDiscountRewardRequest( |
| 82 | + priceInfo=DiscountFixedPriceInfoRequest( |
| 83 | + fixedAmount=100 |
| 84 | + ) |
| 85 | + ), |
| 86 | + visibility="PUBLIC", |
| 87 | + timezone="ASIA_TOKYO" |
| 88 | + ) |
| 89 | + |
| 90 | + response = line_bot_api.create_coupon(coupon_create_request=req) |
| 91 | + |
| 92 | + self.assertEqual(response.coupon_id, expected_coupon_id) |
| 93 | + self.assertEqual(len(httpserver.log), 1) |
| 94 | + |
| 95 | + request, _ = httpserver.log[0] |
| 96 | + got_body = json.loads(request.data.decode('utf-8')) |
| 97 | + self.assertEqual(got_body, expected_body) |
| 98 | + |
| 99 | + def test_coupon_close(self): |
| 100 | + expected_coupon_id = "COUPON123" |
| 101 | + |
| 102 | + with HTTPServer() as httpserver: |
| 103 | + httpserver.expect_request( |
| 104 | + uri=f"/v2/bot/coupon/{expected_coupon_id}/close", |
| 105 | + method="PUT", |
| 106 | + ).respond_with_json({}, status=200) |
| 107 | + |
| 108 | + configuration = Configuration( |
| 109 | + access_token="dummy-channel-access-token", |
| 110 | + host=httpserver.url_for("/") |
| 111 | + ) |
| 112 | + |
| 113 | + with ApiClient(configuration) as api_client: |
| 114 | + line_bot_api = MessagingApi(api_client) |
| 115 | + response = line_bot_api.close_coupon(expected_coupon_id) |
| 116 | + |
| 117 | + self.assertIsNone(response) |
| 118 | + self.assertEqual(len(httpserver.log), 1) |
| 119 | + |
| 120 | + def test_list_coupon(self): |
| 121 | + expected_response_list = { |
| 122 | + "items": [ |
| 123 | + {"couponId": "COUPON123", "title": "Discount Coupon"}, |
| 124 | + {"couponId": "COUPON456", "title": "Special Offer"} |
| 125 | + ], |
| 126 | + "next": "nextPageToken" |
| 127 | + } |
| 128 | + |
| 129 | + with HTTPServer() as httpserver: |
| 130 | + httpserver.expect_request( |
| 131 | + uri="/v2/bot/coupon", |
| 132 | + method="GET", |
| 133 | + ).respond_with_json( |
| 134 | + expected_response_list, |
| 135 | + status=200 |
| 136 | + ) |
| 137 | + |
| 138 | + configuration = Configuration( |
| 139 | + access_token="dummy-channel-access-token", |
| 140 | + host=httpserver.url_for("/") |
| 141 | + ) |
| 142 | + |
| 143 | + with ApiClient(configuration) as api_client: |
| 144 | + line_bot_api = MessagingApi(api_client) |
| 145 | + response = line_bot_api.list_coupon( |
| 146 | + status=["RUNNING", "CLOSED"], |
| 147 | + limit=10, |
| 148 | + ) |
| 149 | + |
| 150 | + self.assertEqual(len(response.items), 2) |
| 151 | + self.assertEqual(response.items[0].coupon_id, "COUPON123") |
| 152 | + self.assertEqual(response.items[0].title, "Discount Coupon") |
| 153 | + self.assertEqual(response.items[1].coupon_id, "COUPON456") |
| 154 | + self.assertEqual(response.items[1].title, "Special Offer") |
| 155 | + self.assertEqual(response.next, expected_response_list["next"]) |
| 156 | + self.assertEqual(len(httpserver.log), 1) |
| 157 | + |
| 158 | + request, _ = httpserver.log[0] |
| 159 | + parsed_url = urlparse(request.url) |
| 160 | + query_params = parse_qs(parsed_url.query) |
| 161 | + |
| 162 | + self.assertIn("status", query_params) |
| 163 | + self.assertIn("limit", query_params) |
| 164 | + self.assertEqual(query_params["status"], ["RUNNING", "CLOSED"]) |
| 165 | + self.assertEqual(query_params["limit"], ["10"]) |
| 166 | + |
| 167 | + def test_get_coupon_detail(self): |
| 168 | + expected_coupon_id = "COUPON123" |
| 169 | + expected_response_detail = { |
| 170 | + "couponId": expected_coupon_id, |
| 171 | + "title": "Discount Coupon", |
| 172 | + "status": "RUNNING" |
| 173 | + } |
| 174 | + |
| 175 | + with HTTPServer() as httpserver: |
| 176 | + httpserver.expect_request( |
| 177 | + uri=f"/v2/bot/coupon/{expected_coupon_id}", |
| 178 | + method="GET", |
| 179 | + ).respond_with_json( |
| 180 | + expected_response_detail, |
| 181 | + status=200 |
| 182 | + ) |
| 183 | + |
| 184 | + configuration = Configuration( |
| 185 | + access_token="dummy-channel-access-token", |
| 186 | + host=httpserver.url_for("/") |
| 187 | + ) |
| 188 | + |
| 189 | + with ApiClient(configuration) as api_client: |
| 190 | + line_bot_api = MessagingApi(api_client) |
| 191 | + response = line_bot_api.get_coupon_detail(expected_coupon_id) |
| 192 | + |
| 193 | + self.assertEqual(response.coupon_id, expected_coupon_id) |
| 194 | + self.assertEqual(response.title, expected_response_detail["title"]) |
| 195 | + self.assertEqual(len(httpserver.log), 1) |
| 196 | + |
| 197 | + def test_coupon_message(self): |
| 198 | + expected_coupon_id = "COUPON123" |
| 199 | + coupon_message = CouponMessage(couponId=expected_coupon_id) |
| 200 | + self.assertEqual(coupon_message.to_dict()["couponId"], expected_coupon_id) |
| 201 | + self.assertEqual(coupon_message.to_dict()["type"], "coupon") |
| 202 | + |
| 203 | + |
| 204 | +if __name__ == '__main__': |
| 205 | + unittest.main() |
0 commit comments