github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/cmd/srv-applet-mgr/apis/middleware/zz_current_publisher_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 TestCurrentPublisher(t *testing.T) {
    25  	ctl := gomock.NewController(t)
    26  	defer ctl.Finish()
    27  
    28  	pub := &middleware.ContextPublisherAuth{}
    29  
    30  	t.Run("#ContextKey", func(t *testing.T) {
    31  		NewWithT(t).Expect(pub.ContextKey()).To(Equal("middleware.ContextAccountAuth"))
    32  	})
    33  
    34  	t.Run("#Output", func(t *testing.T) {
    35  		conf := &confjwt.Jwt{
    36  			Issuer:  "test_context_account_auth",
    37  			ExpIn:   base.Duration(time.Minute),
    38  			SignKey: "__test__",
    39  		}
    40  
    41  		d := mock_sqlx.NewMockDBExecutor(ctl)
    42  		d.EXPECT().T(gomock.Any()).Return(&builder.Table{}).AnyTimes()
    43  
    44  		ctx := contextx.WithContextCompose(
    45  			confjwt.WithConfContext(conf),
    46  			types.WithMgrDBExecutorContext(d),
    47  		)(context.Background())
    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 := pub.Output(ctx)
    55  				mock_sqlx.ExpectError(t, err, status.InvalidAuthPublisherID)
    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, &models.AccessKey{
    61  					AccessKeyInfo: models.AccessKeyInfo{
    62  						IdentityID:   100,
    63  						IdentityType: enums.ACCESS_KEY_IDENTITY_TYPE__ACCOUNT,
    64  					},
    65  				})
    66  				_, err := pub.Output(ctx)
    67  				mock_sqlx.ExpectError(t, err, status.DatabaseError, t.Name())
    68  				t.Log(err)
    69  			})
    70  			t.Run("#PublisherModelQueryFailed", func(t *testing.T) {
    71  				d.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(errFrom(t.Name())).Times(1)
    72  				ctx := context.WithValue(ctx, key, &models.AccessKey{
    73  					AccessKeyInfo: models.AccessKeyInfo{
    74  						IdentityID:   100,
    75  						IdentityType: enums.ACCESS_KEY_IDENTITY_TYPE__PUBLISHER,
    76  					},
    77  				})
    78  				_, err := pub.Output(ctx)
    79  				mock_sqlx.ExpectError(t, err, status.DatabaseError, t.Name())
    80  				t.Log(err)
    81  			})
    82  		})
    83  		t.Run("#Success", func(t *testing.T) {
    84  			t.Run("#AsPublisher", func(t *testing.T) {
    85  				d.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil).Times(1)
    86  				ctx := context.WithValue(ctx, key, "100")
    87  				v, err := pub.Output(ctx)
    88  				NewWithT(t).Expect(err).To(BeNil())
    89  
    90  				_, ok := v.(*middleware.CurrentPublisher)
    91  				NewWithT(t).Expect(ok).To(BeTrue())
    92  			})
    93  			t.Run("#AsAccount", func(t *testing.T) {
    94  				d.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil).Times(1)
    95  				ctx = context.WithValue(ctx, key, &models.AccessKey{
    96  					AccessKeyInfo: models.AccessKeyInfo{
    97  						IdentityID:   100,
    98  						IdentityType: enums.ACCESS_KEY_IDENTITY_TYPE__ACCOUNT,
    99  					},
   100  				})
   101  				v, err := pub.Output(ctx)
   102  				NewWithT(t).Expect(err).To(BeNil())
   103  
   104  				_, ok := v.(*middleware.CurrentAccount)
   105  				NewWithT(t).Expect(ok).To(BeTrue())
   106  			})
   107  		})
   108  	})
   109  }
   110  
   111  func TestMustPublisher(t *testing.T) {
   112  	key := (&middleware.ContextPublisherAuth{}).ContextKey()
   113  	ctx := context.WithValue(context.Background(), key, &middleware.CurrentPublisher{})
   114  
   115  	_ = middleware.MustPublisher(ctx)
   116  	_, ok := middleware.PublisherFromContext(ctx)
   117  	NewWithT(t).Expect(ok).To(BeTrue())
   118  }