require './test/test_helper' class FavoriteTest < Minitest::Test def self.setup_favorites VCR.use_cassette('favorite one of everything') do @offer = Access::Offer.search(member_key: 'API_TEST', access_token: 'token_without_filters_with_program_offers', query: '_exists_:program_keys', all_offers: 'true').offers.first # make sure there is a member that exists first Access::Redeem.redeem_offer(@offer.offer_key, @offer.redemption_methods.first, member_key: "API_TEST", access_timeout: 10) # make sure there is an offer favorited first Access::Favorite.create_offer @offer.offer_key, member_key: 'API_TEST' Access::Favorite.create_location @offer.location.location_key, member_key: 'API_TEST' Access::Favorite.create_store @offer.store.store_key, member_key: 'API_TEST' end @offer end @@offer = FavoriteTest.setup_favorites def test_favorite_search VCR.use_cassette('favorite search') do request = Access::Favorite.search member_key: 'API_TEST' assert request.success assert_kind_of Access::FavoriteResponse, request assert_kind_of Array, request.favorites assert request.favorites.map(&:favoriteType).uniq.count > 1, 'it should have more than one favorite type (offers/stores/locations)' assert_kind_of Access::Favorite, request.favorites.first end end def test_favorite_resource_links_to_the_object favorite = Access::Favorite.new favoriteType: 'location', favorited_date: Time.now, location: {location_key: 123} assert_equal favorite.resource, favorite.location end def test_favorite_search_with_bad_member VCR.use_cassette('favorite search bad member') do request = Access::Favorite.search member_key: 'API_TEST_FAVORITE_BAD_MEMBER' refute request.success assert_equal 400, request.status assert_kind_of Access::FavoriteResponse, request refute_nil request.error assert_kind_of Access::Error, request.error end end def test_favorite_search_with_no_favorites # TODO refactor after fav api update VCR.use_cassette('favorite search no favorites') do request = Access::Favorite.search member_key: 'API_TEST_FAVORITE_NO_FAVORITES' refute request.success assert_kind_of Access::FavoriteResponse, request refute_nil request.error assert_kind_of Access::Error, request.error end end def test_favorite_search_offers VCR.use_cassette('favorite search offers') do request = Access::Favorite.search_offers member_key: 'ALD12345' assert_good_favorite_offer_response(request) end end def test_favorite_search_locations VCR.use_cassette('favorite search locations') do request = Access::Favorite.search_locations member_key: 'ALD12345' assert_good_favorite_location_response(request) end end def test_favorite_search_stores VCR.use_cassette('favorite search stores') do request = Access::Favorite.search_stores member_key: 'ALD12345' assert_good_favorite_store_response(request) end end def test_favorite_find_offer VCR.use_cassette('favorite find offer') do Access::Favorite.create_offer @@offer.offer_key, member_key: 'API_TEST' # create it first, in case it was deleted request = Access::Favorite.find_offer @@offer.offer_key, member_key: 'API_TEST' assert_good_favorite_offer_response(request) end end def test_favorite_find_location VCR.use_cassette('favorite find location') do Access::Favorite.create_location @@offer.location.location_key, member_key: 'API_TEST' # create it first, in case it was deleted request = Access::Favorite.find_location @@offer.location.location_key, member_key: 'API_TEST' assert_good_favorite_location_response(request) end end def test_favorite_find_store VCR.use_cassette('favorite find store') do Access::Favorite.create_store @@offer.store.store_key, member_key: 'API_TEST' # create it first, in case it was deleted request = Access::Favorite.find_store @@offer.store.store_key, member_key: 'API_TEST' assert_good_favorite_store_response(request) end end def test_favorite_create_offer VCR.use_cassette('favorite create offer') do request = Access::Favorite.create_offer @@offer.offer_key, member_key: 'API_TEST' assert_good_favorite_offer_response(request) end end def test_favorite_create_location VCR.use_cassette('favorite create location') do request = Access::Favorite.create_location @@offer.location.location_key, member_key: 'API_TEST' assert_good_favorite_location_response(request) end end def test_favorite_create_store VCR.use_cassette('favorite create store') do request = Access::Favorite.create_store @@offer.store.store_key, member_key: 'API_TEST' assert_good_favorite_store_response(request) end end def test_favorite_delete_offer VCR.use_cassette('favorite delete offer') do Access::Favorite.create_offer @@offer.offer_key, member_key: 'API_TEST' # create it first, in case it was deleted request = Access::Favorite.delete_offer @@offer.offer_key, member_key: 'API_TEST' assert request.success assert_kind_of Access::FavoriteResponse, request end end def test_favorite_delete_location VCR.use_cassette('favorite delete location') do Access::Favorite.delete_location @@offer.location.location_key, member_key: 'API_TEST' # create it first, in case it was deleted request = Access::Favorite.find_location @@offer.location.location_key, member_key: 'API_TEST' assert request.success assert_kind_of Access::FavoriteResponse, request end end def test_favorite_delete_store VCR.use_cassette('favorite delete store') do Access::Favorite.delete_store @@offer.store.store_key, member_key: 'API_TEST' # create it first, in case it was deleted request = Access::Favorite.find_store @@offer.store.store_key, member_key: 'API_TEST' assert request.success assert_kind_of Access::FavoriteResponse, request end end def assert_good_favorite_offer_response(request) assert request.success assert_kind_of Access::FavoriteResponse, request assert_kind_of Array, request.favorites assert_equal 1, request.favorites.map(&:favoriteType).uniq.count assert_equal 'offer', request.favorites.map(&:favoriteType).uniq.first assert_kind_of Access::Favorite, request.favorites.first end def assert_good_favorite_location_response(request) assert request.success assert_kind_of Access::FavoriteResponse, request assert_kind_of Array, request.favorites assert_equal 1, request.favorites.map(&:favoriteType).uniq.count assert_equal 'location', request.favorites.map(&:favoriteType).uniq.first assert_kind_of Access::Favorite, request.favorites.first end def assert_good_favorite_store_response(request) assert request.success assert_kind_of Access::FavoriteResponse, request assert_kind_of Array, request.favorites assert_equal 1, request.favorites.map(&:favoriteType).uniq.count assert_equal 'store', request.favorites.map(&:favoriteType).uniq.first assert_kind_of Access::Favorite, request.favorites.first end end