426 lines
32 KiB
C++
426 lines
32 KiB
C++
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
// Nostro.App without nostrod: how money, days and the bank's capitals are worded (in a fixed time zone, so the
|
||
|
|
// expectations hold anywhere), what each form reads from what was typed and what it refuses, what every error
|
||
|
|
// nostrod can answer with is explained as, and what the app knows of each bank it can connect.
|
||
|
|
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <time.h>
|
||
|
|
|
||
|
|
import std;
|
||
|
|
import Nostro;
|
||
|
|
import Nostro.Ui;
|
||
|
|
import Nostro.App;
|
||
|
|
|
||
|
|
#include "../Support/Check.inc"
|
||
|
|
|
||
|
|
using namespace Nostro;
|
||
|
|
using namespace Nostro::App;
|
||
|
|
using Test::Check;
|
||
|
|
using Test::Equal;
|
||
|
|
using Test::Throws;
|
||
|
|
using Moment = std::chrono::system_clock::time_point;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
constexpr std::string_view iban = "NL91ABNA0417164300";
|
||
|
|
constexpr Currency euro = Currency::Euro;
|
||
|
|
|
||
|
|
void Words() {
|
||
|
|
Equal(GroupIban(iban), std::string("NL91 ABNA 0417 1643 00"), "an IBAN is grouped in fours");
|
||
|
|
for(const std::string_view other : {"", "NL9", "bob@example.org", "nl91abna0417164300", "NL91-ABNA", "N191ABNA", "NLA1ABNA", "NL9AABNA", "Savings"}) {
|
||
|
|
Equal(GroupIban(other), std::string(other), "what is no IBAN is left as it is");
|
||
|
|
}
|
||
|
|
Equal(AccountKey(Account{.name = "Main", .bic = "BUNQNL2A", .iban = std::string(iban), .currency = euro, .balance = 0}), std::string(iban), "an account is found by its IBAN");
|
||
|
|
Equal(AccountKey(Account{.name = "Pocket", .bic = "", .iban = "", .currency = euro, .balance = 0}), std::string("Pocket"), "or by its name where it has none");
|
||
|
|
Equal(PartyName(BunqLabel{.name = "Bob", .iban = std::string(iban)}), std::string("Bob"), "someone is said by name");
|
||
|
|
Equal(PartyName(BunqLabel{.name = "", .iban = std::string(iban)}), std::string("NL91 ABNA 0417 1643 00"), "else by IBAN");
|
||
|
|
Equal(PartyName(BunqLabel{.name = "", .iban = ""}), std::string("Unknown"), "and as unknown where the bank says neither");
|
||
|
|
Equal(PartyName(BunqCounterparty{.type = "IBAN", .value = std::string(iban), .name = "Bob"}), std::string("Bob"), "a payee by the name given");
|
||
|
|
Equal(PartyName(BunqCounterparty{.type = "EMAIL", .value = "bob@example.org", .name = ""}), std::string("bob@example.org"), "else by the address");
|
||
|
|
for(const auto& [minor, said] : std::array<std::pair<std::int64_t, std::string_view>, 7>{{{0, "€0.00"}, {99999, "€999.99"}, {100000, "€1,000.00"}, {-1250000, "-€12,500.00"}, {123456789012, "€1,234,567,890.12"}, {-5, "-€0.05"}, {100000000, "€1,000,000.00"}}}) {
|
||
|
|
Equal(Money(minor, euro), std::string(said), "money has its thousands apart");
|
||
|
|
}
|
||
|
|
Equal(SignedMoney(1250, euro), std::string("+€12.50"), "money that came in has a plus");
|
||
|
|
Equal(SignedMoney(250000, euro), std::string("+€2,500.00"), "and its thousands apart too");
|
||
|
|
Equal(SignedMoney(-300, euro), std::string("-€3.00"), "money that went out a minus");
|
||
|
|
Equal(SignedMoney(0, euro), std::string("€0.00"), "and nothing has neither");
|
||
|
|
|
||
|
|
const Moment now = ParseLocalDate("2026-11-19 15:00");
|
||
|
|
Check(LocalDay(ParseLocalDate("2026-11-19 00:00")) == LocalDay(ParseLocalDate("2026-11-19 23:59")) && LocalDay(ParseLocalDate("2026-11-19 23:59")) != LocalDay(ParseLocalDate("2026-11-20 00:00")), "a day runs from midnight to midnight");
|
||
|
|
Equal(DayLabel(ParseLocalDate("2026-11-19 08:00"), now), std::string("Today"), "today");
|
||
|
|
Equal(DayLabel(ParseLocalDate("2026-11-18 23:59"), now), std::string("Yesterday"), "yesterday");
|
||
|
|
Equal(DayLabel(ParseLocalDate("2026-11-03 12:00"), now), std::string("Tuesday 3 November"), "a day of this year by its name");
|
||
|
|
Equal(DayLabel(ParseLocalDate("2025-12-31 12:00"), now), std::string("Wednesday 31 December 2025"), "one of another year with it");
|
||
|
|
// The night the clocks go back: the day before is 25 hours long.
|
||
|
|
Equal(DayLabel(ParseLocalDate("2026-10-24 23:30"), ParseLocalDate("2026-10-25 00:30")), std::string("Yesterday"), "yesterday across a change of the clocks");
|
||
|
|
Equal(DayLabel(ParseLocalDate("2026-10-25 01:30"), ParseLocalDate("2026-10-26 00:10")), std::string("Yesterday"), "and after it");
|
||
|
|
Equal(ShortDate(ParseLocalDate("2026-11-03"), now), std::string("3 Nov"), "a short date");
|
||
|
|
Equal(ShortDate(ParseLocalDate("2025-12-31"), now), std::string("31 Dec 2025"), "with its year when that is another");
|
||
|
|
Equal(ClockTime(ParseLocalDate("2026-11-03 14:05")), std::string("14:05"), "the time of day");
|
||
|
|
Equal(FullTime(ParseLocalDate("2026-11-03 14:05")), std::string("3 November 2026, 14:05"), "a moment in full");
|
||
|
|
Equal(DateInput(ParseLocalDate("2026-11-03 14:05")), std::string("2026-11-03"), "a day as a field takes it");
|
||
|
|
|
||
|
|
const std::array<std::pair<std::string_view, std::string_view>, 10> kinds = {{
|
||
|
|
{"MASTERCARD/PAYMENT", "Card payment"}, {"EBA_SDD/SDD", "Direct debit"}, {"EBA_SCT/SCT", "Bank transfer"}, {"IDEAL/PAYMENT", "iDEAL"},
|
||
|
|
{"SOFORT/PAYMENT", "Sofort"}, {"SWIFT/PAYMENT", "International transfer"}, {"BUNQ/REQUEST", "Paid request"}, {"BUNQ/PAYMENT", "Payment within the bank"},
|
||
|
|
{"NEW/THING", "NEW/THING"}, {"", ""},
|
||
|
|
}};
|
||
|
|
for(const auto& [kind, label] : kinds) {
|
||
|
|
Equal(KindLabel(kind), std::string(label), "a kind of payment in words");
|
||
|
|
}
|
||
|
|
Equal(StatusLabel("PIN_TRIES_EXCEEDED"), std::string("Pin tries exceeded"), "the bank's capitals as words");
|
||
|
|
Equal(StatusLabel("sandbox"), std::string("Sandbox"), "and its small letters with a capital");
|
||
|
|
Equal(CardProduct("MASTERCARD_DEBIT"), std::string("Mastercard debit"), "a card's product");
|
||
|
|
Equal(CardExpiry("2030-11-30"), std::string("11/30"), "when a card expires, as its face has it");
|
||
|
|
for(const std::string_view other : {"", "11/30", "2030-11-3", "2030/11/30", "2030-11_30", "203O-11-30", "2030-11-30T00"}) {
|
||
|
|
Equal(CardExpiry(other), std::string(other), "what is no day is left as the bank says it");
|
||
|
|
}
|
||
|
|
|
||
|
|
BunqSchedule schedule{.start = ParseLocalDate("2026-11-03")};
|
||
|
|
Equal(ScheduleLabel(schedule, now), std::string("Once on 3 Nov"), "a payment scheduled once");
|
||
|
|
schedule.start = ParseLocalDate("2026-11-03 14:05");
|
||
|
|
Equal(ScheduleLabel(schedule, now), std::string("Once on 3 Nov, 14:05"), "with its time when that is not midnight");
|
||
|
|
schedule.unit = "MONTHLY";
|
||
|
|
schedule.start = ParseLocalDate("2026-11-03");
|
||
|
|
Equal(ScheduleLabel(schedule, now), std::string("Every month from 3 Nov"), "one that repeats");
|
||
|
|
schedule.unit = "WEEKLY";
|
||
|
|
schedule.every = 2;
|
||
|
|
schedule.end = ParseLocalDate("2027-01-01");
|
||
|
|
Equal(ScheduleLabel(schedule, now), std::string("Every 2 weeks from 3 Nov until 1 Jan 2027"), "every so many, until a day");
|
||
|
|
schedule.unit = "FORTNIGHTLY";
|
||
|
|
schedule.end.reset();
|
||
|
|
Equal(ScheduleLabel(schedule, now), std::string("Every 2 Fortnightlys from 3 Nov"), "a unit this does not know is said as bunq says it");
|
||
|
|
for(const auto& [unit, word] : std::array<std::pair<std::string_view, std::string_view>, 3>{{{"HOURLY", "hour"}, {"DAILY", "day"}, {"YEARLY", "year"}}}) {
|
||
|
|
schedule.unit = unit;
|
||
|
|
schedule.every = 1;
|
||
|
|
Equal(ScheduleLabel(schedule, now), std::format("Every {} from 3 Nov", word), "each unit bunq has");
|
||
|
|
}
|
||
|
|
|
||
|
|
Equal(Fingerprint("short"), std::string("••••"), "a short secret shows nothing of itself");
|
||
|
|
Equal(Fingerprint("sandbox_ab12cd34ef56gh78"), std::string("sandbox_…gh78"), "a key shows what it is for and how it ends");
|
||
|
|
Equal(Fingerprint("abcdefghijklmnopqrst"), std::string("abcd…qrst"), "one without a mark its first four");
|
||
|
|
Equal(Fingerprint("abcdefghijklmn_opqrst"), std::string("abcd…qrst"), "and so one whose mark comes late");
|
||
|
|
}
|
||
|
|
|
||
|
|
void Reading() {
|
||
|
|
for(const auto& [text, minor] : std::array<std::pair<std::string_view, std::int64_t>, 6>{{{"12,50", 1250}, {"12.50", 1250}, {" 12 ", 1200}, {"0,05", 5}, {"12.", 1200}, {"999999999999,99", 99999999999999}}}) {
|
||
|
|
const Read<std::int64_t> read = ReadAmount(text, euro);
|
||
|
|
Check(read.value == minor && read.error.empty(), std::format("'{}' is an amount", text));
|
||
|
|
}
|
||
|
|
for(const auto& [text, why] : std::array<std::pair<std::string_view, std::string_view>, 10>{{
|
||
|
|
{"", "Enter an amount"}, {" ", "Enter an amount"}, {"abc", "Not an amount"}, {"-5", "Not an amount"}, {",5", "Not an amount"}, {"1,2,3", "Not an amount"},
|
||
|
|
{"1,234", "At most 2 decimals"}, {"0", "An amount above zero"}, {"0,00", "An amount above zero"}, {"1234567890123", "That is too much"},
|
||
|
|
}}) {
|
||
|
|
const Read<std::int64_t> read = ReadAmount(text, euro);
|
||
|
|
Check(!read.value && read.error.contains(why), std::format("'{}' is refused: {}", text, why));
|
||
|
|
}
|
||
|
|
|
||
|
|
Equal(PartyKind(""), std::string(), "nothing typed is no kind of address yet");
|
||
|
|
Equal(PartyKind(" bob@example.org"), std::string("E-mail address"), "an e-mail address by its @");
|
||
|
|
Equal(PartyKind("+31612345678"), std::string("Phone number"), "a phone number by its +");
|
||
|
|
Equal(PartyKind("NL91"), std::string("IBAN"), "anything else is an IBAN being typed");
|
||
|
|
Read<BunqCounterparty> party = ReadParty(" NL91 ABNA 0417 1643 00 ");
|
||
|
|
Check(party.value && party.value->type == "IBAN" && party.value->value == iban, "an IBAN as it is printed");
|
||
|
|
party = ReadParty("nl91abna0417164300");
|
||
|
|
Check(party.value && party.value->value == iban, "or in small letters");
|
||
|
|
party = ReadParty("bob@example.org");
|
||
|
|
Check(party.value && party.value->type == "EMAIL" && party.value->value == "bob@example.org", "an e-mail address");
|
||
|
|
party = ReadParty("+31 6 1234 5678");
|
||
|
|
Check(party.value && party.value->type == "PHONE_NUMBER" && party.value->value == "+31612345678", "a phone number");
|
||
|
|
for(const auto& [text, why] : std::array<std::pair<std::string_view, std::string_view>, 7>{{
|
||
|
|
{"", "An IBAN, an e-mail address or a phone number"}, {"NL91ABNA0417164301", "Not a valid IBAN"}, {"Bob", "Not a valid IBAN"},
|
||
|
|
{"@example.org", "Not an e-mail address"}, {"bob@", "Not an e-mail address"}, {"bob smith@example.org", "Not an e-mail address"}, {"+31x", "Not a phone number"},
|
||
|
|
}}) {
|
||
|
|
const Read<BunqCounterparty> read = ReadParty(text);
|
||
|
|
Check(!read.value && read.error.contains(why), std::format("'{}' is refused: {}", text, why));
|
||
|
|
}
|
||
|
|
|
||
|
|
Check(ReadDay(" 2026-11-03 ").value == ParseLocalDate("2026-11-03"), "a day is its start");
|
||
|
|
Check(ReadDay("2026-11-03", "14:05").value == ParseLocalDate("2026-11-03 14:05"), "or the time on it");
|
||
|
|
Check(ReadDay("2026-11-03", {}, true).value == ParseLocalDateEnd("2026-11-03"), "or its last moment, for the end of a range");
|
||
|
|
Check(ReadDay("2026-11-03", " 14:05 ", true).value == ParseLocalDate("2026-11-03 14:05"), "a time given is that time either way");
|
||
|
|
for(const auto& [day, time, why] : std::array<std::tuple<std::string_view, std::string_view, std::string_view>, 5>{{
|
||
|
|
{"", "", "A day: YYYY-MM-DD"}, {"2026-13-01", "", "Not a day"}, {"tomorrow", "", "Not a day"}, {"2026-13-01", "10:00", "Not a day"}, {"2026-11-03", "25:00", "Not a time: hh:mm"},
|
||
|
|
}}) {
|
||
|
|
const Read<Moment> read = ReadDay(day, time);
|
||
|
|
Check(!read.value && read.error.contains(why), std::format("'{}' '{}' is refused: {}", day, time, why));
|
||
|
|
}
|
||
|
|
|
||
|
|
Check(ReadCount("3").value == 3u && ReadCount(" 12 ").value == 12u, "a count");
|
||
|
|
for(const std::string_view bad : {"", "0", "x", "1.5", "-1"}) {
|
||
|
|
const Read<std::uint32_t> read = ReadCount(bad);
|
||
|
|
Check(!read.value && read.error == "A whole number, 1 or more", std::format("'{}' is no count", bad));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Paying() {
|
||
|
|
const Moment now = ParseLocalDate("2026-11-19 15:00");
|
||
|
|
PaymentForm form;
|
||
|
|
PaymentOrder order;
|
||
|
|
PaymentForm::Errors errors = form.Check(euro, now, order);
|
||
|
|
Check(errors.Any() && !errors.to.empty() && !errors.amount.empty() && errors.name.empty(), "an empty form lacks who and how much");
|
||
|
|
Check(!PaymentForm::Errors().Any(), "no errors is none");
|
||
|
|
|
||
|
|
form.to.Set("NL91 ABNA 0417 1643 00");
|
||
|
|
form.amount.Set("12,50");
|
||
|
|
form.description.Set(" lunch ");
|
||
|
|
errors = form.Check(euro, now, order);
|
||
|
|
Check(errors.Any() && errors.to.empty() && errors.amount.empty() && errors.name == "The name of the account's holder", "an IBAN needs its holder's name");
|
||
|
|
Equal(order.amount, std::int64_t{0}, "and a form with an error gives no order");
|
||
|
|
form.name.Set(" Bob ");
|
||
|
|
errors = form.Check(euro, now, order);
|
||
|
|
Check(!errors.Any() && order.to.type == "IBAN" && order.to.value == iban && order.to.name == "Bob" && order.amount == 1250 && order.description == "lunch" && order.when == When::Now, "a form filled in is an order");
|
||
|
|
|
||
|
|
form.to.Set("bob@example.org");
|
||
|
|
form.name.Clear();
|
||
|
|
errors = form.Check(euro, now, order);
|
||
|
|
Check(!errors.Any() && order.to.type == "EMAIL" && order.to.name.empty(), "an e-mail address needs no name");
|
||
|
|
|
||
|
|
// A schedule: a first day, an optional time, and for one that repeats how often and until when.
|
||
|
|
form.when = When::Scheduled;
|
||
|
|
errors = form.Check(euro, now, order);
|
||
|
|
Equal(errors.day, std::string("A day: YYYY-MM-DD"), "a schedule needs its first day");
|
||
|
|
form.day.Set("soon");
|
||
|
|
Equal(form.Check(euro, now, order).day, std::string("Not a day: YYYY-MM-DD"), "which is a day");
|
||
|
|
form.day.Set("2026-11-18");
|
||
|
|
Equal(form.Check(euro, now, order).day, std::string("Today or later"), "not one that has passed");
|
||
|
|
form.day.Set("2026-11-19");
|
||
|
|
Check(!form.Check(euro, now, order).Any() && order.when == When::Scheduled && order.schedule.unit == "ONCE" && order.schedule.start == ParseLocalDate("2026-11-19"), "today will do, once, from the start of the day");
|
||
|
|
form.time.Set("25:00");
|
||
|
|
Equal(form.Check(euro, now, order).time, std::string("Not a time: hh:mm"), "its time is one");
|
||
|
|
form.day.Set("2026-11-20");
|
||
|
|
form.time.Set("09:30");
|
||
|
|
// Once has nothing to repeat: what is left in those fields is not looked at.
|
||
|
|
form.every.Set("0");
|
||
|
|
form.until.Set("never");
|
||
|
|
Check(!form.Check(euro, now, order).Any() && order.schedule.start == ParseLocalDate("2026-11-20 09:30") && order.schedule.every == 1 && !order.schedule.end, "a time on the day, once");
|
||
|
|
form.unit = 3;
|
||
|
|
errors = form.Check(euro, now, order);
|
||
|
|
Check(errors.every == "A whole number, 1 or more" && errors.until == "Not a day: YYYY-MM-DD", "one that repeats checks how often and until when");
|
||
|
|
form.every.Set("2");
|
||
|
|
form.until.Set("2026-11-19");
|
||
|
|
Equal(form.Check(euro, now, order).until, std::string("After the first payment"), "it ends after it starts");
|
||
|
|
form.until.Set("2027-01-01");
|
||
|
|
Check(!form.Check(euro, now, order).Any() && order.schedule.unit == "WEEKLY" && order.schedule.every == 2 && order.schedule.end == ParseLocalDateEnd("2027-01-01"), "every other week until the end of a day");
|
||
|
|
form.every.Clear();
|
||
|
|
form.until.Clear();
|
||
|
|
Check(!form.Check(euro, now, order).Any() && order.schedule.every == 1 && !order.schedule.end, "or every week, for as long as it runs");
|
||
|
|
form.day.Set("nonsense");
|
||
|
|
form.until.Set("2027-01-01");
|
||
|
|
Check(form.Check(euro, now, order).until.empty(), "an end is not held against a start that is not there");
|
||
|
|
|
||
|
|
form.when = When::Draft;
|
||
|
|
form.day.Clear();
|
||
|
|
Check(!form.Check(euro, now, order).Any() && order.when == When::Draft, "a draft needs no day");
|
||
|
|
form.tried = true;
|
||
|
|
form.Clear();
|
||
|
|
Check(form.to.value.empty() && form.amount.value.empty() && form.when == When::Now && form.unit == 0 && form.day.value.empty() && !form.tried, "a cleared form is as new");
|
||
|
|
}
|
||
|
|
|
||
|
|
void Asking() {
|
||
|
|
RequestForm form;
|
||
|
|
RequestOrder order;
|
||
|
|
RequestForm::Errors errors = form.Check(euro, order);
|
||
|
|
Check(errors.Any() && !errors.from.empty() && !errors.amount.empty() && errors.name.empty() && !RequestForm::Errors().Any(), "an empty request lacks who and how much");
|
||
|
|
form.from.Set(iban);
|
||
|
|
form.amount.Set("5");
|
||
|
|
Equal(form.Check(euro, order).name, std::string("The name of the account's holder"), "an IBAN needs its holder's name");
|
||
|
|
form.name.Set("Bob");
|
||
|
|
form.description.Set("back");
|
||
|
|
Check(!form.Check(euro, order).Any() && order.from.value == iban && order.from.name == "Bob" && order.amount == 500 && order.description == "back", "a request filled in is an order");
|
||
|
|
form.from.Set("+31612345678");
|
||
|
|
form.name.Clear();
|
||
|
|
Check(!form.Check(euro, order).Any() && order.from.type == "PHONE_NUMBER", "a phone number needs no name");
|
||
|
|
form.tried = true;
|
||
|
|
form.Clear();
|
||
|
|
Check(form.from.value.empty() && form.amount.value.empty() && form.description.value.empty() && !form.tried, "a cleared request is as new");
|
||
|
|
}
|
||
|
|
|
||
|
|
void Filtering() {
|
||
|
|
const Moment now = ParseLocalDate("2026-11-19 15:00");
|
||
|
|
FilterForm form;
|
||
|
|
TransactionQuery query;
|
||
|
|
query.limit = 300;
|
||
|
|
Check(!form.Check(euro, query).Any() && query.search.empty() && !query.amount && query.from == Moment::min() && query.to == Moment::max() && query.limit == 300, "an empty filter asks for everything, as many as before");
|
||
|
|
Check(form.Chips(euro, now).empty() && !FilterForm::Errors().Any(), "and shows nothing");
|
||
|
|
form.search.Set(" rent ");
|
||
|
|
form.amount.Set("12,50");
|
||
|
|
form.from.Set("2026-11-01");
|
||
|
|
form.to.Set("2026-11-30");
|
||
|
|
Check(!form.Check(euro, query).Any() && query.search == "rent" && query.amount == -1250 && query.from == ParseLocalDate("2026-11-01") && query.to == ParseLocalDateEnd("2026-11-30"), "what went out, between two days, the last one whole");
|
||
|
|
Check(form.Chips(euro, now) == std::vector<std::string>{"Paid €12.50", "From 1 Nov", "Until 30 Nov"}, "said in a few words each");
|
||
|
|
form.received = true;
|
||
|
|
Check(!form.Check(euro, query).Any() && query.amount == 1250 && form.Chips(euro, now).front() == "Received €12.50", "or what came in");
|
||
|
|
|
||
|
|
query = TransactionQuery();
|
||
|
|
form.amount.Set("1,234");
|
||
|
|
form.from.Set("soon");
|
||
|
|
form.to.Set("later");
|
||
|
|
const FilterForm::Errors errors = form.Check(euro, query);
|
||
|
|
Check(errors.Any() && errors.amount.contains("At most 2 decimals") && errors.from.contains("Not a day") && errors.to.contains("Not a day") && query.search.empty(), "a filter with a mistake asks for nothing");
|
||
|
|
Check(form.Chips(euro, now).empty(), "and what cannot be read is no chip");
|
||
|
|
form.amount.Clear();
|
||
|
|
form.from.Set("2026-11-30");
|
||
|
|
form.to.Set("2026-11-01");
|
||
|
|
Equal(form.Check(euro, query).to, std::string("On or after the first day"), "a range ends after it starts");
|
||
|
|
form.to.Set("2026-11-30");
|
||
|
|
Check(!form.Check(euro, query).Any(), "one day is a range");
|
||
|
|
form.Reset();
|
||
|
|
Check(form.amount.value.empty() && form.from.value.empty() && form.to.value.empty() && !form.received && form.search.value == " rent ", "a reset keeps the search and drops the rest");
|
||
|
|
}
|
||
|
|
|
||
|
|
void Connecting() {
|
||
|
|
BunqKeyForm key;
|
||
|
|
Check(key.ips.value == "*" && key.Check() == "The API key, from the bunq app", "a new key form allows any address and lacks its key");
|
||
|
|
key.key.Set("sandbox_0123456789abcdef");
|
||
|
|
key.ips.Set("10.0.0.1");
|
||
|
|
key.advanced = true;
|
||
|
|
key.tried = true;
|
||
|
|
Check(key.Check().empty(), "with a key it can be sent");
|
||
|
|
key.Clear();
|
||
|
|
Check(key.key.value.empty() && key.ips.value == "*" && !key.advanced && !key.tried, "a cleared key form is as new");
|
||
|
|
|
||
|
|
BunqOAuthForm oauth;
|
||
|
|
BunqOAuthForm::Errors errors = oauth.Check();
|
||
|
|
Check(errors.Any() && !errors.clientId.empty() && !errors.redirectUri.empty() && !errors.secret.empty() && !BunqOAuthForm::Errors().Any(), "an empty OAuth form lacks everything");
|
||
|
|
oauth.clientId.Set(" client-1 ");
|
||
|
|
oauth.secret.Set("secret-1");
|
||
|
|
oauth.redirectUri.Set("ftp://example.org/cb");
|
||
|
|
Check(!oauth.Check().redirectUri.empty() && oauth.Check().clientId.empty() && oauth.Check().secret.empty(), "a redirect address is a web address");
|
||
|
|
oauth.redirectUri.Set("http://127.0.0.1/cb");
|
||
|
|
Check(!oauth.Check().Any(), "to this computer itself will do");
|
||
|
|
oauth.redirectUri.Set(" https://example.org/cb ");
|
||
|
|
oauth.Begin("state123");
|
||
|
|
Equal(oauth.address, std::string("https://oauth.bunq.com/auth?response_type=code&client_id=client-1&redirect_uri=https%3A%2F%2Fexample.org%2Fcb&state=state123"), "the address to approve at, with this request's state");
|
||
|
|
oauth.sandbox = true;
|
||
|
|
oauth.Begin("state456");
|
||
|
|
Check(oauth.address.starts_with("https://oauth.sandbox.bunq.com/auth?") && oauth.state == "state456", "the sandbox has its own");
|
||
|
|
oauth.redirected.Set(" https://example.org/cb?code=abc&state=state456 ");
|
||
|
|
Equal(oauth.Code(), std::string("abc"), "the code in the address the browser was sent to");
|
||
|
|
oauth.redirected.Set("https://example.org/cb?code=abc&state=state123");
|
||
|
|
Throws<std::runtime_error>([&oauth] { oauth.Code(); }, "its state differs", "an answer to another request is refused");
|
||
|
|
oauth.redirected.Set("https://example.org/cb?error=access_denied&error_description=No");
|
||
|
|
Throws<std::runtime_error>([&oauth] { oauth.Code(); }, "bunq did not authorize", "and so is bunq's refusal");
|
||
|
|
oauth.tried = true;
|
||
|
|
oauth.Clear();
|
||
|
|
Check(oauth.clientId.value.empty() && oauth.redirectUri.value.empty() && oauth.secret.value.empty() && !oauth.sandbox && oauth.redirected.value.empty() && oauth.state.empty() && oauth.address.empty() && !oauth.tried, "a cleared OAuth form is as new");
|
||
|
|
}
|
||
|
|
|
||
|
|
template<typename E>
|
||
|
|
Guidance Explained(E error, Operation operation) {
|
||
|
|
return Explain(std::make_exception_ptr(std::move(error)), operation);
|
||
|
|
}
|
||
|
|
|
||
|
|
void Explaining() {
|
||
|
|
// What nostrod asks polkit for, per the policy nostro installs.
|
||
|
|
const std::array<std::tuple<Operation, std::string_view, bool, bool>, 17> operations = {{
|
||
|
|
{Operation::Load, Bus::actionView, false, false},
|
||
|
|
{Operation::Send, Bus::actionPay, true, true}, {Operation::Schedule, Bus::actionPay, true, true},
|
||
|
|
{Operation::PayRequest, Bus::actionPay, true, true}, {Operation::AcceptDraft, Bus::actionPay, true, true},
|
||
|
|
{Operation::Draft, Bus::actionChange, false, false}, {Operation::Request, Bus::actionChange, false, false},
|
||
|
|
{Operation::RevokeRequest, Bus::actionChange, false, false}, {Operation::RejectRequest, Bus::actionChange, false, false},
|
||
|
|
{Operation::CancelSchedule, Bus::actionChange, false, false}, {Operation::RejectDraft, Bus::actionChange, false, false},
|
||
|
|
{Operation::FreezeCard, Bus::actionChange, false, false},
|
||
|
|
{Operation::UnfreezeCard, Bus::actionUnfreezeCard, true, false}, {Operation::BlockCard, Bus::actionBlockCard, true, false},
|
||
|
|
{Operation::AddConnection, Bus::actionConnectors, true, false}, {Operation::RemoveConnection, Bus::actionConnectors, true, false},
|
||
|
|
{Operation::SetCheckoutAccount, Bus::actionConnectors, true, false},
|
||
|
|
}};
|
||
|
|
for(const auto& [operation, action, password, money] : operations) {
|
||
|
|
Check(ActionOf(operation) == action && AsksPassword(operation) == password && MovesMoney(operation) == money, std::format("operation {} asks for {}", std::to_underlying(operation), action));
|
||
|
|
}
|
||
|
|
|
||
|
|
Guidance told = Explained(DaemonMissing("not there"), Operation::Load);
|
||
|
|
Check(told.problem == Problem::NoService && told.retry && !told.mayHaveHappened && told.detail.contains("README") && told.detail.contains("not there"), "no nostrod: how to get one");
|
||
|
|
for(const std::string_view name : {"org.freedesktop.systemd1.NoSuchUnit", "org.freedesktop.DBus.Error.Spawn.ChildExited", "org.freedesktop.DBus.Error.NameHasNoOwner"}) {
|
||
|
|
Check(Explained(DaemonError(std::string(name), "could not start"), Operation::Send).problem == Problem::NoService, "a nostrod the bus could not start is no nostrod");
|
||
|
|
}
|
||
|
|
told = Explained(NoSystemBus("No such file or directory"), Operation::Load);
|
||
|
|
Check(told.problem == Problem::NoService && told.retry && !told.mayHaveHappened && told.title == "The system bus cannot be reached" && told.detail.contains("No such file or directory"), "no system bus to reach nostrod over");
|
||
|
|
told = Explained(OutcomeUnknown("no answer"), Operation::Send);
|
||
|
|
Check(told.problem == Problem::Unknown && told.mayHaveHappened && !told.retry && told.title == "It may have been paid" && told.detail.contains("activity"), "a payment without a clear answer may have been made");
|
||
|
|
told = Explained(OutcomeUnknown("no answer"), Operation::RevokeRequest);
|
||
|
|
Check(told.problem == Problem::Unknown && told.mayHaveHappened && told.title == "It may have been done", "and any other change may have been");
|
||
|
|
told = Explained(std::invalid_argument("no account is called 'Nope'"), Operation::Send);
|
||
|
|
Check(told.problem == Problem::Input && !told.mayHaveHappened && !told.retry && told.detail == "no account is called 'Nope'. Nothing was sent.", "what nostrod refused before it began");
|
||
|
|
told = Explained(DaemonError(std::string(Bus::errorCancelled), "cancelled"), Operation::Send);
|
||
|
|
Check(told.problem == Problem::NotConfirmed && told.detail.contains("Nothing was done") && told.detail.contains("agent"), "no password: nothing was done, and why no dialog may have come");
|
||
|
|
told = Explained(DaemonError("org.freedesktop.DBus.Error.AccessDenied", "the system's policy does not allow it"), Operation::Send);
|
||
|
|
Check(told.problem == Problem::NotAllowed && told.detail.starts_with("the system's policy does not allow it") && !told.retry, "refused by the system's policy");
|
||
|
|
Check(Explained(DaemonError("org.freedesktop.DBus.Error.AccessDenied", "no"), Operation::Load).retry, "a reading may be tried again");
|
||
|
|
told = Explained(DaemonError("org.freedesktop.DBus.Error.InteractiveAuthorizationRequired", "x"), Operation::Send);
|
||
|
|
Check(told.problem == Problem::CannotAsk && told.detail.contains("README"), "nostrod could not ask");
|
||
|
|
told = Explained(DaemonError(std::string(Bus::errorOAuthCannotPay), "x"), Operation::Send);
|
||
|
|
Check(told.problem == Problem::OwnAccountsOnly && told.title == "This connection cannot pay others" && told.detail.contains("draft"), "a connection that pays the user's own accounts only");
|
||
|
|
Check(!told.detail.contains("bunq") && !told.detail.contains("OAuth"), "said of no bank and of no way to connect to one");
|
||
|
|
told = Explained(DaemonError(std::string(Bus::errorBank), "bunq POST /payment: Not enough money. (400)"), Operation::Send);
|
||
|
|
Check(told.problem == Problem::Bank && told.title == "The bank refused" && told.detail.contains("Not enough money") && !told.retry && !told.mayHaveHappened, "the bank's own refusal");
|
||
|
|
Check(Explained(DaemonError(std::string(Bus::errorBank), "x"), Operation::Load).retry, "of a reading, which may be tried again");
|
||
|
|
for(const std::string_view name : {"org.freedesktop.DBus.Error.NoReply", "org.freedesktop.DBus.Error.Timeout"}) {
|
||
|
|
told = Explained(DaemonError(std::string(name), "timed out"), Operation::Load);
|
||
|
|
Check(told.problem == Problem::Late && told.retry, "a reading that got no answer in time is only late");
|
||
|
|
}
|
||
|
|
told = Explained(DaemonError(std::string(Bus::errorFailed), "the worker went"), Operation::Send);
|
||
|
|
Check(told.problem == Problem::Unknown && told.mayHaveHappened, "a payment nostrod failed at may have been made");
|
||
|
|
told = Explained(DaemonError(std::string(Bus::errorFailed), "already connected"), Operation::AddConnection);
|
||
|
|
Check(told.problem == Problem::Failed && told.detail == "already connected" && !told.mayHaveHappened && !told.retry, "anything else it failed at is said as it says it");
|
||
|
|
Check(Explained(DaemonError(std::string(Bus::errorFailed), "x"), Operation::Load).retry, "and a reading may be tried again");
|
||
|
|
told = Explained(std::system_error(std::make_error_code(std::errc::bad_message), "cannot read"), Operation::Send);
|
||
|
|
Check(told.problem == Problem::Unknown && told.mayHaveHappened, "an answer to a change that cannot be read leaves it open");
|
||
|
|
told = Explained(std::system_error(std::make_error_code(std::errc::not_connected), "cannot call nostrod"), Operation::Load);
|
||
|
|
Check(told.problem == Problem::Failed && told.retry && !told.mayHaveHappened, "one to a reading does not");
|
||
|
|
told = Explained(std::runtime_error("bunq kept card 7 DEACTIVATED"), Operation::UnfreezeCard);
|
||
|
|
Check(told.problem == Problem::Failed && told.detail == "bunq kept card 7 DEACTIVATED" && !told.retry, "any other error is said as it is");
|
||
|
|
Check(Explained(std::runtime_error("x"), Operation::Load).retry, "and a reading may be tried again");
|
||
|
|
}
|
||
|
|
|
||
|
|
// What the app knows of the banks: each with its ways to connect, found by what nostrod calls them, and a bank
|
||
|
|
// or a way it does not know, which a newer nostrod may have, said by nostrod's own name for it.
|
||
|
|
void Knowing() {
|
||
|
|
Check(!Banks().empty(), "there is a bank to connect");
|
||
|
|
for(const Bank& bank : Banks()) {
|
||
|
|
Check(FindBank(bank.id) == &bank && !bank.name.empty() && !bank.ways.empty(), "a bank is found by what nostrod calls it, and has a name and a way in");
|
||
|
|
Equal(BankName(bank.id), std::string(bank.name), "and is said by its own name");
|
||
|
|
std::set<std::string_view> keys;
|
||
|
|
std::set<ConnectForm> forms;
|
||
|
|
for(const Way& way : bank.ways) {
|
||
|
|
Check(FindWay(bank.id, way.auth) == &way, "a way is found by what nostrod calls it");
|
||
|
|
Check(!way.key.empty() && !way.title.empty() && !way.about.empty() && !way.revoke.empty(), "and has its words");
|
||
|
|
Equal(WayName(bank.id, way.auth), std::string(way.title), "it is said by its title");
|
||
|
|
Equal(OwnAccountsOnly(bank.id, way.auth), way.ownAccountsOnly, "what a connection made that way can pay is the way's to say");
|
||
|
|
Equal(Revoking(bank.id, way.auth), std::string(way.revoke), "and so is where it is revoked");
|
||
|
|
keys.insert(way.key);
|
||
|
|
forms.insert(way.form);
|
||
|
|
}
|
||
|
|
Check(keys.size() == bank.ways.size() && forms.size() == bank.ways.size(), "its ways are told apart, each with a form of its own");
|
||
|
|
}
|
||
|
|
|
||
|
|
const Bank* const bunq = FindBank(BunqConnector::connectorId);
|
||
|
|
Check(bunq != nullptr && bunq->name == "bunq" && bunq->ways.size() == 2, "bunq is one of them, by its connector's own id, with two ways in");
|
||
|
|
Check(FindWay("bunq", "api key")->form == ConnectForm::BunqKey && FindWay("bunq", "api key")->key == "key" && !OwnAccountsOnly("bunq", "api key"), "an API key, which does all a user can");
|
||
|
|
Check(FindWay("bunq", "oauth")->form == ConnectForm::BunqOAuth && FindWay("bunq", "oauth")->key == "oauth" && OwnAccountsOnly("bunq", "oauth"), "and OAuth, which bunq lets pay between the user's own accounts only");
|
||
|
|
Equal(WayName("bunq", "api key"), std::string("API key"), "as nostrod's \"api key\" is said");
|
||
|
|
Check(Revoking("bunq", "api key").contains("API key") && Revoking("bunq", "oauth").contains("permission"), "each stays valid at bunq until it is revoked there");
|
||
|
|
|
||
|
|
Check(FindBank("thrift") == nullptr && FindWay("thrift", "api key") == nullptr && FindWay("bunq", "handshake") == nullptr, "a bank or a way the app does not know is not found");
|
||
|
|
Equal(BankName("thrift"), std::string("thrift"), "such a bank is said as nostrod calls it");
|
||
|
|
Equal(WayName("thrift", "password"), std::string("password"), "and so is its way");
|
||
|
|
Equal(WayName("bunq", "handshake"), std::string("handshake"), "or a new way of a bank it knows");
|
||
|
|
Check(!OwnAccountsOnly("thrift", "oauth") && !OwnAccountsOnly("bunq", "handshake"), "of which the app knows no limit");
|
||
|
|
Check(Revoking("thrift", "password").contains("at the bank") && Revoking("bunq", "handshake") == Revoking("thrift", "password"), "and no more of revoking it than that it is done at the bank");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
setenv("TZ", "Europe/Amsterdam", 1);
|
||
|
|
tzset();
|
||
|
|
Words();
|
||
|
|
Reading();
|
||
|
|
Paying();
|
||
|
|
Asking();
|
||
|
|
Filtering();
|
||
|
|
Connecting();
|
||
|
|
Explaining();
|
||
|
|
Knowing();
|
||
|
|
return Test::Result();
|
||
|
|
}
|