Skip to content

Commit

Permalink
test(internal): participation keys and eligibility
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Dec 2, 2024
1 parent f822c74 commit 0b8944f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 44 deletions.
95 changes: 51 additions & 44 deletions internal/participation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,47 @@ import (
"context"
"fmt"
"github.com/algorandfoundation/hack-tui/api"
"github.com/oapi-codegen/oapi-codegen/v2/pkg/securityprovider"
"github.com/algorandfoundation/hack-tui/internal/test"
"github.com/algorandfoundation/hack-tui/internal/test/mock"
"testing"
)

func Test_ToLoraDeeplink(t *testing.T) {
link, err := ToLoraDeepLink("tuinet-v1", true, true, api.ParticipationKey{
Address: "ABC",
EffectiveFirstValid: nil,
EffectiveLastValid: nil,
Id: "",
Key: api.AccountParticipation{},
LastBlockProposal: nil,
LastStateProof: nil,
LastVote: nil,
})
if err != nil {
t.Error(err)
}
if link != "https://lora.algokit.io/localnet/transaction-wizard?type%5B0%5D=keyreg&sender%5B0%5D=ABC" {
t.Error("Link should be a known deeplink")
}

link, err = ToLoraDeepLink("tuinet-v1", false, true, mock.Keys[0])
if err != nil {
t.Error(err)
}
if link != "https://lora.algokit.io/localnet/transaction-wizard?type%5B0%5D=keyreg&fee%5B0%5D=2000000&sender%5B0%5D=ABC&selkey%5B0%5D=VEVTVEtFWQ&sprfkey%5B0%5D=VEVTVEtFWQ&votekey%5B0%5D=VEVTVEtFWQ&votefst%5B0%5D=0&votelst%5B0%5D=30000&votekd%5B0%5D=100" {
t.Error("Link should be a known deeplink fee")
}

link, err = ToLoraDeepLink("tuinet-v1", false, false, mock.Keys[0])
if err != nil {
t.Error(err)
}
if link != "https://lora.algokit.io/localnet/transaction-wizard?type%5B0%5D=keyreg&fee%5B0%5D=1000&sender%5B0%5D=ABC&selkey%5B0%5D=VEVTVEtFWQ&sprfkey%5B0%5D=VEVTVEtFWQ&votekey%5B0%5D=VEVTVEtFWQ&votefst%5B0%5D=0&votelst%5B0%5D=30000&votekd%5B0%5D=100" {
t.Error("Link should be a known deeplink fee")
}

}

func Test_ListParticipationKeys(t *testing.T) {
ctx := context.Background()
client, err := api.NewClientWithResponses("https://mainnet-api.4160.nodely.dev:443")
Expand All @@ -23,16 +60,9 @@ func Test_ListParticipationKeys(t *testing.T) {
}

// Setup elevated client
apiToken, err := securityprovider.NewSecurityProviderApiKey("header", "X-Algo-API-Token", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
if err != nil {
t.Fatal(err)
}
client, err = api.NewClientWithResponses("http://localhost:8080", api.WithRequestEditorFn(apiToken.Intercept))
if err != nil {
t.Fatal(err)
}
tClient := test.GetClient(false)

keys, err := GetPartKeys(ctx, client)
keys, err := GetPartKeys(ctx, tClient)
if err != nil {
t.Fatal(err)
}
Expand All @@ -54,25 +84,17 @@ func Test_ReadParticipationKey(t *testing.T) {
t.Fatal(err)
}

// Setup elevated client
apiToken, err := securityprovider.NewSecurityProviderApiKey("header", "X-Algo-API-Token", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
if err != nil {
t.Fatal(err)
}
client, err = api.NewClientWithResponses("http://localhost:8080", api.WithRequestEditorFn(apiToken.Intercept))
if err != nil {
t.Fatal(err)
}
tClient := test.GetClient(false)

keys, err := GetPartKeys(ctx, client)
keys, err := GetPartKeys(ctx, tClient)
if err != nil {
t.Fatal(err)
}
if keys == nil {
t.Fatal(err)
}

_, err = ReadPartKey(ctx, client, (*keys)[0].Id)
_, err = ReadPartKey(ctx, tClient, (*keys)[0].Id)

if err != nil {
t.Fatal(err)
Expand All @@ -88,31 +110,23 @@ func Test_GenerateParticipationKey(t *testing.T) {
if err != nil {
t.Fatal(err)
}

// Generate error
_, err = GenerateKeyPair(ctx, client, "", nil)
if err == nil {
t.Fatal(err)
}

// Setup elevated client
apiToken, err := securityprovider.NewSecurityProviderApiKey("header", "X-Algo-API-Token", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
if err != nil {
t.Fatal(err)
}
client, err = api.NewClientWithResponses("http://localhost:8080", api.WithRequestEditorFn(apiToken.Intercept))
if err != nil {
t.Fatal(err)
}
// Setup test client
tClient := test.GetClient(false)

params := api.GenerateParticipationKeysParams{
Dilution: nil,
First: 0,
Last: 10000,
Last: 30,
}

// This returns nothing and sucks
key, err := GenerateKeyPair(ctx, client, "QNZ7GONNHTNXFW56Y24CNJQEMYKZKKI566ASNSWPD24VSGKJWHGO6QOP7U", &params)
key, err := GenerateKeyPair(ctx, tClient, "ABC", &params)
if err != nil {
t.Fatal(err)
}
Expand All @@ -121,21 +135,14 @@ func Test_GenerateParticipationKey(t *testing.T) {

func Test_DeleteParticipationKey(t *testing.T) {
ctx := context.Background()
// Setup elevated client
apiToken, err := securityprovider.NewSecurityProviderApiKey("header", "X-Algo-API-Token", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
if err != nil {
t.Fatal(err)
}
client, err := api.NewClientWithResponses("http://localhost:8080", api.WithRequestEditorFn(apiToken.Intercept))
if err != nil {
t.Fatal(err)
}

client := test.GetClient(false)
params := api.GenerateParticipationKeysParams{
Dilution: nil,
First: 0,
Last: 10000,
Last: 30000,
}
key, err := GenerateKeyPair(ctx, client, "QNZ7GONNHTNXFW56Y24CNJQEMYKZKKI566ASNSWPD24VSGKJWHGO6QOP7U", &params)
key, err := GenerateKeyPair(ctx, client, "ABC", &params)
if err != nil {
t.Fatal(err)
}
Expand Down
30 changes: 30 additions & 0 deletions internal/test/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,36 @@ algod_crypto_vrf_hash_total 0`
}
return &res, nil
}
func (c *Client) GetParticipationKeyByIDWithResponse(ctx context.Context, participationId string, reqEditors ...api.RequestEditorFn) (*api.GetParticipationKeyByIDResponse, error) {
var res api.GetParticipationKeyByIDResponse
if !c.Invalid {
httpResponse := http.Response{StatusCode: 200}
res = api.GetParticipationKeyByIDResponse{
Body: nil,
HTTPResponse: &httpResponse,
JSON200: &mock.Keys[0],
JSON400: nil,
JSON401: nil,
JSON404: nil,
JSON500: nil,
}
} else {
httpResponse := http.Response{StatusCode: 404}
res = api.GetParticipationKeyByIDResponse{
Body: nil,
HTTPResponse: &httpResponse,
JSON200: nil,
JSON400: nil,
JSON401: nil,
JSON404: nil,
JSON500: nil,
}
}

Check warning on line 94 in internal/test/client.go

View check run for this annotation

Codecov / codecov/patch

internal/test/client.go#L84-L94

Added lines #L84 - L94 were not covered by tests
if c.Errors {
return nil, errors.New("test error")
}

Check warning on line 97 in internal/test/client.go

View check run for this annotation

Codecov / codecov/patch

internal/test/client.go#L96-L97

Added lines #L96 - L97 were not covered by tests
return &res, nil
}
func (c *Client) GetParticipationKeysWithResponse(ctx context.Context, reqEditors ...api.RequestEditorFn) (*api.GetParticipationKeysResponse, error) {
var res api.GetParticipationKeysResponse
clone := mock.Keys
Expand Down

0 comments on commit 0b8944f

Please sign in to comment.