contract_test.gno

3.56 Kb ยท 105 lines
  1package ghverify
  2
  3import (
  4	"std"
  5	"testing"
  6
  7	"gno.land/p/demo/testutils"
  8)
  9
 10func TestVerificationLifecycle(cur realm, t *testing.T) {
 11	defaultAddress := std.OriginCaller()
 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(cur, "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	testing.SetOriginCaller(user1Address)
 23	RequestVerification(cur, "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(cur realm) {
 29		defer func() {
 30			if r := recover(); r != nil {
 31				errMsg = r.(error).Error()
 32			}
 33		}()
 34		RequestVerification(cur, "deelawn")
 35	}(cur)
 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(cur, "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	testing.SetOriginCaller(user2Address)
 48	RequestVerification(cur, "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	testing.SetOriginCaller(defaultAddress)
 54	result = GnorkleEntrypoint(cur, "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	testing.SetOriginCaller(user1Address)
 65	func(cur realm) {
 66		defer func() {
 67			if r := recover(); r != nil {
 68				errMsg = r.(error).Error()
 69			}
 70		}()
 71		GnorkleEntrypoint(cur, "ingest,"+string(user1Address)+",OK")
 72	}(cur)
 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	testing.SetOriginCaller(defaultAddress)
 79	SetOwner(defaultAddress)
 80
 81	// Now trigger the feed ingestion from the user and new owner and only whitelisted address.
 82	GnorkleEntrypoint(cur, "ingest,"+string(user1Address)+",OK")
 83	GnorkleEntrypoint(cur, "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(cur, "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(cur, string(user1Address)); handle != "deelawn" {
100		t.Fatalf("expected deelawn, got %s", handle)
101	}
102	if address_XXX := GetAddressByHandle(cur, "deelawn"); address_XXX != string(user1Address) {
103		t.Fatalf("expected %s, got %s", string(user1Address), address_XXX)
104	}
105}