Skip to content

Commit

Permalink
Merge branch 'fix/SDK-3750_fetch_ads_response_parsing_dummy_ad' into …
Browse files Browse the repository at this point in the history
…'develop'

SDK-3750. Investigate failure in SdkTest.FetchAds

Closes SDK-3750

See merge request sdk/sdk!5405
  • Loading branch information
alfredo-mega committed Apr 1, 2024
2 parents b237630 + 2f0fc2b commit 45256c9
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 47 deletions.
1 change: 1 addition & 0 deletions include/mega/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,7 @@ typedef std::function<void(Error, string_map)> CommandFetchAdsCompletion;
class MEGA_API CommandFetchAds : public Command
{
CommandFetchAdsCompletion mCompletion;
std::vector<std::string> mAdUnits;
public:
bool procresult(Result, JSON&) override;

Expand Down
84 changes: 48 additions & 36 deletions src/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10811,49 +10811,61 @@ bool CommandFetchAds::procresult(Command::Result r, JSON& json)
}

bool error = false;

while (json.enterobject() && !error)
for (auto adUnit = std::begin(mAdUnits); adUnit != std::end(mAdUnits) && !error; ++adUnit)
{
std::string id;
std::string iu;
bool exit = false;
while (!exit)
if (json.isnumeric())
{
switch (json.getnameid())
// -9 or any other error for the provided ad unit (error results order must match)
result[*adUnit] = std::to_string(json.getint());
}
else if (json.enterobject())
{
std::string id;
std::string iu;
bool exit = false;
while (!exit)
{
case MAKENAMEID2('i', 'd'):
json.storeobject(&id);
break;
switch (json.getnameid())
{
case MAKENAMEID2('i', 'd'):
json.storeobject(&id);
break;

case MAKENAMEID3('s', 'r', 'c'):
json.storeobject(&iu);
break;
case MAKENAMEID3('s', 'r', 'c'):
json.storeobject(&iu);
break;

case EOO:
exit = true;
if (!id.empty() && !iu.empty())
{
result[id] = iu;
}
else
{
error = true;
result.clear();
}
break;
case EOO:
exit = true;
if (!id.empty() && !iu.empty())
{
assert(id == *adUnit);
result[id] = iu;
}
else
{
error = true;
result.clear();
}
break;

default:
if (!json.storeobject())
{
result.clear();
mCompletion(API_EINTERNAL, result);
return false;
}
break;
default:
if (!json.storeobject())
{
result.clear();
mCompletion(API_EINTERNAL, result);
return false;
}
break;
}
}
json.leaveobject();
}
else
{
result.clear();
error = true;
}

json.leaveobject();
}

mCompletion((error ? API_EINTERNAL : API_OK), result);
Expand All @@ -10862,7 +10874,7 @@ bool CommandFetchAds::procresult(Command::Result r, JSON& json)
}

CommandFetchAds::CommandFetchAds(MegaClient* client, int adFlags, const std::vector<std::string> &adUnits, handle publicHandle, CommandFetchAdsCompletion completion)
: mCompletion(completion)
: mCompletion(completion), mAdUnits(adUnits)
{
cmd("adf");
arg("ad", adFlags);
Expand Down
42 changes: 31 additions & 11 deletions tests/integration/SdkTest_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9343,18 +9343,38 @@ TEST_F(SdkTest, FetchAds)
std::unique_ptr<MegaStringList> stringList = std::unique_ptr<MegaStringList>(MegaStringList::createInstance());
std::unique_ptr<RequestTracker> tr = asyncFetchAds(0, MegaApi::ADS_FORCE_ADS, stringList.get(), INVALID_HANDLE);
ASSERT_EQ(API_EARGS, tr->waitForResult()) << "Fetch Ads succeeded with invalid arguments";
stringList->add("dummyAdUnit");
const std::string dummyAd {"dummyAdUnit"};
stringList->add(dummyAd.c_str());
tr = asyncFetchAds(0, MegaApi::ADS_FORCE_ADS, stringList.get(), INVALID_HANDLE);
ASSERT_EQ(API_ENOENT, tr->waitForResult()) << "Fetch Ads didn't fail when it was expected to (dummy Ad case)";
const MegaStringMap* ads = tr->request->getMegaStringMap();
ASSERT_FALSE(ads) << "Fetch Ads should have been nullptr due to expected error code `request`";
const char valiAdSlot[] = "ANDFB";
stringList.reset(MegaStringList::createInstance());
stringList->add(valiAdSlot);
tr = asyncFetchAds(0, MegaApi::ADS_FORCE_ADS, stringList.get(), INVALID_HANDLE);
ASSERT_EQ(API_ENOENT, tr->waitForResult()) << "Fetch Ads didn't fail when it was expected to (correct Ad case)";
ads = tr->request->getMegaStringMap();
ASSERT_FALSE(ads) << "Fetch Ads should have been nullptr to expected error code in `request`";
const auto ab_adse = megaApi[0]->getABTestValue("adse");
const auto ab_adsi = megaApi[0]->getABTestValue("adsi");
LOG_debug << "Account 0 " << megaApi[0]->getMyUserHandle() << " (" << megaApi[0]->getMyEmail()
<< ") ab_adse: " << ab_adse << " ab_adsi: " << ab_adsi;
const bool isUserAllowedToFetchAds = ab_adse > 0u || ab_adsi > 0u;
if (isUserAllowedToFetchAds)
{
ASSERT_EQ(API_OK, tr->waitForResult()) << "Fetch Ads request failed when it wasn't expected";
ASSERT_TRUE(tr->request);
auto ads = tr->request->getMegaStringMap();
ASSERT_TRUE(ads && ads->size() == 1);
ASSERT_STREQ(ads->get(dummyAd.c_str()), "-9") << "Fetch Ads should have received -9 for dummy Ad case";
}
else
{
ASSERT_EQ(API_ENOENT, tr->waitForResult()) << "Fetch Ads didn't fail when it was expected to (dummy Ad case)";
const MegaStringMap* ads = tr->request->getMegaStringMap();
ASSERT_FALSE(ads)
<< "Fetch Ads should have been nullptr due to expected error code `request`";
const char valiAdSlot[] = "ANDFB";
stringList.reset(MegaStringList::createInstance());
stringList->add(valiAdSlot);
tr = asyncFetchAds(0, MegaApi::ADS_FORCE_ADS, stringList.get(), INVALID_HANDLE);
ASSERT_EQ(API_ENOENT, tr->waitForResult())
<< "Fetch Ads didn't fail when it was expected to (correct Ad case)";
ads = tr->request->getMegaStringMap();
ASSERT_FALSE(ads)
<< "Fetch Ads should have been nullptr to expected error code in `request`";
}

// TODO: LOG_debug << "\t# Test suite 2: Fetching ads with containing-ads account";
}
Expand Down

0 comments on commit 45256c9

Please sign in to comment.