2020-07-08 11:56:37 +00:00
|
|
|
package authz
|
2020-03-23 06:01:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-03-23 06:01:59 +00:00
|
|
|
)
|
|
|
|
|
2023-10-25 15:10:45 +00:00
|
|
|
func Test_extractBearerToken(t *testing.T) {
|
2020-03-23 06:01:59 +00:00
|
|
|
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
token string
|
2023-10-25 15:10:45 +00:00
|
|
|
verifier AccessTokenVerifier
|
2020-03-23 06:01:59 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no auth header set",
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
token: "",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong auth header set",
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
token: "Basic sds",
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "auth header set",
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
token: "Bearer AUTH",
|
2023-10-25 15:10:45 +00:00
|
|
|
verifier: AccessTokenVerifierFunc(func(context.Context, string) (string, string, string, string, string, error) {
|
|
|
|
return "", "", "", "", "", nil
|
|
|
|
}),
|
2020-03-23 06:01:59 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-10-25 15:10:45 +00:00
|
|
|
_, err := extractBearerToken(tt.args.token)
|
2020-03-23 06:01:59 +00:00
|
|
|
if tt.wantErr && err == nil {
|
|
|
|
t.Errorf("got wrong result, should get err: actual: %v ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tt.wantErr && err != nil {
|
|
|
|
t.Errorf("got wrong result, should not get err: actual: %v ", err)
|
|
|
|
}
|
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
if tt.wantErr && !zerrors.IsUnauthenticated(err) {
|
2020-03-23 06:01:59 +00:00
|
|
|
t.Errorf("got wrong err: %v ", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|