github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/auth/simple/simple_test.go (about) 1 package simple 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 8 grpcmocks "github.com/projecteru2/core/3rdmocks" 9 10 "github.com/stretchr/testify/assert" 11 "google.golang.org/grpc" 12 "google.golang.org/grpc/metadata" 13 ) 14 15 var defaultSrv = 1024 16 17 func streamHandler(srv interface{}, stream grpc.ServerStream) error { 18 s, ok := srv.(int) 19 if !ok { 20 return errors.New("failed") 21 } 22 if s != defaultSrv { 23 return errors.New("wrong srv") 24 } 25 return nil 26 } 27 28 func unaryHandler(ctx context.Context, req interface{}) (interface{}, error) { 29 s, ok := req.(int) 30 if !ok { 31 return nil, errors.New("failed") 32 } 33 if s != defaultSrv { 34 return nil, errors.New("wrong srv") 35 } 36 return s, nil 37 } 38 39 func TestBasicAuthStream(t *testing.T) { 40 user := "test" 41 pass := "pass" 42 ba := NewBasicAuth(user, pass) 43 ctx := context.Background() 44 45 // no context 46 mockServerStream := &grpcmocks.ServerStream{} 47 mockServerStream.On("Context").Return(ctx) 48 err := ba.StreamInterceptor(defaultSrv, mockServerStream, nil, streamHandler) 49 assert.Error(t, err) 50 // wrong username 51 mockServerStream = &grpcmocks.ServerStream{} 52 incomingCtx := metadata.NewIncomingContext(ctx, metadata.MD{"what": []string{}}) 53 mockServerStream.On("Context").Return(incomingCtx) 54 err = ba.StreamInterceptor(defaultSrv, mockServerStream, nil, streamHandler) 55 assert.Error(t, err) 56 // wrong password 57 mockServerStream = &grpcmocks.ServerStream{} 58 incomingCtx = metadata.NewIncomingContext(ctx, metadata.MD{user: []string{}}) 59 mockServerStream.On("Context").Return(incomingCtx) 60 err = ba.StreamInterceptor(defaultSrv, mockServerStream, nil, streamHandler) 61 assert.Error(t, err) 62 // vaild 63 mockServerStream = &grpcmocks.ServerStream{} 64 incomingCtx = metadata.NewIncomingContext(ctx, metadata.MD{user: []string{pass}}) 65 mockServerStream.On("Context").Return(incomingCtx) 66 err = ba.StreamInterceptor(defaultSrv, mockServerStream, nil, streamHandler) 67 assert.NoError(t, err) 68 } 69 70 func TestBasicAuthUnary(t *testing.T) { 71 user := "test" 72 pass := "pass" 73 ba := NewBasicAuth(user, pass) 74 ctx := context.Background() 75 // no meta context 76 _, err := ba.UnaryInterceptor(ctx, defaultSrv, nil, unaryHandler) 77 assert.Error(t, err) 78 // wrong username 79 incomingCtx := metadata.NewIncomingContext(ctx, metadata.MD{"what": []string{}}) 80 r, err := ba.UnaryInterceptor(incomingCtx, defaultSrv, nil, unaryHandler) 81 assert.Error(t, err) 82 assert.Nil(t, r) 83 // wrong password 84 incomingCtx = metadata.NewIncomingContext(ctx, metadata.MD{user: []string{}}) 85 r, err = ba.UnaryInterceptor(incomingCtx, defaultSrv, nil, unaryHandler) 86 assert.Error(t, err) 87 assert.Nil(t, r) 88 // vaild 89 incomingCtx = metadata.NewIncomingContext(ctx, metadata.MD{user: []string{pass}}) 90 r, err = ba.UnaryInterceptor(incomingCtx, defaultSrv, nil, unaryHandler) 91 assert.NoError(t, err) 92 s, ok := r.(int) 93 assert.True(t, ok) 94 assert.Equal(t, s, defaultSrv) 95 }