Skip to content

Commit 7929758

Browse files
committed
Change ActiveAuthorizations to GetAuthorizations and add GetPurchasesByAuthorizationID
Also clean up some clang-tidy warnings.
1 parent 45135c1 commit 7929758

File tree

9 files changed

+87
-28
lines changed

9 files changed

+87
-28
lines changed

datastore.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ Error Datastore::FileLoad() {
8484
// File probably doesn't exist. Check that we can write here.
8585
return WrapError(FileStore(), "f.fail and FileStore failed");
8686
} else if (!f.good()) {
87-
return MakeCriticalError(utils::Stringer("not f.good; errno=", errno).c_str());
87+
return MakeCriticalError(utils::Stringer("not f.good; errno=", errno));
8888
}
8989

9090
try {
9191
f >> json_;
9292
}
9393
catch (json::exception& e) {
94-
return MakeCriticalError(utils::Stringer("json load failed: ", e.what(), "; id:", e.id).c_str());
94+
return MakeCriticalError(utils::Stringer("json load failed: ", e.what(), "; id:", e.id));
9595
}
9696

9797
return nullerr;
@@ -107,14 +107,14 @@ Error Datastore::FileStore() {
107107
ofstream f;
108108
f.open(file_path_, ios::trunc | ios::binary);
109109
if (!f.is_open()) {
110-
return MakeCriticalError(utils::Stringer("not f.is_open; errno=", errno).c_str());
110+
return MakeCriticalError(utils::Stringer("not f.is_open; errno=", errno));
111111
}
112112

113113
try {
114114
f << json_;
115115
}
116116
catch (json::exception& e) {
117-
return MakeCriticalError(utils::Stringer("json dump failed: ", e.what(), "; id:", e.id).c_str());
117+
return MakeCriticalError(utils::Stringer("json dump failed: ", e.what(), "; id:", e.id));
118118
}
119119

120120
return nullerr;

datastore_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ using json = nlohmann::json;
3131
class TestDatastore : public ::testing::Test, public TempDir
3232
{
3333
public:
34-
TestDatastore() {}
34+
TestDatastore() = default;
3535
};
3636

3737
TEST_F(TestDatastore, InitSimple)

datetime.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ using namespace std;
3434
namespace psicash {
3535
namespace datetime {
3636

37-
const TimePoint kTimePointZero = TimePoint();
37+
static const TimePoint kTimePointZero = TimePoint();
3838

3939
static constexpr const char* ISO8601_FORMAT_STRING = "%FT%TZ";
4040

@@ -83,11 +83,11 @@ DateTime::DateTime(const TimePoint& src)
8383
}
8484

8585
DateTime DateTime::Zero() {
86-
return kTimePointZero;
86+
return DateTime(kTimePointZero);
8787
}
8888

8989
DateTime DateTime::Now() {
90-
return NormalizeTimePoint(Clock::now());
90+
return DateTime(NormalizeTimePoint(Clock::now()));
9191
}
9292

9393
bool DateTime::IsZero() const {
@@ -129,11 +129,11 @@ Duration DateTime::Diff(const DateTime& other) const {
129129
}
130130

131131
DateTime DateTime::Add(const Duration& d) const {
132-
return NormalizeTimePoint(time_point_ + d);
132+
return DateTime(NormalizeTimePoint(time_point_ + d));
133133
}
134134

135135
DateTime DateTime::Sub(const Duration& d) const {
136-
return NormalizeTimePoint(time_point_ - d);
136+
return DateTime(NormalizeTimePoint(time_point_ - d));
137137
}
138138

139139
int64_t DateTime::MillisSinceEpoch() const {
@@ -164,7 +164,7 @@ int64_t DurationToInt64(const Duration& d) {
164164
return d.count();
165165
}
166166

167-
Duration DurationFromInt64(const int64_t d) {
167+
Duration DurationFromInt64(int64_t d) {
168168
return Duration(d);
169169
}
170170

datetime.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class DateTime {
3535
// By default, initializes to the "zero" value.
3636
DateTime();
3737
DateTime(const DateTime& src);
38-
DateTime(const TimePoint& src);
38+
explicit DateTime(const TimePoint& src);
3939
DateTime& operator=(const DateTime&) = default;
4040

4141
static DateTime Zero();
@@ -74,7 +74,7 @@ class DateTime {
7474

7575
// These are intended to help de/serialization of duration values.
7676
int64_t DurationToInt64(const Duration& d);
77-
Duration DurationFromInt64(const int64_t d);
77+
Duration DurationFromInt64(int64_t d);
7878

7979
} // namespace datetime
8080
} // namespace psicash

error.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ Error::Error()
3030
: is_error_(false), critical_(false) {
3131
}
3232

33-
Error::Error(const Error& src)
34-
: is_error_(src.is_error_), critical_(src.critical_), stack_(src.stack_) {
35-
}
36-
3733
Error::Error(bool critical, const std::string& message, const std::string& filename,
3834
const std::string& function, int line)
3935
: is_error_(true), critical_(critical) {

error.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace error {
4040
class Error {
4141
public:
4242
Error();
43-
Error(const Error& src);
43+
Error(const Error& src) = default;
4444
Error(bool critical, const std::string& message,
4545
const std::string& filename, const std::string& function, int line);
4646
Error& operator=(const Error&) = default;
@@ -90,8 +90,8 @@ const Error nullerr;
9090
// Should be prefixed with namespaces: psicash::error::
9191
#define MakeNoncriticalError(message) Error(false, (message), __FILE__, __PRETTY_FUNCTION__, __LINE__)
9292
#define MakeCriticalError(message) Error(true, (message), __FILE__, __PRETTY_FUNCTION__, __LINE__)
93-
#define WrapError(err, message) (err.Wrap((message), __FILE__, __PRETTY_FUNCTION__, __LINE__))
94-
#define PassError(err) (err.Wrap(__FILE__, __PRETTY_FUNCTION__, __LINE__))
93+
#define WrapError(err, message) ((err).Wrap((message), __FILE__, __PRETTY_FUNCTION__, __LINE__))
94+
#define PassError(err) ((err).Wrap(__FILE__, __PRETTY_FUNCTION__, __LINE__))
9595

9696

9797
/*

psicash.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <sstream>
2323
#include <chrono>
2424
#include <thread>
25+
#include <algorithm>
2526
#include "psicash.hpp"
2627
#include "userdata.hpp"
2728
#include "datetime.hpp"
@@ -177,16 +178,29 @@ Purchases PsiCash::ActivePurchases() const {
177178
return res;
178179
}
179180

180-
Authorizations PsiCash::ActiveAuthorizations() const {
181+
Authorizations PsiCash::GetAuthorizations(bool activeOnly/*=false*/) const {
181182
Authorizations res;
182183
for (const auto& p : user_data_->GetPurchases()) {
183-
if (!IsExpired(p) && p.authorization) {
184+
if (p.authorization && (!activeOnly || !IsExpired(p))) {
184185
res.push_back(*p.authorization);
185186
}
186187
}
187188
return res;
188189
}
189190

191+
Purchases PsiCash::GetPurchasesByAuthorizationID(std::vector<std::string> authorization_ids) const {
192+
auto purchases = user_data_->GetPurchases();
193+
194+
auto new_end = std::remove_if(purchases.begin(), purchases.end(), [&authorization_ids](const Purchase& p){
195+
return !p.authorization
196+
|| std::find(authorization_ids.begin(), authorization_ids.end(), p.authorization->id) == authorization_ids.end();
197+
});
198+
199+
purchases.erase(new_end, purchases.end());
200+
201+
return purchases;
202+
}
203+
190204
optional<Purchase> PsiCash::NextExpiringPurchase() const {
191205
optional<Purchase> next;
192206
for (const auto& p : user_data_->GetPurchases()) {

psicash.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,12 @@ class PsiCash {
203203
/// Returns the set of active purchases that are not expired, if any.
204204
Purchases ActivePurchases() const;
205205

206-
/// Returns the set of active (non-expired) purchase authorizations, if any.
207-
Authorizations ActiveAuthorizations() const;
206+
/// Returns all purchase authorizations. If activeOnly is true, only authorizations
207+
/// for non-expired purchases will be returned.
208+
Authorizations GetAuthorizations(bool activeOnly=false) const;
209+
210+
/// Returns all purchases that match the given set of Authorization IDs.
211+
Purchases GetPurchasesByAuthorizationID(std::vector<std::string> authorization_ids) const;
208212

209213
/// Get the next expiring purchase (with local_time_expiry populated).
210214
/// The returned optional will false if there is no outstanding expiring purchase (or

psicash_test.cpp

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ TEST_F(TestPsiCash, DecodeAuthorization) {
388388
ASSERT_TRUE(auth_res_fail.error().Critical());
389389
}
390390

391-
TEST_F(TestPsiCash, ActiveAuthorizations) {
391+
TEST_F(TestPsiCash, GetAuthorizations) {
392392
PsiCashTester pc;
393393
auto err = pc.Init(user_agent_, GetTempDir().c_str(), nullptr, true);
394394
ASSERT_FALSE(err);
@@ -399,7 +399,10 @@ TEST_F(TestPsiCash, ActiveAuthorizations) {
399399
purchases = pc.ActivePurchases();
400400
ASSERT_EQ(purchases.size(), 0);
401401

402-
auto v = pc.ActiveAuthorizations();
402+
auto v = pc.GetAuthorizations(false);
403+
ASSERT_EQ(v.size(), 0);
404+
405+
v = pc.GetAuthorizations(true);
403406
ASSERT_EQ(v.size(), 0);
404407

405408
auto before_now = datetime::DateTime::Now().Sub(datetime::Duration(54321));
@@ -421,9 +424,51 @@ TEST_F(TestPsiCash, ActiveAuthorizations) {
421424
ASSERT_FALSE(err);
422425
ASSERT_EQ(pc.GetPurchases().size(), purchases.size());
423426

424-
v = pc.ActiveAuthorizations();
427+
v = pc.GetAuthorizations(false);
428+
ASSERT_EQ(v.size(), 2);
429+
ASSERT_THAT(v, AnyOf(Contains(*auth_res1), Contains(*auth_res2)));
430+
431+
v = pc.GetAuthorizations(true);
425432
ASSERT_EQ(v.size(), 1);
426-
ASSERT_EQ(v[0].encoded, encoded2);
433+
ASSERT_THAT(v, Contains(*auth_res2));
434+
}
435+
436+
TEST_F(TestPsiCash, GetPurchasesByAuthorizationID) {
437+
PsiCashTester pc;
438+
auto err = pc.Init(user_agent_, GetTempDir().c_str(), nullptr, true);
439+
ASSERT_FALSE(err);
440+
441+
auto purchases = pc.GetPurchases();
442+
ASSERT_EQ(purchases.size(), 0);
443+
444+
// Empty set of auth IDs
445+
purchases = pc.GetPurchasesByAuthorizationID({});
446+
ASSERT_EQ(purchases.size(), 0);
447+
448+
auto before_now = datetime::DateTime::Now().Sub(datetime::Duration(54321));
449+
auto after_now = datetime::DateTime::Now().Add(datetime::Duration(54321));
450+
451+
const auto encoded1 = "eyJBdXRob3JpemF0aW9uIjp7IklEIjoiMFYzRXhUdmlBdFNxTGZOd2FpQXlHNHpaRUJJOGpIYnp5bFdNeU5FZ1JEZz0iLCJBY2Nlc3NUeXBlIjoic3BlZWQtYm9vc3QtdGVzdCIsIkV4cGlyZXMiOiIyMDE5LTAxLTE0VDE3OjIyOjIzLjE2ODc2NDEyOVoifSwiU2lnbmluZ0tleUlEIjoiUUNZTzV2clIvZGhjRDZ6M2FMQlVNeWRuZlJyZFNRL1RWYW1IUFhYeTd0TT0iLCJTaWduYXR1cmUiOiJQL2NrenloVUJoSk5RQ24zMnluM1VTdGpLencxU04xNW9MclVhTU9XaW9scXBOTTBzNVFSNURHVEVDT1FzQk13ODdQdTc1TGE1OGtJTHRIcW1BVzhDQT09In0=";
452+
const auto encoded2 = "eyJBdXRob3JpemF0aW9uIjp7IklEIjoibFRSWnBXK1d3TFJqYkpzOGxBUFVaQS8zWnhmcGdwNDFQY0dkdlI5a0RVST0iLCJBY2Nlc3NUeXBlIjoic3BlZWQtYm9vc3QtdGVzdCIsIkV4cGlyZXMiOiIyMDE5LTAxLTE0VDIxOjQ2OjMwLjcxNzI2NTkyNFoifSwiU2lnbmluZ0tleUlEIjoiUUNZTzV2clIvZGhjRDZ6M2FMQlVNeWRuZlJyZFNRL1RWYW1IUFhYeTd0TT0iLCJTaWduYXR1cmUiOiJtV1Z5Tm9ZU0pFRDNXU3I3bG1OeEtReEZza1M5ZWlXWG1lcDVvVWZBSHkwVmYrSjZaQW9WajZrN3ZVTDNrakIreHZQSTZyaVhQc3FzWENRNkx0eFdBQT09In0=";
453+
454+
auto auth_res1 = psicash::DecodeAuthorization(encoded1);
455+
auto auth_res2 = psicash::DecodeAuthorization(encoded2);
456+
ASSERT_TRUE(auth_res1);
457+
ASSERT_TRUE(auth_res2);
458+
459+
purchases = {{"future_no_auth", "tc1", "d1", after_now, nonstd::nullopt, nonstd::nullopt},
460+
{"past_auth", "tc2", "d2", before_now, nonstd::nullopt, *auth_res1},
461+
{"future_auth", "tc3", "d3", after_now, nonstd::nullopt, *auth_res2}};
462+
463+
err = pc.user_data().SetPurchases(purchases);
464+
ASSERT_FALSE(err);
465+
ASSERT_EQ(pc.GetPurchases().size(), purchases.size());
466+
467+
// One that matches and some that don't
468+
vector<string> authIDs = {"badid", auth_res2->id, "anotherbadid"};
469+
purchases = pc.GetPurchasesByAuthorizationID(authIDs);
470+
ASSERT_THAT(purchases, SizeIs(1));
471+
ASSERT_EQ(purchases[0].authorization->id, auth_res2->id);
427472
}
428473

429474
TEST_F(TestPsiCash, NextExpiringPurchase) {

0 commit comments

Comments
 (0)