|
| 1 | +package ghverify |
| 2 | + |
| 3 | +import ( |
| 4 | + "std" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "gno.land/p/demo/testutils" |
| 8 | +) |
| 9 | + |
| 10 | +func TestVerificationLifecycle(t *testing.T) { |
| 11 | + defaultAddress := std.GetOrigCaller() |
| 12 | + user1Address := std.Address(testutils.TestAddress("user 1")) |
| 13 | + user2Address := std.Address(testutils.TestAddress("user 2")) |
| 14 | + |
| 15 | + // Verify request returns no feeds. |
| 16 | + result := GnorkleEntrypoint("request") |
| 17 | + if result != "[]" { |
| 18 | + t.Fatalf("expected empty request result, got %s", result) |
| 19 | + } |
| 20 | + |
| 21 | + // Make a verification request with the created user. |
| 22 | + std.TestSetOrigCaller(user1Address) |
| 23 | + RequestVerification("deelawn") |
| 24 | + |
| 25 | + // A subsequent request from the same address should panic because there is |
| 26 | + // already a feed with an ID of this user's address. |
| 27 | + var errMsg string |
| 28 | + func() { |
| 29 | + defer func() { |
| 30 | + if r := recover(); r != nil { |
| 31 | + errMsg = r.(error).Error() |
| 32 | + } |
| 33 | + }() |
| 34 | + RequestVerification("deelawn") |
| 35 | + }() |
| 36 | + if errMsg != "feed already exists" { |
| 37 | + t.Fatalf("expected feed already exists, got %s", errMsg) |
| 38 | + } |
| 39 | + |
| 40 | + // Verify the request returns no feeds for this non-whitelisted user. |
| 41 | + result = GnorkleEntrypoint("request") |
| 42 | + if result != "[]" { |
| 43 | + t.Fatalf("expected empty request result, got %s", result) |
| 44 | + } |
| 45 | + |
| 46 | + // Make a verification request with the created user. |
| 47 | + std.TestSetOrigCaller(user2Address) |
| 48 | + RequestVerification("omarsy") |
| 49 | + |
| 50 | + // Set the caller back to the whitelisted user and verify that the feed data |
| 51 | + // returned matches what should have been created by the `RequestVerification` |
| 52 | + // invocation. |
| 53 | + std.TestSetOrigCaller(defaultAddress) |
| 54 | + result = GnorkleEntrypoint("request") |
| 55 | + expResult := `[{"id":"` + string(user1Address) + `","type":"0","value_type":"string","tasks":[{"gno_address":"` + |
| 56 | + string(user1Address) + `","github_handle":"deelawn"}]},` + |
| 57 | + `{"id":"` + string(user2Address) + `","type":"0","value_type":"string","tasks":[{"gno_address":"` + |
| 58 | + string(user2Address) + `","github_handle":"omarsy"}]}]` |
| 59 | + if result != expResult { |
| 60 | + t.Fatalf("expected request result %s, got %s", expResult, result) |
| 61 | + } |
| 62 | + |
| 63 | + // Try to trigger feed ingestion from the non-authorized user. |
| 64 | + std.TestSetOrigCaller(user1Address) |
| 65 | + func() { |
| 66 | + defer func() { |
| 67 | + if r := recover(); r != nil { |
| 68 | + errMsg = r.(error).Error() |
| 69 | + } |
| 70 | + }() |
| 71 | + GnorkleEntrypoint("ingest," + string(user1Address) + ",OK") |
| 72 | + }() |
| 73 | + if errMsg != "caller not whitelisted" { |
| 74 | + t.Fatalf("expected caller not whitelisted, got %s", errMsg) |
| 75 | + } |
| 76 | + |
| 77 | + // Set the caller back to the whitelisted user and transfer contract ownership. |
| 78 | + std.TestSetOrigCaller(defaultAddress) |
| 79 | + SetOwner(defaultAddress) |
| 80 | + |
| 81 | + // Now trigger the feed ingestion from the user and new owner and only whitelisted address. |
| 82 | + GnorkleEntrypoint("ingest," + string(user1Address) + ",OK") |
| 83 | + GnorkleEntrypoint("ingest," + string(user2Address) + ",OK") |
| 84 | + |
| 85 | + // Verify the ingestion autocommitted the value and triggered the post handler. |
| 86 | + data := Render("") |
| 87 | + expResult = `{"deelawn": "` + string(user1Address) + `","omarsy": "` + string(user2Address) + `"}` |
| 88 | + if data != expResult { |
| 89 | + t.Fatalf("expected render data %s, got %s", expResult, data) |
| 90 | + } |
| 91 | + |
| 92 | + // Finally make sure the feed was cleaned up after the data was committed. |
| 93 | + result = GnorkleEntrypoint("request") |
| 94 | + if result != "[]" { |
| 95 | + t.Fatalf("expected empty request result, got %s", result) |
| 96 | + } |
| 97 | + |
| 98 | + // Check that the accessor functions are working as expected. |
| 99 | + if handle := GetHandleByAddress(string(user1Address)); handle != "deelawn" { |
| 100 | + t.Fatalf("expected deelawn, got %s", handle) |
| 101 | + } |
| 102 | + if address := GetAddressByHandle("deelawn"); address != string(user1Address) { |
| 103 | + t.Fatalf("expected %s, got %s", string(user1Address), address) |
| 104 | + } |
| 105 | +} |
0 commit comments