github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/publisher/zz_publisher_test.go (about)

     1  package publisher_test
     2  
     3  import (
     4  	"context"
     5  	"database/sql/driver"
     6  	"runtime"
     7  	"testing"
     8  
     9  	"github.com/agiledragon/gomonkey/v2"
    10  	"github.com/golang/mock/gomock"
    11  	. "github.com/onsi/gomega"
    12  	"github.com/pkg/errors"
    13  
    14  	confid "github.com/machinefi/w3bstream/pkg/depends/conf/id"
    15  	confjwt "github.com/machinefi/w3bstream/pkg/depends/conf/jwt"
    16  	conflog "github.com/machinefi/w3bstream/pkg/depends/conf/log"
    17  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx"
    18  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder"
    19  	"github.com/machinefi/w3bstream/pkg/depends/x/contextx"
    20  	"github.com/machinefi/w3bstream/pkg/enums"
    21  	"github.com/machinefi/w3bstream/pkg/errors/status"
    22  	"github.com/machinefi/w3bstream/pkg/models"
    23  	"github.com/machinefi/w3bstream/pkg/modules/access_key"
    24  	"github.com/machinefi/w3bstream/pkg/modules/publisher"
    25  	mock_sqlx "github.com/machinefi/w3bstream/pkg/test/mock_depends_kit_sqlx"
    26  	"github.com/machinefi/w3bstream/pkg/types"
    27  )
    28  
    29  func TestPublisher(t *testing.T) {
    30  	ctrl := gomock.NewController(t)
    31  	defer ctrl.Finish()
    32  
    33  	var (
    34  		db = &struct {
    35  			*mock_sqlx.MockDBExecutor
    36  			*mock_sqlx.MockTxExecutor
    37  		}{
    38  			MockDBExecutor: mock_sqlx.NewMockDBExecutor(ctrl),
    39  			MockTxExecutor: mock_sqlx.NewMockTxExecutor(ctrl),
    40  		}
    41  		idg = confid.MustNewSFIDGenerator()
    42  		ctx = contextx.WithContextCompose(
    43  			conflog.WithLoggerContext(conflog.Std()),
    44  			types.WithMgrDBExecutorContext(db),
    45  			confid.WithSFIDGeneratorContext(idg),
    46  			types.WithAccountContext(&models.Account{
    47  				RelAccount: models.RelAccount{AccountID: idg.MustGenSFID()},
    48  			}),
    49  			types.WithProjectContext(&models.Project{
    50  				RelProject:  models.RelProject{ProjectID: idg.MustGenSFID()},
    51  				ProjectName: models.ProjectName{Name: "test_project_for_publisher_unit_test"},
    52  			}),
    53  			confjwt.WithConfContext(&confjwt.Jwt{
    54  				Issuer:  "w3bstream_test",
    55  				SignKey: "xxxx",
    56  			}),
    57  		)(context.Background())
    58  	)
    59  
    60  	db.MockDBExecutor.EXPECT().T(gomock.Any()).Return(&builder.Table{}).AnyTimes()
    61  	db.MockDBExecutor.EXPECT().Context().Return(ctx).AnyTimes()
    62  	db.MockTxExecutor.EXPECT().IsTx().Return(true).AnyTimes()
    63  
    64  	t.Run("Create", func(t *testing.T) {
    65  		if runtime.GOOS == `darwin` {
    66  			return
    67  		}
    68  
    69  		patch := gomonkey.NewPatches()
    70  		defer patch.Reset()
    71  
    72  		anyErr := errors.New("any")
    73  
    74  		t.Run("#Failed", func(t *testing.T) {
    75  			t.Run("#CreateAccessKeyFailed", func(t *testing.T) {
    76  				patch = patch.
    77  					ApplyFunc(
    78  						access_key.Create,
    79  						func(_ context.Context, _ *access_key.CreateReq) (*access_key.CreateRsp, error) {
    80  							return nil, anyErr
    81  						},
    82  					)
    83  				_, err := publisher.Create(ctx, &publisher.CreateReq{})
    84  				NewWithT(t).Expect(err).To(Equal(anyErr))
    85  			})
    86  			t.Run("#CreatePublisherFailed", func(t *testing.T) {
    87  				patch = patch.
    88  					ApplyFunc(
    89  						access_key.Create,
    90  						func(_ context.Context, _ *access_key.CreateReq) (*access_key.CreateRsp, error) {
    91  							return &access_key.CreateRsp{
    92  								Name:         "test",
    93  								IdentityType: enums.ACCESS_KEY_IDENTITY_TYPE__PUBLISHER,
    94  								IdentityID:   100,
    95  								AccessKey:    "w3b_any",
    96  							}, nil
    97  						},
    98  					)
    99  				t.Run("#PublisherConflict", func(t *testing.T) {
   100  					db.MockDBExecutor.EXPECT().Exec(gomock.Any()).Return(nil, mock_sqlx.ErrConflict).Times(1)
   101  					_, err := publisher.Create(ctx, &publisher.CreateReq{})
   102  					mock_sqlx.ExpectError(t, err, status.PublisherConflict)
   103  				})
   104  				t.Run("#DatabaseError", func(t *testing.T) {
   105  					db.MockDBExecutor.EXPECT().Exec(gomock.Any()).Return(nil, mock_sqlx.ErrDatabase).Times(1)
   106  					_, err := publisher.Create(ctx, &publisher.CreateReq{})
   107  					mock_sqlx.ExpectError(t, err, status.DatabaseError)
   108  				})
   109  			})
   110  		})
   111  
   112  		t.Run("#Success", func(t *testing.T) {
   113  			db.MockDBExecutor.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(1)
   114  			_, err := publisher.Create(ctx, &publisher.CreateReq{})
   115  			NewWithT(t).Expect(err).To(BeNil())
   116  		})
   117  	})
   118  
   119  	t.Run("Get", func(t *testing.T) {
   120  		t.Run("#Success", func(t *testing.T) {
   121  			db.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil).Times(2)
   122  
   123  			// GetBySFID
   124  			{
   125  				_, err := publisher.GetBySFID(ctx, idg.MustGenSFID())
   126  				NewWithT(t).Expect(err).To(BeNil())
   127  			}
   128  
   129  			// GetByProjectAndKey
   130  			{
   131  				_, err := publisher.GetByProjectAndKey(ctx, idg.MustGenSFID(), "publisher_key")
   132  				NewWithT(t).Expect(err).To(BeNil())
   133  			}
   134  		})
   135  
   136  		t.Run("#PublisherNotFound", func(t *testing.T) {
   137  			db.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(sqlx.NewSqlError(sqlx.SqlErrTypeNotFound, "")).Times(2)
   138  
   139  			// GetBySFID
   140  			{
   141  				_, err := publisher.GetBySFID(ctx, 1)
   142  				NewWithT(t).Expect(err).To(Equal(status.PublisherNotFound))
   143  			}
   144  
   145  			// GetByProjectAndKey
   146  			{
   147  				_, err := publisher.GetByProjectAndKey(ctx, idg.MustGenSFID(), "publisher_key")
   148  				NewWithT(t).Expect(err).To(Equal(status.PublisherNotFound))
   149  			}
   150  		})
   151  	})
   152  
   153  	t.Run("List", func(t *testing.T) {
   154  		t.Run("Success", func(t *testing.T) {
   155  			db.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil).Times(5)
   156  
   157  			// List
   158  			{
   159  				_, err := publisher.List(ctx, &publisher.ListReq{})
   160  				NewWithT(t).Expect(err).To(BeNil())
   161  			}
   162  
   163  			// ListByCond
   164  			{
   165  				_, err := publisher.ListByCond(ctx, &publisher.CondArgs{})
   166  				NewWithT(t).Expect(err).To(BeNil())
   167  			}
   168  
   169  			{
   170  				_, err := publisher.ListDetail(ctx, &publisher.ListReq{})
   171  				NewWithT(t).Expect(err).To(BeNil())
   172  			}
   173  		})
   174  	})
   175  
   176  	t.Run("Remove", func(t *testing.T) {
   177  		t.Run("Success", func(t *testing.T) {
   178  			db.MockDBExecutor.EXPECT().Exec(gomock.Any()).Return(driver.RowsAffected(1), nil)
   179  
   180  			err := publisher.Remove(ctx, types.MustAccountFromContext(ctx), &publisher.CondArgs{})
   181  			NewWithT(t).Expect(err).To(BeNil())
   182  		})
   183  	})
   184  }