github.com/cosmos/cosmos-sdk@v0.50.10/x/authz/authorization_grant_test.go (about)

     1  package authz
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  // TODO: remove and use: robert/expect-error
    11  func expecError(r *require.Assertions, expected string, received error) {
    12  	if expected == "" {
    13  		r.NoError(received)
    14  	} else {
    15  		r.Error(received)
    16  		r.Contains(received.Error(), expected)
    17  	}
    18  }
    19  
    20  func TestNewGrant(t *testing.T) {
    21  	a := NewGenericAuthorization("some-type")
    22  	tcs := []struct {
    23  		title     string
    24  		a         Authorization
    25  		blockTime time.Time
    26  		expire    *time.Time
    27  		err       string
    28  	}{
    29  		{"wrong expire time (1)", a, time.Unix(10, 0), unixTime(8, 0), "expiration must be after"},
    30  		{"wrong expire time (2)", a, time.Unix(10, 0), unixTime(10, 0), "expiration must be after"},
    31  		{"good expire time (1)", a, time.Unix(10, 0), unixTime(10, 1), ""},
    32  		{"good expire time (2)", a, time.Unix(10, 0), unixTime(11, 0), ""},
    33  		{"good expire time (nil)", a, time.Unix(10, 0), nil, ""},
    34  	}
    35  
    36  	for _, tc := range tcs {
    37  		tc := tc
    38  		t.Run(tc.title, func(t *testing.T) {
    39  			_, err := NewGrant(tc.blockTime, tc.a, tc.expire)
    40  			expecError(require.New(t), tc.err, err)
    41  		})
    42  	}
    43  }
    44  
    45  func unixTime(s, ns int64) *time.Time {
    46  	t := time.Unix(s, ns)
    47  	return &t
    48  }