github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/r/gnoland/ghverify/contract_test.gno (about) 1 package ghverify 2 3 import ( 4 "std" 5 "strings" 6 "testing" 7 8 "gno.land/p/demo/testutils" 9 ) 10 11 func TestVerificationLifecycle(t *testing.T) { 12 defaultAddress := std.GetOrigCaller() 13 userAddress := std.Address(testutils.TestAddress("user")) 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(userAddress) 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 // Set the caller back to the whitelisted user and verify that the feed data 47 // returned matches what should have been created by the `RequestVerification` 48 // invocation. 49 std.TestSetOrigCaller(defaultAddress) 50 result = GnorkleEntrypoint("request") 51 expResult := `[{"id":"` + string(userAddress) + `","type":"0","value_type":"string","tasks":[{"gno_address":"` + 52 string(userAddress) + `","github_handle":"deelawn"}]}]` 53 if result != expResult { 54 t.Fatalf("expected request result %s, got %s", expResult, result) 55 } 56 57 // Try to trigger feed ingestion from the non-authorized user. 58 std.TestSetOrigCaller(userAddress) 59 func() { 60 defer func() { 61 if r := recover(); r != nil { 62 errMsg = r.(error).Error() 63 } 64 }() 65 GnorkleEntrypoint("ingest," + string(userAddress) + ",OK") 66 }() 67 if errMsg != "caller not whitelisted" { 68 t.Fatalf("expected caller not whitelisted, got %s", errMsg) 69 } 70 71 // Set the caller back to the whitelisted user and transfer contract ownership. 72 std.TestSetOrigCaller(defaultAddress) 73 SetOwner(userAddress) 74 75 // Now trigger the feed ingestion from the user and new owner and only whitelisted address. 76 std.TestSetOrigCaller(userAddress) 77 GnorkleEntrypoint("ingest," + string(userAddress) + ",OK") 78 79 // Verify the ingestion autocommitted the value and triggered the post handler. 80 data := Render("") 81 expResult = `{"deelawn": "` + string(userAddress) + `"}` 82 if data != expResult { 83 t.Fatalf("expected render data %s, got %s", expResult, data) 84 } 85 86 // Finally make sure the feed was cleaned up after the data was committed. 87 result = GnorkleEntrypoint("request") 88 if result != "[]" { 89 t.Fatalf("expected empty request result, got %s", result) 90 } 91 92 // Check that the accessor functions are working as expected. 93 if handle := GetHandleByAddress(string(userAddress)); handle != "deelawn" { 94 t.Fatalf("expected deelawn, got %s", handle) 95 } 96 if address := GetAddressByHandle("deelawn"); address != string(userAddress) { 97 t.Fatalf("expected %s, got %s", string(userAddress), address) 98 } 99 }