Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/ImmigrationEmailTests.cpp

Source at documentation-generation time. See Test catalog for fixture associations.

1#include "../ImmigrationEmail.h"
2#include <cstdlib>
3#include <iostream>
4#include <limits>
5
6static int checks = 0;
7static void Check(bool condition, const char* message)
8{
9 ++checks;
10 if (!condition) { std::cerr << "FAIL: " << message << '\n'; std::exit(1); }
11}
12
13int main()
14{
16 using Reply = State::Reply;
17 State state;
18 state.Reset(L" OLD@EXAMPLE.COM ");
19 Check(state.Value() == L"old@example.com", "saved address normalized");
20 Check(state.CanEdit(true), "annual revisit unlocks saved email");
21 Check(!state.CanEdit(false), "ordinary missing-field visit keeps saved email locked");
22 Check(state.Status() == State::Passed && state.BeginCheck(0) == 0, "unchanged email needs no provider call");
23 state.Edit(L" Old@Example.com ");
24 Check(state.Status() == State::Passed, "case and whitespace only preserve approval");
25 state.Edit(L"new@example.com");
26 Check(state.Status() == State::Invalid && !state.BeginSave(), "editing immediately invalidates approval");
27 const int first = state.BeginCheck(100);
28 Check(first > 0 && state.Status() == State::Checking, "changed address requests a token");
29 Check(state.BeginCheck(101) == 0, "one check per unchanged pending value");
30 Check(state.Receive(0, true, false) == Reply::Ignored, "legacy uncorrelated approval ignored");
31 Check(state.Receive(-1, true, false) == Reply::Ignored, "negative token ignored");
32 Check(state.Receive(1.5f, true, false) == Reply::Ignored, "fractional token ignored");
33 Check(state.Receive(std::numeric_limits<float>::quiet_NaN(), true, false) == Reply::Ignored, "NaN token ignored");
34 Check(state.Receive(std::numeric_limits<float>::infinity(), true, false) == Reply::Ignored, "infinite token ignored");
35 state.Edit(L"different@example.com");
36 state.Edit(L"new@example.com");
37 const int second = state.BeginCheck(200);
38 Check(second != first, "away and back uses a fresh request token");
39 Check(state.Receive(static_cast<float>(first), true, false) == Reply::Ignored, "stale success cannot approve current text");
40 Check(state.Receive(static_cast<float>(first), false, false) == Reply::Ignored, "stale failure cannot reject current text");
41 Check(state.Receive(static_cast<float>(second), false, true) == Reply::Validation, "matching outage handled");
42 Check(state.Status() == State::Unavailable && !state.BeginSave(), "outage prevents save");
43 const int retry = state.BeginCheck(300);
44 Check(retry != second, "outage can be retried without changing address");
45 Check(state.Receive(static_cast<float>(retry), true, false) == Reply::Validation, "matching success approves");
46 Check(state.Receive(static_cast<float>(retry), false, false) == Reply::Ignored, "duplicate reply cannot revoke approval");
47 Check(state.BeginSave(), "approved changed address may be submitted");
48 Check(!state.CanEdit(true) && !state.CanEdit(false), "pending save locks email");
49 Check(!state.BeginSave() && state.BeginCheck(400) == 0, "pending save prevents duplicate save or validation");
50 state.Edit(L"other@example.com");
51 Check(state.Value() == L"new@example.com", "pending save freezes submitted value");
52 Check(state.Receive(static_cast<float>(retry), false, true) == Reply::Ignored, "late preflight cannot cancel save");
53 Check(state.Receive(0, true, false) == Reply::Ignored, "zero-token validation cannot acknowledge save");
54 Check(state.Receive(0, false, false) == Reply::SaveFailed, "final duplicate rejection restores form state");
55 Check(!state.PendingSave() && state.CanEdit(true), "failed save unlocks email");
56 state.Edit(L"old@example.com");
57 Check(state.Status() == State::Passed, "failed save preserves original server baseline");
58 state.Edit(L"new@example.com");
59 const int expired = state.BeginCheck(1000);
60 Check(!state.Expire(15999) && state.Expire(16000), "response timeout has exact boundary");
61 Check(state.Status() == State::Unavailable, "missing reply becomes retryable outage");
62 Check(state.Receive(static_cast<float>(expired), true, false) == Reply::Ignored, "late success after timeout ignored");
63 const int afterTimeout = state.BeginCheck(17000);
64 Check(afterTimeout != expired, "timeout retry gets new token");
65 state.Receive(static_cast<float>(afterTimeout), true, false);
66 state.BeginSave();
67 Check(state.Receive(0, false, true) == Reply::SaveFailed && state.Status() == State::Unavailable,
68 "final save outage restores retry state");
69 const int finalCheck = state.BeginCheck(18000);
70 state.Receive(static_cast<float>(finalCheck), true, false);
71 state.BeginSave();
72 state.Saved();
73 Check(!state.PendingSave() && state.Status() == State::Passed, "server acknowledgement finishes save");
74 state.Edit(L"old@example.com");
75 Check(state.Status() == State::Invalid, "successful save advances original address");
76 state.Edit(L"new@example.com");
77 Check(state.Status() == State::Passed, "new saved address no longer needs validation");
78 state.Reset(L"");
79 Check(state.CanEdit(false) && state.CanEdit(true), "new account or missing email stays editable");
80 Check(!state.BeginSave() && state.BeginCheck(0) == 0, "blank address never passes or sends");
81 state.Edit(L"fresh@example.com");
82 const int newVisit = state.BeginCheck(10);
83 Check(newVisit != finalCheck, "form reset does not reuse old request IDs");
84 Check(state.Receive(static_cast<float>(finalCheck), true, false) == Reply::Ignored, "earlier form response ignored");
85 state.RejectInput();
86 Check(state.Status() == State::Invalid && !state.BeginSave(), "unrepresentable wire input cannot submit");
87 Check(state.Receive(static_cast<float>(newVisit), true, false) == Reply::Ignored, "rejected input drops pending approval");
88 std::cout << "PASS: " << checks << " immigration email checks\n";
89}