github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/util/wrapper/wrapper_test.go (about) 1 package wrapper 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/tickoalcantara12/micro/v3/service/auth" 8 "github.com/tickoalcantara12/micro/v3/service/context/metadata" 9 "github.com/tickoalcantara12/micro/v3/service/errors" 10 "github.com/tickoalcantara12/micro/v3/service/server" 11 "github.com/tickoalcantara12/micro/v3/util/codec" 12 13 . "github.com/onsi/gomega" 14 ) 15 16 type dummyAuth struct { 17 opts auth.Options 18 } 19 20 func (d dummyAuth) Init(opts ...auth.Option) { 21 } 22 23 func (d dummyAuth) Options() auth.Options { 24 return d.opts 25 } 26 27 func (d dummyAuth) Generate(id string, opts ...auth.GenerateOption) (*auth.Account, error) { 28 options := auth.NewGenerateOptions(opts...) 29 name := options.Name 30 if name == "" { 31 name = id 32 } 33 return &auth.Account{ 34 ID: id, 35 Secret: options.Secret, 36 Metadata: options.Metadata, 37 Scopes: options.Scopes, 38 Issuer: d.Options().Issuer, 39 Name: name, 40 }, nil 41 } 42 43 func (d dummyAuth) Verify(acc *auth.Account, res *auth.Resource, opts ...auth.VerifyOption) error { 44 return nil 45 } 46 47 func (d dummyAuth) Inspect(token string) (*auth.Account, error) { 48 return &auth.Account{ID: token, Issuer: d.Options().Issuer}, nil 49 } 50 51 func (d dummyAuth) Token(opts ...auth.TokenOption) (*auth.AccountToken, error) { 52 return &auth.AccountToken{}, nil 53 } 54 55 func (d dummyAuth) Grant(rule *auth.Rule) error { 56 return nil 57 } 58 59 func (d dummyAuth) Revoke(rule *auth.Rule) error { 60 return nil 61 } 62 63 func (d dummyAuth) Rules(option ...auth.RulesOption) ([]*auth.Rule, error) { 64 return nil, nil 65 } 66 67 func (d dummyAuth) String() string { 68 return "dummyAuth" 69 } 70 71 type dummyReq struct { 72 } 73 74 func (d dummyReq) Service() string { 75 return "dummy" 76 } 77 78 func (d dummyReq) Method() string { 79 return "dummy" 80 } 81 82 func (d dummyReq) Endpoint() string { 83 return "dummy" 84 } 85 86 func (d dummyReq) ContentType() string { 87 return "application/json" 88 } 89 90 func (d dummyReq) Header() map[string]string { 91 return map[string]string{} 92 } 93 94 func (d dummyReq) Body() interface{} { 95 return nil 96 } 97 98 func (d dummyReq) Read() ([]byte, error) { 99 panic("implement me") 100 } 101 102 func (d dummyReq) Codec() codec.Reader { 103 panic("implement me") 104 } 105 106 func (d dummyReq) Stream() bool { 107 panic("implement me") 108 } 109 110 func TestAuthWrapper(t *testing.T) { 111 g := NewWithT(t) 112 113 tcs := []struct { 114 name string 115 authHdr string 116 err error 117 tok string 118 }{ 119 { 120 name: "Bearer auth", 121 authHdr: "Bearer 123355", 122 tok: "123355", 123 err: nil, 124 }, 125 { 126 name: "Basic auth", 127 authHdr: "Basic Zm9vOmJhcg==", 128 tok: "bar", 129 err: nil, 130 }, 131 { 132 name: "Basic auth but wrong format of user:pass", 133 authHdr: "Basic Zm9vYmFyCg==", 134 err: errors.Unauthorized("dummy", "invalid authorization header. Incorrect format"), 135 }, 136 { 137 name: "Unknown auth", 138 authHdr: "Foobar 11111", 139 err: errors.Unauthorized("dummy", "invalid authorization header. Expected Bearer or Basic schema"), 140 }, 141 } 142 143 for _, tc := range tcs { 144 t.Run(tc.name, func(t *testing.T) { 145 auth.DefaultAuth = dummyAuth{} 146 147 w := AuthHandler() 148 dummyInvoked := false 149 tok := "" 150 dummy := func(ctx context.Context, req server.Request, rsp interface{}) error { 151 dummyInvoked = true 152 acc, _ := auth.AccountFromContext(ctx) 153 // dummyAuth sets acc ID to the token string so we can easily test it 154 tok = acc.ID 155 return nil 156 } 157 ctx := context.Background() 158 ctx = metadata.Set(ctx, "Authorization", tc.authHdr) 159 err := w(dummy)(ctx, &dummyReq{}, nil) 160 161 if tc.err == nil { 162 g.Expect(dummyInvoked).To(BeTrue()) 163 g.Expect(tok).To(Equal(tc.tok)) 164 } else { 165 g.Expect(dummyInvoked).To(BeFalse()) 166 g.Expect(err).To(Equal(tc.err)) 167 } 168 169 }) 170 } 171 }