forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
172 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package gh | ||
|
||
import ( | ||
"std" | ||
) | ||
|
||
var( | ||
adminAddr std.Address = "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq" | ||
) | ||
|
||
|
||
func assertIsAdmin() { | ||
if std.GetOrigCaller() != adminAddr { | ||
panic("restricted area") | ||
} | ||
} | ||
|
||
func setAdminAddress(address std.Address) { | ||
adminAddr = address | ||
} | ||
|
||
func SetAdminAddress(address std.Address) { | ||
assertIsAdmin() | ||
setAdminAddress(address) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package gh | ||
|
||
import ( | ||
"std" | ||
"testing" | ||
|
||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/testutils" | ||
) | ||
|
||
func TestAssertIsAdmin(t *testing.T) { | ||
adminAddr = "g1test1234" | ||
randomuser := testutils.TestAddress("g1unknown_player") | ||
defer func() { | ||
if r := recover(); r != nil { | ||
} | ||
}() | ||
std.TestSetOrigCaller(randomuser) | ||
assertIsAdmin() | ||
t.Fatalf("should fail because not admin") | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package gh | ||
|
||
import ( | ||
"std" | ||
"testing" | ||
"time" | ||
|
||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/testutils" | ||
) | ||
|
||
func TestOracleUpsertUserIsNotOracle(t *testing.T) { | ||
oracleAddr = "g1test1234" | ||
var randomuser = "g1unknown_player" | ||
user := testutils.TestAddress("user") | ||
defer func() { | ||
if r := recover(); r != nil { | ||
} | ||
}() | ||
OracleUpsertAccount("acountID", "villaquiranm","john doe","user") | ||
t.Fatalf("should fail because not admin") | ||
} | ||
|
||
func TestOracleUpsertUserOk(t *testing.T) { | ||
oracleAddr = "g1random" | ||
if accounts.Size() != 0 { | ||
t.Fatalf("Accounts is not empty") | ||
} | ||
now := time.Now() | ||
|
||
std.TestSetOrigCaller(oracleAddr) | ||
|
||
var randomuser = "g1unknown_player" | ||
OracleUpsertAccount("acountID", "villaquiranm","john doe","user") | ||
|
||
if accounts.Size() != 1 { | ||
t.Fatalf("User was not created") | ||
} | ||
|
||
OracleUpsertAccount("acountID", "villaquiranm","john doe","user") | ||
|
||
if accounts.Size() != 1 { | ||
t.Fatalf("User was created more than once") | ||
} | ||
|
||
if OracleLastUpdated().Unix() < now.Unix() { | ||
t.Fatalf("OracleLastUpdated was not changed") | ||
} | ||
} | ||
|
||
func TestAssertIsOracle(t *testing.T) { | ||
std.TestSetOrigCaller(adminAddr) | ||
AdminSetOracleAddr("g1random123") | ||
defer func() { | ||
if r := recover(); r != nil { | ||
} | ||
}() | ||
assertIsOracle() | ||
t.Fatalf("should fail because user is not oracle") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
package gh | ||
|
||
import ( | ||
"std" | ||
"gno.land/p/demo/avl" | ||
) | ||
var ( | ||
addressesToAccount avl.Tree // address -> AccountLink | ||
adminAddr std.Address = "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq" | ||
addressToAccount avl.Tree // address -> AccountLink | ||
) | ||
|
||
func init() { | ||
setAdminAddress(std.GetOrigCaller()) | ||
} | ||
|
||
func LinkAccount(accountID string, address std.Address) { | ||
assertIsAdmin() | ||
account := AccountByID(id string) | ||
account := AccountByID(accountID) | ||
if account == nil { | ||
panic("account not found") | ||
} | ||
|
||
addressesToAccount.Set(address, account) | ||
addressToAccount.Set(address.String(), account) | ||
} | ||
|
||
func assertIsAdmin() { | ||
if std.GetOrigCaller() != adminAddr { | ||
panic("restricted area") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package gh | ||
|
||
import ( | ||
"std" | ||
"testing" | ||
|
||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/testutils" | ||
) | ||
|
||
func TestLinkAccountCallerNotAdmin(t *testing.T) { | ||
user := testutils.TestAddress("user") | ||
std.TestSetOrigCaller(user) | ||
defer func() { | ||
if r := recover(); r != nil { | ||
} | ||
}() | ||
LinkAccount("acountID", user) | ||
t.Fatalf("should fail because not admin") | ||
} | ||
|
||
func TestLinkAccount(t *testing.T) { | ||
adminAddr = "g1test1234" | ||
|
||
user := testutils.TestAddress("user") | ||
admin := testutils.TestAddress("admin") | ||
|
||
std.TestSetOrigCaller(admin) | ||
setAdminAddress(admin) | ||
AdminSetOracleAddr(admin) | ||
|
||
OracleUpsertAccount("123","user", "user", UserAccount) | ||
LinkAccount("123", user) | ||
accountInPublic, ok := addressToAccount.Get(user.String()) | ||
if !ok { | ||
t.Fatalf("account not found") | ||
} | ||
account := AccountByID("123") | ||
if accountInPublic != account { | ||
t.Fatalf("account is not same") | ||
} | ||
} |