github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/cmd/srv-applet-mgr/apis/middleware/zz_current_user_test.go (about)

     1  package middleware_test
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/golang/mock/gomock"
    10  	. "github.com/onsi/gomega"
    11  
    12  	"github.com/machinefi/w3bstream/cmd/srv-applet-mgr/apis/middleware"
    13  	base "github.com/machinefi/w3bstream/pkg/depends/base/types"
    14  	confjwt "github.com/machinefi/w3bstream/pkg/depends/conf/jwt"
    15  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder"
    16  	"github.com/machinefi/w3bstream/pkg/depends/x/contextx"
    17  	"github.com/machinefi/w3bstream/pkg/enums"
    18  	"github.com/machinefi/w3bstream/pkg/errors/status"
    19  	"github.com/machinefi/w3bstream/pkg/models"
    20  	mock_sqlx "github.com/machinefi/w3bstream/pkg/test/mock_depends_kit_sqlx"
    21  	"github.com/machinefi/w3bstream/pkg/types"
    22  )
    23  
    24  func TestContextAccountAuth(t *testing.T) {
    25  	ctl := gomock.NewController(t)
    26  	defer ctl.Finish()
    27  
    28  	t.Run("ContextKey", func(t *testing.T) {
    29  		caa := &middleware.ContextAccountAuth{}
    30  		NewWithT(t).Expect(caa.ContextKey()).To(Equal("middleware.ContextAccountAuth"))
    31  	})
    32  
    33  	t.Run("Output", func(t *testing.T) {
    34  		conf := &confjwt.Jwt{
    35  			Issuer:  "test_context_account_auth",
    36  			ExpIn:   base.Duration(time.Minute),
    37  			SignKey: "__test__",
    38  		}
    39  
    40  		d := mock_sqlx.NewMockDBExecutor(ctl)
    41  		d.EXPECT().T(gomock.Any()).Return(&builder.Table{}).AnyTimes()
    42  
    43  		ctx := contextx.WithContextCompose(
    44  			confjwt.WithConfContext(conf),
    45  			types.WithMgrDBExecutorContext(d),
    46  		)(context.Background())
    47  		caa := &middleware.ContextAccountAuth{}
    48  		key := (&confjwt.Auth{}).ContextKey()
    49  		errFrom := func(from string) error { return errors.New(from) }
    50  
    51  		t.Run("#Failed", func(t *testing.T) {
    52  			t.Run("#ParseJwtAuthContentFailed", func(t *testing.T) {
    53  				ctx := context.WithValue(ctx, key, "wrong_auth_content")
    54  				_, err := caa.Output(ctx)
    55  				mock_sqlx.ExpectError(t, err, status.InvalidAuthAccountID)
    56  				t.Log(err)
    57  			})
    58  			t.Run("#AccountModelQueryFailed", func(t *testing.T) {
    59  				d.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(errFrom(t.Name())).Times(1)
    60  				ctx := context.WithValue(ctx, key, "100")
    61  				_, err := caa.Output(ctx)
    62  				mock_sqlx.ExpectError(t, err, status.DatabaseError, t.Name())
    63  				t.Log(err)
    64  			})
    65  			t.Run("#InvalidAuthValue", func(t *testing.T) {
    66  				ctx := context.WithValue(ctx, key, &models.AccessKey{
    67  					AccessKeyInfo: models.AccessKeyInfo{
    68  						IdentityID:   100,
    69  						IdentityType: enums.ACCESS_KEY_IDENTITY_TYPE__PUBLISHER,
    70  					},
    71  				})
    72  				_, err := caa.Output(ctx)
    73  				mock_sqlx.ExpectError(t, err, status.InvalidAuthValue)
    74  				t.Log(err)
    75  			})
    76  		})
    77  		t.Run("#Success", func(t *testing.T) {
    78  			d.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil).Times(1)
    79  			ctx := context.WithValue(ctx, key, "100")
    80  			_, err := caa.Output(ctx)
    81  			NewWithT(t).Expect(err).To(BeNil())
    82  		})
    83  	})
    84  }
    85  
    86  func TestMustAccount(t *testing.T) {
    87  	key := (&middleware.ContextAccountAuth{}).ContextKey()
    88  	ctx := context.WithValue(context.Background(), key, &middleware.CurrentAccount{})
    89  
    90  	_ = middleware.MustCurrentAccountFromContext(ctx)
    91  	_, ok := middleware.CurrentAccountFromContext(ctx)
    92  	NewWithT(t).Expect(ok).To(BeTrue())
    93  }