require "test_helper"
class LinkshareCouponAPITest < Test::Unit::TestCase
def test_coupon_web_service_invalid_token
LinkshareAPI.token = nil
assert_raise LinkshareAPI::AuthenticationError do
LinkshareAPI.coupon_web_service(network: 1, mid: 38605)
end
end
def test_coupon_web_service_internal_error
LinkshareAPI.token = token
xml_response = <<-XML
10
Internal Error Unable To Process Request At This Time
XML
stub_request(
:get,
"http://couponfeed.linksynergy.com/coupon?network=1&token=#{token}"
).
to_return(
status: 200,
body: xml_response,
headers: { "Content-type" => "text/xml; charset=UTF-8" }
)
e = assert_raise LinkshareAPI::InvalidRequestError do
LinkshareAPI.coupon_web_service(network: 1)
end
assert_equal 10, e.code
assert_equal "Internal Error Unable To Process Request At This Time", e.message
end
def test_coupon_web_service_usage_limited
LinkshareAPI.token = token
xml_response = <<-XML
30
Usage Quota Exceeded
XML
stub_request(
:get,
"http://couponfeed.linksynergy.com/coupon?network=1&token=#{token}"
).
to_return(
status: 200,
body: xml_response,
headers: { "Content-type" => "text/xml; charset=UTF-8" }
)
e = assert_raise LinkshareAPI::InvalidRequestError do
LinkshareAPI.coupon_web_service(network: 1)
end
assert_equal 30, e.code
assert_equal "Usage Quota Exceeded", e.message
end
def test_coupon_web_service_invalid_or_missing_parameters
LinkshareAPI.token = token
xml_response = <<-XML
40
Invalid Request
XML
stub_request(
:get,
"http://couponfeed.linksynergy.com/coupon?network=15&token=#{token}"
).
to_return(
status: 200,
body: xml_response,
headers: { "Content-type" => "text/xml; charset=UTF-8" }
)
e = assert_raise LinkshareAPI::InvalidRequestError do
LinkshareAPI.coupon_web_service(network: 15)
end
assert_equal 40, e.code
assert_equal "Invalid Request", e.message
end
def test_coupon_web_service_invalid_or_not_approved_token
LinkshareAPI.token = token
xml_response = <<-XML
20
Access Denied Token ID Is Invalid or Not Approved for Coupon Feed
XML
stub_request(
:get,
"http://couponfeed.linksynergy.com/coupon?network=15&token=#{token}"
).
to_return(
status: 200,
body: xml_response,
headers: { "Content-type" => "text/xml; charset=UTF-8" }
)
e = assert_raise LinkshareAPI::InvalidRequestError do
LinkshareAPI.coupon_web_service(network: 15)
end
assert_equal 20, e.code
assert_equal "Access Denied Token ID Is Invalid or Not Approved for Coupon Feed", e.message
end
def test_coupon_web_service_with_no_results
LinkshareAPI.token = token
xml_response = <<-XML
0
0
1
XML
stub_request(
:get,
"http://couponfeed.linksynergy.com/coupon?network=1&promotiontype=30&token=#{token}"
).
to_return(
status: 200,
body: xml_response,
headers: { "Content-type" => "text/xml; charset=UTF-8" }
)
response = LinkshareAPI.coupon_web_service(network: 1, promotiontype: 30)
assert_equal 0, response.total_matches
assert_equal 0, response.total_pages
assert_equal 1, response.page_number
assert_equal [], response.data
end
def test_with_valid_results
LinkshareAPI.token = token
xml_response = <<-XML.strip
1
1
1
computers
electronics
gifts
percentage off
15 percent off
2009-04-01
2009-05-31
KJEISLD
New Customers Only
http://click.linksynergy.com/fs-bin/click?id=XXXXXXXXXXX&offerid=164317.10002595&type=4&subid=0
http://ad.linksynergy.com/fs-bin/show?id=XXXXXXXXXXX&bids=164317.10002595&type=4&subid=0
000
Sample Advertiser Name
Linkshare Network
computers
electronics
gifts
percentage off
10 percent off
2009-04-01
2009-05-31
KJEISLD
New Customers Only
http://click.linksynergy.com/fs-bin/click?id=XXXXXXXXXXX&offerid=164317.10002595&type=4&subid=0
http://ad.linksynergy.com/fs-bin/show?id=XXXXXXXXXXX&bids=164317.10002595&type=4&subid=0
000
Sample Advertiser Name
Linkshare Network
XML
stub_request(
:get,
"http://couponfeed.linksynergy.com/coupon?network=1&promotiontype=22&token=#{token}"
).
to_return(
status: 200,
body: xml_response,
headers: { "Content-type" => "text/xml; charset=UTF-8" }
)
response = LinkshareAPI.coupon_web_service(network: 1, promotiontype: 22)
assert_equal 1, response.total_matches
assert_equal 1, response.total_pages
assert_equal 1, response.page_number
assert_equal "KJEISLD", response.data.first.couponcode
assert_equal "10 percent off", response.data.last.offerdescription
end
def test_all_with_valid_results
LinkshareAPI.token = token
xml_response1 = <<-XML.strip
6
3
1
computers
electronics
gifts
percentage off
15 percent off
2009-04-01
2009-05-31
KJEISLD
New Customers Only
http://click.linksynergy.com/fs-bin/click?id=XXXXXXXXXXX&offerid=164317.10002595&type=4&subid=0
http://ad.linksynergy.com/fs-bin/show?id=XXXXXXXXXXX&bids=164317.10002595&type=4&subid=0
000
Sample Advertiser Name
Linkshare Network
computers
electronics
gifts
percentage off
10 percent off
2009-04-01
2009-05-31
KJEISLD
New Customers Only
http://click.linksynergy.com/fs-bin/click?id=XXXXXXXXXXX&offerid=164317.10002595&type=4&subid=0
http://ad.linksynergy.com/fs-bin/show?id=XXXXXXXXXXX&bids=164317.10002595&type=4&subid=0
000
Sample Advertiser Name
Linkshare Network
XML
xml_response2 = <<-XML.strip
6
3
2
computers
electronics
gifts
percentage off
15 percent off
2009-04-01
2009-05-31
KJEISLD
New Customers Only
http://click.linksynergy.com/fs-bin/click?id=XXXXXXXXXXX&offerid=164317.10002595&type=4&subid=0
http://ad.linksynergy.com/fs-bin/show?id=XXXXXXXXXXX&bids=164317.10002595&type=4&subid=0
000
Sample Advertiser Name
Linkshare Network
computers
electronics
gifts
percentage off
10 percent off
2009-04-01
2009-05-31
KJEISLD
New Customers Only
http://click.linksynergy.com/fs-bin/click?id=XXXXXXXXXXX&offerid=164317.10002595&type=4&subid=0
http://ad.linksynergy.com/fs-bin/show?id=XXXXXXXXXXX&bids=164317.10002595&type=4&subid=0
000
Sample Advertiser Name
Linkshare Network
XML
xml_response3 = <<-XML.strip
6
3
3
computers
electronics
gifts
percentage off
15 percent off
2009-04-01
2009-05-31
KJEISLD
New Customers Only
http://click.linksynergy.com/fs-bin/click?id=XXXXXXXXXXX&offerid=164317.10002595&type=4&subid=0
http://ad.linksynergy.com/fs-bin/show?id=XXXXXXXXXXX&bids=164317.10002595&type=4&subid=0
000
Sample Advertiser Name
Linkshare Network
computers
electronics
gifts
percentage off
10 percent off
2009-04-01
2009-05-31
KIRK
New Customers Only
http://click.linksynergy.com/fs-bin/click?id=XXXXXXXXXXX&offerid=164317.10002595&type=4&subid=0
http://ad.linksynergy.com/fs-bin/show?id=XXXXXXXXXXX&bids=164317.10002595&type=4&subid=0
000
Sample Advertiser Name
Linkshare Network
XML
stub_request(
:get,
"http://couponfeed.linksynergy.com/coupon?network=1&promotiontype=22&token=#{token}"
).
to_return(
status: 200,
body: xml_response1,
headers: { "Content-type" => "text/xml; charset=UTF-8" }
)
stub_request(
:get,
"http://couponfeed.linksynergy.com/coupon?network=1&promotiontype=22&pagenumber=2&token=#{token}"
).
to_return(
status: 200,
body: xml_response2,
headers: { "Content-type" => "text/xml; charset=UTF-8" }
)
stub_request(
:get,
"http://couponfeed.linksynergy.com/coupon?network=1&promotiontype=22&pagenumber=3&token=#{token}"
).
to_return(
status: 200,
body: xml_response3,
headers: { "Content-type" => "text/xml; charset=UTF-8" }
)
data = LinkshareAPI.coupon_web_service(network: 1, promotiontype: 22).all
assert_equal "KJEISLD", data.first.couponcode
assert_equal "KIRK", data.last.couponcode
end
private
def token
"abcdef"
end
end